diff --git a/CHANGELOG.md b/CHANGELOG.md index db226ce69..6937c595d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ * transaction failure introduced 0.4.0. * respect node-specific credentials. [#1114](https://github.com/ClickHouse/clickhouse-java/issues/1114) * USE statement does nothing. [#1160](https://github.com/ClickHouse/clickhouse-java/issues/1160) +* executeBatch does not support on cluster anymore. [#1261](https://github.com/ClickHouse/clickhouse-java/issues/1261) ## 0.4.1, 2023-02-19 ### Breaking Changes diff --git a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseStatementImpl.java b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseStatementImpl.java index c3ef3268f..cc4854be1 100644 --- a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseStatementImpl.java +++ b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/ClickHouseStatementImpl.java @@ -772,7 +772,7 @@ public long[] executeLargeBatch() throws SQLException { int i = 0; for (ClickHouseSqlStatement s : batchStmts) { try (ClickHouseResponse r = executeStatement(s, null, null, null); ResultSet rs = updateResult(s, r)) { - if (rs != null) { + if (rs != null && s.isQuery()) { throw SqlExceptionUtils.queryInBatchError(results); } results[i] = currentUpdateCount <= 0L ? 0L : currentUpdateCount; diff --git a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/SqlBasedPreparedStatement.java b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/SqlBasedPreparedStatement.java index b6f4307a0..0bf2a22ba 100644 --- a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/SqlBasedPreparedStatement.java +++ b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/SqlBasedPreparedStatement.java @@ -161,7 +161,7 @@ protected long[] executeAny(boolean asBatch) throws SQLException { long rows = 0L; try { r = executeStatement(builder.toString(), reparse); - if (updateResult(parsedStmt, r) != null && asBatch) { + if (updateResult(parsedStmt, r) != null && asBatch && parsedStmt.isQuery()) { throw SqlExceptionUtils.queryInBatchError(results); } rows = r.getSummary().getWrittenRows(); @@ -208,7 +208,7 @@ protected long[] executeAny(boolean asBatch) throws SQLException { preparedQuery.apply(builder, params); try { r = executeStatement(builder.toString(), reparse); - if (updateResult(parsedStmt, r) != null && asBatch) { + if (updateResult(parsedStmt, r) != null && asBatch && parsedStmt.isQuery()) { throw SqlExceptionUtils.queryInBatchError(results); } int count = getUpdateCount(); diff --git a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/TableBasedPreparedStatement.java b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/TableBasedPreparedStatement.java index e33a358c9..df66eba9a 100644 --- a/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/TableBasedPreparedStatement.java +++ b/clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/internal/TableBasedPreparedStatement.java @@ -101,7 +101,7 @@ public long[] executeAny(boolean asBatch) throws SQLException { for (List list : batch) { try (ClickHouseResponse r = executeStatement(sql, null, list, null); ResultSet rs = updateResult(parsedStmt, r)) { - if (asBatch && rs != null) { + if (asBatch && rs != null && parsedStmt.isQuery()) { throw SqlExceptionUtils.queryInBatchError(results); } long rows = getLargeUpdateCount(); diff --git a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java index abaf8d18e..a9301c4a5 100644 --- a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java +++ b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java @@ -694,6 +694,27 @@ public void testInsertQueryDateTime64() throws SQLException { } } + @Test(groups = "integration") + public void testBatchDdl() throws SQLException { + Properties props = new Properties(); + try (ClickHouseConnection conn = newConnection(props)) { + if (!conn.getServerVersion().check("[22.8,)")) { + throw new SkipException("Skip due to error 'unknown key zookeeper_load_balancing'"); + } + try (PreparedStatement stmt = conn.prepareStatement( + "drop table if exists test_batch_dll_on_cluster on cluster test_shard_localhost")) { + stmt.addBatch(); + stmt.addBatch(); + Assert.assertEquals(stmt.executeBatch(), new int[] { 0, 0 }); + } + + try (PreparedStatement stmt = conn.prepareStatement("select 1")) { + stmt.addBatch(); + Assert.assertThrows(BatchUpdateException.class, () -> stmt.executeBatch()); + } + } + } + @Test(groups = "integration") public void testBatchInsert() throws SQLException { try (ClickHouseConnection conn = newConnection(new Properties()); diff --git a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseStatementTest.java b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseStatementTest.java index 707e1458d..cf5da3ba3 100644 --- a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseStatementTest.java +++ b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseStatementTest.java @@ -68,6 +68,26 @@ private Object[][] getConnectionProperties() { new Object[] { emptyProps }, new Object[] { sessionProps } }; } + @Test(groups = "integration") + public void testBatchUpdate() throws SQLException { + Properties props = new Properties(); + try (ClickHouseConnection conn = newConnection(props); ClickHouseStatement stmt = conn.createStatement()) { + if (!conn.getServerVersion().check("[22.8,)")) { + throw new SkipException("Skip due to error 'unknown key zookeeper_load_balancing'"); + } + + stmt.addBatch("drop table if exists test_batch_dll_on_cluster on cluster test_shard_localhost"); + stmt.addBatch( + "create table if not exists test_batch_dll_on_cluster on cluster test_shard_localhost(a Int64) Engine=MergeTree order by a;" + + "drop table if exists test_batch_dll_on_cluster on cluster test_shard_localhost;"); + Assert.assertEquals(stmt.executeBatch(), new int[] { 0, 0, 0 }); + + stmt.addBatch("drop table if exists test_batch_queries"); + stmt.addBatch("select 1"); + Assert.assertThrows(BatchUpdateException.class, () -> stmt.executeBatch()); + } + } + @Test(groups = "integration") public void testBitmap64() throws SQLException { Properties props = new Properties();