Skip to content

Only throw BatchUpdateException for statements for read #1293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public long[] executeAny(boolean asBatch) throws SQLException {
for (List<ClickHouseExternalTable> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down