Skip to content

Commit 06a7956

Browse files
committed
Only throw BatchUpdateException for statements for read
1 parent 28d4d9d commit 06a7956

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseStatementImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ public long[] executeLargeBatch() throws SQLException {
772772
int i = 0;
773773
for (ClickHouseSqlStatement s : batchStmts) {
774774
try (ClickHouseResponse r = executeStatement(s, null, null, null); ResultSet rs = updateResult(s, r)) {
775-
if (rs != null) {
775+
if (rs != null && s.isQuery()) {
776776
throw SqlExceptionUtils.queryInBatchError(results);
777777
}
778778
results[i] = currentUpdateCount <= 0L ? 0L : currentUpdateCount;

clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/SqlBasedPreparedStatement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ protected long[] executeAny(boolean asBatch) throws SQLException {
161161
long rows = 0L;
162162
try {
163163
r = executeStatement(builder.toString(), reparse);
164-
if (updateResult(parsedStmt, r) != null && asBatch) {
164+
if (updateResult(parsedStmt, r) != null && asBatch && parsedStmt.isQuery()) {
165165
throw SqlExceptionUtils.queryInBatchError(results);
166166
}
167167
rows = r.getSummary().getWrittenRows();
@@ -208,7 +208,7 @@ protected long[] executeAny(boolean asBatch) throws SQLException {
208208
preparedQuery.apply(builder, params);
209209
try {
210210
r = executeStatement(builder.toString(), reparse);
211-
if (updateResult(parsedStmt, r) != null && asBatch) {
211+
if (updateResult(parsedStmt, r) != null && asBatch && parsedStmt.isQuery()) {
212212
throw SqlExceptionUtils.queryInBatchError(results);
213213
}
214214
int count = getUpdateCount();

clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/TableBasedPreparedStatement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public long[] executeAny(boolean asBatch) throws SQLException {
101101
for (List<ClickHouseExternalTable> list : batch) {
102102
try (ClickHouseResponse r = executeStatement(sql, null, list, null);
103103
ResultSet rs = updateResult(parsedStmt, r)) {
104-
if (asBatch && rs != null) {
104+
if (asBatch && rs != null && parsedStmt.isQuery()) {
105105
throw SqlExceptionUtils.queryInBatchError(results);
106106
}
107107
long rows = getLargeUpdateCount();

clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,24 @@ public void testInsertQueryDateTime64() throws SQLException {
694694
}
695695
}
696696

697+
@Test(groups = "integration")
698+
public void testBatchDdl() throws SQLException {
699+
Properties props = new Properties();
700+
try (ClickHouseConnection conn = newConnection(props)) {
701+
try (PreparedStatement stmt = conn.prepareStatement(
702+
"drop table if exists test_batch_dll_on_cluster on cluster test_shard_localhost")) {
703+
stmt.addBatch();
704+
stmt.addBatch();
705+
Assert.assertEquals(stmt.executeBatch(), new int[] { 0, 0 });
706+
}
707+
708+
try (PreparedStatement stmt = conn.prepareStatement("select 1")) {
709+
stmt.addBatch();
710+
Assert.assertThrows(BatchUpdateException.class, () -> stmt.executeBatch());
711+
}
712+
}
713+
}
714+
697715
@Test(groups = "integration")
698716
public void testBatchInsert() throws SQLException {
699717
try (ClickHouseConnection conn = newConnection(new Properties());

clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseStatementTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ private Object[][] getConnectionProperties() {
6868
new Object[] { emptyProps }, new Object[] { sessionProps } };
6969
}
7070

71+
@Test(groups = "integration")
72+
public void testBatchUpdate() throws SQLException {
73+
Properties props = new Properties();
74+
try (ClickHouseConnection conn = newConnection(props); ClickHouseStatement stmt = conn.createStatement()) {
75+
stmt.addBatch("drop table if exists test_batch_dll_on_cluster on cluster test_shard_localhost");
76+
stmt.addBatch(
77+
"create table if not exists test_batch_dll_on_cluster on cluster test_shard_localhost(a Int64) Engine=MergeTree order by a;"
78+
+ "drop table if exists test_batch_dll_on_cluster on cluster test_shard_localhost;");
79+
Assert.assertEquals(stmt.executeBatch(), new int[] { 0, 0, 0 });
80+
81+
stmt.addBatch("drop table if exists test_batch_queries");
82+
stmt.addBatch("select 1");
83+
Assert.assertThrows(BatchUpdateException.class, () -> stmt.executeBatch());
84+
}
85+
}
86+
7187
@Test(groups = "integration")
7288
public void testBitmap64() throws SQLException {
7389
Properties props = new Properties();

0 commit comments

Comments
 (0)