Skip to content

fix(optimizer): resolve set operation types during annotation [CODEX] - #8720

Open
georgesittas wants to merge 5 commits into
mainfrom
fix/setop-scope-types
Open

georgesittas wants to merge 5 commits into
mainfrom
fix/setop-scope-types

Conversation

@georgesittas

@georgesittas georgesittas commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

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.

@georgesittas
georgesittas requested review from treysp and a balanced review from Copilot October 21, 2026 14:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 thread sqlglot/optimizer/annotate_types.py Outdated
Comment on lines +644 to +645
cached = self._setop_column_types.get(id(setop))
return dict(cached[1]) if cached else {}
Comment thread sqlglot/optimizer/annotate_types.py Outdated
Comment on lines +653 to +654
cached = self._setop_column_types.pop(id(operand), None)
operands.append(cached[1] if cached else [])
@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

SQLGlot Integration Test Results

❌ 1 regression — see details below

Comparing:

  • this branch (sqlglot:fix/setop-scope-types @ sqlglot 3de8f98)
  • baseline (main @ sqlglot 603dd92)

By Dialect

dialect main feature branch transitions links
bigquery -> bigquery 34290/34695 passed (98.8%) 34290/34695 passed (98.8%) 1 fail -> pass
1 pass -> fail
full result / delta

Overall

main: 182930 total, 165990 passed (pass rate: 90.7%)

sqlglot:fix/setop-scope-types: 182930 total, 165990 passed (pass rate: 90.7%)

Transitions:
1 fail -> pass
1 pass -> fail

❌ 1 regression (view logs)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants