Skip to content

Commit

Permalink
Implement GROUP BY () without columns
Browse files Browse the repository at this point in the history
  • Loading branch information
manticore-projects committed May 28, 2021
1 parent f83ed0a commit 0c2e522
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ public class GroupByElement {

private List<Expression> groupByExpressions = new ArrayList<>();
private List groupingSets = new ArrayList();
private boolean usingBrackets = false;

public boolean isUsingBrackets() {
return usingBrackets;
}

public void setUsingBrackets(boolean usingBrackets) {
this.usingBrackets = usingBrackets;
}

public GroupByElement withUsingBrackets(boolean usingBrackets) {
this.usingBrackets = usingBrackets;
return this;
}

public void accept(GroupByVisitor groupByVisitor) {
groupByVisitor.visit(this);
Expand Down Expand Up @@ -55,12 +69,19 @@ public void addGroupingSet(ExpressionList list) {
}

@Override
@SuppressWarnings({"PMD.CyclomaticComplexity"})
public String toString() {
StringBuilder b = new StringBuilder();
b.append("GROUP BY ");

if (groupByExpressions.size() > 0) {
if (usingBrackets) {
b.append("( ");
}
b.append(PlainSelect.getStringList(groupByExpressions));
if (usingBrackets) {
b.append(" )");
}
} else if (groupingSets.size() > 0) {
b.append("GROUPING SETS (");
boolean first = true;
Expand All @@ -78,6 +99,10 @@ public String toString() {
}
}
b.append(")");
} else {
if (usingBrackets) {
b.append("()");
}
}

return b.toString();
Expand All @@ -94,13 +119,15 @@ public GroupByElement withGroupingSets(List groupingSets) {
}

public GroupByElement addGroupByExpressions(Expression... groupByExpressions) {
List<Expression> collection = Optional.ofNullable(getGroupByExpressions()).orElseGet(ArrayList::new);
List<Expression> collection
= Optional.ofNullable(getGroupByExpressions()).orElseGet(ArrayList::new);
Collections.addAll(collection, groupByExpressions);
return this.withGroupByExpressions(collection);
}

public GroupByElement addGroupByExpressions(Collection<? extends Expression> groupByExpressions) {
List<Expression> collection = Optional.ofNullable(getGroupByExpressions()).orElseGet(ArrayList::new);
List<Expression> collection
= Optional.ofNullable(getGroupByExpressions()).orElseGet(ArrayList::new);
collection.addAll(groupByExpressions);
return this.withGroupByExpressions(collection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,21 @@ public GroupByDeParser(ExpressionVisitor expressionVisitor, StringBuilder buffer
}

@Override
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
public void deParse(GroupByElement groupBy) {
buffer.append("GROUP BY ");
if (groupBy.isUsingBrackets()) {
buffer.append("( ");
}
for (Iterator<Expression> iter = groupBy.getGroupByExpressions().iterator(); iter.hasNext();) {
iter.next().accept(expressionVisitor);
if (iter.hasNext()) {
buffer.append(", ");
}
}
if (groupBy.isUsingBrackets()) {
buffer.append(" )");
}
if (!groupBy.getGroupingSets().isEmpty()) {
buffer.append("GROUPING SETS (");
boolean first = true;
Expand Down
10 changes: 8 additions & 2 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -2326,9 +2326,15 @@ GroupByElement GroupByColumnReferences():
}
{
<K_GROUP> <K_BY>
(
columnReference=SimpleExpression() {groupBy.addGroupByExpression(columnReference); }
( LOOKAHEAD(3) (
("(") ( columnReference=SimpleExpression() {groupBy.addGroupByExpression(columnReference); }
("," columnReference=SimpleExpression() {groupBy.addGroupByExpression(columnReference); } )*
)* (")") { groupBy.setUsingBrackets(true); }
)
|
( columnReference=SimpleExpression() {groupBy.addGroupByExpression(columnReference); }
("," columnReference=SimpleExpression() {groupBy.addGroupByExpression(columnReference); } )*
)
|
<K_GROUPING> <K_SETS> "("
( LOOKAHEAD(2) "(" ")" { groupBy.addGroupingSet(new ExpressionList()); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4571,4 +4571,13 @@ public void testWithValueListWithOutExtraBrackets1135() throws JSQLParserExcepti
public void testKeywordSynonymIssue1211() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("select businessDate as \"bd\", synonym as \"synonym\" from sc.tab", true);
}

@Test
public void testGroupedByIssue1176() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"select id_instrument, count(*)\n" + "from cfe.instrument\n" + "group by (id_instrument)",
true);
assertSqlCanBeParsedAndDeparsed("select count(*)\n" + "from cfe.instrument\n" + "group by ()",
true);
}
}

0 comments on commit 0c2e522

Please sign in to comment.