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

Add support for Hive dialect GROUPING SETS. #1539

Merged
merged 3 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ public String toString() {
if (groupByExpressions.isUsingBrackets()) {
b.append(" )");
}
} else if (groupingSets.size() > 0) {
} else if (groupByExpressions.isUsingBrackets()) {
b.append("()");
}

if (groupingSets.size() > 0) {
if (b.charAt(b.length() - 1) != ' ') {
b.append(' ');
}
b.append("GROUPING SETS (");
boolean first = true;
for (Object o : groupingSets) {
Expand All @@ -112,10 +119,6 @@ public String toString() {
}
}
b.append(")");
} else {
if (groupByExpressions.isUsingBrackets()) {
b.append("()");
}
}

return b.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public void deParse(GroupByElement groupBy) {
buffer.append(" )");
}
if (!groupBy.getGroupingSets().isEmpty()) {
if (buffer.charAt(buffer.length() - 1) != ' ') {
buffer.append(' ');
}
buffer.append("GROUPING SETS (");
boolean first = true;
for (Object o : groupBy.getGroupingSets()) {
Expand Down
26 changes: 26 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -2734,6 +2734,19 @@ GroupByElement GroupByColumnReferences():
<K_GROUP> <K_BY>
( LOOKAHEAD(2) (
"(" ")" { groupBy.withUsingBrackets(true); }
(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you more compress this production? There are three nearly identical copies of this inserted block.

LOOKAHEAD(2) (
<K_GROUPING> <K_SETS> "("
( LOOKAHEAD(2) "(" ")" { groupBy.addGroupingSet(new ExpressionList()); }
| LOOKAHEAD(3) "(" list = SimpleExpressionList(true) ")" { groupBy.addGroupingSet(list); }
| expr = SimpleExpression() { groupBy.addGroupingSet(expr); } )

( "," ( LOOKAHEAD(2) "(" ")" { groupBy.addGroupingSet(new ExpressionList()); }
| LOOKAHEAD(3) "(" list = SimpleExpressionList(true) ")" { groupBy.addGroupingSet(list); }
| expr = SimpleExpression() { groupBy.addGroupingSet(expr); } ) )*
")"
)
)?
)
|
LOOKAHEAD(2) (
Expand All @@ -2750,6 +2763,19 @@ GroupByElement GroupByColumnReferences():
|
LOOKAHEAD(2) (
list = ComplexExpressionList() { groupBy.setGroupByExpressionList(list.withUsingBrackets(false)); }
(
LOOKAHEAD(2) (
<K_GROUPING> <K_SETS> "("
( LOOKAHEAD(2) "(" ")" { groupBy.addGroupingSet(new ExpressionList()); }
| LOOKAHEAD(3) "(" list = SimpleExpressionList(true) ")" { groupBy.addGroupingSet(list); }
| expr = SimpleExpression() { groupBy.addGroupingSet(expr); } )

( "," ( LOOKAHEAD(2) "(" ")" { groupBy.addGroupingSet(new ExpressionList()); }
| LOOKAHEAD(3) "(" list = SimpleExpressionList(true) ")" { groupBy.addGroupingSet(list); }
| expr = SimpleExpression() { groupBy.addGroupingSet(expr); } ) )*
")"
)
)?
)
)
{
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/select/HiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,14 @@ public void testLeftSemiJoin() throws Exception {

assertSqlCanBeParsedAndDeparsed(sql, true);
}

@Test
public void testGroupByGroupingSets() throws Exception {
String sql = "SELECT\n"
+ " C1, C2, C3, MAX(Value)\n"
+ "FROM\n"
+ " Sometable\n"
+ "GROUP BY C1, C2, C3 GROUPING SETS ((C1, C2), (C1, C2, C3), ())";
assertSqlCanBeParsedAndDeparsed(sql, true);
}
}