Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion packages/opencode/src/altimate/native/dbt/lineage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,44 @@ import * as core from "@altimateai/altimate-core"
import type { DbtLineageParams, DbtLineageResult } from "../types"
import { loadRawManifest, findModel, detectDialect, buildSchemaContext } from "./helpers"
import { lineageGraph } from "./graph"
import { projectLineage } from "./project-lineage"
import { projectLineage, stripSql, warehouseNames } from "./project-lineage"

/**
* The lineage with the model's own unaliased casts of a column under the column's name on Postgres and Redshift, as
* impact_analysis's column answer names them (`warehouseNames`): a renamed `_col_N` takes the name, and its `_N`
* twin goes. Every other output, and every other dialect, is left as the engine gives it.
*/
function withWarehouseNames(lineage: Record<string, any>, sql: string, dialect: string): Record<string, unknown> {
if ((dialect !== "postgres" && dialect !== "redshift") || !Array.isArray(lineage.column_lineage)) return lineage
const lower = (text: unknown) => String(text ?? "").toLowerCase()
const edges = lineage.column_lineage.map((e: any) => ({ source: lower(e?.source), to: lower(e?.target) }))
const names = warehouseNames(edges, stripSql(sql), dialect)
const renamed = new Map<string, string>()
for (const [i, e] of edges.entries()) if (names[i] && names[i] !== e.to) renamed.set(e.to, names[i]!)
if (!renamed.size) return lineage
// The `_N` twin of a renamed `_col_N` goes with it, where `warehouseNames` drops every edge into it; a `_N` the SQL
// writes (`amount as _1`) keeps its edges there, and so stays.
const dropped = new Set(
[...renamed.keys()]
.map((label) => `_${label.slice(5)}`)
.filter((label) => edges.some((e) => e.to === label) && edges.every((e, i) => e.to !== label || names[i] === null)),
)
const target = (text: unknown) => (dropped.has(lower(text)) ? null : (renamed.get(lower(text)) ?? String(text)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[🟠 MEDIUM] If text is null or undefined (which is possible given the e?.target usage during target(e?.target) calls), String(text) will cast it to the literal strings "null" or "undefined". It is safer to handle nullish values explicitly to avoid assigning stringified undefined values to the output.

Suggested change:

Suggested change
const target = (text: unknown) => (dropped.has(lower(text)) ? null : (renamed.get(lower(text)) ?? String(text)))
const target = (text: unknown) => (dropped.has(lower(text)) ? null : (renamed.get(lower(text)) ?? (text != null ? String(text) : text)))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving this one. It can't happen: the engine's column-lineage edges always carry a string target, and this pass only runs on Postgres and Redshift when a cast was renamed. The ?. is only the defensive access the module uses everywhere.

const out: Record<string, unknown> = { ...lineage }
out.column_lineage = lineage.column_lineage.flatMap((e: any) => (target(e?.target) === null ? [] : [{ ...e, target: target(e?.target) }]))
if (lineage.column_dict && typeof lineage.column_dict === "object") {
// No prototype, so an output named `__proto__` is an entry like any other.
const dict: Record<string, unknown> = Object.create(null)
for (const [key, sources] of Object.entries<any>(lineage.column_dict)) {
Comment on lines +18 to +39

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[🔵 LOW] According to the TypeScript Types guidelines, the any type should be avoided. There are multiple instances of any in this function (e.g., lineage: Record<string, any>, (e: any), Object.entries<any>). Please consider defining a specific interface or type for the lineage parameter, or add a comment explaining why any is necessary in this context.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving the any as it is: the raw engine lineage is untyped JSON, and this follows the same shape as withoutPlaceholders beside it in the same file.

const name = target(key)
if (name === null) continue
const had = dict[name]
dict[name] = Array.isArray(had) && Array.isArray(sources) ? [...new Set([...had, ...sources])] : sources
}
out.column_dict = dict
}
return out
}

/**
* The lineage with the placeholder columns taken out, and the parents whose placeholder reached the
Expand Down Expand Up @@ -173,6 +210,7 @@ export function dbtLineage(params: DbtLineageParams): DbtLineageResult {
const schema = Object.keys(tables).length ? core.Schema.fromJson(JSON.stringify({ version: "1", tables })) : undefined
const result = core.columnLineage(sql, dialect, schema)
;({ lineage: rawLineage, starred: missed } = withoutPlaceholders(JSON.parse(JSON.stringify(result)), placeholders))
rawLineage = withWarehouseNames(rawLineage, String(sql), dialect)
} catch (e) {
rawLineage = { error: String(e) }
}
Expand Down
Loading
Loading