|
20 | 20 | import java.sql.Types; |
21 | 21 | import java.time.LocalDate; |
22 | 22 | import java.time.LocalDateTime; |
| 23 | +import java.time.ZoneId; |
| 24 | +import java.time.temporal.ChronoUnit; |
| 25 | +import java.time.temporal.TemporalUnit; |
23 | 26 | import java.util.ArrayList; |
24 | 27 | import java.util.Arrays; |
25 | 28 | import java.util.Collection; |
@@ -292,17 +295,129 @@ public void testTernaryOperator() throws Exception { |
292 | 295 |
|
293 | 296 | @Test(groups = "integration") |
294 | 297 | void testWithClause() throws Exception { |
295 | | - int count = 0; |
296 | 298 | try (Connection conn = getJdbcConnection()) { |
297 | 299 | try (PreparedStatement stmt = conn.prepareStatement("with data as (SELECT number FROM numbers(100)) select * from data ")) { |
298 | 300 | stmt.execute(); |
299 | 301 | ResultSet rs = stmt.getResultSet(); |
| 302 | + int count = 0; |
300 | 303 | while (rs.next()) { |
301 | 304 | count++; |
302 | 305 | } |
| 306 | + assertEquals(count, 100); |
| 307 | + } |
| 308 | + } |
| 309 | + } |
| 310 | + |
| 311 | + @Test(groups = "integration") |
| 312 | + void testWithClauseWithParams() throws Exception { |
| 313 | + final String table = "test_with_stmt"; |
| 314 | + try (Connection conn = getJdbcConnection()) { |
| 315 | + try (Statement stmt = conn.createStatement()) { |
| 316 | + stmt.execute("DROP TABLE IF EXISTS " + table); |
| 317 | + stmt.execute("CREATE TABLE " + table + " (v1 String) Engine MergeTree ORDER BY ()"); |
| 318 | + stmt.execute("INSERT INTO " + table + " VALUES ('A'), ('B')"); |
| 319 | + } |
| 320 | + final Timestamp target_time = Timestamp.valueOf(LocalDateTime.now()); |
| 321 | + try (PreparedStatement stmt = conn.prepareStatement("WITH " + |
| 322 | + " toDateTime(?) as target_time, " + |
| 323 | + " (SELECT 123) as magic_number" + |
| 324 | + " SELECT *, target_time, magic_number FROM " + table)) { |
| 325 | + stmt.setTimestamp(1, target_time); |
| 326 | + stmt.execute(); |
| 327 | + ResultSet rs = stmt.getResultSet(); |
| 328 | + int count = 0; |
| 329 | + assertEquals(rs.getMetaData().getColumnCount(), 3); |
| 330 | + while (rs.next()) { |
| 331 | + Assert.assertEquals( |
| 332 | + rs.getTimestamp("target_time").toLocalDateTime().truncatedTo(ChronoUnit.SECONDS), |
| 333 | + target_time.toInstant().atZone(ZoneId.of("UTC")).toLocalDateTime().truncatedTo(ChronoUnit.SECONDS)); |
| 334 | + Assert.assertEquals(rs.getString("magic_number"), "123"); |
| 335 | + Assert.assertEquals( |
| 336 | + rs.getTimestamp(2).toLocalDateTime().truncatedTo(ChronoUnit.SECONDS), |
| 337 | + target_time.toInstant().atZone(ZoneId.of("UTC")).toLocalDateTime().truncatedTo(ChronoUnit.SECONDS)); |
| 338 | + Assert.assertEquals(rs.getString(3), "123"); |
| 339 | + |
| 340 | + count++; |
| 341 | + } |
| 342 | + assertEquals(count, 2, "Expected 2 rows"); |
| 343 | + |
| 344 | + } |
| 345 | + } |
| 346 | + } |
| 347 | + |
| 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++; |
303 | 418 | } |
| 419 | + assertEquals(10, count); |
304 | 420 | } |
305 | | - assertEquals(count, 100); |
306 | 421 | } |
307 | 422 |
|
308 | 423 | @Test(groups = { "integration" }) |
|
0 commit comments