From bcf40b1b8c8043707a775b65905c7ae494bae5fa Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Sat, 7 Nov 2026 01:32:57 -0400 Subject: [PATCH 1/2] fix(jdbc-v2): read Date/DateTime/Time array elements as java.time types via getObject(i, Class) Fixes #3284 --- .../internal/ValueConverters.java | 21 ++++++++++++++++--- history/latest/3284.md | 5 +++++ .../jdbc/types/ArrayResultSetTest.java | 21 +++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 history/latest/3284.md diff --git a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/ValueConverters.java b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/ValueConverters.java index adf2246ba3..e4a3646509 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/ValueConverters.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/ValueConverters.java @@ -108,11 +108,14 @@ public ValueConverters() { mapBuilder.put(String.class, stringMapBuilder.build()); mapBuilder.put(java.sql.Date.class, ImmutableMap.of(java.sql.Date.class, this::convertSqlDateToSqlDate, - String.class, this::convertDateToString)); + String.class, this::convertDateToString, + java.time.LocalDate.class, this::convertSqlDateToLocalDate)); mapBuilder.put(Time.class, ImmutableMap.of(Time.class, this::convertSqlTimeToSqlTime, - String.class, this::convertTimeToString)); + String.class, this::convertTimeToString, + java.time.LocalTime.class, this::convertSqlTimeToLocalTime)); mapBuilder.put(Timestamp.class, ImmutableMap.of(Timestamp.class, this::convertSqlTimestampToSqlTimestamp, - String.class, this::convertTimestampToString)); + String.class, this::convertTimestampToString, + java.time.LocalDateTime.class, this::convertSqlTimestampToLocalDateTime)); classConverters = mapBuilder.build(); } @@ -232,6 +235,18 @@ public Timestamp convertSqlTimestampToSqlTimestamp(Object value) { return (Timestamp) value; } + public java.time.LocalDate convertSqlDateToLocalDate(Object value) { + return ((java.sql.Date) value).toLocalDate(); + } + + public java.time.LocalTime convertSqlTimeToLocalTime(Object value) { + return ((Time) value).toLocalTime(); + } + + public java.time.LocalDateTime convertSqlTimestampToLocalDateTime(Object value) { + return ((Timestamp) value).toLocalDateTime(); + } + public String convertDateToString(Object value) { return value.toString(); } diff --git a/history/latest/3284.md b/history/latest/3284.md new file mode 100644 index 0000000000..691bd0b7d8 --- /dev/null +++ b/history/latest/3284.md @@ -0,0 +1,5 @@ +- **[jdbc-v2]** Fixed `ResultSet#getObject(int, Class)` on the result set returned by `java.sql.Array#getResultSet()` + failing with `Value of class java.sql.Date cannot be converted to class java.time.LocalDate` for arrays of + `Date`/`Date32`, `DateTime`/`DateTime64` and `Time`/`Time64`. These elements can now also be read as + `java.time.LocalDate`, `java.time.LocalDateTime` and `java.time.LocalTime`, the same as for non-array columns. + (https://github.com/ClickHouse/clickhouse-java/issues/3284) diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/types/ArrayResultSetTest.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/types/ArrayResultSetTest.java index 87b7e14d1f..0cde2a5fbd 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/types/ArrayResultSetTest.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/types/ArrayResultSetTest.java @@ -629,4 +629,25 @@ void testArrayOfObjects() throws Exception { assertEquals(rs.getObject(valueColumn, String.class), "2"); assertEquals(rs.getString(valueColumn), rs.getObject(valueColumn, String.class)); } + + @Test + void testJavaTimeConversions() throws Exception { + Date[] dates = {Date.valueOf("2020-01-01"), null}; + ArrayResultSet rs = new ArrayResultSet(dates, ClickHouseColumn.parse("v Array(Date32)").get(0)); + rs.next(); + assertEquals(rs.getObject(2, java.time.LocalDate.class), java.time.LocalDate.of(2020, 1, 1)); + rs.next(); + assertEquals(rs.getObject(2, java.time.LocalDate.class), null); + + Timestamp[] timestamps = {Timestamp.valueOf("2020-01-01 12:34:56.789123"), null}; + rs = new ArrayResultSet(timestamps, ClickHouseColumn.parse("v Array(DateTime64(6))").get(0)); + rs.next(); + assertEquals(rs.getObject(2, java.time.LocalDateTime.class), + java.time.LocalDateTime.of(2020, 1, 1, 12, 34, 56, 789123000)); + + Time[] times = {Time.valueOf("12:34:56"), null}; + rs = new ArrayResultSet(times, ClickHouseColumn.parse("v Array(Time)").get(0)); + rs.next(); + assertEquals(rs.getObject(2, java.time.LocalTime.class), java.time.LocalTime.of(12, 34, 56)); + } } From f46174e5e8d69936795ec580dd43460f6a9f7abf Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Thu, 19 Nov 2026 21:52:08 -0400 Subject: [PATCH 2/2] fix(jdbc-v2): serve java.time array elements from source values instead of converting java.sql types Per review on #3284: do not add Date->LocalDate conversions in ValueConverters (time zone concerns). Array now keeps the elements as read from the server and the array ResultSet returns them for LocalDate/LocalTime/LocalDateTime targets. --- .../internal/ValueConverters.java | 21 ++------ history/latest/3284.md | 8 +-- .../clickhouse/jdbc/internal/JdbcUtils.java | 3 +- .../java/com/clickhouse/jdbc/types/Array.java | 24 ++++++++- .../clickhouse/jdbc/types/ArrayResultSet.java | 25 ++++++++- .../jdbc/types/ArrayResultSetTest.java | 52 ++++++++++++------- 6 files changed, 89 insertions(+), 44 deletions(-) diff --git a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/ValueConverters.java b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/ValueConverters.java index e4a3646509..adf2246ba3 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/ValueConverters.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/ValueConverters.java @@ -108,14 +108,11 @@ public ValueConverters() { mapBuilder.put(String.class, stringMapBuilder.build()); mapBuilder.put(java.sql.Date.class, ImmutableMap.of(java.sql.Date.class, this::convertSqlDateToSqlDate, - String.class, this::convertDateToString, - java.time.LocalDate.class, this::convertSqlDateToLocalDate)); + String.class, this::convertDateToString)); mapBuilder.put(Time.class, ImmutableMap.of(Time.class, this::convertSqlTimeToSqlTime, - String.class, this::convertTimeToString, - java.time.LocalTime.class, this::convertSqlTimeToLocalTime)); + String.class, this::convertTimeToString)); mapBuilder.put(Timestamp.class, ImmutableMap.of(Timestamp.class, this::convertSqlTimestampToSqlTimestamp, - String.class, this::convertTimestampToString, - java.time.LocalDateTime.class, this::convertSqlTimestampToLocalDateTime)); + String.class, this::convertTimestampToString)); classConverters = mapBuilder.build(); } @@ -235,18 +232,6 @@ public Timestamp convertSqlTimestampToSqlTimestamp(Object value) { return (Timestamp) value; } - public java.time.LocalDate convertSqlDateToLocalDate(Object value) { - return ((java.sql.Date) value).toLocalDate(); - } - - public java.time.LocalTime convertSqlTimeToLocalTime(Object value) { - return ((Time) value).toLocalTime(); - } - - public java.time.LocalDateTime convertSqlTimestampToLocalDateTime(Object value) { - return ((Timestamp) value).toLocalDateTime(); - } - public String convertDateToString(Object value) { return value.toString(); } diff --git a/history/latest/3284.md b/history/latest/3284.md index 691bd0b7d8..d72d99a240 100644 --- a/history/latest/3284.md +++ b/history/latest/3284.md @@ -1,5 +1,5 @@ -- **[jdbc-v2]** Fixed `ResultSet#getObject(int, Class)` on the result set returned by `java.sql.Array#getResultSet()` - failing with `Value of class java.sql.Date cannot be converted to class java.time.LocalDate` for arrays of - `Date`/`Date32`, `DateTime`/`DateTime64` and `Time`/`Time64`. These elements can now also be read as - `java.time.LocalDate`, `java.time.LocalDateTime` and `java.time.LocalTime`, the same as for non-array columns. +- **[jdbc-v2]** `ResultSet#getObject(int, Class)` on the result set returned by `Array#getResultSet()` can now read + elements of `Date`/`Date32`, `DateTime`/`DateTime64` and `Time`/`Time64` arrays as `LocalDate`, `LocalDateTime` + and `LocalTime`. The values are taken from the elements as read from the server, without a conversion through + `java.sql.Date`/`Time`/`Timestamp`, so they do not depend on the JVM time zone. (https://github.com/ClickHouse/clickhouse-java/issues/3284) diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java index 31e23efc5a..c919fbc556 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcUtils.java @@ -352,7 +352,8 @@ public static Object convert(Object value, Class type, ClickHouseColumn colum ClickHouseDataType baseType = column.getArrayBaseColumn().getDataType(); Object[] convertedValues = convertArray(arrayValue.getArray(), convertToJavaClass(baseType), column.getArrayNestedLevel()); - return new Array(column, convertedValues, multiDimensionalArrayElementType); + return new Array(column, convertedValues, multiDimensionalArrayElementType, + arrayToObjectArray(arrayValue.getArray())); } return new Array(column, arrayValue.getArrayOfObjects(), multiDimensionalArrayElementType); diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/types/Array.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/types/Array.java index 7a6664356c..732b0145e8 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/types/Array.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/types/Array.java @@ -24,6 +24,8 @@ public class Array implements java.sql.Array { private final ClickHouseDataType baseDataType; private final boolean multiDimensionalElementType; private ArrayResultSet arrayResultSet; + /** Elements as read from the wire (e.g. {@code LocalDate}), parallel to {@link #array}; may be {@code null}. */ + private final Object[] sourceElements; public Array(ClickHouseColumn column, Object[] elements) throws SQLException { this(column, elements, true); @@ -36,6 +38,19 @@ public Array(ClickHouseColumn column, Object[] elements) throws SQLException { * {@link com.clickhouse.jdbc.DriverProperties#MULTI_DIMENSIONAL_ARRAY_ELEMENT_TYPE}. */ public Array(ClickHouseColumn column, Object[] elements, boolean multiDimensionalElementType) throws SQLException { + this(column, elements, multiDimensionalElementType, null); + } + + /** + * @param sourceElements the elements as read from the wire, before conversion to the JDBC classes (for example + * {@code java.time.LocalDate} for {@code Date32}); same length as {@code elements}. They let + * {@link #getResultSet()} serve {@code getObject(i, LocalDate.class)} without a round trip + * through {@code java.sql.Date}. May be {@code null}. + */ + public Array(ClickHouseColumn column, Object[] elements, boolean multiDimensionalElementType, + Object[] sourceElements) throws SQLException { + this.sourceElements = sourceElements != null && elements != null && sourceElements.length == elements.length + ? sourceElements : null; this.column = column; this.array = elements; this.multiDimensionalElementType = multiDimensionalElementType; @@ -109,7 +124,7 @@ public Object getArray(long index, int count, Map> map) throws public synchronized ResultSet getResultSet() throws SQLException { ensureValid(); if (arrayResultSet == null) { - arrayResultSet = new ArrayResultSet(array, column, multiDimensionalElementType); + arrayResultSet = new ArrayResultSet(array, column, multiDimensionalElementType, sourceElements); } return arrayResultSet; } @@ -122,7 +137,12 @@ public ResultSet getResultSet(Map> map) throws SQLException { @Override public ResultSet getResultSet(long index, int count) throws SQLException { ensureValid(); - return new ArrayResultSet(getArray(index, count), column, multiDimensionalElementType); + Object[] sourceSlice = null; + if (sourceElements != null) { + sourceSlice = new Object[count]; + System.arraycopy(sourceElements, (int) index, sourceSlice, 0, count); + } + return new ArrayResultSet(getArray(index, count), column, multiDimensionalElementType, sourceSlice); } @Override diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/types/ArrayResultSet.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/types/ArrayResultSet.java index 31074d3e3e..64a4daa2a9 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/types/ArrayResultSet.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/types/ArrayResultSet.java @@ -38,6 +38,7 @@ public class ArrayResultSet implements ResultSet { private final Object array; + private final Object[] sourceArray; private final int length; private int pos; private boolean closed; @@ -68,6 +69,18 @@ public ArrayResultSet(Object array, ClickHouseColumn column) { * {@link com.clickhouse.jdbc.types.Array#Array(ClickHouseColumn, Object[], boolean)} */ public ArrayResultSet(Object array, ClickHouseColumn column, boolean multiDimensionalElementType) { + this(array, column, multiDimensionalElementType, null); + } + + /** + * @param sourceArray the elements as read from the wire (parallel to {@code array}), used for + * {@code java.time} targets so that no {@code java.sql.Date/Time/Timestamp} round trip (and thus + * no time zone dependent conversion) is involved. May be {@code null}. + */ + public ArrayResultSet(Object array, ClickHouseColumn column, boolean multiDimensionalElementType, + Object[] sourceArray) { + this.sourceArray = sourceArray != null && sourceArray.length == java.lang.reflect.Array.getLength(array) + ? sourceArray : null; this.array = array; this.multiDimensionalElementType = multiDimensionalElementType; this.length = java.lang.reflect.Array.getLength(array); @@ -172,9 +185,14 @@ private Object getValueAsObject(int columnIndex, Class type, Object defaultVa return defaultValue; } + Object sourceValue = sourceArray == null ? null : sourceArray[pos]; if (type == Array.class) { return new com.clickhouse.jdbc.types.Array(elementColumn(column), JdbcUtils.arrayToObjectArray(value), - multiDimensionalElementType); + multiDimensionalElementType, + sourceValue != null && sourceValue.getClass().isArray() + ? JdbcUtils.arrayToObjectArray(sourceValue) : null); + } else if (sourceValue != null && !sourceValue.getClass().isArray() && isJavaTimeType(type)) { + return JdbcUtils.convert(sourceValue, type); } else { Map, Function> valueConverterMap = initValueConverterMapIfNeeded(value); return convertValue(value, type, valueConverterMap); @@ -182,6 +200,11 @@ private Object getValueAsObject(int columnIndex, Class type, Object defaultVa } } + private static boolean isJavaTimeType(Class type) { + return type == java.time.LocalDate.class || type == java.time.LocalTime.class + || type == java.time.LocalDateTime.class; + } + private void throwReadOnlyException() throws SQLException { throw new SQLException("ResultSet is read-only"); } diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/types/ArrayResultSetTest.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/types/ArrayResultSetTest.java index 0cde2a5fbd..9d93ca0051 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/types/ArrayResultSetTest.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/types/ArrayResultSetTest.java @@ -1,6 +1,8 @@ package com.clickhouse.jdbc.types; +import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader; import com.clickhouse.data.ClickHouseColumn; +import com.clickhouse.jdbc.internal.JdbcUtils; import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @@ -24,7 +26,12 @@ import java.sql.Time; import java.sql.Timestamp; import java.sql.Types; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.util.GregorianCalendar; +import java.util.TimeZone; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; @@ -631,23 +638,32 @@ void testArrayOfObjects() throws Exception { } @Test - void testJavaTimeConversions() throws Exception { - Date[] dates = {Date.valueOf("2020-01-01"), null}; - ArrayResultSet rs = new ArrayResultSet(dates, ClickHouseColumn.parse("v Array(Date32)").get(0)); - rs.next(); - assertEquals(rs.getObject(2, java.time.LocalDate.class), java.time.LocalDate.of(2020, 1, 1)); - rs.next(); - assertEquals(rs.getObject(2, java.time.LocalDate.class), null); - - Timestamp[] timestamps = {Timestamp.valueOf("2020-01-01 12:34:56.789123"), null}; - rs = new ArrayResultSet(timestamps, ClickHouseColumn.parse("v Array(DateTime64(6))").get(0)); - rs.next(); - assertEquals(rs.getObject(2, java.time.LocalDateTime.class), - java.time.LocalDateTime.of(2020, 1, 1, 12, 34, 56, 789123000)); - - Time[] times = {Time.valueOf("12:34:56"), null}; - rs = new ArrayResultSet(times, ClickHouseColumn.parse("v Array(Time)").get(0)); - rs.next(); - assertEquals(rs.getObject(2, java.time.LocalTime.class), java.time.LocalTime.of(12, 34, 56)); + void testJavaTimeTargetsReadFromSourceElements() throws SQLException { + TimeZone previous = TimeZone.getDefault(); + // America/Sao_Paulo skipped 2018-11-04 00:00-01:00 (DST start): a java.sql.Timestamp round trip would shift it. + TimeZone.setDefault(TimeZone.getTimeZone("America/Sao_Paulo")); + try { + BinaryStreamReader.ArrayValue dates = new BinaryStreamReader.ArrayValue(LocalDate.class, 2); + dates.append(LocalDate.of(2018, 11, 4)); + dates.append(null); + java.sql.Array dateArray = (java.sql.Array) JdbcUtils.convert(dates, java.sql.Array.class, + ClickHouseColumn.parse("v Array(Nullable(Date32))").get(0), true); + ResultSet rs = dateArray.getResultSet(); + assertTrue(rs.next()); + assertEquals(rs.getObject(2, LocalDate.class), LocalDate.of(2018, 11, 4)); + assertEquals(rs.getObject(2), Date.valueOf(LocalDate.of(2018, 11, 4))); + assertTrue(rs.next()); + assertEquals(rs.getObject(2, LocalDate.class), null); + + BinaryStreamReader.ArrayValue times = new BinaryStreamReader.ArrayValue(ZonedDateTime.class, 1); + times.append(ZonedDateTime.of(2018, 11, 4, 0, 30, 0, 123456000, ZoneOffset.UTC)); + java.sql.Array timeArray = (java.sql.Array) JdbcUtils.convert(times, java.sql.Array.class, + ClickHouseColumn.parse("v Array(DateTime64(6, 'UTC'))").get(0), true); + ResultSet trs = timeArray.getResultSet(0, 1); + assertTrue(trs.next()); + assertEquals(trs.getObject(2, LocalDateTime.class), LocalDateTime.of(2018, 11, 4, 0, 30, 0, 123456000)); + } finally { + TimeZone.setDefault(previous); + } } }