← Back to journal
Governance

A CTE named Orders, a table named orders — SQL lineage

László Hadházy·December 1, 2026·9 min read

OpenLineageLineageSnowflakedbt

The last post was about lineage events that never reach a destination. This one is about events that arrive intact and name a table that does not exist. Both are wrong without raising an error, which is the kind of wrong a lineage consumer cannot detect on its own.

We build compliance evidence from lineage. Qedro, our open-source tool, turns OpenLineage events into the record of processing activities that Article 30 of the GDPR — the DSGVO in German-speaking markets — requires: the register an organisation keeps of which personal data it processes, where that data comes from and why. An event that names the wrong table therefore puts the wrong table into that record.

That is why I was checking OpenLineage's SQL parser, the Rust library that Airflow's OpenLineage provider and other integrations use to turn a SQL statement into lineage: which tables a statement reads and writes, and which input columns each output column comes from. In one statement, a CTE — a common table expression, the named subquery in a WITH clause — was defined as Orders and referenced as orders. In Snowflake those are the same name, and the parser treated them as two.

OpenLineage's SQL parser compared CTE names as plain strings, so it reported orders, a table that does not exist, as the input and lost raw.orders, the table the statement reads — without an error, so every catalog and record built from that lineage inherited the gap. Whether Orders and orders are one name is for the dialect to decide, and the parser now applies each dialect's rule.

What the parser reported

Snowflake, given this statement:

WITH Orders AS (SELECT id FROM raw.orders)
INSERT INTO tgt SELECT id FROM orders

The parser reported orders as the input table and id <- orders.id as the column lineage. raw.orders, the only table the statement reads, appeared in neither. A catalog fed by these events would show tgt depending on a table nobody created, with no link from tgt to raw.orders.

Lineage of the statement before and after the fixTwo lineage graphs for the same Snowflake statement. Before the fix, an edge carrying column id runs from orders, a table that does not exist, drawn with a pink fill and a dotted border, to tgt; raw.orders, drawn with a yellow fill and a dashed border, stands apart with no edge, because the parser did not report it. After the fix, an edge carrying column id runs from raw.orders, drawn with a green fill and a solid border, through the CTE Orders to tgt.

id

id, via CTE Orders

orders
does not exist

tgt
before the fix

raw.orders
not reported before the fix

raw.orders

tgt
after the fix

The diagram, in words: before the fix, the only edge into tgt came from orders, a table that does not exist, and raw.orders had no edge at all. After it, id flows from raw.orders through the CTE into tgt, which is what the statement does.

The parser kept each CTE under its name exactly as written and checked every table reference against that list with an exact string comparison, so orders did not match Orders and the parser took it for a real table. The bug report lists four such cases, each a statement of this shape:

WITH <cte> AS (SELECT id FROM <source>)
INSERT INTO tgt SELECT id FROM <reference>

This is what the parser returned for them before the fix:

Dialect CTE Source Reference Inputs reported Column lineage
Snowflake Orders raw.orders orders orders id <- orders.id
Snowflake C src c c id <- c.id
Postgres C src c c id <- c.id
Postgres "c" src C C id <- C.id

Written with matching case, the same statements reported src and id <- src.id. Mismatched case is common in practice, because SQL formatters change the case of keywords and sometimes of identifiers, and a dbt model collects the habits of everyone who has edited it by hand over the years.

Each dialect decides when two names are the same

Identifier folding is the rule a database applies to an unquoted name before comparing it. The SQL standard folds unquoted names to upper case, so orders and Orders both become ORDERS, and a quoted name keeps its spelling exactly. Snowflake follows the standard and Postgres folds to lower case instead, while several engines ignore case altogether. Here is the rule the parser applies now, per dialect:

Dialect Unquoted name Quoted name
Snowflake, and dialects that follow the standard folded to upper case kept as written
Postgres folded to lower case kept as written
Redshift folded to lower case folded to lower case
BigQuery, Databricks, Hive, DuckDB, SQL Server, SQLite case ignored case ignored
MySQL compared exactly compared exactly

For Redshift and MySQL the parser applies the server's default, because the setting that changes the rule never reaches it. Redshift compares quoted names without regard to case unless the session sets enable_case_sensitive_identifier, and MySQL's case sensitivity for table names depends on the server's lower_case_table_names setting and the file system underneath, where the exact comparison matches the default on Linux.

The same table decides which statements must keep their old result. In Postgres "C" is a different name from C, which folds to c, so this statement reads a real table c and does not touch the CTE:

WITH "C" AS (SELECT id FROM src)
INSERT INTO tgt SELECT id FROM c

A fix that ignored case everywhere would have resolved c to the CTE and reported src, which is wrong in the other direction, so the test suite keeps this statement as a control.

The fix: compare the names a dialect would compare

The parser now keys CTEs by the name an identifier denotes under the dialect, computed by one function. This is the code as merged in commit 763f630:

fn canonical_ident(value: &str, quoted: bool, dialect: &dyn Dialect) -> String {
    if dialect.is::<RedshiftSqlDialect>() {
        value.to_ascii_lowercase()
    } else if quoted {
        value.to_string()
    } else if dialect.is::<PostgreSqlDialect>() {
        value.to_ascii_lowercase()
    } else {
        value.to_ascii_uppercase()
    }
}
 
fn ident_key(ident: &Ident, dialect: &dyn Dialect) -> String {
    if dialect.is::<MySqlDialect>() {
        return ident.value.clone();
    }
    if dialect.is::<BigQueryDialect>()
        || dialect.is::<DatabricksDialect>()
        || dialect.is::<HiveDialect>()
        || dialect.is::<DuckDbDialect>()
        || dialect.is::<MsSqlDialect>()
        || dialect.is::<SQLiteDialect>()
    {
        return ident.value.to_ascii_lowercase();
    }
    canonical_ident(&ident.value, ident.quote_style.is_some(), dialect)
}

Two identifiers get the same key exactly when the dialect treats them as the same name. When a table reference matches a CTE's key, the parser replaces it with the CTE's name as defined, so it finds the CTE's columns under one spelling, also through a chain of CTEs referenced in different cases. With the fix, the first statement reports raw.orders as its input and id <- raw.orders.id as its column lineage, and the Postgres control still reports c.

Column qualifiers: the narrower rule was the one that held

A CTE name is not the only name a column can be qualified by. In SELECT orders.id FROM raw.orders, the qualifier orders refers to the table in the FROM clause, and with a CTE Orders also in scope, the fix had to decide which of the two the qualifier means. My first version bound a qualifier to every FROM item visible from where it was written, including those of enclosing queries, before trying the CTEs.

Each review round on that rule found another valid query it got wrong. A subquery in the FROM clause — a derived table — can see the FROM items listed before it only when it is marked LATERAL, a LATERAL subquery inside a parenthesized join must not be captured by the enclosing query, SQL Server's CROSS APPLY behaves like LATERAL, and DuckDB makes some subqueries lateral without the keyword. Every case had a correct answer, but each one meant more rules in a part of the parser that the issue was not about.

The merged version binds a qualifier only to the names its own SELECT's FROM clause introduces, compared under the same folding. The parser uses a name from an enclosing query for one purpose: to stop a CTE from taking the qualifier, which then stays as written, as it did before the fix. Correlated subqueries — those that refer to columns of an enclosing query — therefore resolve exactly as they did before, and the pull request lists the cases they leave open as follow-ups.

The change came with 32 tests, 22 of which fail on the code before the fix. The other ten pin behaviour that must not change, such as the Postgres control above and an outer FROM item that must never capture a qualifier inside a nested query. The OpenLineage maintainers merged it on 2026-11-26, so the next release of the SQL parser includes it.

Why this matters for an audit trail

Anyone reading a catalog notices a table without an upstream and asks why, but tgt in the example has one — the nonexistent orders — so its lineage looks complete, and nobody questions it until someone compares it with the warehouse.

Where an organisation uses lineage as evidence, as Qedro does for a record of processing activities, a parser bug like this one produces a record that names orders as the source of whatever tgt holds and leaves out raw.orders. That record is internally consistent and wrong, and nobody reading it can tell.

If you consume parser lineage today, a cheap check is to compare the input tables it reports against the warehouse's information schema — the catalog of its own tables that every warehouse exposes as information_schema. An input missing from it is either a CTE the parser did not recognise or a table someone has dropped since, and you want to know about both.


If you have seen lineage name a table that was never there — or if you would push back on any of this — the door's open at cordata.tech/contact.

Further reading