From 06a7956e3d5740e0b4a2c9cbff0978d688eb1d01 Mon Sep 17 00:00:00 2001 From: Zhichun Wu Date: Tue, 21 Mar 2023 20:23:56 +0800 Subject: [PATCH 1/3] Only throw BatchUpdateException for statements for read --- .../jdbc/internal/ClickHouseStatementImpl.java | 2 +- .../internal/SqlBasedPreparedStatement.java | 4 ++-- .../internal/TableBasedPreparedStatement.java | 2 +- .../jdbc/ClickHousePreparedStatementTest.java | 18 ++++++++++++++++++ .../jdbc/ClickHouseStatementTest.java | 16 ++++++++++++++++ 5 files changed, 38 insertions(+), 4 deletions(-) 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..251908aec 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,24 @@ public void testInsertQueryDateTime64() throws SQLException { } } + @Test(groups = "integration") + public void testBatchDdl() throws SQLException { + Properties props = new Properties(); + try (ClickHouseConnection conn = newConnection(props)) { + 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..7304c14ad 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,22 @@ 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()) { + 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(); From ddeb800a6e5a9a79545acaac40430871eae16144 Mon Sep 17 00:00:00 2001 From: Zhichun Wu Date: Tue, 21 Mar 2023 20:27:10 +0800 Subject: [PATCH 2/3] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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 From cbc9e069f9506dd64dbf15b4120b987cf8a8e590 Mon Sep 17 00:00:00 2001 From: Zhichun Wu Date: Tue, 21 Mar 2023 20:44:16 +0800 Subject: [PATCH 3/3] Skip two new tests on 22.3 --- .../com/clickhouse/jdbc/ClickHousePreparedStatementTest.java | 3 +++ .../java/com/clickhouse/jdbc/ClickHouseStatementTest.java | 4 ++++ 2 files changed, 7 insertions(+) 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 251908aec..a9301c4a5 100644 --- a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java +++ b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java @@ -698,6 +698,9 @@ public void testInsertQueryDateTime64() throws SQLException { 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(); 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 7304c14ad..cf5da3ba3 100644 --- a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseStatementTest.java +++ b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseStatementTest.java @@ -72,6 +72,10 @@ private Object[][] getConnectionProperties() { 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;"