|
| 1 | +/** |
| 2 | + * dbt model lineage — column-level lineage from manifest + model name. |
| 3 | + * |
| 4 | + * Ported from Python altimate_engine.dbt.lineage. |
| 5 | + */ |
| 6 | + |
| 7 | +import * as fs from "fs" |
| 8 | +import * as core from "@altimateai/altimate-core" |
| 9 | +import type { |
| 10 | + DbtLineageParams, |
| 11 | + DbtLineageResult, |
| 12 | +} from "../../bridge/protocol" |
| 13 | + |
| 14 | +/** |
| 15 | + * Compute column-level lineage for a dbt model. |
| 16 | + * |
| 17 | + * Loads the manifest, finds the target model, extracts compiled SQL + upstream |
| 18 | + * schemas, and delegates to altimate-core's columnLineage(). |
| 19 | + */ |
| 20 | +export function dbtLineage(params: DbtLineageParams): DbtLineageResult { |
| 21 | + const emptyResult = (factors: string[]): DbtLineageResult => ({ |
| 22 | + model_name: params.model, |
| 23 | + raw_lineage: {}, |
| 24 | + confidence: "low", |
| 25 | + confidence_factors: factors, |
| 26 | + }) |
| 27 | + |
| 28 | + if (!fs.existsSync(params.manifest_path)) { |
| 29 | + return emptyResult(["Manifest file not found"]) |
| 30 | + } |
| 31 | + |
| 32 | + let manifest: any |
| 33 | + try { |
| 34 | + const raw = fs.readFileSync(params.manifest_path, "utf-8") |
| 35 | + manifest = JSON.parse(raw) |
| 36 | + } catch (e) { |
| 37 | + return emptyResult([`Failed to parse manifest: ${e}`]) |
| 38 | + } |
| 39 | + |
| 40 | + const nodes = manifest.nodes || {} |
| 41 | + const sources = manifest.sources || {} |
| 42 | + |
| 43 | + // Find target model by name or unique_id |
| 44 | + const modelNode = findModel(nodes, params.model) |
| 45 | + if (!modelNode) { |
| 46 | + return emptyResult([`Model '${params.model}' not found in manifest`]) |
| 47 | + } |
| 48 | + |
| 49 | + // Extract compiled SQL |
| 50 | + const sql = modelNode.compiled_code || modelNode.compiled_sql || "" |
| 51 | + if (!sql) { |
| 52 | + return emptyResult(["No compiled SQL found — run `dbt compile` first"]) |
| 53 | + } |
| 54 | + |
| 55 | + // Detect dialect |
| 56 | + let dialect = params.dialect |
| 57 | + if (!dialect) { |
| 58 | + dialect = detectDialect(manifest, modelNode) |
| 59 | + } |
| 60 | + |
| 61 | + // Build schema context from upstream dependencies |
| 62 | + const upstreamIds: string[] = modelNode.depends_on?.nodes || [] |
| 63 | + const schemaContext = buildSchemaContext(nodes, sources, upstreamIds) |
| 64 | + |
| 65 | + // Delegate to altimate-core column_lineage |
| 66 | + let rawLineage: Record<string, unknown> |
| 67 | + try { |
| 68 | + const schema = schemaContext |
| 69 | + ? core.Schema.fromJson(JSON.stringify(schemaContext)) |
| 70 | + : undefined |
| 71 | + const result = core.columnLineage(sql, dialect, schema) |
| 72 | + rawLineage = JSON.parse(JSON.stringify(result)) |
| 73 | + } catch (e) { |
| 74 | + rawLineage = { error: String(e) } |
| 75 | + } |
| 76 | + |
| 77 | + return { |
| 78 | + model_name: modelNode.name || params.model, |
| 79 | + model_unique_id: getUniqueId(nodes, params.model), |
| 80 | + compiled_sql: sql, |
| 81 | + raw_lineage: rawLineage, |
| 82 | + confidence: rawLineage.error ? "low" : "high", |
| 83 | + confidence_factors: rawLineage.error ? [String(rawLineage.error)] : [], |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +function findModel(nodes: Record<string, any>, model: string): any | null { |
| 88 | + if (model in nodes) return nodes[model] |
| 89 | + for (const [, node] of Object.entries(nodes)) { |
| 90 | + if (node.resource_type !== "model") continue |
| 91 | + if (node.name === model) return node |
| 92 | + } |
| 93 | + return null |
| 94 | +} |
| 95 | + |
| 96 | +function getUniqueId(nodes: Record<string, any>, model: string): string | undefined { |
| 97 | + if (model in nodes) return model |
| 98 | + for (const [nodeId, node] of Object.entries(nodes)) { |
| 99 | + if (node.resource_type === "model" && node.name === model) return nodeId |
| 100 | + } |
| 101 | + return undefined |
| 102 | +} |
| 103 | + |
| 104 | +function detectDialect(manifest: any, modelNode: any): string { |
| 105 | + const metadata = manifest.metadata || {} |
| 106 | + const adapter = metadata.adapter_type || "" |
| 107 | + if (adapter) { |
| 108 | + const dialectMap: Record<string, string> = { |
| 109 | + snowflake: "snowflake", |
| 110 | + bigquery: "bigquery", |
| 111 | + databricks: "databricks", |
| 112 | + spark: "spark", |
| 113 | + postgres: "postgres", |
| 114 | + redshift: "redshift", |
| 115 | + duckdb: "duckdb", |
| 116 | + } |
| 117 | + return dialectMap[adapter] || adapter |
| 118 | + } |
| 119 | + return "snowflake" |
| 120 | +} |
| 121 | + |
| 122 | +function buildSchemaContext( |
| 123 | + nodes: Record<string, any>, |
| 124 | + sources: Record<string, any>, |
| 125 | + upstreamIds: string[], |
| 126 | +): Record<string, any> | null { |
| 127 | + const tables: Record<string, any> = {} |
| 128 | + |
| 129 | + for (const uid of upstreamIds) { |
| 130 | + const node = nodes[uid] || sources[uid] |
| 131 | + if (!node) continue |
| 132 | + |
| 133 | + const tableName = node.alias || node.name || "" |
| 134 | + if (!tableName) continue |
| 135 | + |
| 136 | + const columnsDict = node.columns || {} |
| 137 | + if (Object.keys(columnsDict).length === 0) continue |
| 138 | + |
| 139 | + const cols = Object.entries(columnsDict).map(([colName, col]: [string, any]) => ({ |
| 140 | + name: col.name || colName, |
| 141 | + type: col.data_type || col.type || "", |
| 142 | + })) |
| 143 | + |
| 144 | + if (cols.length > 0) { |
| 145 | + tables[tableName] = { columns: cols } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if (Object.keys(tables).length === 0) return null |
| 150 | + return { tables, version: "1" } |
| 151 | +} |
0 commit comments