diff --git a/history/latest/3347.md b/history/latest/3347.md new file mode 100644 index 000000000..2d2c8ad49 --- /dev/null +++ b/history/latest/3347.md @@ -0,0 +1,6 @@ +- **[jdbc-v2]** `Connection.createArrayOf(typeName, ...)` and `Connection.createStruct(typeName, ...)` now throw + `SQLException` (SQLState `HY000`) when the driver cannot parse `typeName`, for example an unknown type, unbalanced + parentheses, invalid type parameters, or an empty string. Before, these methods threw an unchecked + `IllegalArgumentException` or `NumberFormatException`, so a `catch (SQLException e)` block and frameworks that + translate `SQLException` did not get the error. The parser exception is the cause of the `SQLException`, and the + message contains the type name. (https://github.com/ClickHouse/clickhouse-java/issues/3347) 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 35542a0ec..d85e47a4a 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java @@ -638,7 +638,7 @@ public Array createArrayOf(String typeName, Object[] elements) throws SQLExcepti throw new SQLFeatureNotSupportedException("typeName cannot be null"); } - ClickHouseColumn column = ClickHouseColumn.of("array", typeName); + ClickHouseColumn column = parseColumnType("array", typeName); try { return new com.clickhouse.jdbc.types.Array(column, elements, config.isFlagSet(DriverProperties.MULTI_DIMENSIONAL_ARRAY_ELEMENT_TYPE)); @@ -653,7 +653,7 @@ public Struct createStruct(String typeName, Object[] attributes) throws SQLExcep if (typeName == null) { throw new SQLFeatureNotSupportedException("typeName cannot be null"); } - ClickHouseColumn column = ClickHouseColumn.of("v", typeName); + ClickHouseColumn column = parseColumnType("v", typeName); if (column.getDataType().equals(ClickHouseDataType.Tuple)) { return new com.clickhouse.jdbc.types.Struct(column, attributes); } else { @@ -661,6 +661,14 @@ public Struct createStruct(String typeName, Object[] attributes) throws SQLExcep } } + private static ClickHouseColumn parseColumnType(String columnName, String typeName) throws SQLException { + try { + return ClickHouseColumn.of(columnName, typeName); + } catch (RuntimeException e) { + throw new SQLException("Invalid type name '" + typeName + "'", ExceptionUtils.SQL_STATE_CLIENT_ERROR, e); + } + } + @Override public void setSchema(String schema) throws SQLException { diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java index 6df60e8a4..a7b8268e0 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java @@ -965,6 +965,44 @@ public void testGetNetworkTimeoutReportsEffectiveSocketTimeout(String socketTime } } + @DataProvider + public static Object[][] invalidTypeNames() { + return new Object[][] { + {"NoSuchType"}, + {"Nullable(Strng)"}, + {"Array(NoSuchType)"}, + {"Tuple(NoSuchType, UInt8)"}, + {"Array(Int32"}, + {"Tuple(Int8, String"}, + {"Decimal(abc, 2)"}, + {"Tuple(Decimal(abc, 2), UInt8)"}, + {""}, + }; + } + + @Test(dataProvider = "invalidTypeNames", timeOut = 10000) + public void testCreateArrayOfAndStructRejectInvalidTypeName(String typeName) throws Exception { + WireMockServer server = new WireMockServer(WireMockConfiguration.options() + .bindAddress("127.0.0.1").dynamicPort()); + server.start(); + try (ConnectionImpl connection = new ConnectionImpl( + "jdbc:clickhouse://127.0.0.1:" + server.port() + "/default", connectionProperties(null))) { + assertInvalidTypeName(expectThrows(SQLException.class, + () -> connection.createArrayOf(typeName, new Object[] {1})), typeName); + assertInvalidTypeName(expectThrows(SQLException.class, + () -> connection.createStruct(typeName, new Object[] {1})), typeName); + } finally { + server.stop(); + } + } + + private static void assertInvalidTypeName(SQLException error, String typeName) { + assertEquals(error.getClass(), SQLException.class); + assertEquals(error.getSQLState(), ExceptionUtils.SQL_STATE_CLIENT_ERROR); + assertTrue(error.getMessage().contains("'" + typeName + "'"), error.getMessage()); + assertTrue(error.getCause() instanceof IllegalArgumentException, String.valueOf(error.getCause())); + } + private static void stubDelayedSelectOne(WireMockServer server, int delayMillis) { // RowBinaryWithNamesAndTypes: one UInt8 column named "1", containing one row with value 1. server.stubFor(WireMock.post(WireMock.anyUrl())