Repository navigation
fix(optimizer): resolve set operation types during annotation [CODEX] - #8720
Open
georgesittas wants to merge 5 commits into
Open
georgesittas wants to merge 5 commits into
georgesittas wants to merge 5 commits into
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Shallow reannotation and compound recursive CTEs can lose resolved output types.
2 open issues
What changed in this PR
Refactors SQLGlot’s type annotator to resolve set-operation outputs eagerly during post-order annotation, replacing lazy subtree traversal.
Changes:
- Resolve nested operations pairwise while preserving positional columns and BY NAME modifiers.
- Add coverage for nested operations, array queries, shared CTEs, and column-count mismatches.
| File | Description |
|---|---|
| tests/test_optimizer.py | Adds set-operation type-inference regressions. |
| sqlglot/optimizer/annotate_types.py | Computes and caches ordered outputs during annotation. |
Review effort: Balanced
Comment on lines
+644
to
+645
| cached = self._setop_column_types.get(id(setop)) | ||
| return dict(cached[1]) if cached else {} |
Comment on lines
+653
to
+654
| cached = self._setop_column_types.pop(id(operand), None) | ||
| operands.append(cached[1] if cached else []) |
Contributor
SQLGlot Integration Test Results❌ 1 regression — see details belowComparing: By Dialect
Overallmain: 182930 total, 165990 passed (pass rate: 90.7%) sqlglot:fix/setop-scope-types: 182930 total, 165990 passed (pass rate: 90.7%) Transitions: ❌ 1 regression (view logs) |
This branch has not been deployed
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.

This PR makes the annotator resolve each set operation's output types during the post-order annotation pass, instead of doing it lazily (context: #8702 (comment)).
This removes the separate set-operation traversal, at the cost of computing outputs eagerly even when no consumer requests them. Performance-wise, this is pretty close to main.