Compare commits

...

3 Commits

Author SHA1 Message Date
qian0817 49e30a8fb9
Merge f4e7606bd2 into c0a58c10b7 2025-07-28 14:30:56 +08:00
林枸 c0a58c10b7 Fix holo array output.
Java CI / Test JDK ${{ matrix.java }}, ${{ matrix.os }} (11, ubuntu-latest) (push) Has been cancelled Details
Java CI / Test JDK ${{ matrix.java }}, ${{ matrix.os }} (17, ubuntu-latest) (push) Has been cancelled Details
Java CI / Test JDK ${{ matrix.java }}, ${{ matrix.os }} (21, ubuntu-latest) (push) Has been cancelled Details
Java CI / Test JDK ${{ matrix.java }}, ${{ matrix.os }} (8, ubuntu-latest) (push) Has been cancelled Details
2025-07-28 14:22:24 +08:00
qian0817 f4e7606bd2 feature: support has comma before grouping sets. 2025-04-15 16:08:37 +08:00
7 changed files with 61 additions and 13 deletions

View File

@ -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;
}

View File

@ -104,4 +104,8 @@ public class HologresOutputVisitor extends PGOutputVisitor {
endLineComment = true;
}
}
public void printArrayExprPrefix() {
print0(ucase ? "ARRAY" : "array");
}
}

View File

@ -202,4 +202,8 @@ public class PrestoOutputVisitor extends SQLASTOutputVisitor implements PrestoAS
timeZone.accept(this);
return false;
}
public void printArrayExprPrefix() {
print0(ucase ? "ARRAY" : "array");
}
}

View File

@ -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;

View File

@ -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) {

View File

@ -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);
}
}

View File

@ -1,3 +1,7 @@
select []
--------------------
SELECT ARRAY[]
------------------------------------------------------------------------------------------------------------------------
select cast(($0 + interval $1 week) as date)
--------------------
SELECT CAST(($0 + INTERVAL '$1' WEEK) AS date)