Skip to content

Commit 3903b2f

Browse files
committed
feat: add drop stage test
1 parent 91a395e commit 3903b2f

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

databend-jdbc/src/main/java/com/databend/jdbc/cloud/DatabendPresignClientV1.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ private void uploadFromStream(InputStream inputStream, String stageName, String
124124
private void uploadFromStream(InputStream inputStream, Headers headers, String presignedUrl, long fileSize)
125125
throws IOException
126126
{
127-
logger.info("Starting upload: size=" + fileSize + " bytes, url=" + presignedUrl);
127+
logger.fine("Starting upload: size=" + fileSize + " bytes, url=" + presignedUrl);
128128
long startTime = System.currentTimeMillis();
129129
try {
130130
Request r = putRequest(headers, presignedUrl, inputStream, fileSize);
131131
executeInternal(r, true);
132-
logger.info("Upload completed in " + (System.currentTimeMillis() - startTime) + "ms");
132+
logger.fine("Upload completed in " + (System.currentTimeMillis() - startTime) + "ms");
133133
}
134134
catch (IOException e) {
135135
logger.severe("Upload failed after " + (System.currentTimeMillis() - startTime) + "ms: " + e.getMessage());

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.sql.Statement;
1717
import java.sql.Timestamp;
1818
import java.sql.Types;
19+
import java.util.UUID;
1920
import java.util.Arrays;
2021
import java.util.Properties;
2122
import java.util.TimeZone;
@@ -431,6 +432,73 @@ public void testAllPreparedStatement() throws SQLException {
431432
}
432433
}
433434

435+
@Test(groups = "IT")
436+
public void TestStageFileRemovedAfterBatchInsert() throws SQLException {
437+
String dbName = ("stage_cleanup_" + UUID.randomUUID()).replace("-", "");
438+
try (Connection c = Utils.createConnection();
439+
Statement s = c.createStatement()) {
440+
c.setAutoCommit(false);
441+
s.execute("create or replace database " + dbName);
442+
s.execute("use " + dbName);
443+
s.execute("create or replace table t_stage_cleanup(a int, b string)");
444+
445+
try (TrackingPreparedStatement ps = new TrackingPreparedStatement((DatabendConnection) c, "insert into t_stage_cleanup values")) {
446+
ps.setInt(1, 1);
447+
ps.setString(2, "hello");
448+
ps.addBatch();
449+
int[] counts = ps.executeBatch();
450+
Assert.assertEquals(counts, new int[] {1});
451+
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+
}
473+
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());
479+
}
480+
}
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();
499+
}
500+
}
501+
434502
@Test(groups = "IT")
435503
public void shouldBuildStageAttachmentWithFileFormatOptions() throws SQLException {
436504
Connection conn = Utils.createConnection();

0 commit comments

Comments
 (0)