From f0835f57c70a007607162e89ad9233529a44a78d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?JB=20Onofr=C3=A9?= Date: Thu, 3 Dec 2026 11:50:43 +0100 Subject: [PATCH] GH-1548: [JDBC] Read offset time zones of timestamps and treat an empty time zone as unset Parse a timestamp time zone that TimeZone.getTimeZone does not know, such as "+03:00", with ZoneId, as the parameter converter does, instead of reading the vector as GMT. Names that TimeZone.getTimeZone knows are parsed as before, and unknown names are still read as GMT. Treat an empty time zone like an unset one when reading and binding, as the Arrow format specifies and as SqlTypes already reports it. Closes #1548. --- ...rrowFlightJdbcTimeStampVectorAccessor.java | 25 +++++- .../TimestampAvaticaParameterConverter.java | 8 +- ...FlightJdbcTimeStampVectorAccessorTest.java | 76 +++++++++++++++++++ ...imestampAvaticaParameterConverterTest.java | 36 +++++++++ 4 files changed, 139 insertions(+), 6 deletions(-) diff --git a/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessor.java b/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessor.java index 813fbc7cfd..a56e42a9d5 100644 --- a/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessor.java +++ b/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessor.java @@ -24,14 +24,15 @@ import java.sql.SQLException; import java.sql.Time; import java.sql.Timestamp; +import java.time.DateTimeException; import java.time.Instant; import java.time.LocalDateTime; import java.time.OffsetDateTime; +import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; import java.util.Calendar; -import java.util.Objects; import java.util.Set; import java.util.TimeZone; import java.util.concurrent.TimeUnit; @@ -252,14 +253,30 @@ protected static TimeZone getTimeZoneForVector(TimeStampVector vector) { ArrowType.Timestamp arrowType = (ArrowType.Timestamp) vector.getField().getFieldType().getType(); - String timezoneName = Objects.requireNonNullElse(arrowType.getTimezone(), "UTC"); - return TimeZone.getTimeZone(timezoneName); + String timezoneName = arrowType.getTimezone(); + if (timezoneName == null || timezoneName.isEmpty()) { + return TimeZone.getTimeZone("UTC"); + } + TimeZone timeZone = TimeZone.getTimeZone(timezoneName); + if (timeZone.getID().equals("GMT") && !timezoneName.equals("GMT")) { + // TimeZone.getTimeZone returns GMT for a name it does not know, including offsets such as + // "+03:00", which the Arrow format allows. Parse those as the parameter converter does. + try { + // normalized() turns "UTC+03:00" into "+03:00", which TimeZone.getTimeZone(ZoneId) + // does not understand before Java 21 + return TimeZone.getTimeZone(ZoneId.of(timezoneName, ZoneId.SHORT_IDS).normalized()); + } catch (DateTimeException e) { + // Not a time zone: keep reading the vector as GMT + } + } + return timeZone; } protected static boolean getVectorIsZoned(TimeStampVector vector) { ArrowType.Timestamp arrowType = (ArrowType.Timestamp) vector.getField().getFieldType().getType(); - return arrowType.getTimezone() != null; + // The Arrow format treats an empty time zone like an unset one + return arrowType.getTimezone() != null && !arrowType.getTimezone().isEmpty(); } } diff --git a/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverter.java b/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverter.java index 9aa8f5da15..16d8fe9188 100644 --- a/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverter.java +++ b/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverter.java @@ -50,14 +50,18 @@ public boolean bindParameter(FieldVector vector, TypedValue typedValue, int inde return true; } final ArrowType.Timestamp type = (ArrowType.Timestamp) vector.getField().getType(); - final Instant instant = toInstant(typedValue.toLocal(), type.getTimezone()); + // The Arrow format treats an empty time zone like an unset one + final String timeZone = + type.getTimezone() == null || type.getTimezone().isEmpty() ? null : type.getTimezone(); + final Instant instant = toInstant(typedValue.toLocal(), timeZone); ((TimeStampVector) vector).setSafe(index, toVectorValue(instant, type)); return true; } /** * Converts the parameter value to the instant to store in the vector. For a vector without a time - * zone, this is the wall-clock time of the value as if it was in UTC. + * zone ({@code vectorTimeZone} is null), this is the wall-clock time of the value as if it was in + * UTC. */ private static Instant toInstant(Object value, String vectorTimeZone) { if (value instanceof LocalDateTime) { diff --git a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessorTest.java b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessorTest.java index e4863bd80e..1972f6bef7 100644 --- a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessorTest.java +++ b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/calendar/ArrowFlightJdbcTimeStampVectorAccessorTest.java @@ -30,6 +30,7 @@ import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.ZoneId; +import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.util.Calendar; import java.util.Objects; @@ -41,6 +42,7 @@ import org.apache.arrow.driver.jdbc.utils.AccessorTestUtils; import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestExtension; import org.apache.arrow.vector.TimeStampMicroVector; +import org.apache.arrow.vector.TimeStampMilliTZVector; import org.apache.arrow.vector.TimeStampMilliVector; import org.apache.arrow.vector.TimeStampNanoVector; import org.apache.arrow.vector.TimeStampSecVector; @@ -50,10 +52,12 @@ import org.hamcrest.CoreMatchers; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; public class ArrowFlightJdbcTimeStampVectorAccessorTest { @@ -487,4 +491,76 @@ private void assertGetStringIsConsistentWithVarCharAccessor(Calendar calendar) t }); } } + + private static final Instant INSTANT = Instant.parse("2024-03-15T09:00:00Z"); + + private static TimeStampMilliTZVector createTimeStampMilliTZVector(String timeZone) { + TimeStampMilliTZVector vector = + new TimeStampMilliTZVector("", rootAllocatorTestExtension.getRootAllocator(), timeZone); + vector.allocateNew(1); + vector.setSafe(0, INSTANT.toEpochMilli()); + vector.setValueCount(1); + return vector; + } + + /** Time zones given as an offset from UTC, as the Arrow format allows, and that offset. */ + public static Stream offsetTimeZones() { + return Stream.of( + Arguments.of("+03:00", ZoneOffset.ofHours(3)), + Arguments.of("-05:30", ZoneOffset.ofHoursMinutes(-5, -30)), + Arguments.of("+0300", ZoneOffset.ofHours(3)), + Arguments.of("UTC+3", ZoneOffset.ofHours(3))); + } + + @ParameterizedTest + @MethodSource("offsetTimeZones") + public void testShouldApplyOffsetTimeZoneOfVector(String timeZone, ZoneOffset offset) + throws Exception { + vector = createTimeStampMilliTZVector(timeZone); + ArrowFlightJdbcTimeStampVectorAccessor accessor = accessorSupplier.supply(vector, () -> 0); + LocalDateTime localDateTime = LocalDateTime.ofInstant(INSTANT, offset); + + assertThat(accessor.getObject(LocalDateTime.class), is(localDateTime)); + assertThat(accessor.getObject(Instant.class), is(INSTANT)); + assertThat(accessor.getObject(OffsetDateTime.class), is(INSTANT.atOffset(offset))); + assertThat(accessor.getTimestamp(null), is(Timestamp.valueOf(localDateTime))); + } + + @Test + public void testShouldReadVectorWithEmptyTimeZoneAsVectorWithoutTimeZone() throws Exception { + // The Arrow format treats an empty time zone like an unset one + vector = createTimeStampMilliTZVector(""); + ArrowFlightJdbcTimeStampVectorAccessor accessor = accessorSupplier.supply(vector, () -> 0); + Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(AMERICA_SAO_PAULO)); + + try (TimeStampMilliVector withoutTimeZone = + new TimeStampMilliVector("", rootAllocatorTestExtension.getRootAllocator())) { + withoutTimeZone.allocateNew(1); + withoutTimeZone.setSafe(0, INSTANT.toEpochMilli()); + withoutTimeZone.setValueCount(1); + ArrowFlightJdbcTimeStampVectorAccessor expected = + accessorSupplier.supply(withoutTimeZone, () -> 0); + + assertThat( + accessor.getObject(LocalDateTime.class), is(expected.getObject(LocalDateTime.class))); + assertThat(accessor.getTimestamp(null), is(expected.getTimestamp(null))); + assertThat(accessor.getTimestamp(calendar), is(expected.getTimestamp(calendar))); + assertThat(accessor.getDate(calendar), is(expected.getDate(calendar))); + assertThat(accessor.getTime(calendar), is(expected.getTime(calendar))); + } + assertThrows(SQLException.class, () -> accessor.getObject(Instant.class)); + assertThrows(SQLException.class, () -> accessor.getObject(OffsetDateTime.class)); + assertThrows(SQLException.class, () -> accessor.getObject(ZonedDateTime.class)); + } + + @ParameterizedTest + @ValueSource(strings = {"Not/AZone", "utc"}) + public void testShouldReadVectorWithUnknownTimeZoneAsGmt(String timeZone) throws Exception { + vector = createTimeStampMilliTZVector(timeZone); + ArrowFlightJdbcTimeStampVectorAccessor accessor = accessorSupplier.supply(vector, () -> 0); + + assertThat( + accessor.getObject(LocalDateTime.class), is(LocalDateTime.parse("2024-03-15T09:00"))); + assertThat(accessor.getObject(Instant.class), is(INSTANT)); + } } diff --git a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverterTest.java b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverterTest.java index 7bafc4e0f6..f20637f930 100644 --- a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverterTest.java +++ b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverterTest.java @@ -29,6 +29,7 @@ import java.time.ZonedDateTime; import java.util.Calendar; import java.util.stream.Stream; +import org.apache.arrow.driver.jdbc.accessor.impl.calendar.ArrowFlightJdbcTimeStampVectorAccessor; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.vector.FieldVector; @@ -44,6 +45,8 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.NullSource; +import org.junit.jupiter.params.provider.ValueSource; /** Tests for {@link TimestampAvaticaParameterConverter}. */ public class TimestampAvaticaParameterConverterTest { @@ -197,6 +200,39 @@ public void testBindInstantTypesToVectorWithoutTimeZone() { } } + @ParameterizedTest + @MethodSource("units") + public void testBindToVectorWithEmptyTimeZone(TimeUnit unit, long unitsPerSecond, long fraction) { + // The Arrow format treats an empty time zone like an unset one + assertEquals( + EPOCH_SECOND * unitsPerSecond + fraction, bind(unit, "", objectValue(LOCAL_DATE_TIME))); + assertThrows( + UnsupportedOperationException.class, + () -> bind(unit, "", objectValue(LOCAL_DATE_TIME.toInstant(ZoneOffset.UTC)))); + } + + @ParameterizedTest + @NullSource + @ValueSource(strings = {"", "UTC", NEW_YORK, "EST", "+03:00", "-05:30", "+0300", "UTC+3"}) + public void testBoundLocalDateTimeReadsBackUnchanged(String timeZone) throws Exception { + LocalDateTime localDateTime = LocalDateTime.parse("2024-03-15T12:34:56"); + for (TimeUnit unit : TimeUnit.values()) { + ArrowType.Timestamp type = new ArrowType.Timestamp(unit, timeZone); + try (FieldVector vector = + FieldType.nullable(type).createNewSingleVector("", allocator, null)) { + assertTrue( + new TimestampAvaticaParameterConverter(type) + .bindParameter(vector, objectValue(localDateTime), 0)); + vector.setValueCount(1); + ArrowFlightJdbcTimeStampVectorAccessor accessor = + new ArrowFlightJdbcTimeStampVectorAccessor( + (TimeStampVector) vector, () -> 0, (boolean wasNull) -> {}); + + assertEquals(localDateTime, accessor.getObject(LocalDateTime.class), unit.toString()); + } + } + } + @Test public void testBindValueThatDoesNotFitInVectorUnit() { TypedValue value = objectValue(LocalDateTime.parse("2300-01-01T00:00:00"));