Commit cf0ffd9
authored
fix(optimizer): qualify (UN)PIVOT on CTE sources (#7560)
* fix(optimizer): qualify (UN)PIVOT on CTE sources
Fixes `SELECT * FROM cte UNPIVOT(...)` and `SELECT alias.col FROM cte
UNPIVOT(...) AS alias` in bigquery and other dialects. Both cases used
to error or silently skip star expansion because column resolution for a
pivoted CTE went to the wrong place.
Background: when a CTE is referenced with a pivot, `scope.py` stores the
pivoted `exp.Table` under the pivot alias (not the CTE's `Scope`) so the
pivot is treated as a new logical source. But the Table has no schema
entry for the CTE name, so column lookups returned `[]`.
Changes:
- parser: UNPIVOT's pre-FOR value column(s) and the FOR field are now
parsed as `Identifier` rather than `Column`. They're new output names,
not references to existing columns. IN-list items stay as `Column`
since those do reference source-table columns. PIVOT is unchanged.
- optimizer/resolver: for pivoted-CTE sources (`Table` with pivots, no
db qualifier, name matches a known CTE), fall back to the CTE's
`Scope` to read pre-pivot columns. Enables star expansion.
- optimizer/qualify_columns: when validating a column against a pivoted
source, validate against the post-pivot column set rather than the
pre-pivot source columns. Direct references like `u.val` pass, typos
like `u.nonexistent` still error.
- optimizer/qualify_columns: `_pivot_output_columns` helper factored out
of `_expand_stars` and reused by the validator. Collapses ~18 lines
of inline logic that computed excluded-vs-output column sets
separately then combined them at the use site.
- optimizer/qualify_columns: the `_unpivot_columns` filter in
`validate_qualify_columns` is removed. With the parser change, unpivot
output names are Identifiers and never enter `scope.unqualified_columns`,
so the filter was dead code.
* Add multi-value/name test1 parent fc6e7cb commit cf0ffd9
2 files changed
Lines changed: 64 additions & 25 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
181 | 181 | | |
182 | 182 | | |
183 | 183 | | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
184 | 211 | | |
185 | 212 | | |
186 | 213 | | |
| |||
605 | 632 | | |
606 | 633 | | |
607 | 634 | | |
| 635 | + | |
608 | 636 | | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
609 | 642 | | |
610 | 643 | | |
611 | 644 | | |
| |||
782 | 815 | | |
783 | 816 | | |
784 | 817 | | |
785 | | - | |
786 | | - | |
787 | | - | |
788 | 818 | | |
789 | | - | |
790 | | - | |
791 | | - | |
792 | | - | |
793 | | - | |
794 | | - | |
795 | | - | |
796 | | - | |
797 | | - | |
798 | | - | |
799 | | - | |
800 | | - | |
801 | | - | |
802 | | - | |
803 | | - | |
804 | | - | |
805 | 819 | | |
806 | 820 | | |
807 | 821 | | |
| |||
865 | 879 | | |
866 | 880 | | |
867 | 881 | | |
868 | | - | |
869 | | - | |
870 | | - | |
871 | | - | |
872 | | - | |
| 882 | + | |
873 | 883 | | |
874 | 884 | | |
875 | 885 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
647 | 647 | | |
648 | 648 | | |
649 | 649 | | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
650 | 679 | | |
651 | 680 | | |
652 | 681 | | |
| |||
0 commit comments