Skip to content

Commit a74cc1f

Browse files
committed
Allow to use () as the GROUP BY nothing for PostgreSQL
Currently, PostgreSQL supports using `()` as one of the group by elements which represent the meaning of nothing. Please refer to GROUP BY Clause section in [PostgreSQL](https://www.postgresql.org/docs/16/sql-select.html). This PR will close #1092.
1 parent 20f7ac5 commit a74cc1f

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
@@ -1474,6 +1474,11 @@ impl<'a> Parser<'a> {
14741474
let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
14751475
self.expect_token(&Token::RParen)?;
14761476
Ok(Expr::Rollup(result))
1477+
} else if self.consume_tokens(&[Token::LParen, Token::RParen]) {
1478+
// PostgreSQL allow to use empty tuple as a group by expression,
1479+
// e.g. `GROUP BY (), name`. Please refer to GROUP BY Clause section in
1480+
// [PostgreSQL](https://www.postgresql.org/docs/16/sql-select.html)
1481+
Ok(Expr::Tuple(vec![]))
14771482
} else {
14781483
self.parse_expr()
14791484
}

tests/sqlparser_postgres.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
mod test_utils;
1919
use test_utils::*;
2020

21+
use sqlparser::ast::Expr::Identifier;
2122
use sqlparser::ast::*;
2223
use sqlparser::dialect::{GenericDialect, PostgreSqlDialect};
2324
use sqlparser::parser::ParserError;
@@ -4441,3 +4442,30 @@ fn test_table_unnest_with_ordinality() {
44414442
_ => panic!("Expecting TableFactor::UNNEST with ordinality"),
44424443
}
44434444
}
4445+
4446+
#[test]
4447+
fn test_group_by_nothing() {
4448+
let Select { group_by, .. } =
4449+
pg_and_generic().verified_only_select("SELECT count(1) FROM t GROUP BY ()");
4450+
{
4451+
assert_eq!(
4452+
GroupByExpr::Expressions(vec![Expr::Tuple(vec![])], vec![]),
4453+
group_by
4454+
);
4455+
}
4456+
4457+
let Select { group_by, .. } =
4458+
pg_and_generic().verified_only_select("SELECT name, count(1) FROM t GROUP BY name, ()");
4459+
{
4460+
assert_eq!(
4461+
GroupByExpr::Expressions(
4462+
vec![
4463+
Identifier(Ident::new("name".to_string())),
4464+
Expr::Tuple(vec![])
4465+
],
4466+
vec![]
4467+
),
4468+
group_by
4469+
);
4470+
}
4471+
}

0 commit comments

Comments
 (0)