Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support new grouping feature #270

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
support new grouping feature
Dairyo Sekine committed Dec 29, 2023
commit aac754bc1ad58d830d04db7b68df3aa63485797c
6 changes: 5 additions & 1 deletion expected_output/select.sql
Original file line number Diff line number Diff line change
@@ -328,6 +328,8 @@ WHERE
;

----- GROUP BY clause -----
SELECT COUNT(*) FROM t GROUP BY ();

SELECT
str,
int,
@@ -346,7 +348,9 @@ GROUP BY
int
;

SELECT x, SUM(y) FROM t GROUP BY ROLLUP(x);
SELECT x, SUM(y) FROM t GROUP BY ROLLUP (x);

SELECT x, SUM(y) FROM t GROUP BY GROUPING SETS (a, CUBE(b), ());

----- HAVING clause -----
SELECT str, COUNT(*) AS cnt FROM t GROUP BY 1 HAVING cnt < 10;
6 changes: 5 additions & 1 deletion input/select.sql
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ with recursive temp as (
union all
select n + 1 from temp where n < 3
)
select n from temp
select n from temp
;

----- SELECT clause -----
@@ -288,6 +288,8 @@ where
;

----- GROUP BY clause -----
select count(*) from t group by ();

select str, int
-- break
from t group by 1, 2
@@ -300,6 +302,8 @@ from t group by str, int

select x, sum(y) from t group by rollup(x);

select x, sum(y) from t group by grouping sets (a, cube(b), ());

----- HAVING clause -----
select str, count(*) cnt from t group by 1 having cnt < 10;

14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@
"typescript": "^4.3.2"
},
"dependencies": {
"bq2cst": "0.4.29"
"bq2cst": "0.4.30"
},
"peerDependencies": {
"prettier": "^2.3.0"
65 changes: 65 additions & 0 deletions src/printer.ts
Original file line number Diff line number Diff line change
@@ -598,6 +598,8 @@ export const printSQL = (
return printDropStatement(path, options, print, node);
case "ElseIfClause":
return printElseIfClause(path, options, print, node);
case "EmptyStruct":
return printEmptyStruct(path, options, print, node);
case "EOF":
return printEOF(path, options, print, node);
case "ExecuteStatement":
@@ -612,6 +614,8 @@ export const printSQL = (
return printForSystemTimeAsOfclause(path, options, print, node);
case "GrantStatement":
return printGrantStatement(path, options, print, node);
case "GroupByExprs":
return printGroupByExprs(path, options, print, node);
case "GroupedExpr":
return printGroupedExpr(path, options, print, node);
case "GroupedExprs":
@@ -2660,6 +2664,35 @@ const printElseIfClause: PrintFunc<bq2cst.ElseIfClause> = (
];
};

const printEmptyStruct: PrintFunc<bq2cst.EmptyStruct> = (
path,
options,
print,
node
) => {
const p = new Printer(path, options, print, node);
const docs: { [Key in Docs<bq2cst.EmptyStruct>]: Doc } = {
leading_comments: printLeadingComments(path, options, print, node),
self: p.self(),
trailing_comments: printTrailingComments(path, options, print, node),
rparen: p.child("rparen"),
as: "", // eslint-disable-line unicorn/no-unused-properties
alias: printAlias(path, options, print, node),
order: printOrder(path, options, print, node),
null_order: "", // eslint-disable-line unicorn/no-unused-properties
comma: printComma(path, options, print, node),
};
return [
group([
docs.leading_comments,
group([docs.self, docs.trailing_comments, docs.rparen]),
]),
docs.alias,
docs.order,
docs.comma,
];
};

const printEOF: PrintFunc<bq2cst.EOF> = (path, options, print, node) => {
const docs: { [Key in Docs<bq2cst.EOF>]: Doc } = {
leading_comments: printLeadingComments(path, options, print, node),
@@ -2892,6 +2925,38 @@ const printGrantStatement: PrintFunc<bq2cst.GrantStatement> = (
];
};

const printGroupByExprs: PrintFunc<bq2cst.GroupByExprs> = (
path,
options,
print,
node
) => {
const p = new Printer(path, options, print, node);
p.setGroupRecommended("exprs");
const docs: { [Key in Docs<bq2cst.GroupByExprs>]: Doc } = {
leading_comments: printLeadingComments(path, options, print, node),
self: p.self("upper"),
trailing_comments: printTrailingComments(path, options, print, node),
by: p.child("by", undefined, "all"),
how: p.child("how", undefined, "all", " "),
exprs: indent(p.child("exprs", (x) => [line, x])),
};
return [
docs.leading_comments,
docs.self,
docs.trailing_comments,
" ",
docs.by,
p.has("how") ? " " : "",
docs.how,
node.children.exprs.NodeVec.every((x) =>
x.token.literal.match(/^[0-9]+$/)
) || p.len("exprs") === 1
? group(docs.exprs)
: docs.exprs,
];
};

const printGroupedExpr: PrintFunc<bq2cst.GroupedExpr> = (
path,
options,