mirror of https://github.com/alibaba/druid.git
Compare commits
3 Commits
8742bd5bfc
...
f7d658bc24
| Author | SHA1 | Date |
|---|---|---|
|
|
f7d658bc24 | |
|
|
ac484261c0 | |
|
|
f4e7606bd2 |
|
|
@ -34,6 +34,7 @@ public class SQLSelectGroupByClause extends SQLObjectImpl implements SQLReplacea
|
||||||
|
|
||||||
private boolean distinct;
|
private boolean distinct;
|
||||||
private boolean paren;
|
private boolean paren;
|
||||||
|
private boolean groupingSetsHaveComma;
|
||||||
|
|
||||||
public SQLSelectGroupByClause() {
|
public SQLSelectGroupByClause() {
|
||||||
}
|
}
|
||||||
|
|
@ -80,6 +81,14 @@ public class SQLSelectGroupByClause extends SQLObjectImpl implements SQLReplacea
|
||||||
this.withCube = withCube;
|
this.withCube = withCube;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isGroupingSetsHaveComma() {
|
||||||
|
return groupingSetsHaveComma;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGroupingSetsHaveComma(boolean groupingSetsHaveComma) {
|
||||||
|
this.groupingSetsHaveComma = groupingSetsHaveComma;
|
||||||
|
}
|
||||||
|
|
||||||
public SQLExpr getHaving() {
|
public SQLExpr getHaving() {
|
||||||
return this.having;
|
return this.having;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ public class CKLexer extends Lexer {
|
||||||
map.put("FINAL", Token.FINAL);
|
map.put("FINAL", Token.FINAL);
|
||||||
map.put("TTL", Token.TTL);
|
map.put("TTL", Token.TTL);
|
||||||
map.put("CODEC", Token.CODEC);
|
map.put("CODEC", Token.CODEC);
|
||||||
|
map.remove("ANY");
|
||||||
|
|
||||||
return new Keywords(map);
|
return new Keywords(map);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ import com.alibaba.druid.DbType;
|
||||||
import com.alibaba.druid.sql.ast.SQLPartitionBy;
|
import com.alibaba.druid.sql.ast.SQLPartitionBy;
|
||||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGOutputVisitor;
|
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGOutputVisitor;
|
||||||
|
import com.alibaba.druid.sql.parser.CharTypes;
|
||||||
|
import com.alibaba.druid.sql.visitor.VisitorFeature;
|
||||||
|
|
||||||
public class HologresOutputVisitor extends PGOutputVisitor {
|
public class HologresOutputVisitor extends PGOutputVisitor {
|
||||||
public HologresOutputVisitor(StringBuilder appender, boolean parameterized) {
|
public HologresOutputVisitor(StringBuilder appender, boolean parameterized) {
|
||||||
|
|
@ -27,4 +29,53 @@ public class HologresOutputVisitor extends PGOutputVisitor {
|
||||||
print0(ucase ? "PARTITION BY " : "partition by ");
|
print0(ucase ? "PARTITION BY " : "partition by ");
|
||||||
partitionBy.accept(this);
|
partitionBy.accept(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void printComment(String comment) {
|
||||||
|
if (comment == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isEnabled(VisitorFeature.OutputSkipMultilineComment) && comment.startsWith("/*")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isEnabled(VisitorFeature.OutputSkipSingleLineComment)
|
||||||
|
&& (comment.startsWith("-") || comment.startsWith("#"))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (comment.startsWith("--")
|
||||||
|
&& comment.length() > 2
|
||||||
|
&& comment.charAt(2) != ' '
|
||||||
|
&& comment.charAt(2) != '-') {
|
||||||
|
print0("-- ");
|
||||||
|
print0(comment.substring(2));
|
||||||
|
} else if (comment.startsWith("#")
|
||||||
|
&& comment.length() > 1
|
||||||
|
&& comment.charAt(1) != ' '
|
||||||
|
&& comment.charAt(1) != '#') {
|
||||||
|
print0("# ");
|
||||||
|
print0(comment.substring(1));
|
||||||
|
} else if (comment.startsWith("/*")) {
|
||||||
|
println();
|
||||||
|
print0(comment);
|
||||||
|
} else if (comment.startsWith("--")) {
|
||||||
|
print0(comment);
|
||||||
|
}
|
||||||
|
|
||||||
|
char first = '\0';
|
||||||
|
for (int i = 0; i < comment.length(); i++) {
|
||||||
|
char c = comment.charAt(i);
|
||||||
|
if (CharTypes.isWhitespace(c)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
first = c;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (first == '-' || first == '#') {
|
||||||
|
endLineComment = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2527,10 +2527,21 @@ public class Lexer {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ch == '*' && charAt(pos + 1) == '/') {
|
if (ch == '*' && charAt(pos + 1) == '/') {
|
||||||
scanChar();
|
int curPos = pos;
|
||||||
scanChar();
|
boolean terminated = true;
|
||||||
if (0 == --depth) {
|
// If '*/' has leading '--' in the same line, just skip it. For example '-- xxxx */'.
|
||||||
break;
|
while (curPos > 0 && charAt(curPos) != '\n') {
|
||||||
|
if (charAt(curPos) == '-' && (curPos + 1) < text.length() && charAt(curPos + 1) == '-') {
|
||||||
|
terminated = false;
|
||||||
|
}
|
||||||
|
curPos--;
|
||||||
|
}
|
||||||
|
if (terminated) {
|
||||||
|
scanChar();
|
||||||
|
scanChar();
|
||||||
|
if (0 == --depth) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -923,6 +923,7 @@ public class SQLSelectParser extends SQLParser {
|
||||||
groupBy.setWithCube(true);
|
groupBy.setWithCube(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean hasComma = false;
|
||||||
for (; ; ) {
|
for (; ; ) {
|
||||||
List<String> comments = null;
|
List<String> comments = null;
|
||||||
if (lexer.hasComment()) {
|
if (lexer.hasComment()) {
|
||||||
|
|
@ -932,6 +933,9 @@ public class SQLSelectParser extends SQLParser {
|
||||||
if (comments != null) {
|
if (comments != null) {
|
||||||
item.addBeforeComment(comments);
|
item.addBeforeComment(comments);
|
||||||
}
|
}
|
||||||
|
if (item instanceof SQLGroupingSetExpr && hasComma) {
|
||||||
|
groupBy.setGroupingSetsHaveComma(true);
|
||||||
|
}
|
||||||
|
|
||||||
item.setParent(groupBy);
|
item.setParent(groupBy);
|
||||||
groupBy.addItem(item);
|
groupBy.addItem(item);
|
||||||
|
|
@ -947,8 +951,10 @@ public class SQLSelectParser extends SQLParser {
|
||||||
&& lexer.line == line + 1) {
|
&& lexer.line == line + 1) {
|
||||||
item.addAfterComment(lexer.readAndResetComments());
|
item.addAfterComment(lexer.readAndResetComments());
|
||||||
}
|
}
|
||||||
|
hasComma = true;
|
||||||
continue;
|
continue;
|
||||||
} else if (lexer.identifierEquals(FnvHash.Constants.GROUPING)) {
|
} else if (lexer.identifierEquals(FnvHash.Constants.GROUPING)) {
|
||||||
|
hasComma = false;
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -51,13 +51,7 @@ import java.sql.Blob;
|
||||||
import java.sql.Clob;
|
import java.sql.Clob;
|
||||||
import java.sql.NClob;
|
import java.sql.NClob;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.time.Instant;
|
import java.time.*;
|
||||||
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.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.time.temporal.TemporalAmount;
|
import java.time.temporal.TemporalAmount;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
@ -105,7 +99,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||||
|
|
||||||
protected transient int lines;
|
protected transient int lines;
|
||||||
private TimeZone timeZone;
|
private TimeZone timeZone;
|
||||||
private boolean endLineComment;
|
protected boolean endLineComment;
|
||||||
|
|
||||||
protected SQLDialect dialect;
|
protected SQLDialect dialect;
|
||||||
|
|
||||||
|
|
@ -2582,7 +2576,9 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||||
if (i != 0) {
|
if (i != 0) {
|
||||||
if (groupItemSingleLine) {
|
if (groupItemSingleLine) {
|
||||||
if (item instanceof SQLGroupingSetExpr) {
|
if (item instanceof SQLGroupingSetExpr) {
|
||||||
if (!item.hasBeforeComment()) {
|
if (x.isGroupingSetsHaveComma()) {
|
||||||
|
println(',');
|
||||||
|
} else if (!item.hasBeforeComment()) {
|
||||||
println();
|
println();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -2590,7 +2586,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (item instanceof SQLGroupingSetExpr) {
|
if (item instanceof SQLGroupingSetExpr) {
|
||||||
println();
|
if (x.isGroupingSetsHaveComma()) {
|
||||||
|
println(',');
|
||||||
|
} else {
|
||||||
|
println();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
print(", ");
|
print(", ");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,4 +17,26 @@ public class GroupingSetsTest extends TestCase {
|
||||||
+ "\nFROM items_sold"
|
+ "\nFROM items_sold"
|
||||||
+ "\nGROUP BY GROUPING SETS ((brand), (size), ());", result);
|
+ "\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,33 @@
|
||||||
|
select a from test
|
||||||
|
-- origin
|
||||||
|
-- ${if(gFinPeriod="季度","/*","")}
|
||||||
|
-- origin end
|
||||||
|
-- new
|
||||||
|
/*
|
||||||
|
-- new end
|
||||||
|
where 1=1
|
||||||
|
-- origin
|
||||||
|
-- ${if(gFinPeriod="季度","","*/")}
|
||||||
|
-- origin end
|
||||||
|
-- new
|
||||||
|
*/
|
||||||
|
--new end
|
||||||
|
--------------------
|
||||||
|
SELECT a
|
||||||
|
FROM test -- origin
|
||||||
|
-- ${if(gFinPeriod="季度","/*","")}
|
||||||
|
-- origin end
|
||||||
|
-- new
|
||||||
|
/*
|
||||||
|
-- new end
|
||||||
|
where 1=1
|
||||||
|
-- origin
|
||||||
|
-- ${if(gFinPeriod="季度","","*/")}
|
||||||
|
-- origin end
|
||||||
|
-- new
|
||||||
|
*/
|
||||||
|
-- new end
|
||||||
|
------------------------------------------------------------------------------------------------------------------------
|
||||||
SELECT sum(ret_amount) AS '应收金额'
|
SELECT sum(ret_amount) AS '应收金额'
|
||||||
FROM test.test
|
FROM test.test
|
||||||
GROUP BY a ORDER BY '应收金额' DESC;
|
GROUP BY a ORDER BY '应收金额' DESC;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue