diff --git a/history/latest/3284.md b/history/latest/3284.md new file mode 100644 index 000000000..d72d99a24 --- /dev/null +++ b/history/latest/3284.md @@ -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) 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 31e23efc5..c919fbc55 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 7a6664356..732b0145e 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 31074d3e3..64a4daa2a 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 87b7e14d1..9d93ca005 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; @@ -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); + } + } }