Repository navigation
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
treysp
force-pushed
the
trey/nested-setops-types
branch
from
October 20, 2026 19:51
e2b265a to
1ca6b68
Compare
| setop_cols = { | ||
| ls.alias_or_name: self._maybe_coerce( | ||
| t.cast(exp.DataType, ls.type), t.cast(exp.DataType, rs.type) | ||
| for node in reversed(set_ops): |
Collaborator
There was a problem hiding this comment.
This isn't this PR's fault, but I'm not a fan of this approach. I understand that we do it because set operation type inference is deferred until someone asks for it via _get_setop_column_types, but this ends up introducing more complexity for no good reason.
I don't remember why we chose to annotate these set operations lazily to begin with, but I think we can probably refactor that. So: annotate set operation branches as you walk up the scopes in post-order and then simplify this by only running the coercions between branches etc.
Maybe I'll take a stab at this separately.
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.
Currently, types for nested set operations are taken from the leftmost query and merged by name, so types in the nested operation are not properly bubbled up.
For example, in this query
annotate_typesinferst.aasINT, while Postgres (double precision) and DuckDB (DOUBLE) both report a double:Cause
this.selectswithexpression.selects. For a parenthesized operand,Subquery.selectsreturns only the projections of its first query (SELECT 2 AS b). The outer UNION therefore never sees theDOUBLE.DOUBLEcorrectly, but stores it underb, which the outer columnanever reads.Left-nested chains currently work only because every operation's left query is the same first query, so the names always line up.
Fix
Resolve set operations pairwise and bottom-up:
walk()order, so nested operations resolve before the ones containing them.selects.BY NAME), and output names come from the left operand.UNKNOWNtype rather than the first query's type.