From db46fa33e4dc14e49488695922ce5702300999d7 Mon Sep 17 00:00:00 2001 From: qzchenwl Date: Thu, 19 May 2022 20:50:42 +0800 Subject: [PATCH 1/2] Add support for Hive GROUPING SETS dialect `GROUP BY a, b, c GROUPING SETS ((a, b), (a, c))` --- .../statement/select/GroupByElement.java | 13 ++++++---- .../util/deparser/GroupByDeParser.java | 3 +++ .../net/sf/jsqlparser/parser/JSqlParserCC.jjt | 26 +++++++++++++++++++ .../jsqlparser/statement/select/HiveTest.java | 20 ++++++++++++++ 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/sf/jsqlparser/statement/select/GroupByElement.java b/src/main/java/net/sf/jsqlparser/statement/select/GroupByElement.java index 4cc9c25c9..15c4f17c5 100644 --- a/src/main/java/net/sf/jsqlparser/statement/select/GroupByElement.java +++ b/src/main/java/net/sf/jsqlparser/statement/select/GroupByElement.java @@ -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) { @@ -112,10 +119,6 @@ public String toString() { } } b.append(")"); - } else { - if (groupByExpressions.isUsingBrackets()) { - b.append("()"); - } } return b.toString(); diff --git a/src/main/java/net/sf/jsqlparser/util/deparser/GroupByDeParser.java b/src/main/java/net/sf/jsqlparser/util/deparser/GroupByDeParser.java index d39a1d934..839ae1adb 100644 --- a/src/main/java/net/sf/jsqlparser/util/deparser/GroupByDeParser.java +++ b/src/main/java/net/sf/jsqlparser/util/deparser/GroupByDeParser.java @@ -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()) { diff --git a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt index c2da5b3ab..fa74535e6 100644 --- a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt +++ b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt @@ -2734,6 +2734,19 @@ GroupByElement GroupByColumnReferences(): ( LOOKAHEAD(2) ( "(" ")" { groupBy.withUsingBrackets(true); } + ( + LOOKAHEAD(2) ( + "(" + ( 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) ( @@ -2750,6 +2763,19 @@ GroupByElement GroupByColumnReferences(): | LOOKAHEAD(2) ( list = ComplexExpressionList() { groupBy.setGroupByExpressionList(list.withUsingBrackets(false)); } + ( + LOOKAHEAD(2) ( + "(" + ( 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); } ) )* + ")" + ) + )? ) ) { diff --git a/src/test/java/net/sf/jsqlparser/statement/select/HiveTest.java b/src/test/java/net/sf/jsqlparser/statement/select/HiveTest.java index 742d7c87f..2697629c5 100644 --- a/src/test/java/net/sf/jsqlparser/statement/select/HiveTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/select/HiveTest.java @@ -46,4 +46,24 @@ public void testLeftSemiJoin() throws Exception { assertSqlCanBeParsedAndDeparsed(sql, true); } + + @Test + public void testGroupByGroupingSets() throws Exception { + String sql; + Statement statement; + + 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), ())"; + + statement = CCJSqlParserUtil.parse(sql); + + System.out.println(statement.toString()); + + Select select = (Select) statement; + assertStatementCanBeDeparsedAs(select, sql, true); + assertSqlCanBeParsedAndDeparsed(sql, true); + } } From 6e40662810de2e108ae116a577d19d9820acbdfb Mon Sep 17 00:00:00 2001 From: qzchenwl Date: Thu, 23 Jun 2022 23:36:42 +0800 Subject: [PATCH 2/2] Simplify HiveTest::testGroupByGroupingSets. --- .../net/sf/jsqlparser/statement/select/HiveTest.java | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/test/java/net/sf/jsqlparser/statement/select/HiveTest.java b/src/test/java/net/sf/jsqlparser/statement/select/HiveTest.java index 2697629c5..8abc10494 100644 --- a/src/test/java/net/sf/jsqlparser/statement/select/HiveTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/select/HiveTest.java @@ -49,21 +49,11 @@ public void testLeftSemiJoin() throws Exception { @Test public void testGroupByGroupingSets() throws Exception { - String sql; - Statement statement; - - sql = "SELECT\n" + 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), ())"; - - statement = CCJSqlParserUtil.parse(sql); - - System.out.println(statement.toString()); - - Select select = (Select) statement; - assertStatementCanBeDeparsedAs(select, sql, true); assertSqlCanBeParsedAndDeparsed(sql, true); } }