Skip to content
Open
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
4 changes: 4 additions & 0 deletions history/latest/3326.md
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down