Skip to content

Commit 98c582c

Browse files
hkaltaiclaude
andcommitted
fix: only an IDENTIFIER() whose whole argument is one plain string naming another column is cleared
Concatenations, bind and session variables, functions and unterminated strings all count as possibly naming the column, so no argument form can produce a confident zero. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7890152 commit 98c582c

2 files changed

Lines changed: 27 additions & 14 deletions

File tree

‎packages/opencode/src/altimate/native/dbt/column-reach.ts‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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).
200201
const 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
*/
211213
export 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$])identifier(?![\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

‎packages/opencode/test/altimate/column-reach.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,18 @@ describe("columnReach through Snowflake IDENTIFIER()", () => {
976976
expect(namesThroughIdentifier(sql, ["revenue"])).toBe(true)
977977
// A tracked column whose quoted name holds a dot.
978978
expect(namesThroughIdentifier(`select identifier('"net.amount"') from t`, ["net.amount"])).toBe(true)
979+
// Anything but one plain string literal counts: concatenations, variables, functions.
980+
for (const sql of [
981+
"select identifier('o.' || :col) from t",
982+
"select identifier('rev' || 'enue') from t",
983+
"select identifier(session.col_name) from t",
984+
"select identifier(col_var) from t",
985+
"select identifier(concat('rev', 'enue')) from t",
986+
"select identifier('order_id'",
987+
])
988+
expect(namesThroughIdentifier(sql, ["revenue"])).toBe(true)
989+
// `identifier` as a plain name is not the function.
990+
expect(namesThroughIdentifier("select identifier, other from t", ["revenue"])).toBe(false)
979991
// An escape, or any text that is not a plain name, is not decoded: it counts.
980992
for (const sql of [
981993
String.raw`select identifier('\x72evenue') from t`,

0 commit comments

Comments
 (0)