feat(lineage): preserve struct-field path on leaf nodes#3
Open
treff7es wants to merge 3 commits into
Open
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.
Use builtin `dict` instead of `t.Dict` (ruff UP006, valid under `from __future__ import annotations`) and rename the source-column loop variable so it no longer shadows the `column` parameter of `to_node`, which mypy resolved to the parameter's `str | int` type.
This was referenced Jun 16, 2026
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. Affects Snowflake VARIANT, BigQuery STRUCT, Iceberg nested types, and Spark complex types.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 and drops theexp.Dotancestor chain holding 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, no new dataclass field, no extra callback.Behavior change (scoped)
Node.namechanges only for leaves that access a struct field:tbl.widget→tbl.widget.metric.a. Plain columns are unchanged (verified by a regression test). Consumers that readNode.namefor struct queries will see the fuller path.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: all pass, no regressions.ruff+mypyclean.Refs: tobymao#7604. A follow-up PR builds on this to resolve struct fields through CTEs, nested constructors, and UNNEST (tobymao#6258).