Skip to content

Commit 86ff2d5

Browse files
committed
BREAKING! Support multiple ROLLUP()-s in GROUP BY
Previosly in BigQuery only one was allowed, but PostgreSQL allows many.
1 parent e52fc2d commit 86ff2d5

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/cst/Select.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export interface GroupByClause extends BaseNode {
265265
type: "group_by_clause";
266266
groupByKw: [Keyword<"GROUP">, Keyword<"BY">];
267267
distinctKw?: Keyword<"ALL" | "DISTINCT">; // PostgreSQL
268-
columns: ListExpr<Expr> | GroupByRollup;
268+
columns: ListExpr<Expr | GroupByRollup>;
269269
withRollupKw?: [Keyword<"WITH">, Keyword<"ROLLUP">]; // MySQL, MariaDB
270270
}
271271

src/parser.pegjs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ where_clause
785785
group_by_clause
786786
= kws:(GROUP __ BY __)
787787
distinctKw:(group_by_distinct __)?
788-
list:(group_by_rollup / list$expr)
788+
list:list$grouping_element
789789
rolKw:(__ WITH __ ROLLUP)? {
790790
return loc({
791791
type: "group_by_clause",
@@ -799,8 +799,12 @@ group_by_clause
799799
group_by_distinct
800800
= kw:(ALL / DISTINCT) &postgres { return kw; }
801801

802+
grouping_element
803+
= group_by_rollup
804+
/ expr
805+
802806
group_by_rollup
803-
= &bigquery kw:(ROLLUP __) cols:paren$list$expr {
807+
= kw:(ROLLUP __) cols:paren$list$expr (&bigquery / &postgres) {
804808
return loc({
805809
type: "group_by_rollup",
806810
rollupKw: read(kw),
@@ -4984,6 +4988,7 @@ list$expr = .
49844988
list$expr_or_default = .
49854989
list$expr_or_explicit_alias = .
49864990
list$func_param = .
4991+
list$grouping_element = .
49874992
list$ident = .
49884993
list$literal = .
49894994
list$named_window = .

test/select/group_by.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ describe("select GROUP BY", () => {
1919
testClauseWc("GROUP BY ROLLUP ( id, name + age )");
2020
testClauseWc("GROUP BY ROLLUP ( id, (name, age) )");
2121
});
22+
23+
dialect("postgresql", () => {
24+
it("supports multiple ROLLUPs in one GROUP BY", () => {
25+
testClauseWc("GROUP BY ROLLUP(id), ROLLUP(name)");
26+
});
27+
28+
it("parses multiple ROLLUPs as group_by_rollup nodes", () => {
29+
const clause = parseClause("GROUP BY ROLLUP(id), ROLLUP(name)");
30+
if (clause.type !== "group_by_clause") {
31+
throw new Error("Expected group_by_clause");
32+
}
33+
expect(clause.columns.items[0].type).toBe("group_by_rollup");
34+
expect(clause.columns.items[1].type).toBe("group_by_rollup");
35+
});
36+
});
2237
});
2338

2439
dialect(["mysql", "mariadb"], () => {

0 commit comments

Comments
 (0)