Skip to content

Commit 2f2f2ea

Browse files
authored
Merge pull request #981 from zhicwu/develop
Stop throwing exception when executing empty batch
2 parents e59bd46 + 844b2b8 commit 2f2f2ea

8 files changed

+26
-10
lines changed

clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/SqlExceptionUtils.java

-4
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@ public static BatchUpdateException batchUpdateError(Throwable e, long[] updateCo
107107
return new BatchUpdateException("Unexpected error", SQL_STATE_SQL_ERROR, 0, updateCounts, cause);
108108
}
109109

110-
public static SQLException emptyBatchError() {
111-
return clientError("Please call addBatch method at least once before batch execution");
112-
}
113-
114110
public static BatchUpdateException queryInBatchError(int[] updateCounts) {
115111
return new BatchUpdateException("Query is not allow in batch update", SQL_STATE_CLIENT_ERROR, updateCounts);
116112
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public ClickHouseConnectionImpl(ConnectionInfo connInfo) throws SQLException {
220220
autoCommit = !jdbcConf.isJdbcCompliant() || jdbcConf.isAutoCommit();
221221

222222
ClickHouseNode node = connInfo.getServer();
223-
log.debug("Connecting to node: %s", node);
223+
log.debug("Connecting to: %s", node);
224224

225225
jvmTimeZone = TimeZone.getDefault();
226226

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.clickhouse.client.ClickHouseSerializer;
2626
import com.clickhouse.client.ClickHouseUtils;
2727
import com.clickhouse.client.ClickHouseValue;
28+
import com.clickhouse.client.ClickHouseValues;
2829
import com.clickhouse.client.config.ClickHouseClientOption;
2930
import com.clickhouse.client.config.ClickHouseConfigChangeListener;
3031
import com.clickhouse.client.config.ClickHouseOption;
@@ -588,7 +589,7 @@ public int[] executeBatch() throws SQLException {
588589
public long[] executeLargeBatch() throws SQLException {
589590
ensureOpen();
590591
if (batchStmts.isEmpty()) {
591-
throw SqlExceptionUtils.emptyBatchError();
592+
return ClickHouseValues.EMPTY_LONG_ARRAY;
592593
}
593594

594595
boolean continueOnError = getConnection().getJdbcConfig().isContinueBatchOnError();

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected long[] executeAny(boolean asBatch) throws SQLException {
9494
boolean continueOnError = false;
9595
if (asBatch) {
9696
if (counter < 1) {
97-
throw SqlExceptionUtils.emptyBatchError();
97+
return ClickHouseValues.EMPTY_LONG_ARRAY;
9898
}
9999
continueOnError = getConnection().getJdbcConfig().isContinueBatchOnError();
100100
} else {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected long[] executeAny(boolean asBatch) throws SQLException {
122122
boolean continueOnError = false;
123123
if (asBatch) {
124124
if (counter < 1) {
125-
throw SqlExceptionUtils.emptyBatchError();
125+
return ClickHouseValues.EMPTY_LONG_ARRAY;
126126
}
127127
continueOnError = getConnection().getJdbcConfig().isContinueBatchOnError();
128128
} else {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.clickhouse.client.ClickHouseRequest;
2020
import com.clickhouse.client.ClickHouseResponse;
2121
import com.clickhouse.client.ClickHouseUtils;
22+
import com.clickhouse.client.ClickHouseValues;
2223
import com.clickhouse.client.data.ClickHouseExternalTable;
2324
import com.clickhouse.client.logging.Logger;
2425
import com.clickhouse.client.logging.LoggerFactory;
@@ -75,7 +76,7 @@ public long[] executeAny(boolean asBatch) throws SQLException {
7576
boolean continueOnError = false;
7677
if (asBatch) {
7778
if (batch.isEmpty()) {
78-
throw SqlExceptionUtils.emptyBatchError();
79+
return ClickHouseValues.EMPTY_LONG_ARRAY;
7980
}
8081
continueOnError = getConnection().getJdbcConfig().isContinueBatchOnError();
8182
} else {

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

+7
Original file line numberDiff line numberDiff line change
@@ -778,15 +778,22 @@ public void testBatchInput() throws SQLException {
778778
public void testBatchQuery() throws SQLException {
779779
try (ClickHouseConnection conn = newConnection(new Properties());
780780
PreparedStatement stmt = conn.prepareStatement("select * from numbers(100) where number < ?")) {
781+
Assert.assertEquals(stmt.executeBatch(), new int[0]);
782+
Assert.assertEquals(stmt.executeLargeBatch(), new long[0]);
781783
Assert.assertThrows(SQLException.class, () -> stmt.setInt(0, 5));
782784
Assert.assertThrows(SQLException.class, () -> stmt.setInt(2, 5));
783785
Assert.assertThrows(SQLException.class, () -> stmt.addBatch());
784786

785787
stmt.setInt(1, 3);
788+
Assert.assertEquals(stmt.executeBatch(), new int[0]);
789+
Assert.assertEquals(stmt.executeLargeBatch(), new long[0]);
786790
stmt.addBatch();
787791
stmt.setInt(1, 2);
788792
stmt.addBatch();
789793
Assert.assertThrows(BatchUpdateException.class, () -> stmt.executeBatch());
794+
795+
Assert.assertEquals(stmt.executeBatch(), new int[0]);
796+
Assert.assertEquals(stmt.executeLargeBatch(), new long[0]);
790797
}
791798
}
792799

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ public void testMaxFloatValues() throws SQLException {
136136
public void testMutation() throws SQLException {
137137
Properties props = new Properties();
138138
try (ClickHouseConnection conn = newConnection(props); ClickHouseStatement stmt = conn.createStatement()) {
139+
Assert.assertEquals(stmt.executeBatch(), new int[0]);
140+
Assert.assertEquals(stmt.executeLargeBatch(), new long[0]);
141+
139142
Assert.assertFalse(stmt.execute("drop table if exists test_mutation;"
140143
+ "create table test_mutation(a String, b UInt32) engine=MergeTree() order by tuple()"),
141144
"Should not return result set");
@@ -156,6 +159,9 @@ public void testMutation() throws SQLException {
156159
stmt.addBatch("drop table non_existing_table");
157160
stmt.addBatch("insert into test_mutation values('2',2)");
158161
Assert.assertThrows(SQLException.class, () -> stmt.executeBatch());
162+
163+
Assert.assertEquals(stmt.executeBatch(), new int[0]);
164+
Assert.assertEquals(stmt.executeLargeBatch(), new long[0]);
159165
}
160166

161167
props.setProperty(JdbcConfig.PROP_CONTINUE_BATCH, "true");
@@ -269,7 +275,12 @@ public void testExecute() throws SQLException {
269275
public void testExecuteBatch() throws SQLException {
270276
Properties props = new Properties();
271277
try (Connection conn = newConnection(props); Statement stmt = conn.createStatement()) {
272-
Assert.assertThrows(SQLException.class, () -> stmt.executeBatch());
278+
Assert.assertEquals(stmt.executeBatch(), new int[0]);
279+
Assert.assertEquals(stmt.executeLargeBatch(), new long[0]);
280+
stmt.addBatch("select 1");
281+
stmt.clearBatch();
282+
Assert.assertEquals(stmt.executeBatch(), new int[0]);
283+
Assert.assertEquals(stmt.executeLargeBatch(), new long[0]);
273284
stmt.addBatch("select 1");
274285
// mixed usage
275286
Assert.assertThrows(SQLException.class, () -> stmt.execute("select 2"));

0 commit comments

Comments
 (0)