diff --git a/packages/opencode/src/altimate/api/sql-lineage.ts b/packages/opencode/src/altimate/api/sql-lineage.ts new file mode 100644 index 0000000000..8936a86ddc --- /dev/null +++ b/packages/opencode/src/altimate/api/sql-lineage.ts @@ -0,0 +1,49 @@ +import { AltimateApi } from "./client" + +/** One parent relation handed to the backend: its warehouse location and its columns. */ +export type SqlLineageModelNode = { + database: string + schema: string + name: string + alias: string + uniqueId: string + columns: Record + path: string | undefined +} + +export type SqlLineageRequest = { + compiled_sql: string + model_info: { model_node: SqlLineageModelNode }[] + model_dialect: string + session_id: string +} + +/** + * One node of the backend's graph, keyed in `details` by an id that edges use. A CTE or the final select carries `sql`; a relation, join, filter, group, sort or + * subquery carries `expression`. A relation, subquery, CTE and the final select have a `name`; a join, filter, group or sort has none. + */ +export type SqlLineageNodeDetails = { + name?: string + type: string + nodeType?: string + nodeId?: string + sql?: string + expression?: string + /** A relation's warehouse database (catalog) and schema. */ + db?: string + schema?: string + columns?: { name: string; datatype?: string; expression?: string }[] +} + +export type SqlLineageResponse = { + tableEdges: [string, string][] + details: Record + nodePositions?: Record +} + +export const SQL_LINEAGE_ENDPOINT = "/dbt/v3/sql_lineage" + +/** The backend's graph of a compiled model's CTEs, joins and filters. */ +export async function sqlLineage(req: SqlLineageRequest, signal?: AbortSignal): Promise { + return (await AltimateApi.apiRequest("POST", SQL_LINEAGE_ENDPOINT, req, signal)) as SqlLineageResponse +} diff --git a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts index 0c4db3c980..c3a950902c 100644 --- a/packages/opencode/src/altimate/tools/dbt-sql-graph.ts +++ b/packages/opencode/src/altimate/tools/dbt-sql-graph.ts @@ -1,16 +1,388 @@ import z from "zod" +import crypto from "crypto" +import fs from "fs" +import path from "path" import { Tool } from "../../tool/tool" +import { + sqlLineage, + type SqlLineageModelNode, + type SqlLineageNodeDetails, + type SqlLineageRequest, + type SqlLineageResponse, +} from "../api/sql-lineage" +import { dbtAdapterFor } from "./dbt-adapter-access" + +const FINAL_SELECT = "__final_select__" + +export type SqlGraphNode = { + /** Unique in the graph; the edges name nodes by it. */ + id: string + /** What the node is called on screen; two nodes can share one (a relation used twice). */ + name: string + /** The backend's node type ("cte", "table", ...); a relation that maps to a dbt node carries its resource type ("model", "source", ...). */ + type: string + nodeId?: string + sql: string + columns: { name: string; datatype?: string; expression?: string }[] +} + +export type SqlGraph = { nodes: SqlGraphNode[]; edges: [string, string][] } + +type ManifestNode = { + unique_id: string + name: string + alias: string + resource_type: string + path?: string + database: string + schema: string + columns: SqlLineageModelNode["columns"] +} +type ManifestSource = { + name: string + database: string + schema: string + tables: { name: string; identifier: string; path?: string; columns: SqlLineageModelNode["columns"] }[] +} + +/** The slice of dbt-integration's adapter this tool uses. */ +export type SqlGraphAdapter = { + parseManifest(): Promise< + | { + nodeMetaMap: { + lookupByUniqueId(id: string): ManifestNode | undefined + lookupByBaseName(name: string): ManifestNode | undefined + } + sourceMetaMap: { get(name: string): ManifestSource | undefined } + } + | undefined + > + unsafeCompileQuery(query: string, originalModelName?: string): Promise + getNonEphemeralParents(keys: string[]): string[] + getBulkSchemaFromDB( + nodes: { unique_id: string; name: string; resource_type: string; table?: string }[], + signal: AbortSignal, + ): Promise> + getAdapterType(): string +} + +export type SqlGraphDeps = { + fetchLineage: (req: SqlLineageRequest, signal?: AbortSignal) => Promise + readFile: (file: string) => string + /** The dbt project's root, to resolve a model's project-relative path. */ + projectRoot: string | undefined + /** Names the caller's session in the backend's session id, with the SQL's hash. */ + sessionId: string +} + +/** Adapters whose unquoted identifiers fold to lower case; the rest fold to upper case. */ +const LOWER_FOLDING = new Set(["trino", "athena", "postgres", "redshift", "duckdb", "risingwave"]) + +/** A name the warehouse would only keep as written if it were quoted: it is not in the adapter's own folding case. */ +function isQuotedIdentifier(name: string, adapter: string): boolean { + return LOWER_FOLDING.has(adapter) ? !/^([_a-z]+[_a-z0-9$]*)$/.test(name) : !/^([_A-Z]+[_A-Z0-9$]*)$/.test(name) +} + +/** A column name as dbt Power User keys it: lower case unless the adapter would only keep it as written when quoted. */ +function columnKey(name: string, adapter: string): string { + return isQuotedIdentifier(name, adapter) ? name : name.toLowerCase() +} + +/** The parent's manifest columns plus any the warehouse reports that the manifest lacks. */ +export function mergeColumns( + manifestColumns: SqlLineageModelNode["columns"], + fromDb: { column: string; dtype: string }[] | undefined, + adapter: string, +): SqlLineageModelNode["columns"] { + // A Map: a column named `constructor` or `__proto__` is a column, not an inherited property. + const merged = new Map() + // A manifest name is documentation spelling: it is case-sensitive only when the column is declared `quote: true`. + const documented = new Set() + for (const [k, v] of Object.entries(manifestColumns ?? {})) { + const quoted = (v as { quote?: boolean }).quote === true + if (!quoted) documented.add(k.toLowerCase()) + merged.set(quoted ? columnKey(k, adapter) : k.toLowerCase(), { ...v }) + } + for (const c of fromDb ?? []) { + let key = columnKey(c.column, adapter) + // The warehouse's own spelling of a column the manifest documents (DuckDB keeps `ID` for a column documented as `id`) is that column. + if (!merged.has(key) && documented.has(c.column.toLowerCase())) key = c.column.toLowerCase() + const existing = merged.get(key) + if (existing) { + existing.data_type = (existing.data_type || c.dtype)?.toLowerCase() + continue + } + merged.set(key, { name: key, data_type: c.dtype?.toLowerCase(), description: "", meta: {} }) + } + return Object.fromEntries(merged) +} + +/** + * The backend names the model's own select `__final_select__`: name it after the model (or ` (final select)` when the SQL has a node of that name), tie each + * relation node to the dbt node it stands for, and drop the layout positions the list doesn't use. + */ +export function remapSqlLineage( + response: SqlLineageResponse, + model: { name: string; uniqueId: string; resourceType: string }, + parents: { + uniqueId: string + alias?: string + sourceTable?: string + schema?: string + database?: string + resourceType: string + }[], +): SqlGraph { + // Null-prototype maps: a model or relation named `__proto__` is an ordinary key. + const details: Record = Object.assign(Object.create(null), response.details) + const mapping: Record = Object.create(null) + // A relation that more than one parent could be is tied to none: an arbitrary dbt node would be shown as the real one. + const claimed = new Map>() + type Parent = (typeof parents)[number] + // The backend keys a relation by its location and a per-occurrence suffix, so the match is on its name, in the parent's database and schema when both say one; + // every occurrence of the relation in the SQL stands for the same dbt node. A quoted `Foo` and `FOO` can be two relations: + // an occurrence goes to the parent whose name it matches exactly, and other occurrences match ignoring case unless another parent owns them exactly. + const matches = (q: Parent, fold: (v: string) => string) => + Object.entries(details).filter(([k, d]) => { + const w = q.sourceTable ?? q.alias ?? "" + if (d.type !== "table" || fold(d.name ?? k) !== fold(w)) return false + if (d.schema && q.schema && fold(d.schema) !== fold(q.schema)) return false + if (d.db && q.database && fold(d.db) !== fold(q.database)) return false + return true + }) + // A source's relation is its table's identifier; any other node's, its alias. + const named = parents.filter((q) => q.sourceTable ?? q.alias) + const exact = new Map>(named.map((q) => [q, new Set(matches(q, (v) => v).map(([k]) => k))])) + for (const p of named) { + const mine = exact.get(p)! + for (const [k] of matches(p, (v) => v.toLowerCase())) { + if (!mine.has(k) && named.some((q) => q !== p && exact.get(q)!.has(k))) continue + claimed.set(k, (claimed.get(k) ?? new Set()).add(p.uniqueId)) + mapping[k] = { nodeId: p.uniqueId, type: p.resourceType } + } + } + for (const [k, owners] of claimed) if (owners.size > 1) delete mapping[k] + // A CTE the SQL names like the model (`with customers as (…)`) keeps its node: the model's own select is told apart. + // The replacement key is one no node already uses, so it can't overwrite another CTE. + let finalKey = model.name + while (Object.hasOwn(details, finalKey)) finalKey = `${finalKey} (final select)` + mapping[finalKey] = { nodeId: model.uniqueId, type: model.resourceType } + const edges = response.tableEdges.map( + (edge) => edge.map((item) => (item === FINAL_SELECT ? finalKey : item)) as [string, string], + ) + if (details[FINAL_SELECT]) { + details[finalKey] = details[FINAL_SELECT] + delete details[FINAL_SELECT] + } + const nodes: SqlGraphNode[] = Object.entries(details).map(([key, d]) => { + const merged = { ...d, ...mapping[key] } + return { + // The key is the node's identity (edges use it); the name is only what is shown. + id: key, + name: key === finalKey ? finalKey : (merged.name ?? key), + type: merged.type, + ...(merged.nodeId ? { nodeId: merged.nodeId } : {}), + sql: merged.sql ?? merged.expression ?? "", + columns: merged.columns ?? [], + } + }) + return { nodes, edges } +} + +/** The promise's result, or an abort as soon as the signal fires: the backend call itself is bounded by the API client's own timeout. */ +function untilAborted(work: Promise, signal: AbortSignal): Promise { + if (signal.aborted) { + work.catch(() => {}) + return Promise.reject(signal.reason) + } + return new Promise((resolve, reject) => { + const onAbort = () => reject(signal.reason) + signal.addEventListener("abort", onAbort, { once: true }) + work.then(resolve, reject).finally(() => signal.removeEventListener("abort", onAbort)) + }) +} + +/** Compile the model, read its parents' columns, ask the backend, and return the remapped graph. */ +export async function buildSqlGraph( + adapter: SqlGraphAdapter, + model: string, + deps: SqlGraphDeps, + signal: AbortSignal, +): Promise { + signal.throwIfAborted() + const manifest = await untilAborted(adapter.parseManifest(), signal) + signal.throwIfAborted() + if (!manifest) throw new Error("the dbt manifest is not available") + const node = manifest.nodeMetaMap.lookupByUniqueId(model) ?? manifest.nodeMetaMap.lookupByBaseName(model) + if (!node) throw new Error(`no model named ${model} in the manifest`) + // A seed, snapshot or analysis shares the node map: only a model's .sql file is compiled and sent. + if (node.resource_type !== "model" || !node.path?.toLowerCase().endsWith(".sql")) + throw new Error(`${node.name} is not a dbt model with a SQL file`) + const file = path.isAbsolute(node.path) ? node.path : path.join(deps.projectRoot ?? "", node.path) + signal.throwIfAborted() + const compiled = await untilAborted(adapter.unsafeCompileQuery(deps.readFile(file), node.name), signal) + signal.throwIfAborted() + if (!compiled) throw new Error(`unable to compile ${node.name}`) + + const parentIds = adapter.getNonEphemeralParents([node.unique_id]) + const requests: { unique_id: string; name: string; resource_type: string; table?: string }[] = [] + const infos: SqlLineageModelNode[] = [] + const parents: { + uniqueId: string + alias?: string + sourceTable?: string + schema?: string + database?: string + resourceType: string + }[] = [] + for (const id of parentIds) { + const parts = id.split(".") + if (parts[0] === "source") { + const source = manifest.sourceMetaMap.get(parts[2]) + const table = source?.tables.find((t) => t.name === parts[3]) + if (!source || !table) continue + requests.push({ unique_id: id, name: source.name, resource_type: "source", table: table.name }) + infos.push({ + database: source.database, + schema: source.schema, + name: table.name, + alias: table.identifier, + uniqueId: id, + columns: table.columns, + path: table.path, + }) + parents.push({ + uniqueId: id, + sourceTable: table.identifier || table.name, + schema: source.schema, + database: source.database, + resourceType: "source", + }) + continue + } + const parent = manifest.nodeMetaMap.lookupByUniqueId(id) + if (!parent) continue + requests.push({ unique_id: id, name: parent.name, resource_type: parent.resource_type }) + infos.push({ + database: parent.database, + schema: parent.schema, + name: parent.name, + alias: parent.alias, + uniqueId: id, + columns: parent.columns, + path: parent.path, + }) + parents.push({ + uniqueId: id, + alias: parent.alias, + schema: parent.schema, + database: parent.database, + resourceType: parent.resource_type, + }) + } + // The warehouse metadata read: once per call, only for the parents. + const fromDb = requests.length ? await untilAborted(adapter.getBulkSchemaFromDB(requests, signal), signal) : {} + signal.throwIfAborted() + const hash = crypto.createHash("md5").update(compiled).digest("hex") + const dialect = adapter.getAdapterType() + const response = await untilAborted( + deps.fetchLineage( + { + compiled_sql: compiled, + model_info: infos.map((n) => ({ + model_node: { ...n, columns: mergeColumns(n.columns, fromDb[n.uniqueId], dialect) }, + })), + model_dialect: dialect, + session_id: `${deps.sessionId}-${hash}`, + }, + signal, + ), + signal, + ) + signal.throwIfAborted() + const isRecord = (v: unknown) => typeof v === "object" && v !== null && !Array.isArray(v) + if ( + !Array.isArray(response?.tableEdges) || + !isRecord(response.details) || + !response.tableEdges.every((e) => Array.isArray(e) && e.length === 2 && e.every((x) => typeof x === "string")) || + !Object.values(response.details).every((d) => { + if (!isRecord(d) || typeof (d as { type?: unknown }).type !== "string") return false + const columns = (d as { columns?: unknown }).columns + return ( + columns === undefined || + (Array.isArray(columns) && + columns.every((c) => isRecord(c) && typeof (c as { name?: unknown }).name === "string")) + ) + }) || + !response.tableEdges.every((e) => e.every((x) => Object.hasOwn(response.details, x))) + ) + throw new Error("the backend returned no SQL graph") + return remapSqlLineage( + response, + { name: node.name, uniqueId: node.unique_id, resourceType: node.resource_type }, + parents, + ) +} + +/** The graph as a list: a line per node with its type, column count and inputs, then its column names and SQL beneath it. */ +export function formatSqlGraph(model: string, graph: SqlGraph): string { + const inputs = new Map() + const nameOf = new Map(graph.nodes.map((n) => [n.id, n.name])) + for (const [from, to] of graph.edges) inputs.set(to, [...(inputs.get(to) ?? []), nameOf.get(from) ?? from]) + const lines = [`SQL graph for ${model}: ${graph.nodes.length} nodes, ${graph.edges.length} edges`] + for (const n of graph.nodes) { + const from = inputs.get(n.id) + lines.push( + `- ${n.name} (${n.type}${n.nodeId ? `, ${n.nodeId}` : ""}): ${n.columns.length} columns${from?.length ? `, from ${from.join(", ")}` : ""}`, + ) + if (n.columns.length) lines.push(` columns: ${n.columns.map((c) => c.name).join(", ")}`) + if (n.sql) + lines.push(` sql: ${n.sql.length > 400 ? `${n.sql.slice(0, 400)}…` : n.sql}`.replace(/\s*\n\s*/g, " ")) + } + return lines.join("\n") +} export const DbtSqlGraphTool = Tool.define("dbt_sql_graph", { - description: "A dbt model's SQL as a graph: its CTEs, joins and filters, and the edges between them.", + description: + "The CTEs, joins and filters of a dbt model as a graph, from the Altimate backend: nodes with their SQL and columns, and the edges between them. Compiles the model with the project's dbt integration, reads its parent relations' columns from the warehouse's metadata, and sends the compiled SQL to the Altimate backend. Needs write permission (starts the dbt integration) and an Altimate key.", parameters: z.object({ - model: z.string().describe("Model name"), + model: z.string().describe("Model name or unique_id (e.g. 'orders', 'model.shop.orders')"), }), - async execute() { - return { - title: "dbt_sql_graph: not implemented", - metadata: { success: false, error: "not implemented" }, - output: "dbt_sql_graph is not implemented in this build of altimate-code yet.", + async execute(args, ctx) { + const access = await dbtAdapterFor(ctx) + if (!access.adapter) { + return { + title: "dbt SQL graph: not available", + metadata: { success: false, model: args.model, reason: access.reason, error: access.reason }, + output: `Not available: ${access.reason}`, + } + } + try { + const { getDbtProjectRoot } = await import("../native/connections/register") + const graph = await buildSqlGraph( + access.adapter as SqlGraphAdapter, + args.model, + { + fetchLineage: sqlLineage, + readFile: (f) => fs.readFileSync(f, "utf8"), + projectRoot: await getDbtProjectRoot(), + sessionId: ctx.sessionID, + }, + ctx.abort, + ) + return { + title: `dbt SQL graph: ${args.model} (${graph.nodes.length} nodes)`, + metadata: { success: true, model: args.model, nodes: graph.nodes, edges: graph.edges }, + output: formatSqlGraph(args.model, graph), + } + } catch (e) { + if (ctx.abort.aborted) throw e + const msg = e instanceof Error ? e.message : String(e) + return { + title: "dbt SQL graph: ERROR", + metadata: { success: false, model: args.model, error: msg }, + output: `Failed: ${msg}`, + } } }, }) diff --git a/packages/opencode/test/altimate/dbt-sql-graph.test.ts b/packages/opencode/test/altimate/dbt-sql-graph.test.ts new file mode 100644 index 0000000000..58d6ae311e --- /dev/null +++ b/packages/opencode/test/altimate/dbt-sql-graph.test.ts @@ -0,0 +1,847 @@ +// altimate_change - new file +// +// dbt_sql_graph: the request built from the compiled model and its parents' columns, and the +// remap of the backend's reply (`__final_select__` to the model's node, relation nodes to dbt unique ids). +// The adapter and the endpoint are fakes, so no dbt, warehouse or backend is involved. +import { afterEach, describe, expect, test } from "bun:test" +import { mkdtemp, rm, writeFile } from "node:fs/promises" +import { realpathSync } from "node:fs" +import os from "node:os" +import path from "node:path" +import { Effect } from "effect" +import { SessionID, MessageID } from "@/session/schema" +import type { Tool } from "@/tool/tool" +import { dbtAdapterInternals, resetDbtAdapter } from "@/altimate/native/connections/register" +import { initTool } from "./tool-fixture" +import { + buildSqlGraph, + formatSqlGraph, + mergeColumns, + remapSqlLineage, + DbtSqlGraphTool, + type SqlGraphAdapter, +} from "@/altimate/tools/dbt-sql-graph" +import type { SqlLineageRequest, SqlLineageResponse } from "@/altimate/api/sql-lineage" + +const col = (name: string, data_type = "") => ({ name, description: "", data_type, meta: {} }) + +const orders = { + unique_id: "model.shop.orders", + name: "orders", + alias: "orders", + resource_type: "model", + path: "/proj/models/orders.sql", + database: "db", + schema: "analytics", + columns: {}, +} +const stgCustomers = { + unique_id: "model.shop.stg_customers", + name: "stg_customers", + alias: "stg_customers", + resource_type: "model", + path: "/proj/models/stg_customers.sql", + database: "db", + schema: "analytics", + columns: { id: col("id", "int") }, +} +const rawOrders = { + name: "raw", + database: "db", + schema: "raw", + tables: [{ name: "raw_orders", identifier: "RAW_ORDERS_T", columns: {} }], +} + +function adapter(overrides: Partial = {}): SqlGraphAdapter & { calls: string[] } { + const calls: string[] = [] + const nodes = [orders, stgCustomers] + return { + calls, + parseManifest: async () => ({ + nodeMetaMap: { + lookupByUniqueId: (id) => nodes.find((n) => n.unique_id === id), + lookupByBaseName: (name) => nodes.find((n) => n.name === name), + }, + sourceMetaMap: { get: (name) => (name === "raw" ? rawOrders : undefined) }, + }), + unsafeCompileQuery: async (q) => { + calls.push(`compile:${q}`) + return "with o as (select * from raw.orders) select * from o join stg_customers c on 1=1" + }, + getNonEphemeralParents: () => ["source.shop.raw.raw_orders", "model.shop.stg_customers"], + getBulkSchemaFromDB: async (requested) => { + calls.push(`schema:${requested.map((r) => r.unique_id).join(",")}`) + return { + "source.shop.raw.raw_orders": [ + { column: "ID", dtype: "INT" }, + { column: "AMOUNT", dtype: "NUMBER" }, + ], + "model.shop.stg_customers": [ + { column: "ID", dtype: "INT" }, + { column: "NAME", dtype: "TEXT" }, + ], + } + }, + getAdapterType: () => "snowflake", + ...overrides, + } +} + +const reply: SqlLineageResponse = { + tableEdges: [ + ["raw_orders", "o"], + ["o", "__final_select__"], + ["stg_customers", "__final_select__"], + ], + details: { + raw_orders: { name: "RAW_ORDERS_T", type: "table", columns: [{ name: "id" }, { name: "amount" }] }, + o: { name: "o", type: "cte", sql: "select * from raw.orders", columns: [{ name: "id" }] }, + stg_customers: { name: "stg_customers", type: "table", sql: "", columns: [{ name: "id" }, { name: "name" }] }, + __final_select__: { name: "__final_select__", type: "final", sql: "select * from o", columns: [{ name: "id" }] }, + }, + nodePositions: { o: [1, 1], __final_select__: [2, 2] }, +} + +const signal = new AbortController().signal + +describe("mergeColumns", () => { + test("adds warehouse columns the manifest lacks, lower-cased, and fills a missing type", () => { + const merged = mergeColumns( + { id: col("id") }, + [ + { column: "ID", dtype: "INT" }, + { column: "NEW_ONE", dtype: "TEXT" }, + ], + "snowflake", + ) + expect(Object.keys(merged)).toEqual(["id", "new_one"]) + expect(merged.id.data_type).toBe("int") + expect(merged.new_one).toEqual({ name: "new_one", data_type: "text", description: "", meta: {} }) + }) + + test("keeps a quoted name as written and tolerates no warehouse answer", () => { + expect(Object.keys(mergeColumns({}, [{ column: '"MixedCase"', dtype: "int" }], "snowflake"))).toEqual([ + '"MixedCase"', + ]) + expect(mergeColumns({ a: col("a", "int") }, undefined, "snowflake").a.data_type).toBe("int") + }) + + test("a name outside the adapter's folding case stays distinct: Postgres Foo and foo, Snowflake Foo and FOO", () => { + const pg = mergeColumns( + {}, + [ + { column: "Foo", dtype: "int" }, + { column: "foo", dtype: "text" }, + ], + "postgres", + ) + expect(Object.keys(pg)).toEqual(["Foo", "foo"]) + expect(pg.Foo.data_type).toBe("int") + expect(pg.foo.data_type).toBe("text") + const sf = mergeColumns( + {}, + [ + { column: "Foo", dtype: "int" }, + { column: "FOO", dtype: "text" }, + ], + "snowflake", + ) + expect(Object.keys(sf)).toEqual(["Foo", "foo"]) + }) + + test("Redshift folds to lower case like Postgres: FOO and foo stay distinct columns", () => { + const rs = mergeColumns( + {}, + [ + { column: "FOO", dtype: "int" }, + { column: "foo", dtype: "text" }, + ], + "redshift", + ) + expect(Object.keys(rs)).toEqual(["FOO", "foo"]) + expect(rs.FOO.data_type).toBe("int") + expect(rs.foo.data_type).toBe("text") + }) + + test("a manifest column documented in upper case meets the warehouse's unquoted lower-case name on Postgres", () => { + const merged = mergeColumns({ ID: col("ID") }, [{ column: "id", dtype: "BIGINT" }], "postgres") + expect(Object.keys(merged)).toEqual(["id"]) + expect(merged.id.data_type).toBe("bigint") + }) + + test("a column documented in lower case meets the warehouse's upper-case spelling of it on DuckDB", () => { + const merged = mergeColumns({ id: col("id") }, [{ column: "ID", dtype: "INTEGER" }], "duckdb") + expect(Object.keys(merged)).toEqual(["id"]) + expect(merged.id.data_type).toBe("integer") + }) + + test("a manifest column and the warehouse's name for it meet under one key in the adapter's case", () => { + const merged = mergeColumns({ id: col("id") }, [{ column: "id", dtype: "BIGINT" }], "postgres") + expect(Object.keys(merged)).toEqual(["id"]) + expect(merged.id.data_type).toBe("bigint") + }) + + test("columns named like Object members are columns, and nothing is written to the built-ins", () => { + const merged = mergeColumns( + {}, + [ + { column: "constructor", dtype: "text" }, + { column: "__proto__", dtype: "int" }, + ], + "postgres", + ) + expect(Object.keys(merged)).toEqual(["constructor", "__proto__"]) + expect(Object.getOwnPropertyDescriptor(merged, "constructor")?.value.data_type).toBe("text") + expect((Object.prototype as any).data_type).toBeUndefined() + expect((Object as any).data_type).toBeUndefined() + }) +}) + +describe("remapSqlLineage", () => { + const parents = [ + { uniqueId: "source.shop.raw.raw_orders", sourceTable: "RAW_ORDERS_T", resourceType: "source" }, + { uniqueId: "model.shop.stg_customers", alias: "STG_CUSTOMERS", resourceType: "model" }, + ] + const graph = remapSqlLineage( + reply, + { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, + parents, + ) + + test("renames __final_select__ to the model in edges and nodes", () => { + expect(graph.edges).toEqual([ + ["raw_orders", "o"], + ["o", "orders"], + ["stg_customers", "orders"], + ]) + expect(graph.nodes.map((n) => n.id)).toEqual(["raw_orders", "o", "stg_customers", "orders"]) + expect(JSON.stringify(graph)).not.toContain("__final_select__") + }) + + test("ties table nodes to dbt unique ids, case-insensitively, and the model to itself", () => { + const byId = Object.fromEntries(graph.nodes.map((n) => [n.id, n])) + expect(byId.raw_orders).toEqual({ + id: "raw_orders", + name: "RAW_ORDERS_T", + type: "source", + nodeId: "source.shop.raw.raw_orders", + sql: "", + columns: [{ name: "id" }, { name: "amount" }], + }) + expect(byId.stg_customers).toEqual({ + id: "stg_customers", + name: "stg_customers", + type: "model", + nodeId: "model.shop.stg_customers", + sql: "", + columns: [{ name: "id" }, { name: "name" }], + }) + expect(byId.orders).toEqual({ + id: "orders", + name: "orders", + type: "model", + nodeId: "model.shop.orders", + sql: "select * from o", + columns: [{ name: "id" }], + }) + }) + + test("leaves CTEs unmapped and drops the layout positions", () => { + const cte = graph.nodes.find((n) => n.id === "o")! + expect(cte).toEqual({ id: "o", name: "o", type: "cte", sql: "select * from raw.orders", columns: [{ name: "id" }] }) + expect(graph).not.toHaveProperty("nodePositions") + }) + + test("a CTE or table named like the model keeps its node; the model's own select is told apart", () => { + const collide: SqlLineageResponse = { + tableEdges: [ + ["stg_customers", "orders"], + ["orders", "__final_select__"], + ], + details: { + stg_customers: { name: "stg_customers", type: "table", sql: "", columns: [] }, + orders: { name: "orders", type: "cte", sql: "select * from stg_customers", columns: [{ name: "id" }] }, + __final_select__: { name: "__final_select__", type: "final", sql: "select * from orders", columns: [] }, + }, + } + const out = remapSqlLineage(collide, { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, []) + expect(out.edges).toEqual([ + ["stg_customers", "orders"], + ["orders", "orders (final select)"], + ]) + expect(out.nodes).toEqual([ + { id: "stg_customers", name: "stg_customers", type: "table", sql: "", columns: [] }, + { id: "orders", name: "orders", type: "cte", sql: "select * from stg_customers", columns: [{ name: "id" }] }, + { + id: "orders (final select)", + name: "orders (final select)", + type: "model", + nodeId: "model.shop.orders", + sql: "select * from orders", + columns: [], + }, + ]) + }) + + test("a source table named like the model stays the source's node; a model named like an Object member is not renamed", () => { + const sameName: SqlLineageResponse = { + tableEdges: [["orders", "__final_select__"]], + details: { + orders: { name: "orders", type: "table", sql: "", columns: [] }, + __final_select__: { name: "__final_select__", type: "final", sql: "select * from orders", columns: [] }, + }, + } + const parentsOf = [{ uniqueId: "source.shop.raw.orders", sourceTable: "orders", resourceType: "source" }] + const out = remapSqlLineage( + sameName, + { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, + parentsOf, + ) + expect(out.nodes.map((n) => [n.name, n.nodeId])).toEqual([ + ["orders", "source.shop.raw.orders"], + ["orders (final select)", "model.shop.orders"], + ]) + const member = remapSqlLineage( + { tableEdges: [["x", "__final_select__"]], details: { __final_select__: sameName.details.__final_select__ } }, + { name: "constructor", uniqueId: "model.shop.constructor", resourceType: "model" }, + [], + ) + expect(member.nodes.map((n) => n.name)).toEqual(["constructor"]) + expect(member.edges).toEqual([["x", "constructor"]]) + }) + + test("a quoted Foo and FOO are two relations: each parent keeps its own exact-case match", () => { + const two = { + tableEdges: [] as [string, string][], + details: { + "Foo#1": { type: "table", name: "Foo", columns: [] }, + "FOO#1": { type: "table", name: "FOO", columns: [] }, + }, + } as any + const g = remapSqlLineage(two, { name: "m", uniqueId: "model.p.m", resourceType: "model" }, [ + { uniqueId: "model.p.mixed", alias: "Foo", resourceType: "model" }, + { uniqueId: "model.p.upper", alias: "FOO", resourceType: "model" }, + ]) + const byId = Object.fromEntries(g.nodes.map((n) => [n.id, n.nodeId])) + expect(byId["Foo#1"]).toBe("model.p.mixed") + expect(byId["FOO#1"]).toBe("model.p.upper") + }) + + test("a repeated relation spelled in two cases still reaches its dbt node", () => { + const rep = { + tableEdges: [] as [string, string][], + details: { + "raw.orders#1": { type: "table", name: "orders", columns: [] }, + "raw.ORDERS#2": { type: "table", name: "ORDERS", columns: [] }, + }, + } as any + const g = remapSqlLineage(rep, { name: "m", uniqueId: "model.p.m", resourceType: "model" }, [ + { uniqueId: "model.p.orders", alias: "orders", resourceType: "model" }, + ]) + expect(g.nodes.filter((n) => n.nodeId === "model.p.orders").length).toBe(2) + }) + + test("does not mutate the backend reply", () => { + expect(reply.details.__final_select__).toBeDefined() + expect(reply.tableEdges[1]).toEqual(["o", "__final_select__"]) + }) +}) + +describe("buildSqlGraph", () => { + const read = (file: string) => `-- ${file}\nselect 1` + + test("sends the compiled SQL, parents' merged columns and dialect, then remaps the reply", async () => { + const a = adapter() + let sent: SqlLineageRequest | undefined + const graph = await buildSqlGraph( + a, + "orders", + { + fetchLineage: async (req) => { + sent = req + return reply + }, + readFile: read, + projectRoot: "/proj", + sessionId: "ses_1", + }, + signal, + ) + expect(a.calls).toEqual([ + "compile:-- /proj/models/orders.sql\nselect 1", + "schema:source.shop.raw.raw_orders,model.shop.stg_customers", + ]) + expect(sent?.model_dialect).toBe("snowflake") + expect(sent?.compiled_sql).toContain("join stg_customers") + expect(sent?.session_id).toMatch(/^ses_1-[0-9a-f]{32}$/) + expect(sent?.model_info).toEqual([ + { + model_node: { + database: "db", + schema: "raw", + name: "raw_orders", + alias: "RAW_ORDERS_T", + uniqueId: "source.shop.raw.raw_orders", + columns: { + id: { name: "id", data_type: "int", description: "", meta: {} }, + amount: { name: "amount", data_type: "number", description: "", meta: {} }, + }, + path: undefined, + }, + }, + { + model_node: { + database: "db", + schema: "analytics", + name: "stg_customers", + alias: "stg_customers", + uniqueId: "model.shop.stg_customers", + columns: { + id: { name: "id", description: "", data_type: "int", meta: {} }, + name: { name: "name", data_type: "text", description: "", meta: {} }, + }, + path: "/proj/models/stg_customers.sql", + }, + }, + ]) + expect(graph.edges).toContainEqual(["o", "orders"]) + expect(graph.nodes.find((n) => n.name === "orders")?.nodeId).toBe("model.shop.orders") + expect(graph.nodes.find((n) => n.id === "raw_orders")?.nodeId).toBe("source.shop.raw.raw_orders") + }) + + test("accepts a unique_id and resolves a project-relative path against the project root", async () => { + const relative = { ...orders, path: "models/orders.sql" } + const a = adapter({ + parseManifest: async () => ({ + nodeMetaMap: { + lookupByUniqueId: (id) => (id === relative.unique_id ? relative : undefined), + lookupByBaseName: () => undefined, + }, + sourceMetaMap: { get: () => undefined }, + }), + getNonEphemeralParents: () => [], + }) + let read: string | undefined + await buildSqlGraph( + a, + "model.shop.orders", + { + fetchLineage: async () => reply, + readFile: (f) => ((read = f), "select 1"), + projectRoot: "/proj", + sessionId: "ses_1", + }, + signal, + ) + expect(read).toBe("/proj/models/orders.sql") + // no parents: no warehouse read + expect(a.calls.some((c) => c.startsWith("schema:"))).toBe(false) + }) + + test("an unknown model, an uncompilable model and a missing manifest each fail with a reason", async () => { + const deps = { fetchLineage: async () => reply, readFile: read, projectRoot: "/proj", sessionId: "ses_1" } + await expect(buildSqlGraph(adapter(), "nope", deps, signal)).rejects.toThrow("no model named nope") + await expect( + buildSqlGraph(adapter({ unsafeCompileQuery: async () => undefined }), "orders", deps, signal), + ).rejects.toThrow("unable to compile orders") + await expect( + buildSqlGraph(adapter({ parseManifest: async () => undefined }), "orders", deps, signal), + ).rejects.toThrow("manifest is not available") + }) + + test("a seed, a snapshot or a model with no .sql file is refused before anything is compiled", async () => { + const deps = { fetchLineage: async () => reply, readFile: read, projectRoot: "/proj", sessionId: "ses_1" } + for (const odd of [ + { ...orders, name: "countries", resource_type: "seed", path: "seeds/countries.csv" }, + { ...orders, name: "snap", resource_type: "snapshot", path: "snapshots/snap.sql" }, + { ...orders, name: "py_model", path: "models/py_model.py" }, + ]) { + const a = adapter({ + parseManifest: async () => ({ + nodeMetaMap: { lookupByUniqueId: () => undefined, lookupByBaseName: () => odd }, + sourceMetaMap: { get: () => undefined }, + }), + }) + await expect(buildSqlGraph(a, odd.name, deps, signal)).rejects.toThrow(`${odd.name} is not a dbt model`) + expect(a.calls).toEqual([]) + } + }) + + test("a cancel while the manifest parses or the model compiles starts no further work", async () => { + const deps = { fetchLineage: async () => reply, readFile: read, projectRoot: "/proj", sessionId: "ses_1" } + const duringParse = new AbortController() + const a = adapter() + const parse = a.parseManifest + a.parseManifest = async () => { + const manifest = await parse() + duringParse.abort() + return manifest + } + await expect(buildSqlGraph(a, "orders", deps, duringParse.signal)).rejects.toThrow() + expect(a.calls).toEqual([]) + + const duringCompile = new AbortController() + const b = adapter({ + unsafeCompileQuery: async () => { + duringCompile.abort() + return "select 1" + }, + }) + await expect(buildSqlGraph(b, "orders", deps, duringCompile.signal)).rejects.toThrow() + expect(b.calls).toEqual([]) + }) + + test("two sessions asking for the same SQL send different session ids", async () => { + const ids: string[] = [] + const fetchLineage = async (req: SqlLineageRequest) => (ids.push(req.session_id), reply) + const base = { fetchLineage, readFile: read, projectRoot: "/proj" } + await buildSqlGraph(adapter(), "orders", { ...base, sessionId: "ses_a" }, signal) + await buildSqlGraph(adapter(), "orders", { ...base, sessionId: "ses_b" }, signal) + expect(ids[0]).not.toBe(ids[1]) + expect(ids[0]?.split("-").slice(-1)[0]).toBe(ids[1]?.split("-").slice(-1)[0]) + }) + + test("a backend failure propagates and sends nothing after a cancel", async () => { + const deps = { + fetchLineage: async () => { + throw new Error("API POST /dbt/v3/sql_lineage failed with status 500") + }, + readFile: read, + projectRoot: "/proj", + sessionId: "ses_1", + } + await expect(buildSqlGraph(adapter(), "orders", deps, signal)).rejects.toThrow("status 500") + // The tool's abort signal reaches the backend request, and a malformed reply is a failure, not an empty graph. + let seen: AbortSignal | undefined + await buildSqlGraph(adapter(), "orders", { ...deps, fetchLineage: async (_req, s) => ((seen = s), reply) }, signal) + expect(seen).toBe(signal) + for (const bad of [ + { ...reply, details: [] }, + { ...reply, details: { a: 1 } }, + { ...reply, tableEdges: [["a"]] }, + { ...reply, details: { a: {} } }, + { ...reply, details: { a: { type: "cte", columns: "id" } } }, + { ...reply, details: { a: { type: "cte", columns: ["id"] } } }, + { ...reply, details: {}, tableEdges: [["a", "__final_select__"]] }, + ]) { + await expect( + buildSqlGraph(adapter(), "orders", { ...deps, fetchLineage: async () => bad as any }, signal), + ).rejects.toThrow("no SQL graph") + } + // A compile that never settles does not hold the call past its cancellation. + let enteredCompile: () => void = () => {} + const entered = new Promise((resolve) => (enteredCompile = resolve)) + const stalled = new AbortController() + const pending = buildSqlGraph( + adapter({ unsafeCompileQuery: () => (enteredCompile(), new Promise(() => {})) }), + "orders", + deps, + stalled.signal, + ) + await entered + stalled.abort(new Error("cancelled")) + await expect(pending).rejects.toThrow("cancelled") + const aborted = AbortSignal.abort() + let parsedManifest = false + await expect( + buildSqlGraph( + adapter({ parseManifest: async () => ((parsedManifest = true), undefined as any) }), + "orders", + deps, + aborted, + ), + ).rejects.toThrow() + expect(parsedManifest).toBe(false) + let called = false + await expect( + buildSqlGraph(adapter(), "orders", { ...deps, fetchLineage: async () => ((called = true), reply) }, aborted), + ).rejects.toThrow() + expect(called).toBe(false) + }) +}) + +describe("formatSqlGraph", () => { + test("lists each node with its inputs and column count", () => { + const graph = remapSqlLineage(reply, { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, []) + const text = formatSqlGraph("orders", graph) + expect(text).toContain("4 nodes, 3 edges") + expect(text).toContain("- orders (model, model.shop.orders): 1 columns, from o, stg_customers") + expect(text).toContain("- o (cte): 1 columns, from RAW_ORDERS_T") + }) +}) + +describe("the backend's real node shapes", () => { + // Relations are keyed by location and a per-occurrence suffix, and carry `name`, `db` and `schema`; joins, filters and the like have no name and carry + // `expression` instead of `sql`. + const real: SqlLineageResponse = { + tableEdges: [ + ["analytics.customers_111", "LEFT_JOIN_9"], + ["raw.customers_222", "LEFT_JOIN_9"], + ["LEFT_JOIN_9", "__final_select__"], + ["analytics.customers_111", "WHERE_7"], + ["WHERE_7", "__final_select__"], + ], + details: { + "analytics.customers_111": { + name: "customers", + type: "table", + expression: "analytics.customers", + db: "db", + schema: "analytics", + columns: [], + }, + "raw.customers_222": { + name: "customers", + type: "table", + expression: "raw.customers", + db: "db", + schema: "raw", + columns: [], + }, + LEFT_JOIN_9: { type: "LEFT_JOIN", expression: "LEFT JOIN raw.customers ON 1 = 1", columns: [] }, + WHERE_7: { type: "FILTER", expression: "WHERE amount > 0" }, + __final_select__: { name: "__final_select__", type: "final", sql: "select 1", columns: [{ name: "id" }] }, + }, + } + const graph = remapSqlLineage(real, { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, [ + { uniqueId: "model.shop.customers", alias: "customers", schema: "analytics", resourceType: "model" }, + { uniqueId: "source.shop.raw.customers", sourceTable: "customers", schema: "raw", resourceType: "source" }, + ]) + + test("every edge ends at a node id, though two relations share a name", () => { + const ids = graph.nodes.map((n) => n.id) + expect(new Set(ids).size).toBe(ids.length) + for (const edge of graph.edges) for (const end of edge) expect(ids).toContain(end) + expect(graph.nodes.filter((n) => n.name === "customers")).toHaveLength(2) + }) + + test("a relation is tied to the dbt node in its own schema, and a join or filter keeps its SQL", () => { + const byId = Object.fromEntries(graph.nodes.map((n) => [n.id, n])) + expect(byId["analytics.customers_111"]).toMatchObject({ + nodeId: "model.shop.customers", + type: "model", + sql: "analytics.customers", + }) + expect(byId["raw.customers_222"]).toMatchObject({ nodeId: "source.shop.raw.customers", type: "source" }) + expect(byId.LEFT_JOIN_9).toEqual({ + id: "LEFT_JOIN_9", + name: "LEFT_JOIN_9", + type: "LEFT_JOIN", + sql: "LEFT JOIN raw.customers ON 1 = 1", + columns: [], + }) + expect(byId.WHERE_7.sql).toBe("WHERE amount > 0") + }) + + test("two relations of one name and schema in different databases are told apart by the database", () => { + const twoDbs: SqlLineageResponse = { + tableEdges: [], + details: { + "a.raw.customers_1": { name: "customers", type: "table", db: "a", schema: "raw" }, + "b.raw.customers_2": { name: "customers", type: "table", db: "b", schema: "raw" }, + }, + } + const parentsIn = (first: string, second: string) => [ + { + uniqueId: `source.p.${first}.customers`, + sourceTable: "customers", + schema: "raw", + database: first, + resourceType: "source", + }, + { + uniqueId: `source.p.${second}.customers`, + sourceTable: "customers", + schema: "raw", + database: second, + resourceType: "source", + }, + ] + const model = { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" } + for (const order of [parentsIn("a", "b"), parentsIn("b", "a")]) { + const out = remapSqlLineage(twoDbs, model, order) + expect(out.nodes.map((n) => n.nodeId)).toEqual(["source.p.a.customers", "source.p.b.customers"]) + } + }) + + test("a relation two parents could both be is tied to neither, whatever the parent order", () => { + const ambiguous: SqlLineageResponse = { + tableEdges: [], + details: { "raw.customers_1": { name: "customers", type: "table", schema: "raw" } }, + } + const model = { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" } + const a = { uniqueId: "source.p.raw.customers", sourceTable: "customers", schema: "raw", resourceType: "source" } + const b = { uniqueId: "model.p.customers", alias: "customers", schema: "raw", resourceType: "model" } + for (const order of [ + [a, b], + [b, a], + ]) { + const out = remapSqlLineage(ambiguous, model, order) + expect(out.nodes).toEqual([{ id: "raw.customers_1", name: "customers", type: "table", sql: "", columns: [] }]) + } + }) + + test("every occurrence of one relation in the SQL is tied to its dbt node", () => { + const twice: SqlLineageResponse = { + tableEdges: [], + details: { + "analytics.customers_1": { name: "customers", type: "table", schema: "analytics" }, + "analytics.customers_2": { name: "customers", type: "table", schema: "analytics" }, + }, + } + const out = remapSqlLineage(twice, { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, [ + { uniqueId: "model.shop.customers", alias: "customers", schema: "analytics", resourceType: "model" }, + ]) + expect(out.nodes.map((n) => n.nodeId)).toEqual(["model.shop.customers", "model.shop.customers"]) + }) +}) + +describe("a cancel while the backend is answering", () => { + test("ends the call at once, and a late answer is ignored", async () => { + const controller = new AbortController() + let release!: (r: SqlLineageResponse) => void + let reached!: () => void + const inBackend = new Promise((resolve) => (reached = resolve)) + const slow = new Promise((resolve) => (release = resolve)) + const call = buildSqlGraph( + adapter(), + "orders", + { + fetchLineage: () => { + reached() + return slow + }, + readFile: (f) => f, + projectRoot: "/proj", + sessionId: "ses_1", + }, + controller.signal, + ) + const settled = call.then( + () => "answered", + () => "aborted", + ) + await inBackend + controller.abort() + expect(await settled).toBe("aborted") + release(reply) + }) + + test("a reply with no graph is a clear failure, not a crash", async () => { + const deps = { + fetchLineage: async () => ({}) as SqlLineageResponse, + readFile: (f: string) => f, + projectRoot: "/proj", + sessionId: "ses_1", + } + await expect(buildSqlGraph(adapter(), "orders", deps, signal)).rejects.toThrow("the backend returned no SQL graph") + }) +}) + +describe("the tool's own answer", () => { + const ctx: Tool.Context = { + sessionID: SessionID.make("ses_sql_graph"), + messageID: MessageID.make("msg_sql_graph"), + agent: "analyst", + abort: new AbortController().signal, + messages: [], + metadata: () => Effect.void, + ask: () => Effect.void, + } + + afterEach(() => { + resetDbtAdapter() + delete dbtAdapterInternals.readConfig + delete dbtAdapterInternals.create + }) + + test("a read-only call is a failure the CLI reports as one: success false and an error", async () => { + const tool = await initTool(DbtSqlGraphTool as any) + const result = await tool.execute({ model: "orders" }, { ...ctx, extra: { forceReadOnly: true } }) + expect(result.metadata).toEqual({ + success: false, + model: "orders", + reason: expect.stringContaining("--allow-write"), + error: expect.stringContaining("--allow-write"), + truncated: false, + }) + expect(result.output).toContain("Not available") + }) + + test("a model named __proto__ keeps its final node", () => { + const out = remapSqlLineage( + JSON.parse( + '{"tableEdges":[["t","__final_select__"]],"details":{"t":{"name":"t","type":"table"},"__final_select__":{"type":"final","sql":"select 1"}}}', + ), + { name: "__proto__", uniqueId: "model.shop.__proto__", resourceType: "model" }, + [], + ) + expect(out.edges).toEqual([["t", "__proto__"]]) + expect(out.nodes.map((n) => n.id)).toEqual(["t", "__proto__"]) + }) + + test("a model the manifest lacks is a failure with the reason", async () => { + const project = realpathSync(await mkdtemp(path.join(os.tmpdir(), "dbt-sql-graph-"))) + try { + await writeFile(path.join(project, "dbt_project.yml"), "name: shop\n") + dbtAdapterInternals.readConfig = async () => ({ + projectRoot: project, + pythonPath: "python3", + dbtIntegration: "corecommand", + queryLimit: 500, + }) + dbtAdapterInternals.create = async () => ({ + isInitialized: () => true, + dispose: async () => {}, + parseManifest: async () => ({ + nodeMetaMap: { lookupByUniqueId: () => undefined, lookupByBaseName: () => undefined }, + sourceMetaMap: { get: () => undefined }, + }), + }) + const tool = await initTool(DbtSqlGraphTool as any) + const result = await tool.execute({ model: "nope" }, ctx) + expect(result.metadata).toMatchObject({ + success: false, + model: "nope", + error: "no model named nope in the manifest", + }) + } finally { + await rm(project, { recursive: true, force: true }) + } + }) +}) + +describe("final-select identity and the text output", () => { + test("a replacement final-select key never overwrites another node", () => { + const response = { + details: { + orders: { type: "cte", sql: "select 1", columns: [] }, + "orders (final select)": { type: "cte", sql: "select 2", columns: [] }, + __final_select__: { type: "select", sql: "select 3", columns: [] }, + }, + tableEdges: [["orders (final select)", "__final_select__"]], + } as unknown as SqlLineageResponse + const graph = remapSqlLineage( + response, + { name: "orders", uniqueId: "model.shop.orders", resourceType: "model" }, + [], + ) + const ids = graph.nodes.map((n) => n.id) + expect(new Set(ids).size).toBe(3) + const [from, to] = graph.edges[0] + expect(from).toBe("orders (final select)") + expect(to).not.toBe(from) + expect(graph.nodes.find((n) => n.id === to)?.sql).toBe("select 3") + }) + + test("the text lists each node's columns and SQL", () => { + const text = formatSqlGraph("m", { + nodes: [ + { id: "a", name: "a", type: "cte", sql: "select\n id from t", columns: [{ name: "id" }, { name: "x" }] }, + ], + edges: [], + } as any) + expect(text).toContain(" columns: id, x") + expect(text).toContain(" sql: select id from t") + }) +})