Skip to content

Commit 6e3ebd0

Browse files
git-hulkayman-sigma
authored andcommitted
Allow to use () as the GROUP BY nothing (apache#1347)
1 parent c3129ee commit 6e3ebd0

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/parser/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,11 @@ impl<'a> Parser<'a> {
14911491
let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
14921492
self.expect_token(&Token::RParen)?;
14931493
Ok(Expr::Rollup(result))
1494+
} else if self.consume_tokens(&[Token::LParen, Token::RParen]) {
1495+
// PostgreSQL allow to use empty tuple as a group by expression,
1496+
// e.g. `GROUP BY (), name`. Please refer to GROUP BY Clause section in
1497+
// [PostgreSQL](https://www.postgresql.org/docs/16/sql-select.html)
1498+
Ok(Expr::Tuple(vec![]))
14941499
} else {
14951500
self.parse_expr()
14961501
}

tests/sqlparser_common.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ mod test_utils;
4242

4343
#[cfg(test)]
4444
use pretty_assertions::assert_eq;
45+
use sqlparser::ast::Expr::Identifier;
4546
use sqlparser::test_utils::all_dialects_except;
4647

4748
#[test]
@@ -10280,3 +10281,30 @@ fn parse_auto_increment_too_large() {
1028010281

1028110282
assert!(res.is_err(), "{res:?}");
1028210283
}
10284+
10285+
#[test]
10286+
fn test_group_by_nothing() {
10287+
let Select { group_by, .. } = all_dialects_where(|d| d.supports_group_by_expr())
10288+
.verified_only_select("SELECT count(1) FROM t GROUP BY ()");
10289+
{
10290+
std::assert_eq!(
10291+
GroupByExpr::Expressions(vec![Expr::Tuple(vec![])], vec![]),
10292+
group_by
10293+
);
10294+
}
10295+
10296+
let Select { group_by, .. } = all_dialects_where(|d| d.supports_group_by_expr())
10297+
.verified_only_select("SELECT name, count(1) FROM t GROUP BY name, ()");
10298+
{
10299+
std::assert_eq!(
10300+
GroupByExpr::Expressions(
10301+
vec![
10302+
Identifier(Ident::new("name".to_string())),
10303+
Expr::Tuple(vec![])
10304+
],
10305+
vec![]
10306+
),
10307+
group_by
10308+
);
10309+
}
10310+
}

0 commit comments

Comments
 (0)