Skip to content

feat: UNPIVOT supports AS #17595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/query/ast/src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use derive_visitor::Drive;
use derive_visitor::DriveMut;
use educe::Educe;

use crate::ast::quote::QuotedString;
use crate::ast::write_comma_separated_list;
use crate::ast::write_comma_separated_string_map;
use crate::ast::write_dot_separated_list;
Expand Down Expand Up @@ -630,23 +631,39 @@ impl Display for Pivot {
}
}

#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
pub struct UnpivotName {
pub ident: Identifier,
pub alias: Option<String>,
}

impl Display for UnpivotName {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self.alias {
Some(alias) => {
write!(f, "{} AS {}", self.ident, QuotedString(alias, '\''))
}
None => write!(f, "{}", self.ident),
}
}
}

#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
pub struct Unpivot {
pub value_column: Identifier,
pub column_name: Identifier,
pub names: Vec<Identifier>,
pub unpivot_column: Identifier,
pub column_names: Vec<UnpivotName>,
}

impl Display for Unpivot {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(
f,
"UNPIVOT({} FOR {} IN (",
self.value_column, self.column_name
self.value_column, self.unpivot_column
)?;
write_comma_separated_list(f, &self.names)?;
write!(f, "))")?;
Ok(())
write_comma_separated_list(f, &self.column_names)?;
write!(f, "))")
}
}

Expand Down
32 changes: 28 additions & 4 deletions src/query/ast/src/parser/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,16 +900,40 @@ fn pivot(i: Input) -> IResult<Pivot> {
)(i)
}

fn unpivot_name(i: Input) -> IResult<UnpivotName> {
let short_alias = map(
rule! {
#literal_string
~ #error_hint(
rule! { AS },
"an alias without `AS` keyword has already been defined before this one, \
please remove one of them"
)
},
|(string, _)| string,
);
let as_alias = map(
rule! {
AS ~ #literal_string
},
|(_, string)| string,
);
map(
rule! {#ident ~ (#short_alias | #as_alias)?},
|(ident, alias)| UnpivotName { ident, alias },
)(i)
}

// UNPIVOT(ident for ident IN (ident, ...))
fn unpivot(i: Input) -> IResult<Unpivot> {
map(
rule! {
UNPIVOT ~ "(" ~ #ident ~ FOR ~ #ident ~ IN ~ "(" ~ #comma_separated_list1(ident) ~ ")" ~ ")"
UNPIVOT ~ "(" ~ #ident ~ FOR ~ #ident ~ IN ~ "(" ~ #comma_separated_list1(unpivot_name) ~ ")" ~ ")"
},
|(_unpivot, _, value_column, _for, column_name, _in, _, names, _, _)| Unpivot {
|(_unpivot, _, value_column, _for, unpivot_column, _in, _, column_names, _, _)| Unpivot {
value_column,
column_name,
names,
unpivot_column,
column_names,
},
)(i)
}
Expand Down
2 changes: 1 addition & 1 deletion src/query/ast/tests/it/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ fn test_query() {
r#"select * from (select * from monthly_sales) pivot(sum(amount) for month in ('JAN', 'FEB', 'MAR', 'APR')) order by empid"#,
r#"select * from monthly_sales pivot(sum(amount) for month in (select distinct month from monthly_sales)) order by empid"#,
r#"select * from (select * from monthly_sales) pivot(sum(amount) for month in ((select distinct month from monthly_sales))) order by empid"#,
r#"select * from monthly_sales_1 unpivot(sales for month in (jan, feb, mar, april)) order by empid"#,
r#"select * from monthly_sales_1 unpivot(sales for month in (jan as '1月', feb 'February', mar As 'MARCH', april)) order by empid"#,
r#"select * from (select * from monthly_sales_1) unpivot(sales for month in (jan, feb, mar, april)) order by empid"#,
r#"select * from range(1, 2)"#,
r#"select sum(a) over w from customer window w as (partition by a order by b)"#,
Expand Down
158 changes: 94 additions & 64 deletions src/query/ast/tests/it/testdata/query.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7154,19 +7154,19 @@ Query {


---------- Input ----------
select * from monthly_sales_1 unpivot(sales for month in (jan, feb, mar, april)) order by empid
select * from monthly_sales_1 unpivot(sales for month in (jan as '1月', feb 'February', mar As 'MARCH', april)) order by empid
---------- Output ---------
SELECT * FROM monthly_sales_1 UNPIVOT(sales FOR month IN (jan, feb, mar, april)) ORDER BY empid
SELECT * FROM monthly_sales_1 UNPIVOT(sales FOR month IN (jan AS '1月', feb AS 'February', mar AS 'MARCH', april)) ORDER BY empid
---------- AST ------------
Query {
span: Some(
0..80,
0..112,
),
with: None,
body: Select(
SelectStmt {
span: Some(
0..80,
0..112,
),
hints: None,
distinct: false,
Expand All @@ -7186,7 +7186,7 @@ Query {
from: [
Table {
span: Some(
14..80,
14..112,
),
catalog: None,
database: None,
Expand All @@ -7212,46 +7212,64 @@ Query {
quote: None,
ident_type: None,
},
column_name: Identifier {
unpivot_column: Identifier {
span: Some(
48..53,
),
name: "month",
quote: None,
ident_type: None,
},
names: [
Identifier {
span: Some(
58..61,
column_names: [
UnpivotName {
ident: Identifier {
span: Some(
58..61,
),
name: "jan",
quote: None,
ident_type: None,
},
alias: Some(
"1月",
),
name: "jan",
quote: None,
ident_type: None,
},
Identifier {
span: Some(
63..66,
UnpivotName {
ident: Identifier {
span: Some(
73..76,
),
name: "feb",
quote: None,
ident_type: None,
},
alias: Some(
"February",
),
name: "feb",
quote: None,
ident_type: None,
},
Identifier {
span: Some(
68..71,
UnpivotName {
ident: Identifier {
span: Some(
89..92,
),
name: "mar",
quote: None,
ident_type: None,
},
alias: Some(
"MARCH",
),
name: "mar",
quote: None,
ident_type: None,
},
Identifier {
span: Some(
73..78,
),
name: "april",
quote: None,
ident_type: None,
UnpivotName {
ident: Identifier {
span: Some(
105..110,
),
name: "april",
quote: None,
ident_type: None,
},
alias: None,
},
],
},
Expand All @@ -7270,15 +7288,15 @@ Query {
OrderByExpr {
expr: ColumnRef {
span: Some(
90..95,
122..127,
),
column: ColumnRef {
database: None,
table: None,
column: Name(
Identifier {
span: Some(
90..95,
122..127,
),
name: "empid",
quote: None,
Expand Down Expand Up @@ -7405,46 +7423,58 @@ Query {
quote: None,
ident_type: None,
},
column_name: Identifier {
unpivot_column: Identifier {
span: Some(
64..69,
),
name: "month",
quote: None,
ident_type: None,
},
names: [
Identifier {
span: Some(
74..77,
),
name: "jan",
quote: None,
ident_type: None,
column_names: [
UnpivotName {
ident: Identifier {
span: Some(
74..77,
),
name: "jan",
quote: None,
ident_type: None,
},
alias: None,
},
Identifier {
span: Some(
79..82,
),
name: "feb",
quote: None,
ident_type: None,
UnpivotName {
ident: Identifier {
span: Some(
79..82,
),
name: "feb",
quote: None,
ident_type: None,
},
alias: None,
},
Identifier {
span: Some(
84..87,
),
name: "mar",
quote: None,
ident_type: None,
UnpivotName {
ident: Identifier {
span: Some(
84..87,
),
name: "mar",
quote: None,
ident_type: None,
},
alias: None,
},
Identifier {
span: Some(
89..94,
),
name: "april",
quote: None,
ident_type: None,
UnpivotName {
ident: Identifier {
span: Some(
89..94,
),
name: "april",
quote: None,
ident_type: None,
},
alias: None,
},
],
},
Expand Down
Loading
Loading