Skip to content

Commit

Permalink
Allow to use () as the GROUP BY nothing (#1347)
Browse files Browse the repository at this point in the history
  • Loading branch information
git-hulk authored Jul 22, 2024
1 parent 48ea564 commit b27abf0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,11 @@ impl<'a> Parser<'a> {
let result = self.parse_comma_separated(|p| p.parse_tuple(true, true))?;
self.expect_token(&Token::RParen)?;
Ok(Expr::Rollup(result))
} else if self.consume_tokens(&[Token::LParen, Token::RParen]) {
// PostgreSQL allow to use empty tuple as a group by expression,
// e.g. `GROUP BY (), name`. Please refer to GROUP BY Clause section in
// [PostgreSQL](https://www.postgresql.org/docs/16/sql-select.html)
Ok(Expr::Tuple(vec![]))
} else {
self.parse_expr()
}
Expand Down
28 changes: 28 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ mod test_utils;

#[cfg(test)]
use pretty_assertions::assert_eq;
use sqlparser::ast::Expr::Identifier;
use sqlparser::test_utils::all_dialects_except;

#[test]
Expand Down Expand Up @@ -10278,3 +10279,30 @@ fn parse_auto_increment_too_large() {

assert!(res.is_err(), "{res:?}");
}

#[test]
fn test_group_by_nothing() {
let Select { group_by, .. } = all_dialects_where(|d| d.supports_group_by_expr())
.verified_only_select("SELECT count(1) FROM t GROUP BY ()");
{
std::assert_eq!(
GroupByExpr::Expressions(vec![Expr::Tuple(vec![])], vec![]),
group_by
);
}

let Select { group_by, .. } = all_dialects_where(|d| d.supports_group_by_expr())
.verified_only_select("SELECT name, count(1) FROM t GROUP BY name, ()");
{
std::assert_eq!(
GroupByExpr::Expressions(
vec![
Identifier(Ident::new("name".to_string())),
Expr::Tuple(vec![])
],
vec![]
),
group_by
);
}
}

0 comments on commit b27abf0

Please sign in to comment.