Skip to content

Commit a469aa4

Browse files
committed
feat: allow GROUP BY ALL and DISTINCT modifiers for PostgreSQL
Resolves #2785
1 parent f6ac257 commit a469aa4

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

‎src/main/java/net/sf/jsqlparser/statement/select/GroupByElement.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public enum SortDirection {
3333
private List<ExpressionList<Expression>> groupingSets = new ArrayList<>();
3434
// postgres rollup is an ExpressionList
3535
private boolean mysqlWithRollup = false;
36+
private boolean all = false;
37+
private boolean distinct = false;
3638

3739
public boolean isUsingBrackets() {
3840
return groupByExpressions.isUsingBrackets();
@@ -115,6 +117,12 @@ public String toString() {
115117
public void appendTo(StringBuilder builder, Consumer<ExpressionList<?>> listRenderer,
116118
Consumer<Expression> expressionRenderer) {
117119
builder.append("GROUP BY ");
120+
if (all) {
121+
builder.append("ALL ");
122+
}
123+
if (distinct) {
124+
builder.append("DISTINCT ");
125+
}
118126
if (groupByExpressions != null) {
119127
if (hasGroupBySortDirections()) {
120128
appendOrderedExpressions(builder, expressionRenderer);
@@ -200,4 +208,22 @@ public GroupByElement setMysqlWithRollup(boolean mysqlWithRollup) {
200208
this.mysqlWithRollup = mysqlWithRollup;
201209
return this;
202210
}
211+
212+
public boolean isAll() {
213+
return all;
214+
}
215+
216+
public GroupByElement setAll(boolean all) {
217+
this.all = all;
218+
return this;
219+
}
220+
221+
public boolean isDistinct() {
222+
return distinct;
223+
}
224+
225+
public GroupByElement setDistinct(boolean distinct) {
226+
this.distinct = distinct;
227+
return this;
228+
}
203229
}

‎src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10528,6 +10528,11 @@ GroupByElement GroupByColumnReferences():
1052810528
}
1052910529
{
1053010530
<K_GROUP> <K_BY>
10531+
// PostgreSQL: GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
10532+
[ LOOKAHEAD({ Dialect.POSTGRESQL.name().equals(getAsString(Feature.dialect))
10533+
&& (getToken(1).kind == K_ALL || getToken(1).kind == K_DISTINCT) })
10534+
( <K_ALL> { groupBy.setAll(true); } | <K_DISTINCT> { groupBy.setDistinct(true); } )
10535+
]
1053110536
(
1053210537
LOOKAHEAD(2) (
1053310538
<K_GROUPING> <K_SETS>

‎src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,6 +2388,20 @@ public void testIsNotFalse() throws JSQLParserException {
23882388
assertSqlCanBeParsedAndDeparsed(statement);
23892389
}
23902390

2391+
@Test
2392+
public void testGroupByAllDistinct_PostgreSql() throws JSQLParserException {
2393+
// PostgreSQL: GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
2394+
assertSqlCanBeParsedAndDeparsed(
2395+
"SELECT 1 GROUP BY DISTINCT GROUPING SETS ((1), (1))",
2396+
false, parser -> parser.withDialect(Dialect.POSTGRESQL));
2397+
assertSqlCanBeParsedAndDeparsed(
2398+
"SELECT 1 GROUP BY ALL GROUPING SETS ((1), (2))",
2399+
false, parser -> parser.withDialect(Dialect.POSTGRESQL));
2400+
assertSqlCanBeParsedAndDeparsed(
2401+
"SELECT a FROM t GROUP BY DISTINCT a",
2402+
false, parser -> parser.withDialect(Dialect.POSTGRESQL));
2403+
}
2404+
23912405
@Test
23922406
public void testIsUnknown() throws JSQLParserException {
23932407
String statement = "SELECT col FROM tbl WHERE col IS UNKNOWN";

0 commit comments

Comments
 (0)