From f9f19941883986344b403c85e45d8e5f8ef1e224 Mon Sep 17 00:00:00 2001 From: Luke Gannon Date: Wed, 25 Nov 2026 13:25:14 +0000 Subject: [PATCH 1/3] jdbc-v2: accept java.sql.Date in the RowBinary writer's setObject With beta.row_binary_for_simple_insert=true, setObject(i, java.sql.Date) on a Date/Date32 column failed at addBatch() with "Cannot convert to Long", while setDate worked and setObject worked on the default text path. WriterStatementImpl.setObject passes the value straight to the RowBinary serializer, and SerializerUtils.writeDate/writeDate32 only accepted LocalDate, ZonedDateTime and OffsetDateTime. writeDateTime already accepts java.sql.Timestamp, so Date was inconsistent. Convert java.sql.Date to LocalDate first, matching setDate. Fixes #3386 Co-Authored-By: Claude Opus 4.8 --- .../internal/SerializerUtils.java | 6 +++ .../clickhouse/jdbc/JDBCDateTimeTests.java | 39 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java index 50f9136e2..f34be783b 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/SerializerUtils.java @@ -1555,6 +1555,9 @@ public static void writeBoolean(OutputStream output, boolean value) throws IOExc public static void writeDate(OutputStream output, Object value, ZoneId targetTz) throws IOException { int epochDays = 0; + if (value instanceof java.sql.Date) { + value = ((java.sql.Date) value).toLocalDate(); + } if (value instanceof LocalDate) { LocalDate d = (LocalDate) value; epochDays = (int) d.atStartOfDay(targetTz).toLocalDate().toEpochDay(); @@ -1572,6 +1575,9 @@ public static void writeDate(OutputStream output, Object value, ZoneId targetTz) public static void writeDate32(OutputStream output, Object value, ZoneId targetTz) throws IOException { int epochDays= 0; + if (value instanceof java.sql.Date) { + value = ((java.sql.Date) value).toLocalDate(); + } if (value instanceof LocalDate) { LocalDate d = (LocalDate) value; epochDays = (int) d.atStartOfDay(targetTz).toLocalDate().toEpochDay(); diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/JDBCDateTimeTests.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/JDBCDateTimeTests.java index 10094ad63..f42346c1f 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/JDBCDateTimeTests.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/JDBCDateTimeTests.java @@ -313,4 +313,43 @@ void testTimestampInRange() throws Exception { } } } + + // Regression for #3386: java.sql.Date passed through setObject on the RowBinary writer + // (beta.row_binary_for_simple_insert) used to fail with "Cannot convert ... to Long". + @Test(groups = {"integration"}) + void testSetObjectJavaSqlDateRowBinaryWriter() throws Exception { + Properties props = new Properties(); + props.put("beta.row_binary_for_simple_insert", "true"); + Date d = Date.valueOf("2013-07-15"); + LocalDate expected = LocalDate.parse("2013-07-15"); + try (Connection conn = getJdbcConnection(props); + Statement stmt = conn.createStatement()) { + stmt.executeUpdate("DROP TABLE IF EXISTS test_set_object_sql_date"); + stmt.executeUpdate("CREATE TABLE test_set_object_sql_date " + + "(d Date, d32 Date32, arr Array(Date32), t Tuple(Date32, Int32), n Nullable(Date32)) ENGINE = Memory"); + try (PreparedStatement ps = conn.prepareStatement( + "INSERT INTO test_set_object_sql_date VALUES (?, ?, ?, ?, ?)")) { + ps.setObject(1, d); // Date + ps.setObject(2, d); // Date32 + ps.setObject(3, new Date[] { d, d }); // Array(Date32) + ps.setObject(4, new Object[] { d, 7 }); // Tuple(Date32, Int32) + ps.setObject(5, d); // Nullable(Date32) + ps.addBatch(); + ps.executeBatch(); + } + try (ResultSet rs = stmt.executeQuery("SELECT d, d32, arrayElement(arr, 1) AS a1, " + + "t.1 AS t1, t.2 AS t2, length(arr) AS alen, n FROM test_set_object_sql_date")) { + Assert.assertTrue(rs.next()); + Assert.assertEquals(rs.getDate("d").toLocalDate(), expected); + Assert.assertEquals(rs.getDate("d32").toLocalDate(), expected); + Assert.assertEquals(rs.getDate("a1").toLocalDate(), expected); + Assert.assertEquals(rs.getDate("t1").toLocalDate(), expected); + Assert.assertEquals(rs.getInt("t2"), 7); + Assert.assertEquals(rs.getInt("alen"), 2); + Assert.assertEquals(rs.getDate("n").toLocalDate(), expected); + Assert.assertFalse(rs.next()); + } + stmt.executeUpdate("DROP TABLE IF EXISTS test_set_object_sql_date"); + } + } } From 3bef9b322097f15746c2a9d5bba76ca6d5015bd3 Mon Sep 17 00:00:00 2001 From: Luke Gannon Date: Wed, 25 Nov 2026 13:43:00 +0000 Subject: [PATCH 2/3] jdbc-v2: changelog for the setObject(java.sql.Date) RowBinary fix (#3386) Co-Authored-By: Claude Opus 4.8 --- history/latest/3386.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 history/latest/3386.md diff --git a/history/latest/3386.md b/history/latest/3386.md new file mode 100644 index 000000000..242398a2e --- /dev/null +++ b/history/latest/3386.md @@ -0,0 +1,5 @@ +- **[jdbc-v2]** `setObject(..., java.sql.Date)` now works on the RowBinary writer + (`beta.row_binary_for_simple_insert`) for `Date` and `Date32`, including dates nested in + `Array`, `Map`, `Tuple` and `Nullable`. Previously it failed with + `IllegalArgumentException: Cannot convert to Long`. + (https://github.com/ClickHouse/clickhouse-java/issues/3386) From c77faff282a2c2f86f8842ffab01a8f7ee333b02 Mon Sep 17 00:00:00 2001 From: Luke Gannon Date: Wed, 25 Nov 2026 16:42:14 +0000 Subject: [PATCH 3/3] jdbc-v2: cover Map(String, Date32) in the setObject(java.sql.Date) test (#3386) Co-Authored-By: Claude Opus 4.8 --- .../java/com/clickhouse/jdbc/JDBCDateTimeTests.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/JDBCDateTimeTests.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/JDBCDateTimeTests.java index f42346c1f..0fbe2779c 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/JDBCDateTimeTests.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/JDBCDateTimeTests.java @@ -325,20 +325,23 @@ void testSetObjectJavaSqlDateRowBinaryWriter() throws Exception { try (Connection conn = getJdbcConnection(props); Statement stmt = conn.createStatement()) { stmt.executeUpdate("DROP TABLE IF EXISTS test_set_object_sql_date"); - stmt.executeUpdate("CREATE TABLE test_set_object_sql_date " + - "(d Date, d32 Date32, arr Array(Date32), t Tuple(Date32, Int32), n Nullable(Date32)) ENGINE = Memory"); + stmt.executeUpdate("CREATE TABLE test_set_object_sql_date (d Date, d32 Date32, " + + "arr Array(Date32), t Tuple(Date32, Int32), n Nullable(Date32), m Map(String, Date32)) ENGINE = Memory"); try (PreparedStatement ps = conn.prepareStatement( - "INSERT INTO test_set_object_sql_date VALUES (?, ?, ?, ?, ?)")) { + "INSERT INTO test_set_object_sql_date VALUES (?, ?, ?, ?, ?, ?)")) { + java.util.Map dateMap = new java.util.HashMap<>(); + dateMap.put("k", d); ps.setObject(1, d); // Date ps.setObject(2, d); // Date32 ps.setObject(3, new Date[] { d, d }); // Array(Date32) ps.setObject(4, new Object[] { d, 7 }); // Tuple(Date32, Int32) ps.setObject(5, d); // Nullable(Date32) + ps.setObject(6, dateMap); // Map(String, Date32) ps.addBatch(); ps.executeBatch(); } try (ResultSet rs = stmt.executeQuery("SELECT d, d32, arrayElement(arr, 1) AS a1, " + - "t.1 AS t1, t.2 AS t2, length(arr) AS alen, n FROM test_set_object_sql_date")) { + "t.1 AS t1, t.2 AS t2, length(arr) AS alen, n, m['k'] AS mk FROM test_set_object_sql_date")) { Assert.assertTrue(rs.next()); Assert.assertEquals(rs.getDate("d").toLocalDate(), expected); Assert.assertEquals(rs.getDate("d32").toLocalDate(), expected); @@ -347,6 +350,7 @@ void testSetObjectJavaSqlDateRowBinaryWriter() throws Exception { Assert.assertEquals(rs.getInt("t2"), 7); Assert.assertEquals(rs.getInt("alen"), 2); Assert.assertEquals(rs.getDate("n").toLocalDate(), expected); + Assert.assertEquals(rs.getDate("mk").toLocalDate(), expected); Assert.assertFalse(rs.next()); } stmt.executeUpdate("DROP TABLE IF EXISTS test_set_object_sql_date");