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
6 changes: 6 additions & 0 deletions history/latest/3347.md
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 10 additions & 2 deletions jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -653,14 +653,22 @@ 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 {
throw new SQLException("Only Tuple datatype is supported for Struct", ExceptionUtils.SQL_STATE_CLIENT_ERROR);
}
}

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 {
Expand Down
38 changes: 38 additions & 0 deletions jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Loading