Skip to content

Commit 5de341b

Browse files
committed
Fix: GROUPING SETS accept values without parenthesis (apache#1867)
1 parent 35314a1 commit 5de341b

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/parser/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9428,7 +9428,13 @@ impl<'a> Parser<'a> {
94289428
}
94299429
if self.parse_keywords(&[Keyword::GROUPING, Keyword::SETS]) {
94309430
self.expect_token(&Token::LParen)?;
9431-
let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
9431+
let result = self.parse_comma_separated(|p| {
9432+
if p.peek_token_ref().token == Token::LParen {
9433+
p.parse_tuple(true, true)
9434+
} else {
9435+
Ok(vec![p.parse_expr()?])
9436+
}
9437+
})?;
94329438
self.expect_token(&Token::RParen)?;
94339439
modifiers.push(GroupByWithModifier::GroupingSets(Expr::GroupingSets(
94349440
result,

tests/sqlparser_common.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,6 +2777,38 @@ fn parse_group_by_special_grouping_sets() {
27772777
}
27782778
}
27792779

2780+
#[test]
2781+
fn parse_group_by_grouping_sets_single_values() {
2782+
let sql = "SELECT a, b, SUM(c) FROM tab1 GROUP BY a, b GROUPING SETS ((a, b), a, (b), c, ())";
2783+
let canonical =
2784+
"SELECT a, b, SUM(c) FROM tab1 GROUP BY a, b GROUPING SETS ((a, b), (a), (b), (c), ())";
2785+
match all_dialects().one_statement_parses_to(sql, canonical) {
2786+
Statement::Query(query) => {
2787+
let group_by = &query.body.as_select().unwrap().group_by;
2788+
assert_eq!(
2789+
group_by,
2790+
&GroupByExpr::Expressions(
2791+
vec![
2792+
Expr::Identifier(Ident::new("a")),
2793+
Expr::Identifier(Ident::new("b"))
2794+
],
2795+
vec![GroupByWithModifier::GroupingSets(Expr::GroupingSets(vec![
2796+
vec![
2797+
Expr::Identifier(Ident::new("a")),
2798+
Expr::Identifier(Ident::new("b"))
2799+
],
2800+
vec![Expr::Identifier(Ident::new("a"))],
2801+
vec![Expr::Identifier(Ident::new("b"))],
2802+
vec![Expr::Identifier(Ident::new("c"))],
2803+
vec![]
2804+
]))]
2805+
)
2806+
);
2807+
}
2808+
_ => unreachable!(),
2809+
}
2810+
}
2811+
27802812
#[test]
27812813
fn parse_select_having() {
27822814
let sql = "SELECT foo FROM bar GROUP BY foo HAVING COUNT(*) > 1";

0 commit comments

Comments
 (0)