Skip to content

Feature: extract table-to-table dependencies from INSERT/CTAS/MERGE, not just CREATE VIEW #1572

Open

Description

@OliverViola

What I'm trying to do

Build a table-to-table dependency (lineage) graph from a SQL/ETL codebase — "table A is populated from tables B and C".

Current behavior (v0.9.3, extract_sql in extract.py)

.sql is parsed via tree-sitter-sql and produces good schema structure, but dataflow edges are only walked (_walk_from_refs) inside create_view, create_function, and create_procedure. As a result:

  • CREATE TABLE … AS SELECT … (CTAS) → emits the table node but no references edges to the source tables in its SELECT.
  • INSERT INTO tgt SELECT … FROM src … → produces nothing (no target node, no source edges).
  • MERGE INTO … USING … / UPDATE … FROM … → not handled.

On a real ETL repo (~700 .sql files, mostly INSERT … SELECT bodies), only ~16% of files yielded any node, and essentially zero source→target lineage edges.

Requested feature

Extend extract_sql to emit references/reads_from edges from the FROM/JOIN tables to the write target for:

  • INSERT INTO <tgt> … SELECT … FROM/JOIN <src…>
  • CREATE TABLE <tgt> AS SELECT … FROM/JOIN <src…> (CTAS)
  • MERGE INTO <tgt> USING <src> and UPDATE <tgt> … FROM <src>

i.e. reuse the existing _walk_from_refs logic for these statement types so a lineage graph can be built directly.

Nice-to-have

  • Register .ddl as a SQL/code extension (some projects keep CREATE TABLE schemas in .ddl files, currently ignored).
  • Optional hook to normalize templated identifiers (e.g. {project}.{dataset}.tbl from Python .format()/Jinja) so placeholder table names resolve instead of becoming literal node labels.

Happy to help test against a real ETL corpus. Thanks for graphify!

Activity

  1. silversaddleai-create commented on Aug 1, 2026

    @silversaddleai-create

    Independent corroboration on a different corpus shape, in case a second data point helps prioritise this.

    Setup: graphifyy 0.9.31, tree-sitter-sql 0.3.11, extract --code-only --force over a Databricks medallion codebase (bronze → silver → gold, Unity Catalog three-part names). Not the INSERT … SELECT shape in the original report; this corpus is almost entirely CREATE OR REPLACE TABLE … AS SELECT.

    In the .sql sources In graph.json
    FROM / JOIN table references 189 7 reads_from edges
    CREATE TABLE statements 66 —
    Distinct tables referenced 79 14 nodes

    So ~4% of lineage edges and ~18% of tables, closely matching the ~16% node yield reported above.

    The 7 edges that did resolve are all flat FROM <table> reads inside data-dictionary builders against information_schema. That suggests one addition worth folding into the request: it is not only the statement type (CTAS/INSERT/MERGE) but also where the source sits inside the SELECT. Even once CTAS is walked, a direct FROM <table> is the rarer shape in real pipeline SQL. Sources are usually one level down, in a CTE or a subquery:

    -- (a) flat CTAS — the case most likely to be fixed first
    CREATE OR REPLACE TABLE cat.silver.orders AS
    SELECT * FROM cat.bronze.orders;
    
    -- (b) CTAS whose source is inside a CTE — the dedupe pattern, extremely common
    CREATE OR REPLACE TABLE cat.silver.customers AS
    WITH d AS (
      SELECT *, row_number() OVER (PARTITION BY id ORDER BY _ingested_at DESC) rn
      FROM cat.bronze.customers
    )
    SELECT * FROM d WHERE rn = 1;
    
    -- (c) CTAS whose sources are a subquery plus a JOIN
    CREATE OR REPLACE TABLE cat.gold.fact_order AS
    SELECT o.*, c.segment
    FROM (SELECT * FROM cat.silver.orders WHERE status = 'complete') o
    LEFT JOIN cat.silver.customers c USING (customer_id);

    Expected reads_from: bronze.orders → silver.orders, bronze.customers → silver.customers, and both silver.orders → gold.fact_order and silver.customers → gold.fact_order.
    Actual: none of the four.

    If _walk_from_refs is extended to these statement types, recursing through cte/with_clause and nested subquery nodes rather than only the top-level from_clause would be what makes it useful on real ETL rather than only on the flat case.

    One consumer-side note, offered as a suggestion rather than a complaint: the shortfall is currently silent. The graph builds cleanly, reports a healthy node count, and shows a small number of confident reads_from edges with nothing indicating that most are missing. A reader who did not go and count the FROM clauses by hand would reasonably conclude the pipelines are only loosely connected. A warning in the same style as the existing missing-grammar notice would help, something like N CREATE TABLE … AS SELECT statements produced no lineage edges (see #1572). That would have saved me the manual count, and it is probably cheap next to the extraction work itself.

    Happy to re-run the counts against a build once this lands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions