fix: names a SELECT list writes, and repeats beside any star

RA
2d ago
+437-52

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 _N or _col_N counts as written only when a SELECT list gives it, in any SELECT of the SQL. A table alias _1 no longer keeps the engine's fake _1 column, and a _col_1 table alias or WHERE column 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 qualified t.* is checked against the columns the model passes on unchanged, so select o.*, c.name over 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" for id_1.
  • On Postgres and Redshift, dbt_lineage names 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 other dbt_lineage output 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_N counts 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 order1 are whole names.
  • dbt_lineage now names a model's own unaliased casts after their columns on Postgres and Redshift, keeping written _1 and __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.