fix: names a SELECT list writes, and repeats beside any star
Β
RA
2d ago+437+437+012345678901234567890123456789-52-52-01234567890123456789
Issue for this PR
Closes #1850
Type of change
- Bug fix
What does this PR do?
Follow-ups from the reviews of #1754 and #1760: names the column answer and dbt_lineage read from a model's SQL.
- A
_Nor_col_Ncounts as written only when a SELECT list gives it, in any SELECT of the SQL. A table alias_1no longer keeps the engine's fake_1column, and a_col_1table alias orWHEREcolumn no longer stops a Postgres or Redshift cast from taking its column's name. - Every SELECT list with a star, the outer one or one in a CTE, is checked for a repeated name. A bare
*is checked against the parents' columns, as before. A qualifiedt.*is checked against the columns the model passes on unchanged, soselect o.*, c.nameover a join still settles. A DuckDB repeat beside a qualified or CTE-wrapped star now leaves the model unsettled instead of throwing "has no column" forid_1. - On Postgres and Redshift,
dbt_lineagenames a model's own unaliased cast after its column, as the column answer does. One shared pass names the engine's labels for both. Every otherdbt_lineageoutput is as before.
Nothing is reported unaffected, as before.
How did you verify your code works?
Each shape has a test that fails on main, and one join stays settled as a control. The demo project's answer is unchanged: 11 carry it, 28 may carry it, of 39.
Cost side effects
None (local manifest only).
Checklist
- I have tested my changes locally
- I have not included unrelated changes in this PR
- I checked for cost side effects (model calls, queries or cloud resources that grow with user data) and described them, or wrote "none"
π€ Generated with Claude Code
https://claude.ai/code/session_01BXxzvqLZuTJdDJ1Ku2ZMDY
Summary by cubic
Fixes how engine-generated _N/_col_N column labels are recognized and how name repeats beside stars are settled.
- A
_N/_col_Ncounts as written only when a SELECT list in any SELECT produces it; table aliases and WHERE columns no longer count. - A repeat beside a qualified or CTE-wrapped star now leaves the model unsettled, matching bare
*behavior. - A star's qualifier resolves in its own SELECT's FROM clause: an alias hides a parent of the same name, schema and database pick between same-named parents, aliases inside a CTE don't reach an outer star, and words that can be aliases (
start,offset) don't end the clause. - Later UNION branches name no outputs, upper-case keywords end a SELECT list, and end words like
order1are whole names. dbt_lineagenow names a model's own unaliased casts after their columns on Postgres and Redshift, keeping written_1and__proto__names.
Summary by CodeRabbit
- Bug Fixes
- Improved column lineage names in PostgreSQL and Redshift, including for unaliased casts and renamed outputs, while preserving aliases written in SQL.
- Reduced incorrect or ambiguous column results in queries with repeated names, joins, star projections, and set operations.
- Improved output-name handling across CTEs and subqueries, including qualified stars and duplicate names, for more consistent lineage results.
- Correctly distinguishes names explicitly written in SELECT clauses from engine-generated names, including across UNION branches.