Repository navigation
fix: column names at the edges of the column answer #1754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
755204d
7e7c657
fc5166b
f49e1b7
1469220
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -102,10 +102,139 @@ function engineMade(name: string, stripped: string): boolean { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ENGINE_NAME.test(name) && !new RegExp(`(?<![\\w$])${name}(?![\\w$])`, "i").test(stripped) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Whether a SELECT list's output names are the columns the warehouse makes: none unaliased, none repeated. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function namesItsOutputs(outputs: string[], stripped: string): boolean { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Whether a SELECT list's output names are the columns the warehouse makes. The engine lists no outputs for an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * unaliased expression or a `*`, so an empty list doesn't settle; neither does a repeated name, which the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * warehouse renames. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function namesItsOutputs(outputs: string[]): boolean { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const names = outputs.map((name) => name.toLowerCase()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return names.length > 0 && new Set(names).size === names.length && !names.some((name) => engineMade(name, stripped)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return names.length > 0 && new Set(names).size === names.length | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const QUOTED = '"(?:[^"]|"")*"|`[^`]*`|\\[[^\\]]*\\]' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const unquoteName = (name: string) => name.replace(/^["`[]|["`\]]$/g, "").toLowerCase() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decode escaped quotes when removing identifier delimiters. For The Postgres cast-renaming path can therefore assign a nonexistent lineage target. Decode identifier escapes after removing the delimiters.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * The items of the SQL's outermost SELECT list (the first one outside parentheses, so after any WITH), split at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * top-level commas, from literal- and comment-stripped SQL; [] when there is none. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function selectItems(stripped: string): string[] { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A quoted name is blanked whole, so `"from"` or `"a,b"` ends no item. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const text = stripped.replace(new RegExp(QUOTED, "g"), (q) => " ".repeat(q.length)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let depth = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let start = -1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const items: string[] = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let from = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (let i = 0; i < text.length; i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const ch = text[i]! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+122
to
+130
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [🟠 MEDIUM] In Suggestion: Instead of just replacing Suggested change:
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as cubic's thread on this line. Fixed in 7e7c657: the quoted name is blanked whole. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (ch === "(") depth++ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else if (ch === ")") depth-- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (depth !== 0) continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const word = /^[a-z_]+/i.exec(text.slice(i))?.[0]?.toLowerCase() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Match complete identifiers before testing clause keywords.
The positional cast lookup then cannot find
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const boundary = i === 0 || !/[\w$]/.test(text[i - 1]!) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (start < 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (boundary && word === "select") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| start = i + 6 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A whole word only: `allocation` and `distinct_users` are column names, not modifiers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const lead = /^\s+(?:distinct\s+on\s*\([^)]*\)|distinct|all|top\s+\d+)(?![\w$])/i.exec(text.slice(start)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [SUGGESTION]: the trailing lookahead also disables the
Scoping the lookahead to the bare-word alternatives keeps both:
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right: the lookahead belongs on the bare-word modifiers only, and your form does that. Without a space after the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (lead) start += lead[0].length | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from = start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| i = start - 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (ch === ",") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| items.push(stripped.slice(from, i).trim()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from = i + 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if (boundary && word && /^(from|into|where|union|intersect|except|group|order|limit|qualify|having|window)$/.test(word)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| items.push(stripped.slice(from, i).trim()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return items.filter((one) => one !== "") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (start >= 0) items.push(stripped.slice(from).trim()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return items.filter((one) => one !== "") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Words that end an expression (`x is null`, `case … end`) rather than alias it. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const ENDS_EXPRESSION = /^(?:null|true|false|end)$/i | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Words after which the next word is an operand (`a and b`, `sum(x) over w`), not an alias. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const TAKES_OPERAND = /^(?:and|or|not|is|in|like|ilike|rlike|regexp|similar|to|between|case|when|then|else|over|collate|escape|interval|distinct|by|as|at|zone|any|all|some|exists|div|mod|xor)$/i | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve implicit aliases of qualified columns.
For
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * A SELECT item's output name when the SQL gives one (`x as name`, `x name`, a bare `t.col`), `*` for a star, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * else null. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function itemName(item: string): string | null { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (/^(?:[\w$]+\.)*\*(?:\s|$)/.test(item)) return "*" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const as = new RegExp(`\\s+as\\s+(${QUOTED}|[\\w$]+)$`, "i").exec(item) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (as) return unquoteName(as[1]!) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const bare = new RegExp(`^(?:(?:${QUOTED}|[\\w$]+)\\.)*(${QUOTED}|[\\w$]+)$`).exec(item) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (bare) return unquoteName(bare[1]!) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // An alias without AS: the last word, after an expression that ends in a name, a `)`, a quote or a literal. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const plain = new RegExp(`^([\\s\\S]*?[\\w$)"\`\\]'])\\s+(${QUOTED}|[\\w$]+)$`).exec(item) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!plain) return null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const before = /([\w$]+)\s*$/.exec(plain[1]!)?.[1] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ((before && TAKES_OPERAND.test(before)) || /(?<![\w$])interval\s+(?:''|[\d.]+)$/i.test(plain[1]!)) return null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve aliases after complete interval literals.
Reject interval field suffixes such as
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ENDS_EXPRESSION.test(plain[2]!) ? null : unquoteName(plain[2]!) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+168
to
+180
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [🔴 HIGH] The Consider adding regex support to match implicit aliases (e.g., a trailing identifier separated by whitespace), while being careful not to incorrectly match the end of an expression.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as cubic's thread on |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** The column a SELECT item casts, unaliased (`amount::numeric`, `cast(t.amount as numeric)`); null otherwise. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function castColumn(item: string): string | null { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const ref = `(?:(?:${QUOTED}|[\\w$]+)\\.)*(${QUOTED}|[\\w$]+)` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A type name: words (`double precision`, `pg_catalog.int4`), a size (`numeric(10, 2)`), array marks (`int[]`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // `int[3]`), a quoted name, a further `::` cast. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const type = `((?:[\\w\\s(),:.]|\\[\\d*\\]|"(?:[^"]|"")*")+)` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const m = new RegExp(`^${ref}\\s*::\\s*${type}$`, "i").exec(item) ?? new RegExp(`^cast\\s*\\(\\s*${ref}\\s+as\\s+${type}\\)$`, "i").exec(item) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor (Codex). Mixed or nested cast chains still get the engine name on Postgres:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Only |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!m || NOT_TYPE.test(m[2]!.replace(new RegExp(QUOTED, "g"), " "))) return null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return unquoteName(m[1]!) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Words that make the rest of an item more than a type (`amount::numeric is not null`, `amount::int as x`). */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const NOT_TYPE = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /(?<![\w$])(?:as|is|not|isnull|notnull|null|true|false|between|in|like|ilike|similar|and|or|div|mod|xor|collate|at|over|filter|case|when|then|else|end|escape)(?![\w$])/i | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION: This regex repeats No wrong name results today:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, and no wrong name today. Noted for the follow-up: one shared word list, plus each guard's own extras. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * The alias templates `COLUMNS(...) AS "…"` items give (DuckDB's `"col_\0"`, which hold a `\N` marker), lower-cased. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * An alias without a marker is the output's own name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function columnsTemplates(stripped: string): Set<string> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const out = new Set<string>() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const call = new RegExp(COLUMNS_CALL.source, "gi") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A quoted name is blanked whole, so a `)` or `columns(` inside one neither ends nor starts a call. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const text = stripped.replace(new RegExp(QUOTED, "g"), (q) => " ".repeat(q.length)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (let m = call.exec(text); m; m = call.exec(text)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let depth = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let i = m.index + m[0].length - 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (; i < text.length; i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (text[i] === "(") depth++ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else if (text[i] === ")" && --depth === 0) break | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const alias = new RegExp(`^\\s*as\\s+(${QUOTED}|[\\w$]+)`, "i").exec(stripped.slice(i + 1)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (alias && /\\\d/.test(alias[1]!)) out.add(unquoteName(alias[1]!)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return out | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+201
to
+217
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [🟠 MEDIUM] In Suggestion: Mask quoted identifiers in Suggested change:
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * The names DuckDB gives a SELECT list's repeated outputs, in list order: an output whose name is already used takes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * the first free `_N` (`val, val, val` is `val, val_1, val_2`; `id, id, amount as id_1` is `id, id_1, id_1_1`). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function duckdbRepeats(outputs: string[]): string[] { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const used = new Set<string>() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const out: string[] = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const raw of outputs) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const name = raw.toLowerCase() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!used.has(name)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| used.add(name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let k = 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (used.has(`${name}_${k}`)) k++ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| used.add(`${name}_${k}`) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| out.push(`${name}_${k}`) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return out | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -504,14 +633,21 @@ export class ProjectLineage { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (n?.resource_type === "seed") for (const c of this.seedHeader(n)) add(c) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this.traced(id) && !this.busy.has(id)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Names from the SQL that aren't the warehouse's: the engine's own name for an unaliased expression | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // (`_1`, `_col_1`), and, where the SQL calls COLUMNS(), an alias template with a backslash and a digit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // (`as "col_\0"`). A documented name is kept as it is. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // (`_1`, `_col_1`), and the alias template a COLUMNS(...) item gives (`as "col_\0"`). A documented name is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // kept as it is. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sql = this.sqlOf(id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const stripped = sql ? stripSql(sql) : "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const template = COLUMNS_CALL.test(stripped) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const real = (name: string) => !engineMade(name, stripped) && !(template && /\\\d/.test(name)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const e of this.lineage(id).edges) if (real(e.to)) add(e.to) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (sql) for (const c of this.outputColumns(sql)) if (real(c)) add(c) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const templates = columnsTemplates(stripped) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const edges = this.lineage(id).edges | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A name a parent column of the same name passes on (`select * from r` over r's real `_1`) is that column. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const passed = new Set(edges.filter((e) => e.fromCol === e.to).map((e) => e.to)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const real = (name: string) => !templates.has(name.toLowerCase()) && (passed.has(name) || !engineMade(name, stripped)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const e of edges) if (real(e.to)) add(e.to) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const outputs = sql ? this.outputColumns(sql).filter(real) : [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const c of outputs) add(c) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // DuckDB keeps a repeated output under a new name (`val`, `val_1`): a `select *` over it reads both. Only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // the SELECT list decides the names; a documented `val_1` is that rename. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this.dialect === "duckdb") for (const c of duckdbRepeats(outputs)) add(c) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const out = [...found.values()] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.columnMemo.set(id, out) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -541,14 +677,24 @@ export class ProjectLineage { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (settled && this.traced(id)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sql = this.sqlOf(id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| settled = !!sql && !/(?<![\w$])(?:identifier|columns)\s*\(/i.test(stripSql(sql)) && (selectsStar(sql) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? !this.lineage(id).unresolved && this.parents(id).every((p) => this.settledColumns(p) !== undefined) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : namesItsOutputs(this.outputColumns(sql), stripSql(sql))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? !this.lineage(id).unresolved && this.parents(id).every((p) => this.settledColumns(p) !== undefined) && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !this.renamesBesideStar(id, stripSql(sql)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SUGGESTION: It is called on line 665 and again here, and
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving this one. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : namesItsOutputs(this.outputColumns(sql))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const out = settled ? names : undefined | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.settledMemo.set(id, out) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return out | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Whether a bare `*` sits beside an output that repeats a name the star brings in (`select *, val as id`). */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private renamesBesideStar(id: string, stripped: string): boolean { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const items = selectItems(stripped) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!items.some((item) => /^\*(?:\s|$)/.test(item))) return false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: The duplicate-output guard ignores qualified stars such as Prompt for AI agents
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is R1 in the review log. The check can't tell which parent a qualified star reads, so checking
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor (Codex and lead). Only a bare
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, it's R1. I've listed it as deferred on #1753. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const brought = new Set(this.parents(id).flatMap((p) => [...(this.settledColumns(p) ?? [])])) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const explicit = items.map(itemName).filter((name): name is string => !!name && name !== "*") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new Set(explicit).size !== explicit.length || explicit.some((name) => brought.has(name)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lineage(id: string): ModelLineage { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const memo = this.lineageMemo.get(id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (memo) return memo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -628,10 +774,22 @@ export class ProjectLineage { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // The engine names an unaliased expression twice: `_N`, as if the value passed through unchanged, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // `_col_N`, how unknown. Keep the second; the warehouse picks the real name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A `_N` the SQL itself writes (`amount as _1`) is a real alias and stays. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const stripped = stripSql(raw) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const named = new Set(edges.filter((e) => /^_col_\d+$/.test(e.to)).map((e) => `${e.from}|${e.fromCol}|${e.to.slice(5)}`)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (let i = edges.length - 1; i >= 0; i--) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const e = edges[i]! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (/^_\d+$/.test(e.to) && named.has(`${e.from}|${e.fromCol}|${e.to.slice(1)}`)) edges.splice(i, 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (/^_\d+$/.test(e.to) && engineMade(e.to, stripped) && named.has(`${e.from}|${e.fromCol}|${e.to.slice(1)}`)) edges.splice(i, 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor (lead; Codex accepts).
The #1710 round-4 review probed this exact shape and found the dedupe removed it. Not blocking (edge input; the label changes and no model leaves a tier). Checking only the SELECT items, as you planned for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed: it changes a label only, and no model leaves a tier. The fix is the one planned for |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Postgres and Redshift name an unaliased cast of a column after the column (`amount::numeric` is `amount`). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this.dialect === "postgres" || this.dialect === "redshift") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const items = selectItems(stripped) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const e of edges) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // A `_col_N` the SQL writes itself (`amount::numeric as _col_1`) is that output's name: keep it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const n = engineMade(e.to, stripped) ? /^_col_(\d+)$/.exec(e.to)?.[1] : undefined | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: This guard treats any occurrence of Prompt for AI agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right: a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Limit the SQL-written alias check for cast renames to the SELECT list.
Check for SQL-written aliases only among the SELECT items.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const name = n === undefined ? null : castColumn(items[Number(n)] ?? "") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: The only covered shape is a single unaliased cast at position 1 (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The engine numbers |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (name && !edges.some((o) => o.from === e.from && o.fromCol === e.fromCol && o.to === name)) e.to = name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const errors: unknown[] = Array.isArray(result.errors) ? result.errors : [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const firstError = (): string => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2:
namesItsOutputsno longer rejects engine-made names, so a SELECT with an unaliased expression can now be treated as settled. Restore theengineMaderejection, or show thatextractOutputColumnsnever returns_Nfor unaliased expressions. OtherwisemissingColumncan wrongly report the expression's column as absent.Prompt for AI agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leaving this one.
extractOutputColumnsnever names an unaliased expression: it throws "unable to determine output column name for expression". I checkeda + b,upper(name),count(*),1,cast(a as int),a::intand a CASE on snowflake, duckdb, postgres, bigquery, redshift and databricks.outputColumnsturns the throw into[], andnamesItsOutputsdoesn't settle on that. So a_Nin the outputs is one the SQL writes, which C2 counts as a real column.