diff --git a/docs/features.md b/docs/features.md index ef27f4018..2cbabb071 100644 --- a/docs/features.md +++ b/docs/features.md @@ -123,6 +123,7 @@ Compatibility-sensitive traits: - Standard `FORMAT JSON` output has ClickHouse-specific `meta` and `data` fields and is not exposed as a JDBC `ResultSet`. JDBC callers that need it should unwrap to `ConnectionImpl`, call `getClient()`, and parse the `QueryResponse` stream directly. - Binary parameters passed through `setBytes()` are encoded as ClickHouse `unhex(...)` expressions rather than text literals; empty byte arrays map to an empty string expression. - Stream and reader setters (`setAsciiStream`, `setUnicodeStream`, `setBinaryStream`, `setCharacterStream`, `setNCharacterStream`) are treated as text input encoded with the same string-escaping rules, including length-based truncation when a length is supplied. +- With `beta.row_binary_for_simple_insert=true`, the `setObject` overloads with a target SQL type (`int` or `SQLType`, with or without `scaleOrLength`) check the value the same way as the SQL path: a `DATE`, `TIME`, `TIME_WITH_TIMEZONE`, `TIMESTAMP` or `TIMESTAMP_WITH_TIMEZONE` target type accepts only the Java date and time classes that the SQL path accepts, and other values fail with `SQLException`. Then the writer stores the value the same way as `setObject(int, Object)`, so these overloads accept the same values as that overload. The writer converts the value to the column type; it does not use the target type or `scaleOrLength` for the conversion. - `getString()` formatting for temporal values is stable output: `Date` uses `yyyy-MM-dd`, `DateTime` uses `yyyy-MM-dd HH:mm:ss`, and `DateTime64` preserves fractional precision, all interpreted in server timezone context where applicable. - Date and timestamp setters with `Calendar` are timezone-sensitive by design. Preserving the current day-shift and instant-preserving behavior is important for compatibility. - `setObject()` temporal behavior is specific and should not drift: `LocalDateTime` and `Instant` are rendered through `fromUnixTimestamp64Nano(...)`, while `Timestamp` and `Date` use quoted textual forms. diff --git a/history/latest/3408.md b/history/latest/3408.md new file mode 100644 index 000000000..c7362ee16 --- /dev/null +++ b/history/latest/3408.md @@ -0,0 +1,9 @@ +## Bug Fixes + +- **[jdbc-v2]** Fixed the RowBinary writer for simple `INSERT ... VALUES (?, ...)` statements + (`beta.row_binary_for_simple_insert=true`). The `setObject` overloads with a target SQL type + (`setObject(int, Object, int)`, `setObject(int, Object, int, int)`, `setObject(int, Object, SQLType)` and + `setObject(int, Object, SQLType, int)`) failed with "This form of setObject is not supported yet". Frameworks use these + overloads, for example Spring `JdbcTemplate` with `SqlParameterValue`. Now they check the value against the target + type the same way as the SQL path, and then store it the same way as `setObject(int, Object)`. + (https://github.com/ClickHouse/clickhouse-java/issues/3408) diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java index c2487d289..7bf67034a 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java @@ -999,7 +999,7 @@ private static String encodeCharacterStream(Reader reader, Long length) throws S } } - private void isValidForTargetType(Object value, int targetType) throws SQLException { + void isValidForTargetType(Object value, int targetType) throws SQLException { if (value == null) { return; // NULL is handled in encoding and server checks if value can be NULL } diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/WriterStatementImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/WriterStatementImpl.java index a01944f5d..bb3c639af 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/WriterStatementImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/WriterStatementImpl.java @@ -423,13 +423,15 @@ public void setNString(int parameterIndex, String value) throws SQLException { @Override public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength) throws SQLException { ensureOpen(); - throw new SQLException("This form of setObject is not supported yet", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); + isValidForTargetType(x, targetSqlType); + setObject(parameterIndex, x); } @Override public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { ensureOpen(); - throw new SQLException("This form of setObject is not supported yet", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); + isValidForTargetType(x, targetSqlType); + setObject(parameterIndex, x); } @Override @@ -441,13 +443,15 @@ public void setObject(int parameterIndex, Object x) throws SQLException { @Override public void setObject(int parameterIndex, Object x, SQLType targetSqlType) throws SQLException { ensureOpen(); - throw new SQLException("This form of setObject is not supported yet", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); + isValidForTargetType(x, targetSqlType.getVendorTypeNumber()); + setObject(parameterIndex, x); } @Override public void setObject(int parameterIndex, Object x, SQLType targetSqlType, int scaleOrLength) throws SQLException { ensureOpen(); - throw new SQLException("This form of setObject is not supported yet", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED); + isValidForTargetType(x, targetSqlType.getVendorTypeNumber()); + setObject(parameterIndex, x); } @Override diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/WriterStatementImplTest.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/WriterStatementImplTest.java index d6c12f96e..b446454d6 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/WriterStatementImplTest.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/WriterStatementImplTest.java @@ -9,6 +9,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.lang.reflect.Field; +import java.math.BigDecimal; import java.sql.Connection; import java.sql.JDBCType; import java.sql.PreparedStatement; @@ -16,6 +17,8 @@ import java.sql.SQLException; import java.sql.Statement; import java.sql.Types; +import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -26,19 +29,83 @@ public class WriterStatementImplTest extends JdbcIntegrationTest { - @Test(groups = {"integration"}) - public void testTargetTypeMethodThrowException() throws SQLException { + @FunctionalInterface + private interface TypedSetter { + void set(PreparedStatement stmt, int index, Object value, JDBCType type, int scaleOrLength) throws SQLException; + } + + @DataProvider(name = "typedSetObjectOverloads") + public static Object[][] typedSetObjectOverloads() { + return new Object[][] { + {"int", (TypedSetter) (s, i, v, t, n) -> s.setObject(i, v, t.getVendorTypeNumber())}, + {"int, int", (TypedSetter) (s, i, v, t, n) -> s.setObject(i, v, t.getVendorTypeNumber(), n)}, + {"SQLType", (TypedSetter) (s, i, v, t, n) -> s.setObject(i, v, t)}, + {"SQLType, int", (TypedSetter) (s, i, v, t, n) -> s.setObject(i, v, t, n)}, + }; + } + + @Test(groups = {"integration"}, dataProvider = "typedSetObjectOverloads") + public void testTypedSetObjectWritesValues(String overload, TypedSetter setter) throws Exception { + String table = "writer_stmt_typed_set_object_" + UUID.randomUUID().toString().replace('-', '_'); + Properties properties = new Properties(); + properties.setProperty(DriverProperties.BETA_ROW_BINARY_WRITER.getKey(), "true"); + properties.setProperty(ASYNC_INSERT_SETTING_KEY, ServerSettings.OFF); + try (Connection connection = getJdbcConnection(properties)) { + try (Statement stmt = connection.createStatement()) { + stmt.execute("CREATE TABLE " + table + " (id Int32, d Date, ts DateTime64(3, 'UTC'), " + + "dec Decimal(10, 2), s String, n Nullable(Date), tail Int32) ORDER BY id"); + } + + try { + try (PreparedStatement ps = connection.prepareStatement( + "INSERT INTO " + table + " VALUES (?, ?, ?, ?, ?, ?, ?)")) { + Assert.assertTrue(ps instanceof WriterStatementImpl); + setter.set(ps, 1, 1, JDBCType.INTEGER, 0); + setter.set(ps, 2, LocalDate.of(2024, 2, 29), JDBCType.DATE, 0); + setter.set(ps, 3, LocalDateTime.of(2024, 2, 29, 13, 14, 15, 123_000_000), JDBCType.TIMESTAMP, 0); + setter.set(ps, 4, new BigDecimal("12.34"), JDBCType.DECIMAL, 0); + setter.set(ps, 5, "abcdef", JDBCType.VARCHAR, 3); + setter.set(ps, 6, null, JDBCType.DATE, 0); + setter.set(ps, 7, 7, JDBCType.VARCHAR, 0); + Assert.assertEquals(ps.executeUpdate(), 1); + } + + try (Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT id, toString(d), toString(ts), toString(dec), s, n, tail FROM " + + table)) { + Assert.assertTrue(rs.next()); + Assert.assertEquals(rs.getInt(1), 1); + Assert.assertEquals(rs.getString(2), "2024-02-29"); + Assert.assertEquals(rs.getString(3), "2024-02-29 13:14:15.123"); + Assert.assertEquals(rs.getString(4), "12.34"); + Assert.assertEquals(rs.getString(5), "abcdef"); + Assert.assertNull(rs.getObject(6)); + Assert.assertEquals(rs.getInt(7), 7); + Assert.assertFalse(rs.next()); + } + } finally { + try (Statement stmt = connection.createStatement()) { + stmt.execute("DROP TABLE IF EXISTS " + table); + } + } + } + } + @Test(groups = {"integration"}, dataProvider = "typedSetObjectOverloads") + public void testTypedSetObjectRejectsValueOfOtherType(String overload, TypedSetter setter) throws SQLException { Properties properties = new Properties(); properties.setProperty(DriverProperties.BETA_ROW_BINARY_WRITER.getKey(), "true"); try (Connection connection = getJdbcConnection(properties); PreparedStatement stmt = connection.prepareStatement("INSERT INTO system.numbers VALUES (?, ?)")) { Assert.assertTrue(stmt instanceof WriterStatementImpl); - Assert.expectThrows(SQLException.class, () -> stmt.setObject(1, "", JDBCType.VARCHAR.getVendorTypeNumber())); - Assert.expectThrows(SQLException.class, () -> stmt.setObject(1, "", JDBCType.VARCHAR)); - Assert.expectThrows(SQLException.class, () -> stmt.setObject(1, "", JDBCType.DECIMAL.getVendorTypeNumber(), 3)); - Assert.expectThrows(SQLException.class, () -> stmt.setObject(1, "", JDBCType.DECIMAL, 3)); + SQLException e = Assert.expectThrows(SQLException.class, + () -> setter.set(stmt, 1, "2024-02-29", JDBCType.DATE, 0)); + Assert.assertEquals(e.getMessage(), "Cannot convert value of type class java.lang.String to SQL type DATE"); + e = Assert.expectThrows(SQLException.class, () -> setter.set(stmt, 1, "13:14:15", JDBCType.TIME, 0)); + Assert.assertEquals(e.getMessage(), "Cannot convert value of type class java.lang.String to SQL type TIME"); + e = Assert.expectThrows(SQLException.class, () -> setter.set(stmt, 1, 1L, JDBCType.TIMESTAMP, 0)); + Assert.assertEquals(e.getMessage(), "Cannot convert value of type class java.lang.Long to SQL type TIMESTAMP"); } }