Commit 8f572f8
authored
fix(optimizer)!: qualify UNPIVOT on CTE sources (#7550)
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.1 parent e3fee4d commit 8f572f8
4 files changed
Lines changed: 49 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
138 | 138 | | |
139 | 139 | | |
140 | 140 | | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | 141 | | |
149 | 142 | | |
150 | 143 | | |
| |||
188 | 181 | | |
189 | 182 | | |
190 | 183 | | |
191 | | - | |
| 184 | + | |
192 | 185 | | |
193 | 186 | | |
194 | 187 | | |
195 | | - | |
| 188 | + | |
196 | 189 | | |
197 | | - | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
198 | 196 | | |
199 | 197 | | |
200 | 198 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
139 | 139 | | |
140 | 140 | | |
141 | 141 | | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
142 | 153 | | |
143 | 154 | | |
144 | 155 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
275 | 275 | | |
276 | 276 | | |
277 | 277 | | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
278 | 287 | | |
279 | 288 | | |
280 | 289 | | |
| |||
5142 | 5151 | | |
5143 | 5152 | | |
5144 | 5153 | | |
| 5154 | + | |
| 5155 | + | |
| 5156 | + | |
| 5157 | + | |
| 5158 | + | |
| 5159 | + | |
5145 | 5160 | | |
5146 | 5161 | | |
5147 | 5162 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
632 | 632 | | |
633 | 633 | | |
634 | 634 | | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
635 | 650 | | |
636 | 651 | | |
637 | 652 | | |
| |||
0 commit comments