Repository navigation
fix(jdbc-v2): read Date/DateTime/Time array elements as java.time types via getObject(i, Class) #3311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix(jdbc-v2): read Date/DateTime/Time array elements as java.time types via getObject(i, Class) #3311
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| - **[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) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<String, Class<?>> 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<String, Class<?>> 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slice copies before bounds checksMedium Severity
Reviewed by Cursor Bugbot for commit f46174e. Configure here. |
||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,16 +185,26 @@ 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nested array sources are droppedMedium Severity Nested date/time arrays lose Additional Locations (1)Reviewed by Cursor Bugbot for commit f46174e. Configure here. |
||
| } else if (sourceValue != null && !sourceValue.getClass().isArray() && isJavaTimeType(type)) { | ||
| return JdbcUtils.convert(sourceValue, type); | ||
| } else { | ||
| Map<Class<?>, Function<Object, Object>> valueConverterMap = initValueConverterMapIfNeeded(value); | ||
| return convertValue(value, type, valueConverterMap); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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"); | ||
| } | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
List arrays omit source elements
High Severity
sourceElementsis only attached when converting aBinaryStreamReader.ArrayValue, but JDBC mapsArraytoList(defaultTypeHintMapping), soResultSet.getArray()goes throughconvert’sListbranch and buildsArraywithout the wire values.Array#getResultSet().getObject(i, LocalDate/LocalTime/LocalDateTime.class)still fails with the original conversion error, so the reported issue is not fixed on the real read path. The new unit test only exercises theArrayValuebranch.Reviewed by Cursor Bugbot for commit f46174e. Configure here.