Skip to content
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

Stop throwing exception when executing empty batch #981

Merged
merged 1 commit into from
Jul 4, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ public static BatchUpdateException batchUpdateError(Throwable e, long[] updateCo
return new BatchUpdateException("Unexpected error", SQL_STATE_SQL_ERROR, 0, updateCounts, cause);
}

public static SQLException emptyBatchError() {
return clientError("Please call addBatch method at least once before batch execution");
}

public static BatchUpdateException queryInBatchError(int[] updateCounts) {
return new BatchUpdateException("Query is not allow in batch update", SQL_STATE_CLIENT_ERROR, updateCounts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public ClickHouseConnectionImpl(ConnectionInfo connInfo) throws SQLException {
autoCommit = !jdbcConf.isJdbcCompliant() || jdbcConf.isAutoCommit();

ClickHouseNode node = connInfo.getServer();
log.debug("Connecting to node: %s", node);
log.debug("Connecting to: %s", node);

jvmTimeZone = TimeZone.getDefault();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.clickhouse.client.ClickHouseSerializer;
import com.clickhouse.client.ClickHouseUtils;
import com.clickhouse.client.ClickHouseValue;
import com.clickhouse.client.ClickHouseValues;
import com.clickhouse.client.config.ClickHouseClientOption;
import com.clickhouse.client.config.ClickHouseConfigChangeListener;
import com.clickhouse.client.config.ClickHouseOption;
Expand Down Expand Up @@ -588,7 +589,7 @@ public int[] executeBatch() throws SQLException {
public long[] executeLargeBatch() throws SQLException {
ensureOpen();
if (batchStmts.isEmpty()) {
throw SqlExceptionUtils.emptyBatchError();
return ClickHouseValues.EMPTY_LONG_ARRAY;
}

boolean continueOnError = getConnection().getJdbcConfig().isContinueBatchOnError();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected long[] executeAny(boolean asBatch) throws SQLException {
boolean continueOnError = false;
if (asBatch) {
if (counter < 1) {
throw SqlExceptionUtils.emptyBatchError();
return ClickHouseValues.EMPTY_LONG_ARRAY;
}
continueOnError = getConnection().getJdbcConfig().isContinueBatchOnError();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected long[] executeAny(boolean asBatch) throws SQLException {
boolean continueOnError = false;
if (asBatch) {
if (counter < 1) {
throw SqlExceptionUtils.emptyBatchError();
return ClickHouseValues.EMPTY_LONG_ARRAY;
}
continueOnError = getConnection().getJdbcConfig().isContinueBatchOnError();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.clickhouse.client.ClickHouseRequest;
import com.clickhouse.client.ClickHouseResponse;
import com.clickhouse.client.ClickHouseUtils;
import com.clickhouse.client.ClickHouseValues;
import com.clickhouse.client.data.ClickHouseExternalTable;
import com.clickhouse.client.logging.Logger;
import com.clickhouse.client.logging.LoggerFactory;
Expand Down Expand Up @@ -75,7 +76,7 @@ public long[] executeAny(boolean asBatch) throws SQLException {
boolean continueOnError = false;
if (asBatch) {
if (batch.isEmpty()) {
throw SqlExceptionUtils.emptyBatchError();
return ClickHouseValues.EMPTY_LONG_ARRAY;
}
continueOnError = getConnection().getJdbcConfig().isContinueBatchOnError();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -778,15 +778,22 @@ public void testBatchInput() throws SQLException {
public void testBatchQuery() throws SQLException {
try (ClickHouseConnection conn = newConnection(new Properties());
PreparedStatement stmt = conn.prepareStatement("select * from numbers(100) where number < ?")) {
Assert.assertEquals(stmt.executeBatch(), new int[0]);
Assert.assertEquals(stmt.executeLargeBatch(), new long[0]);
Assert.assertThrows(SQLException.class, () -> stmt.setInt(0, 5));
Assert.assertThrows(SQLException.class, () -> stmt.setInt(2, 5));
Assert.assertThrows(SQLException.class, () -> stmt.addBatch());

stmt.setInt(1, 3);
Assert.assertEquals(stmt.executeBatch(), new int[0]);
Assert.assertEquals(stmt.executeLargeBatch(), new long[0]);
stmt.addBatch();
stmt.setInt(1, 2);
stmt.addBatch();
Assert.assertThrows(BatchUpdateException.class, () -> stmt.executeBatch());

Assert.assertEquals(stmt.executeBatch(), new int[0]);
Assert.assertEquals(stmt.executeLargeBatch(), new long[0]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ public void testMaxFloatValues() throws SQLException {
public void testMutation() throws SQLException {
Properties props = new Properties();
try (ClickHouseConnection conn = newConnection(props); ClickHouseStatement stmt = conn.createStatement()) {
Assert.assertEquals(stmt.executeBatch(), new int[0]);
Assert.assertEquals(stmt.executeLargeBatch(), new long[0]);

Assert.assertFalse(stmt.execute("drop table if exists test_mutation;"
+ "create table test_mutation(a String, b UInt32) engine=MergeTree() order by tuple()"),
"Should not return result set");
Expand All @@ -156,6 +159,9 @@ public void testMutation() throws SQLException {
stmt.addBatch("drop table non_existing_table");
stmt.addBatch("insert into test_mutation values('2',2)");
Assert.assertThrows(SQLException.class, () -> stmt.executeBatch());

Assert.assertEquals(stmt.executeBatch(), new int[0]);
Assert.assertEquals(stmt.executeLargeBatch(), new long[0]);
}

props.setProperty(JdbcConfig.PROP_CONTINUE_BATCH, "true");
Expand Down Expand Up @@ -269,7 +275,12 @@ public void testExecute() throws SQLException {
public void testExecuteBatch() throws SQLException {
Properties props = new Properties();
try (Connection conn = newConnection(props); Statement stmt = conn.createStatement()) {
Assert.assertThrows(SQLException.class, () -> stmt.executeBatch());
Assert.assertEquals(stmt.executeBatch(), new int[0]);
Assert.assertEquals(stmt.executeLargeBatch(), new long[0]);
stmt.addBatch("select 1");
stmt.clearBatch();
Assert.assertEquals(stmt.executeBatch(), new int[0]);
Assert.assertEquals(stmt.executeLargeBatch(), new long[0]);
stmt.addBatch("select 1");
// mixed usage
Assert.assertThrows(SQLException.class, () -> stmt.execute("select 2"));
Expand Down