@@ -196,33 +196,34 @@ export function joinsNaturally(sql: string): boolean {
196196 return false
197197}
198198
199- // `IDENTIFIER (`, comments allowed before and inside the parenthesis, then its string or bind variable.
199+ // `IDENTIFIER (` with comments allowed around its parts; when its whole argument is one string
200+ // literal, that string (group 1) and the closing parenthesis (group 2).
200201const IDENTIFIER_CALL = new RegExp (
201- String . raw `^identifier(?:${ GAP } )?\((?:${ GAP } )?(?:'((?:[^'\\]|''|\\[\s\S])*)'|([$:?])) ` ,
202+ String . raw `^identifier(?:${ GAP } )?\((?:${ GAP } )?(?:'((?:[^'\\]|''|\\[\s\S])*)'(?: ${ GAP } )?(\))?)? ` ,
202203 "i" ,
203204)
204205
205206/**
206- * Whether the SQL names one of `columns` through Snowflake's `IDENTIFIER('…')`, or passes it a bind
207- * variable (`IDENTIFIER($col)`): the lineage engine reads the argument as a string, so it reports no
208- * edge for the column. Bounded like joinsNaturally: each `identifier` is read in a window, and past
209- * 100 of them the SQL counts as naming it.
207+ * Whether the SQL may name one of `columns` through `IDENTIFIER(…)` (Snowflake, Databricks): the
208+ * lineage engine reads the argument as a string, so it reports no edge for the column. Only an
209+ * argument that is one plain string literal naming none of them is cleared; anything else (a bind or
210+ * session variable, a concatenation, a function, an escape) counts, which only adds doubt. Bounded like
211+ * joinsNaturally: each `identifier` is read in a window, and past 100 of them the SQL counts.
210212 */
211213export function namesThroughIdentifier ( sql : string , columns : string [ ] ) : boolean {
212214 const wanted = new Set ( columns . map ( ( one ) => one . toLowerCase ( ) ) )
213215 let seen = 0
214216 for ( const match of sql . matchAll ( / (?< ! [ \w $ ] ) i d e n t i f i e r (? ! [ \w $ ] ) / gi) ) {
215217 if ( ++ seen > MAX_NATURALS ) return true
216218 const call = IDENTIFIER_CALL . exec ( sql . slice ( match . index ! , match . index ! + NATURAL_WINDOW ) )
219+ // `identifier` not called is a name, not the function.
217220 if ( ! call ) continue
218- if ( call [ 2 ] ) return true
219- // Any mention of the column in the string counts (qualified, quoted, with dots in a quoted name):
220- // telling those apart needs a parse, and a wider match only adds doubt.
221- // A string that is not a plain name (an escape such as `\x72`, any other character) is not decoded:
222- // it counts, which only adds doubt.
223- if ( ! / ^ [ \w $ . " ] * $ / . test ( call [ 1 ] ! . replace ( / ' ' / g, "'" ) ) ) return true
224- // A doubled quote inside a quoted name (`"a""b"`) is one quote in the name.
225- const text = call [ 1 ] ! . replace ( / ' ' / g, "'" ) . replace ( / " " / g, '"' ) . toLowerCase ( )
221+ if ( call [ 1 ] === undefined || ! call [ 2 ] ) return true
222+ const literal = call [ 1 ] . replace ( / ' ' / g, "'" )
223+ if ( ! / ^ [ \w $ . " ] * $ / . test ( literal ) ) return true
224+ // Any mention of the column in the string counts (qualified, quoted, with dots in a quoted name);
225+ // a doubled quote inside a quoted name (`"a""b"`) is one quote.
226+ const text = literal . replace ( / " " / g, '"' ) . toLowerCase ( )
226227 if ( [ ...wanted ] . some ( ( column ) => text . includes ( column ) ) ) return true
227228 }
228229 return false
0 commit comments