Skip to content

Commit 52a73cd

Browse files
committed
feat: allow GROUP BY ALL and DISTINCT modifiers for PostgreSQL
Resolves #2785
1 parent 6c5fc6c commit 52a73cd

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
@@ -11031,6 +11031,11 @@ GroupByElement GroupByColumnReferences():
1103111031
}
1103211032
{
1103311033
<K_GROUP> <K_BY>
11034+
// PostgreSQL: GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
11035+
[ LOOKAHEAD({ Dialect.POSTGRESQL.name().equals(getAsString(Feature.dialect))
11036+
&& (getToken(1).kind == K_ALL || getToken(1).kind == K_DISTINCT) })
11037+
( <K_ALL> { groupBy.setAll(true); } | <K_DISTINCT> { groupBy.setDistinct(true); } )
11038+
]
1103411039
(
1103511040
LOOKAHEAD(2) (
1103611041
<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
@@ -2398,6 +2398,20 @@ public void testBooleanTestAfterPredicate() throws JSQLParserException {
23982398
assertSqlCanBeParsedAndDeparsed("SELECT * FROM t WHERE a IS TRUE IS FALSE");
23992399
}
24002400

2401+
@Test
2402+
public void testGroupByAllDistinct_PostgreSql() throws JSQLParserException {
2403+
// PostgreSQL: GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
2404+
assertSqlCanBeParsedAndDeparsed(
2405+
"SELECT 1 GROUP BY DISTINCT GROUPING SETS ((1), (1))",
2406+
false, parser -> parser.withDialect(Dialect.POSTGRESQL));
2407+
assertSqlCanBeParsedAndDeparsed(
2408+
"SELECT 1 GROUP BY ALL GROUPING SETS ((1), (2))",
2409+
false, parser -> parser.withDialect(Dialect.POSTGRESQL));
2410+
assertSqlCanBeParsedAndDeparsed(
2411+
"SELECT a FROM t GROUP BY DISTINCT a",
2412+
false, parser -> parser.withDialect(Dialect.POSTGRESQL));
2413+
}
2414+
24012415
@Test
24022416
public void testIsUnknown() throws JSQLParserException {
24032417
String statement = "SELECT col FROM tbl WHERE col IS UNKNOWN";

0 commit comments

Comments
 (0)