From 14ba55b24699dc32c29459eb34f6da3546fbce77 Mon Sep 17 00:00:00 2001 From: roha4naik Date: Thu, 19 Nov 2026 11:23:26 +0530 Subject: [PATCH] fix(jdbc-v2): throw SQLException when the writer schema lookup fails in prepareStatement With beta.row_binary_for_simple_insert=true, prepareStatement() calls Client#getTableSchema. Its unchecked ServerException / ClientException left prepareStatement() unchanged. Convert it with ExceptionUtils.toSqlState, as the other jdbc-v2 call sites do. Closes #3326 --- history/latest/3326.md | 4 +++ .../com/clickhouse/jdbc/ConnectionImpl.java | 7 ++++- .../jdbc/JDBCErrorHandlingTests.java | 26 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 history/latest/3326.md diff --git a/history/latest/3326.md b/history/latest/3326.md new file mode 100644 index 0000000000..ad2d50760e --- /dev/null +++ b/history/latest/3326.md @@ -0,0 +1,4 @@ +- **[jdbc-v2]** Fixed `Connection#prepareStatement()` with `beta.row_binary_for_simple_insert=true`, which threw the + unchecked `ServerException` or `ClientException` when the table schema lookup failed (for example an unknown table). + It now throws `SQLException` with the same SQL state and error code mapping as the rest of the driver. + (https://github.com/ClickHouse/clickhouse-java/issues/3326) diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java index 35542a0ece..297ba9d2ae 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java @@ -475,7 +475,12 @@ public PreparedStatement prepareStatement(String sql, int resultSetType, int res if (!parsedStatement.isInsertWithSelect() && parsedStatement.getAssignValuesGroups() == 1 && !parsedStatement.isUseFunction() && table != null && !ClickHouseSqlStatement.DEFAULT_TABLE.equals(table)) { - TableSchema tableSchema = client.getTableSchema(table, schema); + TableSchema tableSchema; + try { + tableSchema = client.getTableSchema(table, schema); + } catch (Exception e) { + throw ExceptionUtils.toSqlState(e); + } return new WriterStatementImpl(this, sql, tableSchema, parsedStatement); } } diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/JDBCErrorHandlingTests.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/JDBCErrorHandlingTests.java index b7bae4a66d..5564205b05 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/JDBCErrorHandlingTests.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/JDBCErrorHandlingTests.java @@ -2,7 +2,9 @@ import com.clickhouse.client.api.ServerException; +import com.clickhouse.jdbc.internal.ExceptionUtils; import org.testng.Assert; +import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.io.ByteArrayOutputStream; @@ -53,6 +55,30 @@ public void testQueryIDPropagatedToException() throws Exception { Assert.assertEquals(queryIds.size(), requests); } + @DataProvider(name = "writerSchemaLookupFailures") + public static Object[][] writerSchemaLookupFailures() { + return new Object[][] { + {"INSERT INTO unknown_table_for_writer VALUES (?)", "42S02", ServerException.ErrorCodes.TABLE_NOT_FOUND.getCode()}, + {"INSERT INTO unknown_db_for_writer.t VALUES (?)", ExceptionUtils.SQL_STATE_DATA_EXCEPTION, ServerException.ErrorCodes.DATABASE_NOT_FOUND.getCode()}, + }; + } + + /** + * With the RowBinary writer enabled, {@code prepareStatement()} asks the server for the table schema. + * A failure of that lookup must be reported as {@link SQLException}, not as an unchecked client exception. + */ + @Test(groups = {"integration"}, dataProvider = "writerSchemaLookupFailures") + public void testWriterSchemaLookupFailureThrowsSQLException(String sql, String sqlState, int errorCode) throws Exception { + Properties properties = new Properties(); + properties.setProperty(DriverProperties.BETA_ROW_BINARY_WRITER.getKey(), "true"); + try (Connection conn = getJdbcConnection(properties)) { + SQLException e = Assert.expectThrows(SQLException.class, () -> conn.prepareStatement(sql)); + Assert.assertEquals(e.getSQLState(), sqlState); + Assert.assertEquals(e.getErrorCode(), errorCode); + Assert.assertTrue(e.getCause() instanceof ServerException, "cause: " + e.getCause()); + } + } + /** * When result-set metadata cannot be resolved because the DESCRIBE fails (here: an unknown table), * {@code getMetaData()} must fall back to untyped metadata instead of throwing, and the failure must