Repository navigation
Pair the branches of a set operation by position - #8
Merged
Merged
Conversation
SQL pairs the columns of a UNION, an INTERSECT or an EXCEPT by position:
the output takes the first branch's names, and each later branch's column
feeds the output column at its place. The analyzer added each branch's
columns to the target under their own names instead, so a later branch
naming a column otherwise lost it to the column of that name, or added
one of its own.
SELECT k, a AS x FROM t1
UNION ALL
SELECT m.k, m.b FROM t2 AS m
came out with three outputs, k, x and b, x derived from t1.a alone.
A later branch's columns now take the first branch's names, position by
position, stars expanded. Only the columns it gives the set operation's
target are renamed: a derived table or a subquery inside the branch keeps
its own. UNION BY NAME still pairs by name. A nested set operation hands
its first branch's names up, so a third branch pairs with the first.
The recursive CTE snapshot gains the edge its second branch writes:
`SELECT e.employee_id, e.employee_id, e.name` feeds manager_id from
employee_id. The test of CTE aliases reused across UNION branches now
finds one output column, read from each branch's own CTE.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017VTEw2dNNL9Pn57Hkeeirn
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.
Patch 9 of the engine fork that collin pins (dbt-collin decision 0028): the branches of a set operation pair by position, as SQL pairs them, not by name.
A later branch of a
UNION, anINTERSECTor anEXCEPTadded its columns to the target under their own names. A column that a later branch names differently from the first branch was therefore lost to whichever output column had that name, or became an output of its own:That query came out with three outputs,
k,xandb, andxderived fromt1.aalone.The change
UNION BY NAMEstill pairs by name.Tests
BY NAME, and a derived table inside a later branch. The first two fail without the patch.cte_self_join_alias_columns_do_not_leak_across_union_scopesasserted the old pairing: it expected the second branch's alias,dept_name, as an output. It now finds one output,emp_name, read from each branch's own CTE. If the second branch's alias leaked to the first CTE, it would seeempalone.recursive_ctesnapshot gains the edge its second branch writes:SELECT e.employee_id, e.employee_id, e.namefeedsmanager_idfromemployee_id.cargo fmt --checkis clean.Measured through collin
uace_code,uza_name, andcol_0tocol_4twice); 4 edges added, 3 filter reads placedI read each moved edge against the SQL.
Nothing goes to pondpilot/flowscope.
🤖 Generated with Claude Code
https://claude.ai/code/session_017VTEw2dNNL9Pn57Hkeeirn