EnricoMi commented on code in PR #37407:
URL: https://github.com/apache/spark/pull/37407#discussion_r970561575
##########
sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBaseParser.g4:
##########
@@ -618,6 +618,46 @@ pivotValue
: expression (AS? identifier)?
;
+unpivotClause
+ : UNPIVOT nullOperator=unpivotNullClause? LEFT_PAREN
+ operator=unpivotOperator
+ RIGHT_PAREN (AS? identifier)?
+ ;
+
+unpivotNullClause
+ : (INCLUDE | EXCLUDE) NULLS
+ ;
+
+unpivotOperator
+ : (unpivotSingleValueColumnClause | unpivotMultiValueColumnClause)
+ ;
+
+unpivotSingleValueColumnClause
+ : unpivotValueColumn FOR unpivotNameColumn IN LEFT_PAREN
unpivotColumns+=unpivotColumn (COMMA unpivotColumns+=unpivotColumn)* RIGHT_PAREN
+ ;
+
+unpivotMultiValueColumnClause
+ : LEFT_PAREN unpivotValueColumns+=unpivotValueColumn (COMMA
unpivotValueColumns+=unpivotValueColumn)* RIGHT_PAREN
+ FOR unpivotNameColumn
+ IN LEFT_PAREN unpivotColumnSets+=unpivotColumnSet (COMMA
unpivotColumnSets+=unpivotColumnSet)* RIGHT_PAREN
+ ;
+
+unpivotColumnSet
+ : LEFT_PAREN unpivotColumns+=unpivotColumn (COMMA
unpivotColumns+=unpivotColumn)* RIGHT_PAREN (AS? identifier)?
Review Comment:
Without an alias result contains string representation of the column name
tuples:
https://github.com/apache/spark/pull/37407/files#diff-f306d1dfcd20c4cdcdb5d631416ee9d92bf6893422b0a9683ca4493d7215e5e8R199-R212
```
-- !query
SELECT * FROM courseEarningsAndSales
UNPIVOT (
(earnings, sales) FOR year IN ((earnings2012, sales2012), (earnings2013,
sales2013), (earnings2014, sales2014))
)
-- !query schema
struct<course:string,year:string,earnings:int,sales:int>
-- !query output
Java (`earnings2012`,`sales2012`) 20000 1
Java (`earnings2013`,`sales2013`) 30000 2
Java (`earnings2014`,`sales2014`) NULL NULL
dotNET (`earnings2012`,`sales2012`) 15000 2
dotNET (`earnings2013`,`sales2013`) 48000 1
dotNET (`earnings2014`,`sales2014`) 22500 1
```
If users are happy with that, why forcing an alias?
This is optional in
[BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#unpivot_operator)
as well, the other SQL flavours mentioned in the PR description are not
specific in that matter.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]