-
Notifications
You must be signed in to change notification settings - Fork 630
feat: support multiple value for pivot #1970
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
3fade60
9b2e8d0
132502e
a245336
12f8543
62189bc
a69ec0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10820,6 +10820,18 @@ impl<'a> Parser<'a> { | |
self.parse_parenthesized_column_list_inner(optional, allow_empty, |p| p.parse_identifier()) | ||
} | ||
|
||
pub fn parse_parenthesized_compound_identifier_list( | ||
&mut self, | ||
optional: IsOptional, | ||
allow_empty: bool, | ||
) -> Result<Vec<Expr>, ParserError> { | ||
self.parse_parenthesized_column_list_inner(optional, allow_empty, |p| { | ||
Ok(Expr::CompoundIdentifier( | ||
p.parse_period_separated(|p| p.parse_identifier())?, | ||
)) | ||
}) | ||
} | ||
|
||
/// Parses a parenthesized comma-separated list of index columns, which can be arbitrary | ||
/// expressions with ordering information (and an opclass in some dialects). | ||
fn parse_parenthesized_index_column_list(&mut self) -> Result<Vec<IndexColumn>, ParserError> { | ||
|
@@ -13828,7 +13840,13 @@ impl<'a> Parser<'a> { | |
self.expect_token(&Token::LParen)?; | ||
let aggregate_functions = self.parse_comma_separated(Self::parse_aliased_function_call)?; | ||
self.expect_keyword_is(Keyword::FOR)?; | ||
let value_column = self.parse_period_separated(|p| p.parse_identifier())?; | ||
let value_column = if self.peek_token_ref().token == Token::LParen { | ||
self.parse_parenthesized_compound_identifier_list(Mandatory, false)? | ||
} else { | ||
vec![Expr::CompoundIdentifier( | ||
self.parse_period_separated(|p| p.parse_identifier())?, | ||
)] | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar to unpivot can we call parse_expr directly here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, we cannot do it here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah right that makes sense, I think we can do something this instead to reuse existing capabilities of the parser? let value_column = if self.peek_token_ref().token == Token::LParen {
self.parse_parenthesized_column_list_inner(Mandatory, false, |p| {
p.parse_subexpr(self.dialect.prec_value(Precedence::Between))
})?
} else {
vec![self.parse_subexpr(self.dialect.prec_value(Precedence::Between))?]
}; |
||
self.expect_keyword_is(Keyword::IN)?; | ||
|
||
self.expect_token(&Token::LParen)?; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10875,7 +10875,10 @@ fn parse_pivot_table() { | |
expected_function("b", Some("t")), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add tests to demonstrate the new behavior? it seems like those are currently lacking in the PR? |
||
expected_function("c", Some("u")), | ||
], | ||
value_column: vec![Ident::new("a"), Ident::new("MONTH")], | ||
value_column: vec![Expr::CompoundIdentifier(vec![ | ||
Ident::new("a"), | ||
Ident::new("MONTH") | ||
])], | ||
value_source: PivotValueSource::List(vec![ | ||
ExprWithAlias { | ||
expr: Expr::value(number("1")), | ||
|
@@ -10922,6 +10925,15 @@ fn parse_pivot_table() { | |
verified_stmt(sql_without_table_alias).to_string(), | ||
sql_without_table_alias | ||
); | ||
|
||
let sql_with_multiple_value_column = concat!( | ||
"SELECT * FROM person ", | ||
"PIVOT(SUM(age) AS a, AVG(class) AS c FOR (name, age) IN (('John', 30) AS c1, ('Mike', 40) AS c2))" | ||
); | ||
assert_eq!( | ||
verified_stmt(sql_with_multiple_value_column).to_string(), | ||
sql_with_multiple_value_column | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm this assertion is probably not needed, I think verified_stmt already does the same assertion |
||
} | ||
|
||
#[test] | ||
|
@@ -11143,7 +11155,7 @@ fn parse_pivot_unpivot_table() { | |
expr: call("sum", [Expr::Identifier(Ident::new("population"))]), | ||
alias: None | ||
}], | ||
value_column: vec![Ident::new("year")], | ||
value_column: vec![Expr::CompoundIdentifier(vec![Ident::new("year")])], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm heads up that this representation wouldn't be ideal - a compound identifier would be expected to have more than one ident in it |
||
value_source: PivotValueSource::List(vec![ | ||
ExprWithAlias { | ||
expr: Expr::Value( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.