Skip to content

Commit 394a534

Browse files
authored
Fix: GROUPING SETS accept values without parenthesis (#1867)
1 parent 80d47ee commit 394a534

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
@@ -10111,7 +10111,13 @@ impl<'a> Parser<'a> {
1011110111
}
1011210112
if self.parse_keywords(&[Keyword::GROUPING, Keyword::SETS]) {
1011310113
self.expect_token(&Token::LParen)?;
10114-
let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
10114+
let result = self.parse_comma_separated(|p| {
10115+
if p.peek_token_ref().token == Token::LParen {
10116+
p.parse_tuple(true, true)
10117+
} else {
10118+
Ok(vec![p.parse_expr()?])
10119+
}
10120+
})?;
1011510121
self.expect_token(&Token::RParen)?;
1011610122
modifiers.push(GroupByWithModifier::GroupingSets(Expr::GroupingSets(
1011710123
result,

tests/sqlparser_common.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2822,6 +2822,38 @@ fn parse_group_by_special_grouping_sets() {
28222822
}
28232823
}
28242824

2825+
#[test]
2826+
fn parse_group_by_grouping_sets_single_values() {
2827+
let sql = "SELECT a, b, SUM(c) FROM tab1 GROUP BY a, b GROUPING SETS ((a, b), a, (b), c, ())";
2828+
let canonical =
2829+
"SELECT a, b, SUM(c) FROM tab1 GROUP BY a, b GROUPING SETS ((a, b), (a), (b), (c), ())";
2830+
match all_dialects().one_statement_parses_to(sql, canonical) {
2831+
Statement::Query(query) => {
2832+
let group_by = &query.body.as_select().unwrap().group_by;
2833+
assert_eq!(
2834+
group_by,
2835+
&GroupByExpr::Expressions(
2836+
vec![
2837+
Expr::Identifier(Ident::new("a")),
2838+
Expr::Identifier(Ident::new("b"))
2839+
],
2840+
vec![GroupByWithModifier::GroupingSets(Expr::GroupingSets(vec![
2841+
vec![
2842+
Expr::Identifier(Ident::new("a")),
2843+
Expr::Identifier(Ident::new("b"))
2844+
],
2845+
vec![Expr::Identifier(Ident::new("a"))],
2846+
vec![Expr::Identifier(Ident::new("b"))],
2847+
vec![Expr::Identifier(Ident::new("c"))],
2848+
vec![]
2849+
]))]
2850+
)
2851+
);
2852+
}
2853+
_ => unreachable!(),
2854+
}
2855+
}
2856+
28252857
#[test]
28262858
fn parse_select_having() {
28272859
let sql = "SELECT foo FROM bar GROUP BY foo HAVING COUNT(*) > 1";

0 commit comments

Comments
 (0)