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
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

java.sql.Date carries a timestamp so coverting into local date is unsafe (we do not know value origin)
client-v2 deliberately do not work with java.sql.Date / Time / Timestamp because it is old API and has a lot issues within. Application should handle it because it understands better what value is.

}
if (value instanceof LocalDate) {
LocalDate d = (LocalDate) value;
epochDays = (int) d.atStartOfDay(targetTz).toLocalDate().toEpochDay();
Expand All @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions history/latest/3386.md
Original file line number Diff line number Diff line change
@@ -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 <date> to Long`.
(https://github.com/ClickHouse/clickhouse-java/issues/3386)
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,47 @@ 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), m Map(String, Date32)) ENGINE = Memory");
try (PreparedStatement ps = conn.prepareStatement(
"INSERT INTO test_set_object_sql_date VALUES (?, ?, ?, ?, ?, ?)")) {
java.util.Map<String, Object> 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, 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);
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.assertEquals(rs.getDate("mk").toLocalDate(), expected);
Assert.assertFalse(rs.next());
}
stmt.executeUpdate("DROP TABLE IF EXISTS test_set_object_sql_date");
}
}
}
Loading