Skip to content

Commit bcf3981

Browse files
fix(opencode): bind manifest-answer edges by schema name first, keep dotted columns, honour late cancels
A parent's schema name (`alias || name`) now wins over another parent's plain name or identifier. An output column named `"customer.id"` stays whole. A lookup that resolves after a cancel rejects. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VEmKQssMKzdJnQoPnWoCBf
1 parent 8ee906a commit bcf3981

4 files changed

Lines changed: 98 additions & 17 deletions

File tree

‎packages/opencode/src/altimate/native/dbt/lineage.ts‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -214,24 +214,27 @@ function outputColumns(sql: string, dialect: string, name: string, lineage: Reco
214214
}
215215

216216
/**
217-
* Each parent's table names, lowercased, as altimate-core can report them: the keys
218-
* buildSchemaContext uses (alias, then name), then a source's identifier. The first parent to
219-
* claim a name keeps it. `shared` holds the parents whose schema name (`alias || name`) another
220-
* parent has too: the schema keeps only one of them, so a column can bind to the wrong one.
217+
* Each parent's table names, lowercased, as altimate-core can report them. The schema names
218+
* buildSchemaContext uses (`alias || name`) are claimed first, then the other names and a source's
219+
* identifier; the first parent to claim a name keeps it. `shared` holds the parents whose schema
220+
* name another parent has too: the schema keeps only one of them, so a column can bind to the wrong one.
221221
*/
222222
function tablesOf(parentIds: string[], nodes: Record<string, any>, sources: Record<string, any>) {
223223
const byTable = new Map<string, string>()
224224
const bySchemaName = new Map<string, string[]>()
225+
const claim = (key: unknown, uid: string) => {
226+
if (typeof key === "string" && key && !byTable.has(key.toLowerCase())) byTable.set(key.toLowerCase(), uid)
227+
}
228+
const nodeOf = (uid: string) => nodes[uid] ?? sources[uid]
225229
for (const uid of parentIds) {
226-
const node = nodes[uid] ?? sources[uid]
227-
const schemaName = node?.alias || node?.name
228-
if (typeof schemaName === "string" && schemaName) {
229-
const key = schemaName.toLowerCase()
230-
bySchemaName.set(key, [...(bySchemaName.get(key) ?? []), uid])
231-
}
232-
for (const key of [node?.alias, node?.name, node?.identifier]) {
233-
if (typeof key === "string" && key && !byTable.has(key.toLowerCase())) byTable.set(key.toLowerCase(), uid)
234-
}
230+
const schemaName = nodeOf(uid)?.alias || nodeOf(uid)?.name
231+
if (typeof schemaName !== "string" || !schemaName) continue
232+
claim(schemaName, uid)
233+
const key = schemaName.toLowerCase()
234+
bySchemaName.set(key, [...(bySchemaName.get(key) ?? []), uid])
235+
}
236+
for (const uid of parentIds) {
237+
for (const key of [nodeOf(uid)?.alias, nodeOf(uid)?.name, nodeOf(uid)?.identifier]) claim(key, uid)
235238
}
236239
const shared = new Set([...bySchemaName.values()].filter((ids) => ids.length > 1).flat())
237240
return { byTable, shared }
@@ -275,10 +278,8 @@ export function lineageEdges(
275278
const from = nameParts(String(e?.source ?? ""))
276279
const column = from.length >= 2 ? from[from.length - 1].toLowerCase() : ""
277280
const table = from.length >= 2 ? from[from.length - 2].toLowerCase() : ""
278-
const target =
279-
nameParts(String(e?.target ?? ""))
280-
.pop()
281-
?.toLowerCase() ?? ""
281+
// The target is an output column's name, which can contain a dot (`as "customer.id"`).
282+
const target = String(e?.target ?? "").toLowerCase()
282283
const parent = byTable.get(table)
283284
if (!parent || !column || !target) {
284285
errors.push(

‎packages/opencode/src/altimate/tools/dbt-lineage.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ async function fromAdapter(params: LineageParams, signal: AbortSignal): Promise<
142142
if (adapter.isInitialized?.() === false) return { reason: "the dbt adapter didn't load the project" }
143143
if (typeof adapter.getModelLineage !== "function") return { reason: "dbt-integration has no getModelLineage" }
144144
const result: unknown = await adapter.getModelLineage(params, signal)
145+
// A lookup that finishes after a cancel doesn't answer.
146+
signal.throwIfAborted()
145147
if (!isModelLineage(result)) return { reason: "dbt-integration returned a lineage this tool can't read" }
146148
return { result, reason: "" }
147149
} catch (e) {

‎packages/opencode/test/altimate/dbt-lineage-structured.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,24 @@ describe("dbt_lineage without dbt-integration's lineage", () => {
349349
}
350350
})
351351

352+
test("doesn't answer with a lookup that finishes after the call was cancelled", async () => {
353+
useConfig()
354+
const controller = new AbortController()
355+
dbtAdapterInternals.create = async () =>
356+
fakeAdapter(true, async () => {
357+
controller.abort()
358+
return TRACE
359+
})
360+
const call = manifestOnly()
361+
try {
362+
const tool = await initTool(DbtLineageTool)
363+
await expect(tool.execute({ model: "customers" }, { ...ctx, abort: controller.signal })).rejects.toThrow()
364+
expect(call).not.toHaveBeenCalled()
365+
} finally {
366+
call.mockRestore()
367+
}
368+
})
369+
352370
test("stops on a call cancelled while the fallback looks for the manifest", async () => {
353371
const controller = new AbortController()
354372
useConfig()

‎packages/opencode/test/altimate/dbt-model-lineage.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,66 @@ join "jaffle"."main"."stg_customers" c on c.customer_id = o.customer_id`)
170170
)
171171
})
172172

173+
// Parents whose name, alias and schema name overlap across parents.
174+
const withParents = (sql: string, parents: [string, string][]) => {
175+
const manifest = lineageManifest(sql) as ReturnType<typeof lineageManifest> & { nodes: Record<string, any> }
176+
for (const [name, alias] of parents) {
177+
manifest.nodes[`model.jaffle_shop.${name}`] = {
178+
unique_id: `model.jaffle_shop.${name}`,
179+
resource_type: "model",
180+
name,
181+
alias,
182+
config: { materialized: "table" },
183+
depends_on: { nodes: [] },
184+
columns: { id: { name: "id" } },
185+
}
186+
}
187+
manifest.nodes[CUSTOMERS].depends_on.nodes = parents.map(([name]) => `model.jaffle_shop.${name}`)
188+
return manifest
189+
}
190+
191+
test("maps a table name to the parent whose schema name it is before another's plain name", async () => {
192+
const manifest = withParents(`select b.id from "jaffle"."main"."orders" b`, [
193+
["orders", "historical_orders"],
194+
["current_orders", "orders"],
195+
])
196+
const result = dbtModelLineage({ manifest_path: await manifestFile("precedence", manifest), model: "customers" })
197+
expect(result.columnLineage.map((e) => e.source)).toEqual([["model.jaffle_shop.current_orders", "id"]])
198+
expect(result.errors).toEqual([])
199+
})
200+
201+
test("keeps the edges of a parent whose schema name another parent's shared name shadows", async () => {
202+
const manifest = withParents(
203+
`select o.id as clean_id, d.id as dup_id from "jaffle"."main"."orders" o join "jaffle"."main"."dup" d on d.id = o.id`,
204+
[
205+
["orders", "dup"],
206+
["other_orders", "dup"],
207+
["clean_orders", "orders"],
208+
],
209+
)
210+
const result = dbtModelLineage({ manifest_path: await manifestFile("shadowed", manifest), model: "customers" })
211+
expect(result.errors).toEqual([
212+
"customers reads model.jaffle_shop.orders, model.jaffle_shop.other_orders, which share a table name in altimate-core's schema, so their column edges into it were left out",
213+
])
214+
expect(result.columnLineage.map((e) => e.source)).toEqual([["model.jaffle_shop.clean_orders", "id"]])
215+
})
216+
217+
test("keeps a dotted output column's name whole", async () => {
218+
const sql = `select c.customer_id as "customer.id" from "jaffle"."main"."stg_customers" c`
219+
const result = dbtModelLineage({
220+
manifest_path: await manifestFile("dotted", lineageManifest(sql)),
221+
model: "customers",
222+
column: "customer.id",
223+
})
224+
expect(result.nodes[0].columns.map((c) => c.name)).toEqual(["customer.id"])
225+
expect(result.columnLineage.map((e) => [e.source, e.target])).toEqual([
226+
[
227+
[STG_CUSTOMERS, "customer_id"],
228+
[CUSTOMERS, "customer.id"],
229+
],
230+
])
231+
})
232+
173233
test("lists the model without columns when the manifest has no compiled SQL", async () => {
174234
const result = dbtModelLineage({
175235
manifest_path: await manifestFile("uncompiled", lineageManifest(null)),

0 commit comments

Comments
 (0)