Skip to content

Commit b5b8594

Browse files
committed
[PIVOT] Optional AS keyword for value aliases
1 parent 8e36e8e commit b5b8594

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

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
@@ -16200,6 +16200,15 @@ impl<'a> Parser<'a> {
1620016200
Ok(ExprWithAlias { expr, alias })
1620116201
}
1620216202

16203+
/// Parse an expression followed by an optional alias; Unlike
16204+
/// [Self::parse_expr_with_alias] the "AS" keyword between the expression
16205+
/// and the alias is optional.
16206+
fn parse_expr_with_alias_optional_as_keyword(&mut self) -> Result<ExprWithAlias, ParserError> {
16207+
let expr = self.parse_expr()?;
16208+
let alias = self.parse_identifier_optional_alias()?;
16209+
Ok(ExprWithAlias { expr, alias })
16210+
}
16211+
1620316212
/// Parse a PIVOT table factor (ClickHouse/Oracle style pivot), returning a TableFactor.
1620416213
pub fn parse_pivot_table_factor(
1620516214
&mut self,
@@ -16228,7 +16237,9 @@ impl<'a> Parser<'a> {
1622816237
} else if self.peek_sub_query() {
1622916238
PivotValueSource::Subquery(self.parse_query()?)
1623016239
} else {
16231-
PivotValueSource::List(self.parse_comma_separated(Self::parse_expr_with_alias)?)
16240+
PivotValueSource::List(
16241+
self.parse_comma_separated(Self::parse_expr_with_alias_optional_as_keyword)?,
16242+
)
1623216243
};
1623316244
self.expect_token(&Token::RParen)?;
1623416245

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)