Skip to content

Commit ab5252a

Browse files
committed
fix
1 parent 3903b2f commit ab5252a

File tree

1 file changed

+26
-45
lines changed

1 file changed

+26
-45
lines changed

databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.sql.Statement;
1717
import java.sql.Timestamp;
1818
import java.sql.Types;
19+
import java.util.HashSet;
20+
import java.util.Set;
1921
import java.util.UUID;
2022
import java.util.Arrays;
2123
import java.util.Properties;
@@ -442,60 +444,39 @@ public void TestStageFileRemovedAfterBatchInsert() throws SQLException {
442444
s.execute("use " + dbName);
443445
s.execute("create or replace table t_stage_cleanup(a int, b string)");
444446

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")) {
446455
ps.setInt(1, 1);
447456
ps.setString(2, "hello");
448457
ps.addBatch();
449458
int[] counts = ps.executeBatch();
450459
Assert.assertEquals(counts, new int[] {1});
460+
}
451461

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+
}
473468

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));
479473
}
480474
}
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+
}
499480
}
500481
}
501482

0 commit comments

Comments
 (0)