mirror of https://github.com/alibaba/druid.git
Compare commits
3 Commits
348a0da30b
...
49e30a8fb9
| Author | SHA1 | Date |
|---|---|---|
|
|
49e30a8fb9 | |
|
|
c0a58c10b7 | |
|
|
f4e7606bd2 |
|
|
@ -34,6 +34,7 @@ public class SQLSelectGroupByClause extends SQLObjectImpl implements SQLReplacea
|
|||
|
||||
private boolean distinct;
|
||||
private boolean paren;
|
||||
private boolean groupingSetsHaveComma;
|
||||
|
||||
public SQLSelectGroupByClause() {
|
||||
}
|
||||
|
|
@ -80,6 +81,14 @@ public class SQLSelectGroupByClause extends SQLObjectImpl implements SQLReplacea
|
|||
this.withCube = withCube;
|
||||
}
|
||||
|
||||
public boolean isGroupingSetsHaveComma() {
|
||||
return groupingSetsHaveComma;
|
||||
}
|
||||
|
||||
public void setGroupingSetsHaveComma(boolean groupingSetsHaveComma) {
|
||||
this.groupingSetsHaveComma = groupingSetsHaveComma;
|
||||
}
|
||||
|
||||
public SQLExpr getHaving() {
|
||||
return this.having;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -104,4 +104,8 @@ public class HologresOutputVisitor extends PGOutputVisitor {
|
|||
endLineComment = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void printArrayExprPrefix() {
|
||||
print0(ucase ? "ARRAY" : "array");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -202,4 +202,8 @@ public class PrestoOutputVisitor extends SQLASTOutputVisitor implements PrestoAS
|
|||
timeZone.accept(this);
|
||||
return false;
|
||||
}
|
||||
|
||||
public void printArrayExprPrefix() {
|
||||
print0(ucase ? "ARRAY" : "array");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -923,6 +923,7 @@ public class SQLSelectParser extends SQLParser {
|
|||
groupBy.setWithCube(true);
|
||||
}
|
||||
|
||||
boolean hasComma = false;
|
||||
for (; ; ) {
|
||||
List<String> comments = null;
|
||||
if (lexer.hasComment()) {
|
||||
|
|
@ -932,6 +933,9 @@ public class SQLSelectParser extends SQLParser {
|
|||
if (comments != null) {
|
||||
item.addBeforeComment(comments);
|
||||
}
|
||||
if (item instanceof SQLGroupingSetExpr && hasComma) {
|
||||
groupBy.setGroupingSetsHaveComma(true);
|
||||
}
|
||||
|
||||
item.setParent(groupBy);
|
||||
groupBy.addItem(item);
|
||||
|
|
@ -947,8 +951,10 @@ public class SQLSelectParser extends SQLParser {
|
|||
&& lexer.line == line + 1) {
|
||||
item.addAfterComment(lexer.readAndResetComments());
|
||||
}
|
||||
hasComma = true;
|
||||
continue;
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.GROUPING)) {
|
||||
hasComma = false;
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -51,13 +51,7 @@ import java.sql.Blob;
|
|||
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.*;
|
||||
|
|
@ -2582,7 +2576,9 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
|||
if (i != 0) {
|
||||
if (groupItemSingleLine) {
|
||||
if (item instanceof SQLGroupingSetExpr) {
|
||||
if (!item.hasBeforeComment()) {
|
||||
if (x.isGroupingSetsHaveComma()) {
|
||||
println(',');
|
||||
} else if (!item.hasBeforeComment()) {
|
||||
println();
|
||||
}
|
||||
} else {
|
||||
|
|
@ -2590,7 +2586,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
|||
}
|
||||
} else {
|
||||
if (item instanceof SQLGroupingSetExpr) {
|
||||
println();
|
||||
if (x.isGroupingSetsHaveComma()) {
|
||||
println(',');
|
||||
} else {
|
||||
println();
|
||||
}
|
||||
} else {
|
||||
print(", ");
|
||||
}
|
||||
|
|
@ -7650,6 +7650,8 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
|||
return false;
|
||||
}
|
||||
|
||||
public void printArrayExprPrefix() {
|
||||
}
|
||||
@Override
|
||||
public boolean visit(SQLArrayExpr x) {
|
||||
SQLExpr expr = x.getExpr();
|
||||
|
|
@ -7661,10 +7663,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
|||
} else if (expr != null) {
|
||||
expr.accept(this);
|
||||
} else {
|
||||
boolean trino = dbType == DbType.trino || dbType == DbType.presto || dbType == DbType.supersql;
|
||||
if (trino) {
|
||||
print0(ucase ? "ARRAY" : "array");
|
||||
}
|
||||
printArrayExprPrefix();
|
||||
}
|
||||
|
||||
if (x.getDataType() != null) {
|
||||
|
|
|
|||
|
|
@ -17,4 +17,26 @@ public class GroupingSetsTest extends TestCase {
|
|||
+ "\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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,7 @@
|
|||
select []
|
||||
--------------------
|
||||
SELECT ARRAY[]
|
||||
------------------------------------------------------------------------------------------------------------------------
|
||||
select cast(($0 + interval $1 week) as date)
|
||||
--------------------
|
||||
SELECT CAST(($0 + INTERVAL '$1' WEEK) AS date)
|
||||
|
|
|
|||
Loading…
Reference in New Issue