Skip to content

Commit c8d4222

Browse files
Fix: Transpile bug with pivot subquery string literal column names
1 parent de4c3bf commit c8d4222

7 files changed

Lines changed: 15 additions & 10 deletions

File tree

sqlglot-integration-tests

sqlglot/generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2492,7 +2492,7 @@ def _pivot_in_value_aliases(self, expression: exp.Pivot) -> list[exp.Expression]
24922492

24932493
# Derive the per-value suffix from the first stored column vs the first IN-list value.
24942494
# This correctly handles dialects (e.g. Spark single-agg) that ignore agg aliases.
2495-
first_base = in_exprs[0].alias_or_name
2495+
first_base = in_exprs[0].sql() if src_identify_pivot_strings else in_exprs[0].alias_or_name
24962496
first_stored = columns[0].name
24972497

24982498
# exit if only suffix matches, not prefix. (e.g. BigQuery, which cannot be fixed)

sqlglot/parser.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5270,7 +5270,12 @@ def _parse_pivot(self) -> exp.Pivot | None:
52705270
if isinstance(seq_get(pivot_field_expressions, 0), exp.PivotAny):
52715271
continue
52725272

5273-
all_fields.append([fld.alias_or_name for fld in pivot_field_expressions])
5273+
all_fields.append(
5274+
[
5275+
fld.sql() if self.IDENTIFY_PIVOT_STRINGS else fld.alias_or_name
5276+
for fld in pivot_field_expressions
5277+
]
5278+
)
52745279

52755280
if all_fields:
52765281
if names:

tests/dialects/test_duckdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ def test_duckdb(self):
830830
},
831831
)
832832
self.validate_all(
833-
"SELECT * FROM produce PIVOT(SUM(sales) FOR quarter IN ('Q1', 'Q2'))",
833+
"SELECT * FROM produce PIVOT(SUM(sales) FOR quarter IN ('Q1' AS \"'Q1'\", 'Q2' AS \"'Q2'\"))",
834834
read={
835835
"snowflake": "SELECT * FROM produce PIVOT(SUM(produce.sales) FOR produce.quarter IN ('Q1', 'Q2'))",
836836
},

tests/dialects/test_spark.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,19 +712,19 @@ def test_spark(self):
712712
},
713713
)
714714
self.validate_all(
715-
"SELECT piv.Q1 FROM (SELECT * FROM produce PIVOT(SUM(sales) FOR quarter IN ('Q1', 'Q2'))) AS piv",
715+
"SELECT piv.Q1 FROM (SELECT * FROM produce PIVOT(SUM(sales) FOR quarter IN ('Q1' AS `'Q1'`, 'Q2' AS `'Q2'`))) AS piv",
716716
read={
717717
"snowflake": "SELECT piv.Q1 FROM produce PIVOT(SUM(sales) FOR quarter IN ('Q1', 'Q2')) piv",
718718
},
719719
)
720720
self.validate_all(
721-
"SELECT piv.Q1 FROM (SELECT * FROM (SELECT * FROM produce) PIVOT(SUM(sales) FOR quarter IN ('Q1', 'Q2'))) AS piv",
721+
"SELECT piv.Q1 FROM (SELECT * FROM (SELECT * FROM produce) PIVOT(SUM(sales) FOR quarter IN ('Q1' AS `'Q1'`, 'Q2' AS `'Q2'`))) AS piv",
722722
read={
723723
"snowflake": "SELECT piv.Q1 FROM (SELECT * FROM produce) PIVOT(SUM(sales) FOR quarter IN ('Q1', 'Q2')) piv",
724724
},
725725
)
726726
self.validate_all(
727-
"SELECT * FROM produce PIVOT(SUM(produce.sales) FOR quarter IN ('Q1', 'Q2'))",
727+
"SELECT * FROM produce PIVOT(SUM(produce.sales) FOR quarter IN ('Q1' AS `'Q1'`, 'Q2' AS `'Q2'`))",
728728
read={
729729
"snowflake": "SELECT * FROM produce PIVOT (SUM(produce.sales) FOR produce.quarter IN ('Q1', 'Q2'))",
730730
},

tests/fixtures/optimizer/optimizer.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,8 @@ PIVOT(SUM(`u_cte`.`f`) AS `sum` FOR `u_cte`.`h` IN ('x', 'y')) AS `_0`;
639639
SELECT * FROM u PIVOT (SUM(f) FOR h IN ('x', 'y'));
640640
SELECT
641641
"_0"."G" AS "G",
642-
"_0"."X" AS "X",
643-
"_0"."Y" AS "Y"
642+
"_0"."'x'" AS "'x'",
643+
"_0"."'y'" AS "'y'"
644644
FROM "U" AS "U"
645645
PIVOT(SUM("U"."F") FOR "U"."H" IN ('x', 'y')) AS "_0";
646646

tests/test_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ def test_pivot_columns(self):
688688
"bigquery": ["prop", "rudder"],
689689
"duckdb": ["prop", "rudder"],
690690
"redshift": ["prop", "rudder"],
691-
"snowflake": ["prop", "rudder"],
691+
"snowflake": ['''"'prop'"''', '''"'rudder'"'''],
692692
"spark": ["prop", "rudder"],
693693
},
694694
everything_aliased: {

0 commit comments

Comments
 (0)