From 4df30c7bd9df12535ca44611ad24b7ebc19d3ae7 Mon Sep 17 00:00:00 2001 From: "Ralph Sto. Domingo" Date: Tue, 17 Nov 2026 16:06:06 +0800 Subject: [PATCH 1/9] fix: read a cast after a SELECT modifier or in parentheses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - `distinct on (…)` is read to its matching `)`, with or without a space after it; `top N` too - `(amount)::numeric`, `distinct(amount)::numeric` and `cast(amount as numeric)::int` are casts of `amount` - Tests for `all$x` and a mixed-case DuckDB repeat Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VEmKQwEiHMwQ7BdwgPo9sb --- .../altimate/native/dbt/project-lineage.ts | 80 ++++++++++++++++--- .../test/altimate/column-answer.test.ts | 42 +++++++++- 2 files changed, 108 insertions(+), 14 deletions(-) diff --git a/packages/opencode/src/altimate/native/dbt/project-lineage.ts b/packages/opencode/src/altimate/native/dbt/project-lineage.ts index ab22c8dc07..037ba3ee24 100644 --- a/packages/opencode/src/altimate/native/dbt/project-lineage.ts +++ b/packages/opencode/src/altimate/native/dbt/project-lineage.ts @@ -136,9 +136,7 @@ export function selectItems(stripped: string): string[] { 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)) - if (lead) start += lead[0].length + start += modifierLength(text, start) from = start i = start - 1 } @@ -179,15 +177,73 @@ export function itemName(item: string): string | null { return ENDS_EXPRESSION.test(plain[2]!) ? null : unquoteName(plain[2]!) } -/** 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) - if (!m || NOT_TYPE.test(m[2]!.replace(new RegExp(QUOTED, "g"), " "))) return null - return unquoteName(m[1]!) +/** + * The length of the modifier a SELECT list starts with at `at` in quote-blanked text: `distinct on (…)` up to its + * matching `)`, `top N`, or the whole word `distinct` or `all` (`allocation` and `distinct_users` are names); 0 when + * there is none. + */ +function modifierLength(text: string, at: number): number { + const rest = text.slice(at) + const on = /^\s+distinct\s+on\s*\(/i.exec(rest) + if (on) { + const end = closingParen(rest, on[0].length - 1) + return end < 0 ? 0 : end + 1 + } + return /^\s+(?:top\s+\d+|(?:distinct|all)(?![\w$]))/i.exec(rest)?.[0].length ?? 0 +} + +/** The index of the `)` that closes the `(` at `open` in quote-blanked text, -1 when it isn't closed. */ +function closingParen(text: string, open: number): number { + let depth = 0 + for (let i = open; i < text.length; i++) { + if (text[i] === "(") depth++ + else if (text[i] === ")" && --depth === 0) return i + } + return -1 +} + +/** Where the last `pattern` outside parentheses starts and ends in quote-blanked text; null when there is none. */ +function lastOutsideParens(text: string, pattern: RegExp): { start: number; end: number } | null { + const depth: number[] = [] + let d = 0 + for (const ch of text) { + if (ch === ")") d-- + depth.push(d) + if (ch === "(") d++ + } + let last: { start: number; end: number } | null = null + for (const m of text.matchAll(new RegExp(pattern.source, "gi"))) if (depth[m.index] === 0) last = { start: m.index, end: m.index + m[0].length } + return last +} + +/** + * The column a SELECT item casts, unaliased, through parentheses and any mix of casts (`amount::numeric`, + * `cast(t.amount as numeric)`, `(amount)::numeric`, `cast(amount as numeric)::int`); null otherwise. + */ +function castColumn(item: string, inner = false): string | null { + const s = item.trim() + const blank = s.replace(new RegExp(QUOTED, "g"), (q) => " ".repeat(q.length)) + const ref = new RegExp(`^(?:(?:${QUOTED}|[\\w$]+)\\.)*(${QUOTED}|[\\w$]+)$`).exec(s) + if (ref) { + const name = ref[1]! + return inner && !/^\d/.test(name) && !ENDS_EXPRESSION.test(name) ? unquoteName(name) : null + } + if (blank.startsWith("(") && closingParen(blank, 0) === blank.length - 1) return castColumn(s.slice(1, -1), true) + const call = /^cast\s*\(/i.exec(blank) + if (call && closingParen(blank, call[0].length - 1) === blank.length - 1) { + const at = lastOutsideParens(blank.slice(call[0].length, -1), /\sas\s/) + if (!at) return null + const body = s.slice(call[0].length, -1) + return isType(body.slice(at.end)) ? castColumn(body.slice(0, at.start), true) : null + } + const at = lastOutsideParens(blank, /::/) + return at && isType(s.slice(at.end)) ? castColumn(s.slice(0, at.start), true) : null +} + +/** Whether text is a type name: words (`double precision`, `pg_catalog.int4`), a size (`numeric(10, 2)`), array marks + * (`int[]`, `int[3]`) or a quoted name, and nothing that makes it more (`numeric is not null`). */ +function isType(text: string): boolean { + return /^\s*(?:[\w\s(),.]|\[\d*\]|"(?:[^"]|"")*")+$/.test(text) && !NOT_TYPE.test(text.replace(new RegExp(QUOTED, "g"), " ")) } /** Words that make the rest of an item more than a type (`amount::numeric is not null`, `amount::int as x`). */ diff --git a/packages/opencode/test/altimate/column-answer.test.ts b/packages/opencode/test/altimate/column-answer.test.ts index c2bb551bdc..7cf0e43ffc 100644 --- a/packages/opencode/test/altimate/column-answer.test.ts +++ b/packages/opencode/test/altimate/column-answer.test.ts @@ -1303,7 +1303,7 @@ describe("both tools: names at the edges", () => { }) test("a column name that starts with all or distinct isn't read as a SELECT modifier", () => { - for (const name of ["allocation", "distinct_users", "all_time"]) { + for (const name of ["allocation", "distinct_users", "all_time", "all$x"]) { const path = write(nodes("postgres", [ model("f", [], `select 2.5 as ${name}`, [name]), model("r", ["f"], `with q as (select * from ${T("f")}) select ${name}::numeric from q`), @@ -1316,8 +1316,38 @@ describe("both tools: names at the edges", () => { } }) + test("a cast after a SELECT modifier is named after its column, so a settled model has it", () => { + // Each throws "r has no column amount" on main: the modifier or the parentheses hid the cast. + for (const dialect of ["postgres", "redshift"]) { + const lists = ["distinct on(id)amount::numeric, id", "distinct on (coalesce(id, 0)) id, amount::numeric", "distinct(amount)::numeric, id", + "all(amount)::numeric, id", "distinct ((amount))::numeric, id", "id, (amount)::numeric", "id, cast((amount) as numeric)"] + if (dialect === "postgres") lists.push("distinct on (id) id, amount::numeric") + if (dialect === "redshift") lists.push("top 5amount::numeric, id", "top 5 amount::numeric, id") + for (const list of lists) { + const path = write(nodes(dialect, [ + model("f", [], "select 1 as id, 2.5 as amount", ["id", "amount"]), + model("r", ["f"], `with q as (select * from ${T("f")}) select ${list} from q`), + model("child", ["r"], `select * from ${T("r")}`), + ])) + expect(() => columnReach(path, "r", "amount")).not.toThrow() + expect(carried(columnAnswer(path, "r", "amount"))).toEqual(["child.amount"]) + } + } + }) + + test("mixed and nested cast chains of a column are named after it", () => { + for (const cast of ["cast(amount as numeric)::int", "cast(cast(amount as numeric) as int)", "(amount::numeric)::int"]) { + const path = write(nodes("postgres", [ + model("orders", [], "select 1 as order_id, 2.5 as amount", ["order_id", "amount"]), + model("r", ["orders"], `select order_id, ${cast} from ${T("orders")}`), + ])) + expect(carried(columnAnswer(path, "orders", "amount"))).toEqual(["r.amount"]) + } + }) + test("an expression that only starts with a cast isn't named after the column, and a _col_N the SQL writes stays", () => { - for (const item of ["amount::numeric is not null", "amount::int in (1, 2)", "cast(amount as int) between 1 and 2", "amount::numeric as _col_1"]) { + for (const item of ["amount::numeric is not null", "amount::int in (1, 2)", "cast(amount as int) between 1 and 2", "amount::numeric as _col_1", + "(amount + 1)::int", "amount::numeric + 1", "cast(amount as int) * 2"]) { const path = write(nodes("postgres", [ model("orders", [], "select 1 as order_id, 2.5 as amount", ["order_id", "amount"]), model("r", ["orders"], `select order_id, ${item} from ${T("orders")}`), @@ -1352,6 +1382,14 @@ describe("both tools: names at the edges", () => { ])) const named = dbtLineage({ manifest_path: clash, model: "child" }) expect(Object.keys((named.raw_lineage as any).column_dict ?? {}).sort()).toEqual(["id", "id_1", "id_1_1"]) + // Names repeat regardless of case: `Val, val` is `val, val_1`. + const cased = write(nodes("duckdb", [ + model("f", [], "select 1 as id, 2.5 as amount", ["id", "amount"]), + model("r", ["f"], `select id, amount as Val, id as val from ${T("f")}`), + model("child", ["r"], `select * from ${T("r")}`), + ])) + const folded = dbtLineage({ manifest_path: cased, model: "child" }) + expect(Object.keys((folded.raw_lineage as any).column_dict ?? {}).sort()).toEqual(["id", "val", "val_1"]) }) test("the text line says how many may-carry models are only not traced, without suggesting no effect", () => { From 36bd06a68f377bcba75d588ce2dac0b602080c79 Mon Sep 17 00:00:00 2001 From: "Ralph Sto. Domingo" Date: Tue, 17 Nov 2026 16:48:41 +0800 Subject: [PATCH 2/9] test: keep the DISTINCT ON cases to Postgres Redshift has no DISTINCT ON; its cases are TOP only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VEmKQwEiHMwQ7BdwgPo9sb --- packages/opencode/test/altimate/column-answer.test.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/opencode/test/altimate/column-answer.test.ts b/packages/opencode/test/altimate/column-answer.test.ts index 7cf0e43ffc..ee09f6d96d 100644 --- a/packages/opencode/test/altimate/column-answer.test.ts +++ b/packages/opencode/test/altimate/column-answer.test.ts @@ -1319,9 +1319,11 @@ describe("both tools: names at the edges", () => { test("a cast after a SELECT modifier is named after its column, so a settled model has it", () => { // Each throws "r has no column amount" on main: the modifier or the parentheses hid the cast. for (const dialect of ["postgres", "redshift"]) { - const lists = ["distinct on(id)amount::numeric, id", "distinct on (coalesce(id, 0)) id, amount::numeric", "distinct(amount)::numeric, id", - "all(amount)::numeric, id", "distinct ((amount))::numeric, id", "id, (amount)::numeric", "id, cast((amount) as numeric)"] - if (dialect === "postgres") lists.push("distinct on (id) id, amount::numeric") + const lists = ["distinct(amount)::numeric, id", "all(amount)::numeric, id", "distinct ((amount))::numeric, id", "id, (amount)::numeric", + "id, cast((amount) as numeric)"] + // DISTINCT ON is Postgres only; TOP is Redshift's. + if (dialect === "postgres") + lists.push("distinct on(id)amount::numeric, id", "distinct on (coalesce(id, 0)) id, amount::numeric", "distinct on (id) id, amount::numeric") if (dialect === "redshift") lists.push("top 5amount::numeric, id", "top 5 amount::numeric, id") for (const list of lists) { const path = write(nodes(dialect, [ From 946651cc9d094162d646b2c8134edcbed22c2900 Mon Sep 17 00:00:00 2001 From: "Ralph Sto. Domingo" Date: Tue, 17 Nov 2026 17:20:04 +0800 Subject: [PATCH 3/9] fix: count parenthesis depth by UTF-16 index The depth array now lines up with matchAll indices when text holds characters outside the BMP. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VEmKQwEiHMwQ7BdwgPo9sb --- .../opencode/src/altimate/native/dbt/project-lineage.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/opencode/src/altimate/native/dbt/project-lineage.ts b/packages/opencode/src/altimate/native/dbt/project-lineage.ts index 037ba3ee24..345868bcde 100644 --- a/packages/opencode/src/altimate/native/dbt/project-lineage.ts +++ b/packages/opencode/src/altimate/native/dbt/project-lineage.ts @@ -206,10 +206,11 @@ function closingParen(text: string, open: number): number { function lastOutsideParens(text: string, pattern: RegExp): { start: number; end: number } | null { const depth: number[] = [] let d = 0 - for (const ch of text) { - if (ch === ")") d-- + // By UTF-16 index, as matchAll reports them. + for (let i = 0; i < text.length; i++) { + if (text[i] === ")") d-- depth.push(d) - if (ch === "(") d++ + if (text[i] === "(") d++ } let last: { start: number; end: number } | null = null for (const m of text.matchAll(new RegExp(pattern.source, "gi"))) if (depth[m.index] === 0) last = { start: m.index, end: m.index + m[0].length } From f6183a0678780bc8c6c12085346d7729b16d76ef Mon Sep 17 00:00:00 2001 From: "Ralph Sto. Domingo" Date: Tue, 17 Nov 2026 19:21:15 +0800 Subject: [PATCH 4/9] fix: a qualified column named end or null keeps its name - `q.end::int` and `cast(q.end as int)` name `end` again, as on main; the literal check applies to a bare name only - `cast((amount)as numeric)` reads with no space before `as` - A direct `castColumn` test pins the literal and expression guards Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VEmKQwEiHMwQ7BdwgPo9sb --- .../altimate/native/dbt/project-lineage.ts | 9 ++++-- .../test/altimate/column-answer.test.ts | 28 +++++++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/packages/opencode/src/altimate/native/dbt/project-lineage.ts b/packages/opencode/src/altimate/native/dbt/project-lineage.ts index 345868bcde..f12544c018 100644 --- a/packages/opencode/src/altimate/native/dbt/project-lineage.ts +++ b/packages/opencode/src/altimate/native/dbt/project-lineage.ts @@ -221,18 +221,21 @@ function lastOutsideParens(text: string, pattern: RegExp): { start: number; end: * The column a SELECT item casts, unaliased, through parentheses and any mix of casts (`amount::numeric`, * `cast(t.amount as numeric)`, `(amount)::numeric`, `cast(amount as numeric)::int`); null otherwise. */ -function castColumn(item: string, inner = false): string | null { +export function castColumn(item: string, inner = false): string | null { const s = item.trim() const blank = s.replace(new RegExp(QUOTED, "g"), (q) => " ".repeat(q.length)) const ref = new RegExp(`^(?:(?:${QUOTED}|[\\w$]+)\\.)*(${QUOTED}|[\\w$]+)$`).exec(s) if (ref) { const name = ref[1]! - return inner && !/^\d/.test(name) && !ENDS_EXPRESSION.test(name) ? unquoteName(name) : null + // A bare `1` or `null` is a literal; `q.end` and `"null"` are columns. + const literal = ref[0] === name && (/^\d/.test(name) || ENDS_EXPRESSION.test(name)) + return inner && !literal ? unquoteName(name) : null } if (blank.startsWith("(") && closingParen(blank, 0) === blank.length - 1) return castColumn(s.slice(1, -1), true) const call = /^cast\s*\(/i.exec(blank) if (call && closingParen(blank, call[0].length - 1) === blank.length - 1) { - const at = lastOutsideParens(blank.slice(call[0].length, -1), /\sas\s/) + // `as` after a space or a `)`: `cast((amount)as numeric)`. + const at = lastOutsideParens(blank.slice(call[0].length, -1), /(?:\s|(?<=\)))as\s/) if (!at) return null const body = s.slice(call[0].length, -1) return isType(body.slice(at.end)) ? castColumn(body.slice(0, at.start), true) : null diff --git a/packages/opencode/test/altimate/column-answer.test.ts b/packages/opencode/test/altimate/column-answer.test.ts index ee09f6d96d..2f3ace8c4a 100644 --- a/packages/opencode/test/altimate/column-answer.test.ts +++ b/packages/opencode/test/altimate/column-answer.test.ts @@ -5,7 +5,7 @@ import { join } from "node:path" import { ENGINE, MAX_ANSWER_COLUMNS, columnAnswer, describeAnswer, type ColumnAnswer } from "../../src/altimate/native/dbt/column-answer" import { columnReach, namesColumn } from "../../src/altimate/native/dbt/column-reach" import { dbtLineage } from "../../src/altimate/native/dbt/lineage" -import { clearLineageCaches, hasNestedWith, howOf, itemName, quotingIsClear, relationUse, shapeFlags, stripAggFilter, stripSql } from "../../src/altimate/native/dbt/project-lineage" +import { castColumn, clearLineageCaches, hasNestedWith, howOf, itemName, quotingIsClear, relationUse, shapeFlags, stripAggFilter, stripSql } from "../../src/altimate/native/dbt/project-lineage" const dirs: string[] = [] afterEach(() => { @@ -1317,7 +1317,8 @@ describe("both tools: names at the edges", () => { }) test("a cast after a SELECT modifier is named after its column, so a settled model has it", () => { - // Each throws "r has no column amount" on main: the modifier or the parentheses hid the cast. + // Most throw "r has no column amount" on main, where the modifier or the parentheses hid the cast. + // `distinct on (id) id, …` and `top 5 amount…` already answer there; they stay as controls. for (const dialect of ["postgres", "redshift"]) { const lists = ["distinct(amount)::numeric, id", "all(amount)::numeric, id", "distinct ((amount))::numeric, id", "id, (amount)::numeric", "id, cast((amount) as numeric)"] @@ -1337,6 +1338,29 @@ describe("both tools: names at the edges", () => { } }) + test("a qualified column named like a literal keeps its name through a cast", () => { + for (const dialect of ["postgres", "redshift"]) { + for (const [name, cast] of [["end", "q.end::int"], ["end", "cast(q.end as int)"], ["end", "(q.end)::int"], ["null", "q.null::int"], ["true", "q.true::int"]]) { + const path = write(nodes(dialect, [ + model("f", [], `select 1 as id, 2 as "${name}"`, ["id", name!]), + model("r", ["f"], `with q as (select * from ${T("f")}) select id, ${cast} from q`), + model("child", ["r"], `select * from ${T("r")}`), + ])) + expect(() => columnReach(path, "r", name!)).not.toThrow() + expect(carried(columnAnswer(path, "r", name!))).toEqual([`child.${name}`]) + } + } + }) + + test("castColumn names a column through casts and parentheses, and nothing else", () => { + const named = { "amount::numeric": "amount", "(amount)::numeric": "amount", "cast((amount)as numeric)": "amount", "q.end::int": "end", + '("null")::int': "null", "(q.end)::int": "end", "cast(amount as numeric)::int": "amount", "((amount))::numeric": "amount" } + for (const [item, name] of Object.entries(named)) expect(castColumn(item)).toBe(name) + for (const item of ["amount", "(null)::int", "(1)::int", "(true)::boolean", "coalesce(amount)::numeric", "-(amount)::numeric", + "(amount)::numeric * 2", "(amount + 1)::int", "cast(amount as int) between 1 and 2"]) + expect(castColumn(item)).toBeNull() + }) + test("mixed and nested cast chains of a column are named after it", () => { for (const cast of ["cast(amount as numeric)::int", "cast(cast(amount as numeric) as int)", "(amount::numeric)::int"]) { const path = write(nodes("postgres", [ From 945b54cd2324427efba5bcd2bdb5ef1639afd991 Mon Sep 17 00:00:00 2001 From: "Ralph Sto. Domingo" Date: Tue, 17 Nov 2026 19:32:02 +0800 Subject: [PATCH 5/9] fix: a decimal literal under a cast is not a column `cast(1.5 as numeric)` read as qualifier `1` and name `5`; a leading digit on the whole operand now marks a literal. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VEmKQwEiHMwQ7BdwgPo9sb --- packages/opencode/src/altimate/native/dbt/project-lineage.ts | 4 ++-- packages/opencode/test/altimate/column-answer.test.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/opencode/src/altimate/native/dbt/project-lineage.ts b/packages/opencode/src/altimate/native/dbt/project-lineage.ts index f12544c018..c5aeddd031 100644 --- a/packages/opencode/src/altimate/native/dbt/project-lineage.ts +++ b/packages/opencode/src/altimate/native/dbt/project-lineage.ts @@ -227,8 +227,8 @@ export function castColumn(item: string, inner = false): string | null { const ref = new RegExp(`^(?:(?:${QUOTED}|[\\w$]+)\\.)*(${QUOTED}|[\\w$]+)$`).exec(s) if (ref) { const name = ref[1]! - // A bare `1` or `null` is a literal; `q.end` and `"null"` are columns. - const literal = ref[0] === name && (/^\d/.test(name) || ENDS_EXPRESSION.test(name)) + // A number (`1`, `1.5`) or a bare `null` is a literal; `q.end` and `"null"` are columns. + const literal = /^\d/.test(ref[0]!) || (ref[0] === name && ENDS_EXPRESSION.test(name)) return inner && !literal ? unquoteName(name) : null } if (blank.startsWith("(") && closingParen(blank, 0) === blank.length - 1) return castColumn(s.slice(1, -1), true) diff --git a/packages/opencode/test/altimate/column-answer.test.ts b/packages/opencode/test/altimate/column-answer.test.ts index 2f3ace8c4a..b6c66af8bd 100644 --- a/packages/opencode/test/altimate/column-answer.test.ts +++ b/packages/opencode/test/altimate/column-answer.test.ts @@ -1356,7 +1356,7 @@ describe("both tools: names at the edges", () => { const named = { "amount::numeric": "amount", "(amount)::numeric": "amount", "cast((amount)as numeric)": "amount", "q.end::int": "end", '("null")::int': "null", "(q.end)::int": "end", "cast(amount as numeric)::int": "amount", "((amount))::numeric": "amount" } for (const [item, name] of Object.entries(named)) expect(castColumn(item)).toBe(name) - for (const item of ["amount", "(null)::int", "(1)::int", "(true)::boolean", "coalesce(amount)::numeric", "-(amount)::numeric", + for (const item of ["amount", "(null)::int", "(1)::int", "cast(1.5 as numeric)", "(1.0)::numeric", "(true)::boolean", "coalesce(amount)::numeric", "-(amount)::numeric", "(amount)::numeric * 2", "(amount + 1)::int", "cast(amount as int) between 1 and 2"]) expect(castColumn(item)).toBeNull() }) From e1cfcee018ed164c60a92036211192cd9caa18c8 Mon Sep 17 00:00:00 2001 From: "Ralph Sto. Domingo" Date: Tue, 17 Nov 2026 19:37:35 +0800 Subject: [PATCH 6/9] fix: find AS in a cast beside a quoted name `cast("amount"as numeric)` and `cast(amount as"numeric")` lost a quote to the `\s` around `as`; it is now matched by zero-width word bounds. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VEmKQwEiHMwQ7BdwgPo9sb --- packages/opencode/src/altimate/native/dbt/project-lineage.ts | 5 +++-- packages/opencode/test/altimate/column-answer.test.ts | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/altimate/native/dbt/project-lineage.ts b/packages/opencode/src/altimate/native/dbt/project-lineage.ts index c5aeddd031..76a360ff40 100644 --- a/packages/opencode/src/altimate/native/dbt/project-lineage.ts +++ b/packages/opencode/src/altimate/native/dbt/project-lineage.ts @@ -234,8 +234,9 @@ export function castColumn(item: string, inner = false): string | null { if (blank.startsWith("(") && closingParen(blank, 0) === blank.length - 1) return castColumn(s.slice(1, -1), true) const call = /^cast\s*\(/i.exec(blank) if (call && closingParen(blank, call[0].length - 1) === blank.length - 1) { - // `as` after a space or a `)`: `cast((amount)as numeric)`. - const at = lastOutsideParens(blank.slice(call[0].length, -1), /(?:\s|(?<=\)))as\s/) + // `as` as a whole word, by zero-width bounds so a quote or `)` beside it stays in place: `cast("amount"as numeric)`, + // `cast(amount as"numeric")`, `cast((amount)as numeric)`. + const at = lastOutsideParens(blank.slice(call[0].length, -1), /(? { test("castColumn names a column through casts and parentheses, and nothing else", () => { const named = { "amount::numeric": "amount", "(amount)::numeric": "amount", "cast((amount)as numeric)": "amount", "q.end::int": "end", + 'cast("amount"as numeric)': "amount", 'cast(amount as"numeric")': "amount", "cast(amount as int)": "amount", "cast(t.has as int)": "has", '("null")::int': "null", "(q.end)::int": "end", "cast(amount as numeric)::int": "amount", "((amount))::numeric": "amount" } for (const [item, name] of Object.entries(named)) expect(castColumn(item)).toBe(name) for (const item of ["amount", "(null)::int", "(1)::int", "cast(1.5 as numeric)", "(1.0)::numeric", "(true)::boolean", "coalesce(amount)::numeric", "-(amount)::numeric", From 4bac89ed923cc604843f425f7b866279ff4c4d58 Mon Sep 17 00:00:00 2001 From: "Ralph Sto. Domingo" Date: Tue, 17 Nov 2026 19:44:22 +0800 Subject: [PATCH 7/9] refactor: read casts with a small tokenizer instead of regex slices castColumn now walks tokens (names, quoted names, numbers, parens, `::`, `cast`, `as`) in one grammar; three review findings in a row came from slicing blanked text. A table test holds every cast form found so far. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VEmKQwEiHMwQ7BdwgPo9sb --- .../altimate/native/dbt/project-lineage.ts | 143 ++++++++++++------ .../test/altimate/column-answer.test.ts | 40 ++++- 2 files changed, 129 insertions(+), 54 deletions(-) diff --git a/packages/opencode/src/altimate/native/dbt/project-lineage.ts b/packages/opencode/src/altimate/native/dbt/project-lineage.ts index 76a360ff40..00d7a1b4c7 100644 --- a/packages/opencode/src/altimate/native/dbt/project-lineage.ts +++ b/packages/opencode/src/altimate/native/dbt/project-lineage.ts @@ -189,7 +189,8 @@ function modifierLength(text: string, at: number): number { const end = closingParen(rest, on[0].length - 1) return end < 0 ? 0 : end + 1 } - return /^\s+(?:top\s+\d+|(?:distinct|all)(?![\w$]))/i.exec(rest)?.[0].length ?? 0 + // The same name characters as the cast reader's, so `allé` is a name too. + return /^\s+(?:top\s+\d+|(?:distinct|all)(?![\p{L}\p{N}_$]))/iu.exec(rest)?.[0].length ?? 0 } /** The index of the `)` that closes the `(` at `open` in quote-blanked text, -1 when it isn't closed. */ @@ -202,58 +203,108 @@ function closingParen(text: string, open: number): number { return -1 } -/** Where the last `pattern` outside parentheses starts and ends in quote-blanked text; null when there is none. */ -function lastOutsideParens(text: string, pattern: RegExp): { start: number; end: number } | null { - const depth: number[] = [] - let d = 0 - // By UTF-16 index, as matchAll reports them. - for (let i = 0; i < text.length; i++) { - if (text[i] === ")") d-- - depth.push(d) - if (text[i] === "(") d++ +/** A token of a SELECT item for the cast reader. `other` is any character the reader never accepts (`+`, `-`, `|`, …). */ +type CastToken = { kind: "name" | "quoted" | "number" | "string" | "punct" | "other"; text: string } + +const CAST_TOKEN = + /\s+|("(?:[^"]|"")*"|`(?:[^`]|``)*`)|('(?:[^']|'')*')|(\d+(?:\.\d*)?(?:e[+-]?\d+)?|\.\d+(?:e[+-]?\d+)?)|([\p{L}_$][\p{L}\p{N}_$]*)|(::|[().,[\]])|([^])/iuy + +/** The tokens of a literal- and comment-stripped SELECT item, whitespace dropped. */ +function castTokens(item: string): CastToken[] { + const out: CastToken[] = [] + CAST_TOKEN.lastIndex = 0 + for (let m = CAST_TOKEN.exec(item); m; m = CAST_TOKEN.exec(item)) { + const kind = m[1] ? "quoted" : m[2] ? "string" : m[3] ? "number" : m[4] ? "name" : m[5] ? "punct" : m[6] ? "other" : undefined + if (kind) out.push({ kind, text: m[0] }) } - let last: { start: number; end: number } | null = null - for (const m of text.matchAll(new RegExp(pattern.source, "gi"))) if (depth[m.index] === 0) last = { start: m.index, end: m.index + m[0].length } - return last + return out } +/** Words a type name never holds: an item with one after `::` is more than a cast (`amount::numeric is not null`). */ +const NOT_TYPE = + /^(?: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)$/i + /** - * The column a SELECT item casts, unaliased, through parentheses and any mix of casts (`amount::numeric`, - * `cast(t.amount as numeric)`, `(amount)::numeric`, `cast(amount as numeric)::int`); null otherwise. + * The column a SELECT item casts, unaliased; null otherwise. It reads the item as tokens, in this grammar, and the + * whole item must match: + * + * item = operand { "::" type } (at least one cast or one pair of parentheses) + * operand = column | "(" item ")" | "cast" "(" item "as" type ")" + * column = name { "." name } (a bare `null`, `true`, `false` or `end` is a literal) + * type = word { word | "." | "(" number { "," number } ")" | "[" [ number ] "]" } + * + * So `amount::numeric`, `(amount)::numeric`, `cast((amount)as numeric)`, `cast(amount as numeric)::int` and `q.end::int` + * name a column; `amount::numeric + 1`, `coalesce(amount)::numeric`, `(1.5)::numeric` and `(null)::int` don't. */ -export function castColumn(item: string, inner = false): string | null { - const s = item.trim() - const blank = s.replace(new RegExp(QUOTED, "g"), (q) => " ".repeat(q.length)) - const ref = new RegExp(`^(?:(?:${QUOTED}|[\\w$]+)\\.)*(${QUOTED}|[\\w$]+)$`).exec(s) - if (ref) { - const name = ref[1]! - // A number (`1`, `1.5`) or a bare `null` is a literal; `q.end` and `"null"` are columns. - const literal = /^\d/.test(ref[0]!) || (ref[0] === name && ENDS_EXPRESSION.test(name)) - return inner && !literal ? unquoteName(name) : null - } - if (blank.startsWith("(") && closingParen(blank, 0) === blank.length - 1) return castColumn(s.slice(1, -1), true) - const call = /^cast\s*\(/i.exec(blank) - if (call && closingParen(blank, call[0].length - 1) === blank.length - 1) { - // `as` as a whole word, by zero-width bounds so a quote or `)` beside it stays in place: `cast("amount"as numeric)`, - // `cast(amount as"numeric")`, `cast((amount)as numeric)`. - const at = lastOutsideParens(blank.slice(call[0].length, -1), /(? t[k]?.kind === "punct" && t[k]!.text === text + const word = (k: number, text: string) => t[k]?.kind === "name" && t[k]!.text.toLowerCase() === text + const named = (k: number) => t[k]?.kind === "quoted" || t[k]?.kind === "name" -/** Whether text is a type name: words (`double precision`, `pg_catalog.int4`), a size (`numeric(10, 2)`), array marks - * (`int[]`, `int[3]`) or a quoted name, and nothing that makes it more (`numeric is not null`). */ -function isType(text: string): boolean { - return /^\s*(?:[\w\s(),.]|\[\d*\]|"(?:[^"]|"")*")+$/.test(text) && !NOT_TYPE.test(text.replace(new RegExp(QUOTED, "g"), " ")) -} + const type = (): boolean => { + let words = 0 + for (;;) { + const k = t[at] + if (k && (k.kind === "quoted" || (k.kind === "name" && !NOT_TYPE.test(k.text)))) at++, words++ + else if (words && punct(at, ".")) at++ + else if (words && punct(at, "(") && t[at + 1]?.kind === "number") { + at += 2 + while (punct(at, ",") && t[at + 1]?.kind === "number") at += 2 + if (!punct(at, ")")) return false + at++ + } else if (words && punct(at, "[")) { + at += t[at + 1]?.kind === "number" ? 2 : 1 + if (!punct(at, "]")) return false + at++ + } else return words > 0 + } + } + const column = (): string | null => { + if (!named(at)) return null + const start = at++ + while (punct(at, ".") && named(at + 1)) at += 2 + const last = t[at - 1]! + if (last.kind === "quoted") return last.text.slice(1, -1).replace(/""/g, '"').replace(/``/g, "`").toLowerCase() + return at - start === 1 && ENDS_EXPRESSION.test(last.text) ? null : last.text.toLowerCase() + } + const operand = (): string | null => { + if (punct(at, "(")) { + at++ + const name = chain() + if (name === null || !punct(at, ")")) return null + at++ + shaped = true + return name + } + if (word(at, "cast") && punct(at + 1, "(")) { + at += 2 + const name = chain() + if (name === null || !word(at, "as")) return null + at++ + if (!type() || !punct(at, ")")) return null + at++ + shaped = true + return name + } + return column() + } + const chain = (): string | null => { + const name = operand() + if (name === null) return null + while (punct(at, "::")) { + at++ + if (!type()) return null + shaped = true + } + return name + } -/** Words that make the rest of an item more than a type (`amount::numeric is not null`, `amount::int as x`). */ -const NOT_TYPE = - /(? { } }) - test("castColumn names a column through casts and parentheses, and nothing else", () => { - const named = { "amount::numeric": "amount", "(amount)::numeric": "amount", "cast((amount)as numeric)": "amount", "q.end::int": "end", - 'cast("amount"as numeric)': "amount", 'cast(amount as"numeric")': "amount", "cast(amount as int)": "amount", "cast(t.has as int)": "has", - '("null")::int': "null", "(q.end)::int": "end", "cast(amount as numeric)::int": "amount", "((amount))::numeric": "amount" } - for (const [item, name] of Object.entries(named)) expect(castColumn(item)).toBe(name) - for (const item of ["amount", "(null)::int", "(1)::int", "cast(1.5 as numeric)", "(1.0)::numeric", "(true)::boolean", "coalesce(amount)::numeric", "-(amount)::numeric", - "(amount)::numeric * 2", "(amount + 1)::int", "cast(amount as int) between 1 and 2"]) - expect(castColumn(item)).toBeNull() + // Every cast form the reviews of this reader found, with the name Postgres gives it (null: not a cast of a column, so + // the engine's `_col_N` stays). Items are as `selectItems` hands them over: string literals already stripped to `''`. + const CAST_FORMS: [string, string | null][] = [ + // Whole-item casts of a column. + ["amount::numeric", "amount"], ["cast(amount as numeric)", "amount"], ["t.amount::numeric", "amount"], + ["cast(t.amount as numeric)", "amount"], ["amount::int[]", "amount"], ["cast(amount as int[])", "amount"], + ["amount::int[3]", "amount"], ['amount::"numeric"', "amount"], ["amount::pg_catalog.numeric", "amount"], + ["amount::double precision", "amount"], ["amount::numeric(10, 2)", "amount"], ["cast(amount as numeric(10, 2))", "amount"], + ["amount::timestamp(3) with time zone", "amount"], + // Chains and parentheses. + ["amount::numeric::int", "amount"], ["cast(amount as numeric)::int", "amount"], ["cast(cast(amount as numeric) as int)", "amount"], + ["(amount::numeric)::int", "amount"], ["(amount)::numeric", "amount"], ["((amount))::numeric", "amount"], + ["cast((amount) as numeric)", "amount"], ["(amount)", "amount"], + // No space beside `as`, and quoted names. + ["cast((amount)as numeric)", "amount"], ['cast("amount"as numeric)', "amount"], ['cast(amount as"numeric")', "amount"], + ['"Amount"::int', "amount"], ['("a""b")::numeric', 'a"b'], ['("null")::int', "null"], + // Qualified names that read like literals or keywords. + ["q.end::int", "end"], ["cast(q.end as int)", "end"], ["(q.end)::int", "end"], ["q.null::int", "null"], ["q.true::int", "true"], + ["cast(t.has as int)", "has"], ["allé::numeric", "allé"], + // Not a cast of a column. + ["amount", null], ["t.amount", null], ["(null)::int", null], ["(true)::boolean", null], ["(1)::int", null], ["1::int", null], + ["cast(1.5 as numeric)", null], ["(1.0)::numeric", null], ["''::int", null], ["coalesce(amount)::numeric", null], + ["-(amount)::numeric", null], ["(amount)::numeric * 2", null], ["(amount + 1)::int", null], ["amount::numeric + 1", null], + ["cast(amount as int) * 2", null], ["amount::text || ''", null], ["cast(amount as int) between 1 and 2", null], + ["amount::numeric is not null", null], ["amount::int in (1, 2)", null], ["amount::numeric as _col_1", null], + ["amount::timestamp at time zone ''", null], ["amount::text collate \"C\"", null], ["arr[1]::int", null], ["cast(amount)", null], + ["cast(amount as)", null], ["amount::", null], ["(amount", null], + ] + + test("castColumn reads every cast form found so far", () => { + const wrong = CAST_FORMS.filter(([item, name]) => castColumn(item) !== name).map(([item, name]) => `${item} → ${castColumn(item)} (want ${name})`) + expect(wrong).toEqual([]) }) test("mixed and nested cast chains of a column are named after it", () => { From 4c8e0ee56a6c45e61762071c8a42f6c9bd5adad1 Mon Sep 17 00:00:00 2001 From: "Ralph Sto. Domingo" Date: Tue, 17 Nov 2026 20:38:40 +0800 Subject: [PATCH 8/9] fix: a type modifier may be a name, and a quote unescapes only itself - `amount::geometry(Point,4326)` and `amount::varchar(max)` name `amount` again, as on main - `"a``b"` stays `a``b`: only the token's own doubled quote is an escape Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VEmKQwEiHMwQ7BdwgPo9sb --- .../src/altimate/native/dbt/project-lineage.ts | 12 +++++++++--- .../opencode/test/altimate/column-answer.test.ts | 5 +++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/opencode/src/altimate/native/dbt/project-lineage.ts b/packages/opencode/src/altimate/native/dbt/project-lineage.ts index 00d7a1b4c7..3034e2f78d 100644 --- a/packages/opencode/src/altimate/native/dbt/project-lineage.ts +++ b/packages/opencode/src/altimate/native/dbt/project-lineage.ts @@ -243,6 +243,8 @@ export function castColumn(item: string): string | null { const punct = (k: number, text: string) => t[k]?.kind === "punct" && t[k]!.text === text const word = (k: number, text: string) => t[k]?.kind === "name" && t[k]!.text.toLowerCase() === text const named = (k: number) => t[k]?.kind === "quoted" || t[k]?.kind === "name" + // A type modifier: a number (`numeric(10, 2)`) or a name (`varchar(max)`, `geometry(Point, 4326)`). + const modifier = (k: number) => t[k]?.kind === "number" || named(k) const type = (): boolean => { let words = 0 @@ -250,9 +252,9 @@ export function castColumn(item: string): string | null { const k = t[at] if (k && (k.kind === "quoted" || (k.kind === "name" && !NOT_TYPE.test(k.text)))) at++, words++ else if (words && punct(at, ".")) at++ - else if (words && punct(at, "(") && t[at + 1]?.kind === "number") { + else if (words && punct(at, "(") && modifier(at + 1)) { at += 2 - while (punct(at, ",") && t[at + 1]?.kind === "number") at += 2 + while (punct(at, ",") && modifier(at + 1)) at += 2 if (!punct(at, ")")) return false at++ } else if (words && punct(at, "[")) { @@ -267,7 +269,11 @@ export function castColumn(item: string): string | null { const start = at++ while (punct(at, ".") && named(at + 1)) at += 2 const last = t[at - 1]! - if (last.kind === "quoted") return last.text.slice(1, -1).replace(/""/g, '"').replace(/``/g, "`").toLowerCase() + // Only the token's own quote is escaped by doubling: `"a``b"` is a``b. + if (last.kind === "quoted") { + const q = last.text[0]! + return last.text.slice(1, -1).replaceAll(q + q, q).toLowerCase() + } return at - start === 1 && ENDS_EXPRESSION.test(last.text) ? null : last.text.toLowerCase() } const operand = (): string | null => { diff --git a/packages/opencode/test/altimate/column-answer.test.ts b/packages/opencode/test/altimate/column-answer.test.ts index 4f691fffe2..8cbc27fa1e 100644 --- a/packages/opencode/test/altimate/column-answer.test.ts +++ b/packages/opencode/test/altimate/column-answer.test.ts @@ -1360,14 +1360,15 @@ describe("both tools: names at the edges", () => { ["cast(t.amount as numeric)", "amount"], ["amount::int[]", "amount"], ["cast(amount as int[])", "amount"], ["amount::int[3]", "amount"], ['amount::"numeric"', "amount"], ["amount::pg_catalog.numeric", "amount"], ["amount::double precision", "amount"], ["amount::numeric(10, 2)", "amount"], ["cast(amount as numeric(10, 2))", "amount"], - ["amount::timestamp(3) with time zone", "amount"], + ["amount::timestamp(3) with time zone", "amount"], ["amount::geometry(Point,4326)", "amount"], + ["cast(amount as geometry(Point, 4326))", "amount"], ["amount::varchar(max)", "amount"], // Chains and parentheses. ["amount::numeric::int", "amount"], ["cast(amount as numeric)::int", "amount"], ["cast(cast(amount as numeric) as int)", "amount"], ["(amount::numeric)::int", "amount"], ["(amount)::numeric", "amount"], ["((amount))::numeric", "amount"], ["cast((amount) as numeric)", "amount"], ["(amount)", "amount"], // No space beside `as`, and quoted names. ["cast((amount)as numeric)", "amount"], ['cast("amount"as numeric)', "amount"], ['cast(amount as"numeric")', "amount"], - ['"Amount"::int', "amount"], ['("a""b")::numeric', 'a"b'], ['("null")::int', "null"], + ['"Amount"::int', "amount"], ['("a""b")::numeric', 'a"b'], ['("null")::int', "null"], ['"a``b"::int', "a``b"], ['`a""b`::int', 'a""b'], // Qualified names that read like literals or keywords. ["q.end::int", "end"], ["cast(q.end as int)", "end"], ["(q.end)::int", "end"], ["q.null::int", "null"], ["q.true::int", "true"], ["cast(t.has as int)", "has"], ["allé::numeric", "allé"], From 46120132fbcfaac52bfba47e5d169e382551878f Mon Sep 17 00:00:00 2001 From: "Ralph Sto. Domingo" Date: Tue, 17 Nov 2026 20:45:27 +0800 Subject: [PATCH 9/9] docs: the cast grammar names a type modifier as a number or a name Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VEmKQwEiHMwQ7BdwgPo9sb --- packages/opencode/src/altimate/native/dbt/project-lineage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opencode/src/altimate/native/dbt/project-lineage.ts b/packages/opencode/src/altimate/native/dbt/project-lineage.ts index 3034e2f78d..4f1acbf3b6 100644 --- a/packages/opencode/src/altimate/native/dbt/project-lineage.ts +++ b/packages/opencode/src/altimate/native/dbt/project-lineage.ts @@ -231,7 +231,7 @@ const NOT_TYPE = * item = operand { "::" type } (at least one cast or one pair of parentheses) * operand = column | "(" item ")" | "cast" "(" item "as" type ")" * column = name { "." name } (a bare `null`, `true`, `false` or `end` is a literal) - * type = word { word | "." | "(" number { "," number } ")" | "[" [ number ] "]" } + * type = word { word | "." | "(" mod { "," mod } ")" | "[" [ number ] "]" } (mod = number | name) * * So `amount::numeric`, `(amount)::numeric`, `cast((amount)as numeric)`, `cast(amount as numeric)::int` and `q.end::int` * name a column; `amount::numeric + 1`, `coalesce(amount)::numeric`, `(1.5)::numeric` and `(null)::int` don't.