|
16 | 16 | import java.sql.Statement; |
17 | 17 | import java.sql.Timestamp; |
18 | 18 | import java.sql.Types; |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.Set; |
19 | 21 | import java.util.UUID; |
20 | 22 | import java.util.Arrays; |
21 | 23 | import java.util.Properties; |
@@ -442,60 +444,39 @@ public void TestStageFileRemovedAfterBatchInsert() throws SQLException { |
442 | 444 | s.execute("use " + dbName); |
443 | 445 | s.execute("create or replace table t_stage_cleanup(a int, b string)"); |
444 | 446 |
|
445 | | - try (TrackingPreparedStatement ps = new TrackingPreparedStatement((DatabendConnection) c, "insert into t_stage_cleanup values")) { |
| 447 | + Set<String> before = new HashSet<>(); |
| 448 | + try (ResultSet rs = s.executeQuery("LIST @~/")) { |
| 449 | + while (rs.next()) { |
| 450 | + before.add(rs.getString(1)); |
| 451 | + } |
| 452 | + } |
| 453 | + |
| 454 | + try (PreparedStatement ps = c.prepareStatement("insert into t_stage_cleanup values")) { |
446 | 455 | ps.setInt(1, 1); |
447 | 456 | ps.setString(2, "hello"); |
448 | 457 | ps.addBatch(); |
449 | 458 | int[] counts = ps.executeBatch(); |
450 | 459 | Assert.assertEquals(counts, new int[] {1}); |
| 460 | + } |
451 | 461 |
|
452 | | - String location = ps.getLastAttachmentLocation(); |
453 | | - Assert.assertNotNull(location); |
454 | | - System.out.println("[DEBUG] uploaded stage file: " + location); |
455 | | - String dir = location.substring(0, location.lastIndexOf('/') + 1); |
456 | | - System.out.println("location dir is:"+dir); |
457 | | - try (ResultSet rs = s.executeQuery("LIST " + dir)) { |
458 | | - if (rs.next()) { |
459 | | - Assert.fail("Stage directory not empty after batch insert, unexpected entry: " + rs.getString(1)); |
460 | | - } |
461 | | - } catch (SQLException e) { |
462 | | - if (e.getErrorCode() != 1003) { |
463 | | - throw e; |
464 | | - } |
465 | | - } finally { |
466 | | - try { |
467 | | - System.out.println("[DEBUG] drop stage path: " + location); |
468 | | - s.execute("REMOVE " + location); |
469 | | - } catch (SQLException ignore) { |
470 | | - // best-effort cleanup |
471 | | - } |
472 | | - } |
| 462 | + try (ResultSet rs = s.executeQuery("SELECT a, b FROM t_stage_cleanup")) { |
| 463 | + Assert.assertTrue(rs.next()); |
| 464 | + Assert.assertEquals(rs.getInt(1), 1); |
| 465 | + Assert.assertEquals(rs.getString(2), "hello"); |
| 466 | + Assert.assertFalse(rs.next()); |
| 467 | + } |
473 | 468 |
|
474 | | - try (ResultSet rs = s.executeQuery("SELECT a, b FROM t_stage_cleanup")) { |
475 | | - Assert.assertTrue(rs.next()); |
476 | | - Assert.assertEquals(rs.getInt(1), 1); |
477 | | - Assert.assertEquals(rs.getString(2), "hello"); |
478 | | - Assert.assertFalse(rs.next()); |
| 469 | + Set<String> after = new HashSet<>(); |
| 470 | + try (ResultSet rs = s.executeQuery("LIST @~/")) { |
| 471 | + while (rs.next()) { |
| 472 | + after.add(rs.getString(1)); |
479 | 473 | } |
480 | 474 | } |
481 | | - } |
482 | | - } |
483 | | - |
484 | | - private static final class TrackingPreparedStatement extends DatabendPreparedStatement { |
485 | | - private StageAttachment lastAttachment; |
486 | | - |
487 | | - TrackingPreparedStatement(DatabendConnection connection, String sql) throws SQLException { |
488 | | - super(connection, stmt -> {}, sql); |
489 | | - } |
490 | | - |
491 | | - @Override |
492 | | - boolean dropStageAttachment(StageAttachment attachment) { |
493 | | - lastAttachment = attachment; |
494 | | - return super.dropStageAttachment(attachment); |
495 | | - } |
496 | | - |
497 | | - String getLastAttachmentLocation() { |
498 | | - return lastAttachment == null ? null : lastAttachment.getLocation(); |
| 475 | + Set<String> diff = new HashSet<>(after); |
| 476 | + diff.removeAll(before); |
| 477 | + if (!diff.isEmpty()) { |
| 478 | + Assert.fail("Stage has unexpected leftover entries: " + diff); |
| 479 | + } |
499 | 480 | } |
500 | 481 | } |
501 | 482 |
|
|
0 commit comments