Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 11 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2273,6 +2273,16 @@ impl<'a> Parser<'a> {
}
}

// Helper function to parse an element within GROUPING SETS
// This element can either be a single expression or a parenthesized list of expressions.
fn parse_grouping_set_element(&mut self) -> Result<Vec<Expr>, ParserError> {
if self.peek_token_ref().token == Token::LParen {
self.parse_tuple(true, true)
} else {
Ok(vec![self.parse_expr()?])
}
}

pub fn parse_case_expr(&mut self) -> Result<Expr, ParserError> {
let mut operand = None;
if !self.parse_keyword(Keyword::WHEN) {
Expand Down Expand Up @@ -10045,7 +10055,7 @@ impl<'a> Parser<'a> {
}
if self.parse_keywords(&[Keyword::GROUPING, Keyword::SETS]) {
self.expect_token(&Token::LParen)?;
let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
let result = self.parse_comma_separated(|p| p.parse_grouping_set_element())?;
self.expect_token(&Token::RParen)?;
modifiers.push(GroupByWithModifier::GroupingSets(Expr::GroupingSets(
result,
Expand Down
32 changes: 32 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2822,6 +2822,38 @@ fn parse_group_by_special_grouping_sets() {
}
}

#[test]
fn parse_group_by_grouping_sets_single_values() {
let sql = "SELECT a, b, SUM(c) FROM tab1 GROUP BY a, b GROUPING SETS ((a, b), a, (b), c, ())";
let canonical =
"SELECT a, b, SUM(c) FROM tab1 GROUP BY a, b GROUPING SETS ((a, b), (a), (b), (c), ())";
match all_dialects().one_statement_parses_to(sql, canonical) {
Statement::Query(query) => {
let group_by = &query.body.as_select().unwrap().group_by;
assert_eq!(
group_by,
&GroupByExpr::Expressions(
vec![
Expr::Identifier(Ident::new("a")),
Expr::Identifier(Ident::new("b"))
],
vec![GroupByWithModifier::GroupingSets(Expr::GroupingSets(vec![
vec![
Expr::Identifier(Ident::new("a")),
Expr::Identifier(Ident::new("b"))
],
vec![Expr::Identifier(Ident::new("a"))],
vec![Expr::Identifier(Ident::new("b"))],
vec![Expr::Identifier(Ident::new("c"))],
vec![]
]))]
)
);
}
_ => unreachable!(),
}
}

#[test]
fn parse_select_having() {
let sql = "SELECT foo FROM bar GROUP BY foo HAVING COUNT(*) > 1";
Expand Down