Skip to content

Commit ce3dce3

Browse files
feat: allow GROUP BY ALL and DISTINCT for PostgreSQL (#2842)
* feat: allow GROUP BY ALL and DISTINCT modifiers for PostgreSQL Resolves #2785 * style: rename new test to camelCase for Codacy
1 parent bfafeaa commit ce3dce3

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
@@ -11316,6 +11316,11 @@ GroupByElement GroupByColumnReferences():
1131611316
}
1131711317
{
1131811318
<K_GROUP> <K_BY>
11319+
// PostgreSQL: GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
11320+
[ LOOKAHEAD({ Dialect.POSTGRESQL.name().equals(getAsString(Feature.dialect))
11321+
&& (getToken(1).kind == K_ALL || getToken(1).kind == K_DISTINCT) })
11322+
( <K_ALL> { groupBy.setAll(true); } | <K_DISTINCT> { groupBy.setDistinct(true); } )
11323+
]
1131911324
(
1132011325
LOOKAHEAD(2) (
1132111326
<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
@@ -2413,6 +2413,20 @@ public void testSelectWithoutTargetListPostgreSql() throws JSQLParserException {
24132413
false, parser -> parser.withDialect(Dialect.POSTGRESQL));
24142414
}
24152415

2416+
@Test
2417+
public void testGroupByAllDistinctPostgreSql() throws JSQLParserException {
2418+
// PostgreSQL: GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
2419+
assertSqlCanBeParsedAndDeparsed(
2420+
"SELECT 1 GROUP BY DISTINCT GROUPING SETS ((1), (1))",
2421+
false, parser -> parser.withDialect(Dialect.POSTGRESQL));
2422+
assertSqlCanBeParsedAndDeparsed(
2423+
"SELECT 1 GROUP BY ALL GROUPING SETS ((1), (2))",
2424+
false, parser -> parser.withDialect(Dialect.POSTGRESQL));
2425+
assertSqlCanBeParsedAndDeparsed(
2426+
"SELECT a FROM t GROUP BY DISTINCT a",
2427+
false, parser -> parser.withDialect(Dialect.POSTGRESQL));
2428+
}
2429+
24162430
@Test
24172431
public void testIsUnknown() throws JSQLParserException {
24182432
String statement = "SELECT col FROM tbl WHERE col IS UNKNOWN";

0 commit comments

Comments
 (0)