Skip to content
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 @@ -33,9 +33,11 @@
private List<ExpressionList<Expression>> groupingSets = new ArrayList<>();
// postgres rollup is an ExpressionList
private boolean mysqlWithRollup = false;
private boolean all = false;
private boolean distinct = false;

public boolean isUsingBrackets() {
return groupByExpressions.isUsingBrackets();

Check warning on line 40 in src/main/java/net/sf/jsqlparser/statement/select/GroupByElement.java

View workflow job for this annotation

GitHub Actions / Maven Verify (macos-latest)

[deprecation] isUsingBrackets() in ExpressionList has been deprecated

Check warning on line 40 in src/main/java/net/sf/jsqlparser/statement/select/GroupByElement.java

View workflow job for this annotation

GitHub Actions / Maven Verify (windows-latest)

[deprecation] isUsingBrackets() in ExpressionList has been deprecated

Check warning on line 40 in src/main/java/net/sf/jsqlparser/statement/select/GroupByElement.java

View workflow job for this annotation

GitHub Actions / Maven Verify (ubuntu-latest)

[deprecation] isUsingBrackets() in ExpressionList has been deprecated
}

public <T, S> T accept(GroupByVisitor<T> groupByVisitor, S context) {
Expand Down Expand Up @@ -115,6 +117,12 @@
public void appendTo(StringBuilder builder, Consumer<ExpressionList<?>> listRenderer,
Consumer<Expression> expressionRenderer) {
builder.append("GROUP BY ");
if (all) {
builder.append("ALL ");
}
if (distinct) {
builder.append("DISTINCT ");
}
if (groupByExpressions != null) {
if (hasGroupBySortDirections()) {
appendOrderedExpressions(builder, expressionRenderer);
Expand Down Expand Up @@ -200,4 +208,22 @@
this.mysqlWithRollup = mysqlWithRollup;
return this;
}

public boolean isAll() {
return all;
}

public GroupByElement setAll(boolean all) {
this.all = all;
return this;
}

public boolean isDistinct() {
return distinct;
}

public GroupByElement setDistinct(boolean distinct) {
this.distinct = distinct;
return this;
}
}
5 changes: 5 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -11316,6 +11316,11 @@ GroupByElement GroupByColumnReferences():
}
{
<K_GROUP> <K_BY>
// PostgreSQL: GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
[ LOOKAHEAD({ Dialect.POSTGRESQL.name().equals(getAsString(Feature.dialect))
&& (getToken(1).kind == K_ALL || getToken(1).kind == K_DISTINCT) })
( <K_ALL> { groupBy.setAll(true); } | <K_DISTINCT> { groupBy.setDistinct(true); } )
]
(
LOOKAHEAD(2) (
<K_GROUPING> <K_SETS>
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2413,6 +2413,20 @@ public void testSelectWithoutTargetListPostgreSql() throws JSQLParserException {
false, parser -> parser.withDialect(Dialect.POSTGRESQL));
}

@Test
public void testGroupByAllDistinctPostgreSql() throws JSQLParserException {
// PostgreSQL: GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
assertSqlCanBeParsedAndDeparsed(
"SELECT 1 GROUP BY DISTINCT GROUPING SETS ((1), (1))",
false, parser -> parser.withDialect(Dialect.POSTGRESQL));
assertSqlCanBeParsedAndDeparsed(
"SELECT 1 GROUP BY ALL GROUPING SETS ((1), (2))",
false, parser -> parser.withDialect(Dialect.POSTGRESQL));
assertSqlCanBeParsedAndDeparsed(
"SELECT a FROM t GROUP BY DISTINCT a",
false, parser -> parser.withDialect(Dialect.POSTGRESQL));
}

@Test
public void testIsUnknown() throws JSQLParserException {
String statement = "SELECT col FROM tbl WHERE col IS UNKNOWN";
Expand Down
Loading