Skip to content

Commit d1703cc

Browse files
committed
fixes CTE parsing issue and using table as alias
1 parent b319fd6 commit d1703cc

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

jdbc-v2/src/main/antlr4/com/clickhouse/jdbc/internal/ClickHouseParser.g4

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ columnAliases
5555
cteUnboundCol
5656
: (literal AS identifier) # CteUnboundColLiteral
5757
| (QUERY AS identifier) # CteUnboundColParam
58-
| LPAREN columnExpr RPAREN AS identifier # CteUnboundColExpr
58+
| LPAREN? columnExpr RPAREN? AS identifier # CteUnboundColExpr
5959
| LPAREN ctes? selectStmt RPAREN AS identifier # CteUnboundNestedSelect
6060
;
6161

@@ -1237,6 +1237,7 @@ keywordForAlias
12371237
| CURRENT
12381238
| INDEX
12391239
| TABLES
1240+
| TABLE
12401241
| TEST
12411242
| VIEW
12421243
| PRIMARY

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/ParsedPreparedStatement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public void setHasErrors(boolean hasErrors) {
136136
public void enterQueryStmt(ClickHouseParser.QueryStmtContext ctx) {
137137
ClickHouseParser.QueryContext qCtx = ctx.query();
138138
if (qCtx != null) {
139-
if (qCtx.selectStmt() != null || qCtx.selectUnionStmt() != null || qCtx.showStmt() != null || qCtx.describeStmt() != null) {
139+
if (qCtx.selectStmt() != null || qCtx.selectUnionStmt() != null || qCtx.showStmt() != null || qCtx.describeStmt() != null || qCtx.ctes() != null) {
140140
setHasResultSet(true);
141141
}
142142
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/SqlParserTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ public void testCTEStatements(String sql, int args) {
237237
ParsedPreparedStatement stmt = parser.parsePreparedStatement(sql);
238238
Assert.assertFalse(stmt.isHasErrors());
239239
Assert.assertEquals(stmt.getArgCount(), args);
240+
Assert.assertTrue(stmt.isHasResultSet());
240241
}
241242

242243
@DataProvider
@@ -248,7 +249,10 @@ public static Object[][] testCTEStmtsDP() {
248249
{"with a as (select 1) select * from a; ", 0},
249250
{"(with ? as a select a);", 1},
250251
{"select * from ( with x as ( select 9 ) select * from x );", 0},
251-
{"WITH toDateTime(?) AS target_time SELECT * FROM table", 1}
252+
{"WITH toDateTime(?) AS target_time SELECT * FROM table", 1},
253+
{"WITH toDateTime('2025-08-20 12:34:56') AS target_time SELECT * FROM table", 0},
254+
{"WITH toDate('2025-08-20') as DATE_END, events AS ( SELECT 1 ) SELECT * FROM events", 0},
255+
{"WITH toDate(?) as DATE_END, events AS ( SELECT 1 ) SELECT * FROM events", 1}
252256
};
253257
}
254258

@@ -326,6 +330,8 @@ public Object[][] testMiscStmtDp() {
326330
{"WITH 'hello' REGEXP 'h' AS result SELECT 1", 0},
327331
{"WITH (select 1) as a, z AS (select 2) SELECT 1", 0},
328332
{"SELECT result FROM test_view(myParam = ?)", 1},
333+
{"WITH toDate('2025-08-20') as DATE_END, events AS ( SELECT 1 ) SELECT * FROM events", 0},
334+
{"select 1 table where 1 = ?", 1}
329335
};
330336
}
331337

0 commit comments

Comments
 (0)