feat(lineage): preserve struct-field path on leaf nodes#2
Closed
treff7es wants to merge 2 commits into
Closed
Conversation
sqlglot.lineage collapsed struct/JSON field access to the bare column: `SELECT MIN(widget.metric.a, widget.metric.b) FROM tbl` produced a single leaf `tbl.widget`, losing both `.metric.a` and `.metric.b`. Two causes: 1. `set(find_all_in_scope(select, exp.Column))` deduped the two value-equal `widget` columns into one before the leaf loop ran. 2. The leaf was named from `c.sql()`, which renders only the column and drops the `exp.Dot` ancestor chain holding the subfield path. Introduce `_struct_access_root`, which climbs the left spine of the Dot chain to the maximal access expression, and use it both as the dedup key (so distinct subfield accesses stay distinct) and as the leaf name (so the full path `tbl.widget.metric.a` is preserved). Plain columns are unchanged. Resolves the leaf-level cases in tobymao#7604. Field access via CTE/derived-table recursion (tobymao#6258) is unaffected and still requires threading the path through to_node recursion.
Add focused tests for the struct-field lineage fix: two subfields of the same column staying distinct with full paths, single and deeply-nested field access, and a regression guard that a plain column is unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
sqlglot.lineagecollapses struct/JSON field access down to the bare column. Forlineage returns a single leaf
tbl.widget— both.metric.aand.metric.bare lost. This affects Snowflake VARIANT, BigQuery STRUCT, Iceberg nested types, and Spark complex types — anywhere users access struct fields.There are two root causes in
to_node:set(find_all_in_scope(select, exp.Column))dedupes the two value-equalwidgetcolumns into one before the leaf loop runs.c.sql(), which renders only the column reference and drops theexp.Dotancestor chain that holds the subfield path.Change
_struct_access_root(column)— climbs the left spine of theexp.Dotchain to the maximal access expression (returns the column unchanged when there is no Dot parent).t.s.aandt.s.bstay distinct) and as the leaf name (sotbl.widget.metric.ais preserved).This is the "first-class" representation — the path lives in
Node.name, with no new dataclass field and no extra callback.Results
MIN(widget.metric.a, widget.metric.b)['tbl.widget']['tbl.widget.metric.a', 'tbl.widget.metric.b']t.s.a['t.s']['t.s.a']COALESCE(t.s.a, t.s.b)['t.s']['t.s.a', 't.s.b']t.s.a.b.c['t.s']['t.s.a.b.c']a['tbl.a']['tbl.a'](unchanged)tests/test_lineage.py: 43/43 pass, no regressions.Scope / known limitation
Resolves the leaf-level cases (sqlglot#7604). Struct-field access resolved through a CTE / derived table (sqlglot#6258, Example 2) is unaffected — that path recurses with the bare column name and still needs the subfield threaded through
to_noderecursion. The inlinestruct(...)construction error (sqlglot#6258, Example 1) is a separate code path and not addressed here.Refs: tobymao#7604, tobymao#6258