Skip to content

Commit b6359c1

Browse files
committed
[PIVOT] Optional AS keyword for value aliases
1 parent 329a94c commit b6359c1

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/ast/query.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,7 @@ pub enum TableFactor {
15891589
///
15901590
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#pivot_operator)
15911591
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/constructs/pivot)
1592+
/// [Oracle](https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/SELECT.html#GUID-CFA006CA-6FF1-4972-821E-6996142A51C6__GUID-68257B27-1C4C-4C47-8140-5C60E0E65D35)
15921593
Pivot {
15931594
/// The input table to pivot.
15941595
table: Box<TableFactor>,
@@ -1610,8 +1611,10 @@ pub enum TableFactor {
16101611
/// table UNPIVOT [ { INCLUDE | EXCLUDE } NULLS ] (value FOR name IN (column1, [ column2, ... ])) [ alias ]
16111612
/// ```
16121613
///
1613-
/// See <https://docs.snowflake.com/en/sql-reference/constructs/unpivot>.
1614-
/// See <https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-unpivot>.
1614+
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/constructs/unpivot)
1615+
/// [Databricks](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-unpivot)
1616+
/// [BigQuery](https://docs.cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#unpivot_operator)
1617+
/// [Oracle](https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/SELECT.html#GUID-CFA006CA-6FF1-4972-821E-6996142A51C6__GUID-9B4E0389-413C-4014-94A1-0A0571BDF7E1)
16151618
Unpivot {
16161619
/// The input table to unpivot.
16171620
table: Box<TableFactor>,

src/parser/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16172,6 +16172,15 @@ impl<'a> Parser<'a> {
1617216172
Ok(ExprWithAlias { expr, alias })
1617316173
}
1617416174

16175+
/// Parse an expression followed by an optional alias; Unlike
16176+
/// [Self::parse_expr_with_alias] the "AS" keyword between the expression
16177+
/// and the alias is optional.
16178+
fn parse_expr_with_alias_optional_as_keyword(&mut self) -> Result<ExprWithAlias, ParserError> {
16179+
let expr = self.parse_expr()?;
16180+
let alias = self.parse_identifier_optional_alias()?;
16181+
Ok(ExprWithAlias { expr, alias })
16182+
}
16183+
1617516184
/// Parse a PIVOT table factor (ClickHouse/Oracle style pivot), returning a TableFactor.
1617616185
pub fn parse_pivot_table_factor(
1617716186
&mut self,
@@ -16200,7 +16209,9 @@ impl<'a> Parser<'a> {
1620016209
} else if self.peek_sub_query() {
1620116210
PivotValueSource::Subquery(self.parse_query()?)
1620216211
} else {
16203-
PivotValueSource::List(self.parse_comma_separated(Self::parse_expr_with_alias)?)
16212+
PivotValueSource::List(
16213+
self.parse_comma_separated(Self::parse_expr_with_alias_optional_as_keyword)?,
16214+
)
1620416215
};
1620516216
self.expect_token(&Token::RParen)?;
1620616217

tests/sqlparser_common.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11357,6 +11357,12 @@ fn parse_pivot_table() {
1135711357
verified_stmt(multiple_value_columns_sql).to_string(),
1135811358
multiple_value_columns_sql
1135911359
);
11360+
11361+
// assert optional "AS" keyword for aliases for pivot values
11362+
one_statement_parses_to(
11363+
"SELECT * FROM t PIVOT(SUM(1) FOR a.abc IN (1 x, 'two' y, three z))",
11364+
"SELECT * FROM t PIVOT(SUM(1) FOR a.abc IN (1 AS x, 'two' AS y, three AS z))",
11365+
);
1136011366
}
1136111367

1136211368
#[test]

0 commit comments

Comments
 (0)