11import Foundation
22
33// DuckDB reads a file named as a table. A quoted identifier, in `"` or a backtick, counts only with a `/`, a `.` or a
4- // `\`.
4+ // `\`. So does a dotted name ending in a file extension, and a table function DuckDB does not keep inside the database.
55public enum SQLFileNamedAsTable {
66 private static let tableIntroducers : Set < String > = [
77 " FROM " , " JOIN " , " USING " , " TABLE " , " DESCRIBE " , " DESC " , " SHOW " , " SUMMARIZE " , " PIVOT " , " UNPIVOT " ,
@@ -16,6 +16,23 @@ public enum SQLFileNamedAsTable {
1616 // `extract(year FROM '2026-01-01'::DATE)` separates arguments with FROM.
1717 private static let functionsTakingFrom : Set < String > = [ " EXTRACT " , " TRIM " , " SUBSTRING " , " OVERLAY " ]
1818
19+ // Only a FROM or JOIN list holds table functions; PIVOT's and USING's parentheses hold expressions.
20+ private static let callingIntroducers : Set < String > = [ " FROM " , " JOIN " ]
21+
22+ // An allowlist: an extension DuckDB loads on its own brings table functions no list here knows.
23+ static let tableFunctionsInsideDatabase : Set < String > = [
24+ " RANGE " , " GENERATE_SERIES " , " UNNEST " , " REPEAT " , " JSON_EACH " , " JSON_TREE " , " DUCKDB_COLUMNS " ,
25+ " DUCKDB_CONSTRAINTS " , " DUCKDB_DATABASES " , " DUCKDB_DEPENDENCIES " , " DUCKDB_FUNCTIONS " , " DUCKDB_INDEXES " ,
26+ " DUCKDB_KEYWORDS " , " DUCKDB_SCHEMAS " , " DUCKDB_SEQUENCES " , " DUCKDB_SETTINGS " , " DUCKDB_TABLES " , " DUCKDB_TYPES " ,
27+ " DUCKDB_VARIABLES " , " DUCKDB_VIEWS " , " PRAGMA_DATABASE_SIZE " , " PRAGMA_PLATFORM " , " PRAGMA_SHOW " ,
28+ " PRAGMA_STORAGE_INFO " , " PRAGMA_TABLE_INFO " , " PRAGMA_VERSION " ,
29+ ]
30+
31+ // DuckDB joins a dotted table name with `.` and reads it as a file in the working folder.
32+ static let fileExtensions : Set < String > = [
33+ " CSV " , " TSV " , " GZ " , " ZST " , " PARQUET " , " JSON " , " JSONL " , " NDJSON " , " GEOJSON " , " GEOJSONL " , " DB " , " DDB " , " DUCKDB " ,
34+ ]
35+
1936 private static let nameContinuations = Set ( " .:& " . utf16 )
2037
2138 private static let pathMarks = CharacterSet ( charactersIn : " /. \\ " )
@@ -31,6 +48,8 @@ public enum SQLFileNamedAsTable {
3148 var separatesArgumentsWithFrom = false
3249 var listsTables = false
3350 var expects = Expectation . nothing
51+ var introducer = " "
52+ var nameParts = 0
3453 }
3554
3655 public static func appears ( in statement : String , grammar : SQLLexicalGrammar ) -> Bool {
@@ -46,32 +65,53 @@ public enum SQLFileNamedAsTable {
4665 break
4766 case . word ( let word ) where tableIntroducers . contains ( word ) :
4867 levels [ top ] . expects = . table
68+ levels [ top ] . introducer = word
69+ levels [ top ] . nameParts = 0
4970 if word == " FROM " { levels [ top ] . listsTables = true }
5071 case . word ( let word ) where tableListEnders . contains ( word ) && expected != . table :
5172 levels [ top ] . listsTables = false
52- case . word ( " ONLY " ) where expected == . table :
73+ case . word ( " ONLY " ) where expected == . table , . word ( " LATERAL " ) where expected == . table :
5374 levels [ top ] . expects = . table
54- case . word , . name :
75+ case . word ( let word ) :
76+ guard expected == . table else { break }
77+ levels [ top ] . nameParts += 1
78+ if levels [ top ] . nameParts > 1 , fileExtensions . contains ( word ) { return true }
79+ levels [ top ] . expects = . restOfName
80+ case . name :
5581 if expected == . table { levels [ top ] . expects = . restOfName }
5682 case . quoted :
5783 guard expected == . table else { break }
5884 if namesFile ( at : token . location , in : source , grammar : grammar ) { return true }
85+ levels [ top ] . nameParts += 1
86+ let quotedName = identifier ( at : token . location , in : source , grammar : grammar ) ? . uppercased ( )
87+ if levels [ top ] . nameParts > 1 , let quotedName , fileExtensions . contains ( quotedName ) { return true }
5988 levels [ top ] . expects = . restOfName
6089 case . open :
90+ if expected == . restOfName , callingIntroducers . contains ( levels [ top ] . introducer ) ,
91+ ! callsTableFunctionInsideDatabase ( before : index , in : tokens , source : source , grammar : grammar ) {
92+ return true
93+ }
6194 // `FROM (notes JOIN 'payroll.csv' USING (id))` groups tables, and any other `(` starts afresh.
6295 levels . append (
6396 Parentheses (
6497 separatesArgumentsWithFrom : callsFunctionTakingFrom ( before : index , in : tokens , source : source ) ,
65- expects : expected == . table ? . table : . nothing
98+ expects : expected == . table ? . table : . nothing ,
99+ introducer : expected == . table ? levels [ top ] . introducer : " "
66100 )
67101 )
68102 case . close :
69103 if top > 0 { levels . removeLast ( ) }
70104 case . comma :
71- if levels [ top ] . listsTables { levels [ top ] . expects = . table }
105+ if levels [ top ] . listsTables {
106+ levels [ top ] . expects = . table
107+ levels [ top ] . nameParts = 0
108+ }
72109 case . other :
73- let continuesName = nameContinuations . contains ( source . character ( at : token . location ) )
74- if expected == . restOfName , continuesName { levels [ top ] . expects = . table }
110+ let mark = source . character ( at : token . location )
111+ if expected == . restOfName , nameContinuations . contains ( mark ) {
112+ levels [ top ] . expects = . table
113+ if mark != period { levels [ top ] . nameParts = 0 }
114+ }
75115 case . terminator :
76116 break
77117 }
@@ -106,6 +146,33 @@ public enum SQLFileNamedAsTable {
106146 return tableIntroducers . first ( where : word . hasSuffix ) . map { . word ( $0 ) } ?? token . kind
107147 }
108148
149+ // A name the scanner cannot read counts as unknown.
150+ private static func callsTableFunctionInsideDatabase (
151+ before index : Int ,
152+ in tokens : [ TSQLTokens . Token ] ,
153+ source : NSString ,
154+ grammar : SQLLexicalGrammar
155+ ) -> Bool {
156+ guard index >= 1 else { return false }
157+ let callee = tokens [ index - 1 ]
158+ let name : String ? = switch callee . kind {
159+ case . word ( let word ) : word
160+ case . quoted : identifier ( at : callee . location , in : source , grammar : grammar ) ? . uppercased ( )
161+ default : nil
162+ }
163+ guard let name else { return false }
164+ return name == " VALUES " || tableFunctionsInsideDatabase . contains ( name )
165+ }
166+
167+ private static func identifier ( at location : Int , in source : NSString , grammar : SQLLexicalGrammar ) -> String ? {
168+ guard [ SqlLexer . doubleQuote , SqlLexer . backtick ] . contains ( source . character ( at : location ) ) ,
169+ let span = SQLNonCodeSpan . span ( at : location , in : source , grammar : grammar )
170+ else {
171+ return nil
172+ }
173+ return source . substring ( with : NSRange ( location : location + 1 , length : max ( 0 , span . contentEnd - location - 1 ) ) )
174+ }
175+
109176 private static func namesFile ( at location : Int , in source : NSString , grammar : SQLLexicalGrammar ) -> Bool {
110177 guard [ SqlLexer . doubleQuote , SqlLexer . backtick ] . contains ( source . character ( at : location ) ) ,
111178 let span = SQLNonCodeSpan . span ( at : location , in : source , grammar : grammar )
0 commit comments