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
5 changes: 5 additions & 0 deletions history/latest/3284.md
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
Expand Up @@ -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,

Copy link
Copy Markdown

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

sourceElements is only attached when converting a BinaryStreamReader.ArrayValue, but JDBC maps Array to List (defaultTypeHintMapping), so ResultSet.getArray() goes through convert’s List branch and builds Array without 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 the ArrayValue branch.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit f46174e. Configure here.

arrayToObjectArray(arrayValue.getArray()));
}

return new Array(column, arrayValue.getArrayOfObjects(), multiDimensionalArrayElementType);
Expand Down
24 changes: 22 additions & 2 deletions jdbc-v2/src/main/java/com/clickhouse/jdbc/types/Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Slice copies before bounds checks

Medium Severity

getResultSet(index, count) copies sourceElements before getArray(index, count) validates the range. With sourceElements set, a negative count throws NegativeArraySizeException and an out-of-range index/count throws ArrayIndexOutOfBoundsException instead of the SQLException that getArray previously returned.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit f46174e. Configure here.

}

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nested array sources are dropped

Medium Severity

Nested date/time arrays lose sourceElements. Wire nested values are ArrayValue or List, not Java arrays, so sourceValue.getClass().isArray() is false and the nested Array is created with a null source. arrayToObjectArray(arrayValue.getArray()) also does not recurse (ArrayValue.toObjectArray() would). Inner getResultSet().getObject(i, LocalDate.class) still fails for Array(Array(Date32)) and similar types.

Additional Locations (1)
Fix in Cursor Fix in Web

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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -629,4 +636,34 @@ void testArrayOfObjects() throws Exception {
assertEquals(rs.getObject(valueColumn, String.class), "2");
assertEquals(rs.getString(valueColumn), rs.getObject(valueColumn, String.class));
}

@Test
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);
}
}
}
Loading