fix(optimizer)!: qualify UNPIVOT on CTE sources#7550
Merged
georgesittas merged 1 commit intomainfrom Apr 23, 2026
Merged
Conversation
Two fixes that together let `SELECT * FROM cte UNPIVOT(...)` qualify
correctly in BigQuery and other dialects.
- parser: `UNPIVOT`'s pre-`FOR` value column(s) and the `FOR` field are now
parsed as `Identifier` (was `Column`). Those names don't reference
existing columns — they're new output names. The `IN`-list items stay as
`Column` since they do reference source-table columns. `PIVOT` is
unchanged.
- optimizer/resolver: when a CTE is pivoted, the scope stores an
`exp.Table` under the pivot alias rather than the CTE's `Scope`, so
column resolution couldn't see the CTE's columns. Fall back to
`scope.cte_sources` in that case. Guarded on `not source.db` and
`source.args.get("pivots")` so a real `db.x` that happens to share a
name with a CTE doesn't misroute through the CTE scope.
- optimizer/qualify_columns: removes the post-hoc filter in
`validate_qualify_columns` that excluded unpivot output names from
`scope.unqualified_columns` — they're `Identifier`s now and never land
there. `_unpivot_columns` yields `Identifier`s; `output_name` still
works.
Test: new assertion in `test_qualify_columns` covering the CTE + `UNPIVOT` shape.
geooo109
approved these changes
Apr 23, 2026
georgesittas
commented
Apr 23, 2026
Comment on lines
+5154
to
+5158
| if unpivot: | ||
| pivot.set("expressions", [_unpivot_target(e) for e in pivot.expressions]) | ||
| for pivot_field in pivot.fields: | ||
| if isinstance(pivot_field, exp.In): | ||
| pivot_field.set("this", _unpivot_target(pivot_field.this)) |
Collaborator
Author
There was a problem hiding this comment.
See https://docs.cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#unpivot_operator for reference.
The Produce table contains:
/*---------+----+----+----+----+
| product | Q1 | Q2 | Q3 | Q4 |
+---------+----+----+----+----+
| Kale | 51 | 23 | 45 | 3 |
| Apple | 77 | 0 | 25 | 2 |
+---------+----+----+----+----*/
So, for this query:
SELECT * FROM Produce
UNPIVOT(sales FOR quarter IN (Q1, Q2, Q3, Q4))
/*---------+-------+---------+
| product | sales | quarter |
+---------+-------+---------+
| Kale | 51 | Q1 |
| Kale | 23 | Q2 |
| Kale | 45 | Q3 |
| Kale | 3 | Q4 |
| Apple | 77 | Q1 |
| Apple | 0 | Q2 |
| Apple | 25 | Q3 |
| Apple | 2 | Q4 |
+---------+-------+---------*/
We can't treat sales and quarter as Column nodes. They're really Identifier nodes that determine what the UNPIVOT operator's output schema is.
Contributor
SQLGlot Integration Test ResultsComparing:
By Dialect
Overallmain: 113234 total, 112044 passed (pass rate: 98.9%), sqlglot version: sqlglot:jo/improve_unpivot_support: 101035 total, 101035 passed (pass rate: 100.0%), sqlglot version: Transitions: Dialect pair changes: 0 previous results not found, 3 current results not found ✅ 34 test(s) passed |
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.
Two fixes that together let
SELECT * FROM cte UNPIVOT(...)qualifycorrectly in BigQuery and other dialects.
parser:
UNPIVOT's pre-FORvalue column(s) and theFORfield are nowparsed as
Identifier(wasColumn). Those names don't referenceexisting columns — they're new output names. The
IN-list items stay asColumnsince they do reference source-table columns.PIVOTisunchanged.
optimizer/resolver: when a CTE is pivoted, the scope stores an
exp.Tableunder the pivot alias rather than the CTE'sScope, socolumn resolution couldn't see the CTE's columns. Fall back to
scope.cte_sourcesin that case. Guarded onnot source.dbandsource.args.get("pivots")so a realdb.xthat happens to share aname with a CTE doesn't misroute through the CTE scope.
optimizer/qualify_columns: removes the post-hoc filter in
validate_qualify_columnsthat excluded unpivot output names fromscope.unqualified_columns— they'reIdentifiers now and never landthere.
_unpivot_columnsyieldsIdentifiers;output_namestillworks.
Test: new assertion in
test_qualify_columnscovering the CTE +UNPIVOTshape.