8000 Support sql has comma before grouping sets by qian0817 · Pull Request #6402 · alibaba/druid · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support sql has comma before grouping sets #6402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -34,6 +34,7 @@ public class SQLSelectGroupByClause extends SQLObjectImpl implements SQLReplacea

private boolean distinct;
private boolean paren;
private boolean groupingSetsHaveComma;

public SQLSelectGroupByClause() {
}
Expand Down Expand Up @@ -80,6 +81,14 @@ public void setWithCube(boolean withCube) {
this.withCube = withCube;
}

public boolean isGroupingSetsHaveComma() {
return groupingSetsHaveComma;
}

public void setGroupingSetsHaveComma(boolean groupingSetsHaveComma) {
this.groupingSetsHaveComma = groupingSetsHaveComma;
}

public SQLExpr getHaving() {
return this.having;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ public void parseGroupBy(SQLSelectQueryBlock queryBlock) {
groupBy.setWithCube(true);
}

boolean hasComma = false;
for (; ; ) {
List<String> comments = null;
if (lexer.hasComment()) {
Expand All @@ -931,6 +932,9 @@ public void parseGroupBy(SQLSelectQueryBlock queryBlock) {
if (comments != null) {
item.addBeforeComment(comments);
}
if (item instanceof SQLGroupingSetExpr && hasComma) {
groupBy.setGroupingSetsHaveComma(true);
}

item.setParent(groupBy);
groupBy.addItem(item);
Expand All @@ -946,8 +950,10 @@ public void parseGroupBy(SQLSelectQueryBlock queryBlock) {
&& lexer.line == line + 1) {
item.addAfterComment(lexer.readAndResetComments());
}
hasComma = true;
continue;
} else if (lexer.identifierEquals(FnvHash.Constants.GROUPING)) {
hasComma = false;
continue;
} else {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,7 @@
import java.sql.Clob;
import java.sql.NClob;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAmount;
import java.util.*;
Expand Down Expand Up @@ -2575,15 +2569,21 @@ public boolean visit(SQLSelectGroupByClause x) {
if (i != 0) {
if (groupItemSingleLine) {
if (item instanceof SQLGroupingSetExpr) {
if (!item.hasBeforeComment()) {
if (x.isGroupingSetsHaveComma()) {
println(',');
} else if (!item.hasBeforeComment()) {
println();
}
} else {
println(',');
}
} else {
if (item instanceof SQLGroupingSetExpr) {
println();
if (x.isGroupingSetsHaveComma()) {
println(',');
} else {
println();
}
} else {
print(", ");
}
Expand Down
22 changes: 22 additions & 0 deletions core/src/test/java/com/alibaba/druid/bvt/sql/GroupingSetsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,26 @@ public void test_groupingSets() throws Exception {
+ "\nFROM items_sold"
+ "\nGROUP BY GROUPING SETS ((brand), (size), ());", result);
}

public void test_groupingSetsHasComma() throws Exception {
String sql = "SELECT brand, size, sum(sales) FROM items_sold GROUP BY brand, size, GROUPING SETS ((brand), (size), ());";

String result = SQLUtils.format(sql, (DbType) null);

Assert.assertEquals("SELECT brand, size, sum(sales)\n" +
"FROM items_sold\n" +
"GROUP BY brand, size,\n" +
"\tGROUPING SETS ((brand), (size), ());", result);
}

public void test_groupingSetsNoComma() throws Exception {
String sql = "SELECT brand, size, sum(sales) FROM items_sold GROUP BY brand, size GROUPING SETS ((brand), (size), ());";

String result = SQLUtils.format(sql, (DbType) null);

Assert.assertEquals("SELECT brand, size, sum(sales)\n" +
"FROM items_sold\n" +
"GROUP BY brand, size\n" +
"\tGROUPING SETS ((brand), (size), ());", result);
}
}
0