Skip to content

Commit fe265b3

Browse files
committed
Addded more tests and fixed another variant of WITH statement
1 parent 318509c commit fe265b3

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ topClause
382382

383383
fromClause
384384
: FROM joinExpr
385+
| FROM identifier LPAREN QUERY RPAREN
385386
;
386387

387388
arrayJoinClause

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ public void enterInsertParameter(ClickHouseParser.InsertParameterContext ctx) {
188188
appendParameter(ctx.start.getStartIndex());
189189
}
190190

191+
@Override
192+
public void enterFromClause(ClickHouseParser.FromClauseContext ctx) {
193+
if (ctx.QUERY() != null) {
194+
appendParameter(ctx.QUERY().getSymbol().getStartIndex());
195+
}
196+
}
197+
191198
private void appendParameter(int startIndex) {
192199
argCount++;
193200
if (argCount > paramPositions.length) {

jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,81 @@ void testWithClauseWithParams() throws Exception {
345345
}
346346
}
347347

348+
@Test(groups = { "integration" })
349+
void testMultipleWithClauses() throws Exception {
350+
try (Connection conn = getJdbcConnection();
351+
PreparedStatement stmt = conn.prepareStatement(
352+
"WITH data1 AS (SELECT 1 AS a), " +
353+
" data2 AS (SELECT a + 1 AS b FROM data1) " +
354+
"SELECT * FROM data2")) {
355+
ResultSet rs = stmt.executeQuery();
356+
assertTrue(rs.next());
357+
assertEquals(2, rs.getInt(1));
358+
assertFalse(rs.next());
359+
}
360+
}
361+
362+
@Test(groups = { "integration" })
363+
void testRecursiveWithClause() throws Exception {
364+
try (Connection conn = getJdbcConnection();
365+
PreparedStatement stmt = conn.prepareStatement(
366+
"WITH RECURSIVE numbers AS (" +
367+
" SELECT 1 AS n " +
368+
" UNION ALL " +
369+
" SELECT n + 1 FROM numbers WHERE n < 5" +
370+
") " +
371+
"SELECT * FROM numbers ORDER BY n")) {
372+
ResultSet rs = stmt.executeQuery();
373+
for (int i = 1; i <= 5; i++) {
374+
assertTrue(rs.next());
375+
assertEquals(i, rs.getInt(1));
376+
}
377+
assertFalse(rs.next());
378+
}
379+
}
380+
381+
@Test(groups = { "integration" })
382+
void testWithClauseWithMultipleParameters() throws Exception {
383+
try (Connection conn = getJdbcConnection();
384+
PreparedStatement stmt = conn.prepareStatement(
385+
"WITH data AS (" +
386+
" (SELECT number AS n " +
387+
" FROM numbers(?) " +
388+
" WHERE n > ?)" +
389+
") " +
390+
"SELECT * FROM data WHERE n < ?")) {
391+
//"WITH data AS ( (SELECT number AS n FROM numbers(?) WHERE n > ?)) SELECT * FROM data WHERE n < ?"
392+
stmt.setInt(1, 10); // numbers(10) = 0-9
393+
stmt.setInt(2, 3); // n > 3
394+
stmt.setInt(3, 7); // n < 7
395+
396+
ResultSet rs = stmt.executeQuery();
397+
int count = 0;
398+
int expected = 4; // 4,5,6
399+
while (rs.next()) {
400+
count++;
401+
int n = rs.getInt(1);
402+
assertTrue(n > 3 && n < 7);
403+
}
404+
assertEquals(3, count);
405+
}
406+
}
407+
408+
@Test(groups = { "integration" })
409+
void testSelectFromArray() throws Exception {
410+
try (Connection conn = getJdbcConnection();
411+
PreparedStatement stmt = conn.prepareStatement(
412+
"SELECT * FROM numbers(?)")) {
413+
stmt.setInt(1, 10); // numbers(10) = 0-9
414+
ResultSet rs = stmt.executeQuery();
415+
int count = 0;
416+
while (rs.next()) {
417+
count++;
418+
}
419+
assertEquals(10, count);
420+
}
421+
}
422+
348423
@Test(groups = { "integration" })
349424
void testInsert() throws Exception {
350425
int ROWS = 1000;

0 commit comments

Comments
 (0)