Skip to content

Commit b27abf0

Browse files
authored
Allow to use () as the GROUP BY nothing (#1347)
1 parent 48ea564 commit b27abf0

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
@@ -1487,6 +1487,11 @@ impl<'a> Parser<'a> {
14871487
let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
14881488
self.expect_token(&Token::RParen)?;
14891489
Ok(Expr::Rollup(result))
1490+
} else if self.consume_tokens(&[Token::LParen, Token::RParen]) {
1491+
// PostgreSQL allow to use empty tuple as a group by expression,
1492+
// e.g. `GROUP BY (), name`. Please refer to GROUP BY Clause section in
1493+
// [PostgreSQL](https://www.postgresql.org/docs/16/sql-select.html)
1494+
Ok(Expr::Tuple(vec![]))
14901495
} else {
14911496
self.parse_expr()
14921497
}

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]
@@ -10278,3 +10279,30 @@ fn parse_auto_increment_too_large() {
1027810279

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

0 commit comments

Comments
 (0)