From df71accdbe96907b42d7716ce2e4ca8a77dd58ec Mon Sep 17 00:00:00 2001 From: Ivan Chesnov Date: Tue, 1 Dec 2026 15:25:55 +0200 Subject: [PATCH 1/3] GH-1540: Bind TIMESTAMP parameters in the vector unit and accept java.time values Convert TIMESTAMP parameter values in the Flight SQL JDBC driver to the unit of the parameter vector instead of writing milliseconds into every unit, and accept LocalDateTime, Instant, OffsetDateTime and ZonedDateTime at full precision. Closes #1540. Co-Authored-By: Claude Fable 5.2 --- .../TimestampAvaticaParameterConverter.java | 105 ++++++---- ...imestampAvaticaParameterConverterTest.java | 182 ++++++++++++++++++ 2 files changed, 253 insertions(+), 34 deletions(-) create mode 100644 flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverterTest.java 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 add3e30598..b54deac242 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 @@ -16,15 +16,14 @@ */ package org.apache.arrow.driver.jdbc.converter.impl; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.util.TimeZone; import org.apache.arrow.vector.FieldVector; -import org.apache.arrow.vector.TimeStampMicroTZVector; -import org.apache.arrow.vector.TimeStampMicroVector; -import org.apache.arrow.vector.TimeStampMilliTZVector; -import org.apache.arrow.vector.TimeStampMilliVector; -import org.apache.arrow.vector.TimeStampNanoTZVector; -import org.apache.arrow.vector.TimeStampNanoVector; -import org.apache.arrow.vector.TimeStampSecTZVector; -import org.apache.arrow.vector.TimeStampSecVector; +import org.apache.arrow.vector.TimeStampVector; import org.apache.arrow.vector.types.pojo.ArrowType; import org.apache.arrow.vector.types.pojo.Field; import org.apache.calcite.avatica.AvaticaParameter; @@ -33,37 +32,75 @@ /** AvaticaParameterConverter for Timestamp Arrow types. */ public class TimestampAvaticaParameterConverter extends BaseAvaticaParameterConverter { + private static final long MILLIS_PER_SECOND = 1_000L; + private static final long MICROS_PER_SECOND = 1_000_000L; + private static final long NANOS_PER_SECOND = 1_000_000_000L; + public TimestampAvaticaParameterConverter(ArrowType.Timestamp type) {} @Override public boolean bindParameter(FieldVector vector, TypedValue typedValue, int index) { - long value = (long) typedValue.toLocal(); - if (vector instanceof TimeStampSecVector) { - ((TimeStampSecVector) vector).setSafe(index, value); - return true; - } else if (vector instanceof TimeStampMicroVector) { - ((TimeStampMicroVector) vector).setSafe(index, value); - return true; - } else if (vector instanceof TimeStampMilliVector) { - ((TimeStampMilliVector) vector).setSafe(index, value); - return true; - } else if (vector instanceof TimeStampNanoVector) { - ((TimeStampNanoVector) vector).setSafe(index, value); - return true; - } else if (vector instanceof TimeStampSecTZVector) { - ((TimeStampSecTZVector) vector).setSafe(index, value); - return true; - } else if (vector instanceof TimeStampMicroTZVector) { - ((TimeStampMicroTZVector) vector).setSafe(index, value); - return true; - } else if (vector instanceof TimeStampMilliTZVector) { - ((TimeStampMilliTZVector) vector).setSafe(index, value); - return true; - } else if (vector instanceof TimeStampNanoTZVector) { - ((TimeStampNanoTZVector) vector).setSafe(index, value); - return true; + if (!(vector instanceof TimeStampVector)) { + return false; } - return false; + final ArrowType.Timestamp type = (ArrowType.Timestamp) vector.getField().getType(); + final Instant instant = toInstant(typedValue.toLocal(), type.getTimezone()); + ((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. + */ + private static Instant toInstant(Object value, String vectorTimeZone) { + if (value instanceof LocalDateTime) { + final LocalDateTime localDateTime = (LocalDateTime) value; + return vectorTimeZone == null + ? localDateTime.toInstant(ZoneOffset.UTC) + : localDateTime.atZone(TimeZone.getTimeZone(vectorTimeZone).toZoneId()).toInstant(); + } else if (value instanceof Instant + || value instanceof OffsetDateTime + || value instanceof ZonedDateTime) { + if (vectorTimeZone == null) { + throw new UnsupportedOperationException( + String.format( + "Can't bind %s to a timestamp parameter without a time zone; use LocalDateTime.", + value.getClass().getSimpleName())); + } + if (value instanceof Instant) { + return (Instant) value; + } + return value instanceof OffsetDateTime + ? ((OffsetDateTime) value).toInstant() + : ((ZonedDateTime) value).toInstant(); + } + // Avatica passes java.sql.Timestamp and java.util.Date as milliseconds since the epoch, + // already adjusted by the offset of the connection calendar. + return Instant.ofEpochMilli((long) value); + } + + private static long toVectorValue(Instant instant, ArrowType.Timestamp type) { + final long seconds = instant.getEpochSecond(); + final long nanos = instant.getNano(); + switch (type.getUnit()) { + case SECOND: + return seconds; + case MILLISECOND: + return toUnit(seconds, nanos, MILLIS_PER_SECOND); + case MICROSECOND: + return toUnit(seconds, nanos, MICROS_PER_SECOND); + case NANOSECOND: + return toUnit(seconds, nanos, NANOS_PER_SECOND); + default: + throw new UnsupportedOperationException("Invalid Arrow time unit"); + } + } + + private static long toUnit(long seconds, long nanos, long unitsPerSecond) { + // Fails with ArithmeticException instead of binding a wrong value if it does not fit in a long + return Math.addExact( + Math.multiplyExact(seconds, unitsPerSecond), nanos / (NANOS_PER_SECOND / unitsPerSecond)); } @Override 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 new file mode 100644 index 0000000000..e39ad6945b --- /dev/null +++ b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverterTest.java @@ -0,0 +1,182 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.arrow.driver.jdbc.converter.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +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.util.Calendar; +import java.util.stream.Stream; +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.memory.RootAllocator; +import org.apache.arrow.vector.FieldVector; +import org.apache.arrow.vector.TimeStampVector; +import org.apache.arrow.vector.types.TimeUnit; +import org.apache.arrow.vector.types.pojo.ArrowType; +import org.apache.arrow.vector.types.pojo.FieldType; +import org.apache.calcite.avatica.ColumnMetaData; +import org.apache.calcite.avatica.remote.TypedValue; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +/** Tests for {@link TimestampAvaticaParameterConverter}. */ +public class TimestampAvaticaParameterConverterTest { + + private static final String NEW_YORK = "America/New_York"; + private static final LocalDateTime LOCAL_DATE_TIME = + LocalDateTime.parse("2024-03-15T12:34:56.123456789"); + // 2024-03-15T12:34:56 UTC + private static final long EPOCH_SECOND = 1_710_506_096L; + // New York is at UTC-4 on that day + private static final long NEW_YORK_EPOCH_SECOND = EPOCH_SECOND + 4 * 60 * 60; + + private BufferAllocator allocator; + + @BeforeEach + public void setUp() { + allocator = new RootAllocator(); + } + + @AfterEach + public void tearDown() { + allocator.close(); + } + + /** Time unit, and the expected vector value for 12:34:56.123456789 at the given epoch second. */ + public static Stream units() { + return Stream.of( + Arguments.of(TimeUnit.SECOND, 1L, 0L), + Arguments.of(TimeUnit.MILLISECOND, 1_000L, 123L), + Arguments.of(TimeUnit.MICROSECOND, 1_000_000L, 123_456L), + Arguments.of(TimeUnit.NANOSECOND, 1_000_000_000L, 123_456_789L)); + } + + private long bind(TimeUnit unit, String timeZone, TypedValue value) { + ArrowType.Timestamp type = new ArrowType.Timestamp(unit, timeZone); + try (FieldVector vector = FieldType.nullable(type).createNewSingleVector("", allocator, null)) { + assertTrue(new TimestampAvaticaParameterConverter(type).bindParameter(vector, value, 0)); + vector.setValueCount(1); + return ((TimeStampVector) vector).get(0); + } + } + + /** The value Avatica creates for {@code PreparedStatement.setObject(i, value)}. */ + private static TypedValue objectValue(Object value) { + return TypedValue.ofJdbc(value, Calendar.getInstance()); + } + + @ParameterizedTest + @MethodSource("units") + public void testBindSqlTimestampConvertsToVectorUnit( + TimeUnit unit, long unitsPerSecond, long fraction) { + // For PreparedStatement.setTimestamp(i, timestamp), Avatica passes the wall-clock time of the + // timestamp as milliseconds since the epoch, so nothing below milliseconds reaches the + // converter. + long millis = EPOCH_SECOND * 1_000 + 123; + TypedValue value = TypedValue.ofLocal(ColumnMetaData.Rep.JAVA_SQL_TIMESTAMP, millis); + long expected = EPOCH_SECOND * unitsPerSecond + (123L * unitsPerSecond) / 1_000; + + assertEquals(expected, bind(unit, null, value)); + assertEquals(expected, bind(unit, "UTC", value)); + assertEquals(expected, bind(unit, NEW_YORK, value)); + } + + @ParameterizedTest + @MethodSource("units") + public void testBindSqlTimestampBeforeEpoch(TimeUnit unit, long unitsPerSecond, long fraction) { + // 1969-12-31T23:59:59.500 is half a second before the epoch + TypedValue value = TypedValue.ofLocal(ColumnMetaData.Rep.JAVA_SQL_TIMESTAMP, -500L); + long expected = unit == TimeUnit.SECOND ? -1L : -unitsPerSecond / 2; + + assertEquals(expected, bind(unit, null, value)); + } + + @ParameterizedTest + @MethodSource("units") + public void testBindLocalDateTime(TimeUnit unit, long unitsPerSecond, long fraction) { + TypedValue value = objectValue(LOCAL_DATE_TIME); + + assertEquals(EPOCH_SECOND * unitsPerSecond + fraction, bind(unit, null, value)); + assertEquals(EPOCH_SECOND * unitsPerSecond + fraction, bind(unit, "UTC", value)); + // Wall-clock time in the time zone of the vector + assertEquals(NEW_YORK_EPOCH_SECOND * unitsPerSecond + fraction, bind(unit, NEW_YORK, value)); + } + + @ParameterizedTest + @MethodSource("units") + public void testBindInstantTypesToVectorWithTimeZone( + TimeUnit unit, long unitsPerSecond, long fraction) { + Instant instant = LOCAL_DATE_TIME.toInstant(ZoneOffset.UTC); + long expected = EPOCH_SECOND * unitsPerSecond + fraction; + + for (String timeZone : new String[] {"UTC", NEW_YORK}) { + assertEquals(expected, bind(unit, timeZone, objectValue(instant))); + assertEquals( + expected, + bind( + unit, + timeZone, + objectValue(OffsetDateTime.ofInstant(instant, ZoneOffset.ofHours(5))))); + assertEquals( + expected, + bind(unit, timeZone, objectValue(ZonedDateTime.ofInstant(instant, ZoneId.of(NEW_YORK))))); + } + } + + @Test + public void testBindInstantTypesToVectorWithoutTimeZone() { + Instant instant = LOCAL_DATE_TIME.toInstant(ZoneOffset.UTC); + Object[] values = { + instant, instant.atOffset(ZoneOffset.UTC), instant.atZone(ZoneId.of(NEW_YORK)) + }; + for (Object value : values) { + assertThrows( + UnsupportedOperationException.class, + () -> bind(TimeUnit.MICROSECOND, null, objectValue(value))); + } + } + + @Test + public void testBindValueThatDoesNotFitInVectorUnit() { + TypedValue value = objectValue(LocalDateTime.parse("2300-01-01T00:00:00")); + + assertEquals(10_413_792_000L, bind(TimeUnit.SECOND, null, value)); + assertThrows(ArithmeticException.class, () -> bind(TimeUnit.NANOSECOND, null, value)); + } + + @Test + public void testBindUnsupportedValue() { + assertThrows( + ClassCastException.class, + () -> + bind( + TimeUnit.MICROSECOND, + null, + TypedValue.ofLocal(ColumnMetaData.Rep.STRING, "2024-03-15 12:34:56"))); + } +} From adf1b16c01748225487167807444deb66cf00894 Mon Sep 17 00:00:00 2001 From: Ivan Chesnov Date: Tue, 1 Dec 2026 19:16:59 +0200 Subject: [PATCH 2/3] Address review feedback - Write values bound with setLong or setObject(Long) to the timestamp vector unchanged, as before, instead of reading them as milliseconds - Add a test that binds a long to each unit, with and without a time zone Co-Authored-By: Claude Fable 5.2 --- .../TimestampAvaticaParameterConverter.java | 6 ++++++ ...TimestampAvaticaParameterConverterTest.java | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) 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 b54deac242..5aa2a1c2ab 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 @@ -27,6 +27,7 @@ import org.apache.arrow.vector.types.pojo.ArrowType; import org.apache.arrow.vector.types.pojo.Field; import org.apache.calcite.avatica.AvaticaParameter; +import org.apache.calcite.avatica.ColumnMetaData; import org.apache.calcite.avatica.remote.TypedValue; /** AvaticaParameterConverter for Timestamp Arrow types. */ @@ -43,6 +44,11 @@ public boolean bindParameter(FieldVector vector, TypedValue typedValue, int inde if (!(vector instanceof TimeStampVector)) { return false; } + if (typedValue.type == ColumnMetaData.Rep.LONG) { + // setLong / setObject(Long): the value is already in the unit of the vector + ((TimeStampVector) vector).setSafe(index, (long) typedValue.toLocal()); + return true; + } final ArrowType.Timestamp type = (ArrowType.Timestamp) vector.getField().getType(); final Instant instant = toInstant(typedValue.toLocal(), type.getTimezone()); ((TimeStampVector) vector).setSafe(index, toVectorValue(instant, type)); 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 e39ad6945b..39ee5ee949 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 @@ -116,6 +116,24 @@ public void testBindSqlTimestampBeforeEpoch(TimeUnit unit, long unitsPerSecond, assertEquals(expected, bind(unit, null, value)); } + @ParameterizedTest + @MethodSource("units") + public void testBindLongIsNotConverted(TimeUnit unit, long unitsPerSecond, long fraction) { + // For PreparedStatement.setLong(i, value) and setObject(i, Long), the value is already in the + // unit of the vector. The last value does not fit in a long if it is read as milliseconds. + long[] longs = { + 0L, -1L, EPOCH_SECOND, 1_710_506_096_123L, 1_710_506_096_123_456L, 1_710_506_096_123_456_789L + }; + for (long expected : longs) { + TypedValue value = + TypedValue.ofJdbc(ColumnMetaData.Rep.LONG, expected, Calendar.getInstance()); + + assertEquals(expected, bind(unit, null, value)); + assertEquals(expected, bind(unit, "UTC", value)); + assertEquals(expected, bind(unit, NEW_YORK, value)); + } + } + @ParameterizedTest @MethodSource("units") public void testBindLocalDateTime(TimeUnit unit, long unitsPerSecond, long fraction) { From 35c0ae10979d3e36465a4f6147044f45f52b08f3 Mon Sep 17 00:00:00 2001 From: Ivan Chesnov Date: Thu, 3 Dec 2026 10:18:25 +0200 Subject: [PATCH 3/3] Address review feedback Parse numeric offsets in the time zone of a timestamp vector, and bind values at the lower bound of the vector unit without a false overflow. Co-Authored-By: Claude Fable 5.2 --- .../TimestampAvaticaParameterConverter.java | 24 ++++++-- ...imestampAvaticaParameterConverterTest.java | 56 +++++++++++++++++++ 2 files changed, 76 insertions(+), 4 deletions(-) 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 5aa2a1c2ab..9aa8f5da15 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 @@ -19,9 +19,9 @@ 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.util.TimeZone; import org.apache.arrow.vector.FieldVector; import org.apache.arrow.vector.TimeStampVector; import org.apache.arrow.vector.types.pojo.ArrowType; @@ -64,7 +64,7 @@ private static Instant toInstant(Object value, String vectorTimeZone) { final LocalDateTime localDateTime = (LocalDateTime) value; return vectorTimeZone == null ? localDateTime.toInstant(ZoneOffset.UTC) - : localDateTime.atZone(TimeZone.getTimeZone(vectorTimeZone).toZoneId()).toInstant(); + : localDateTime.atZone(toZoneId(vectorTimeZone)).toInstant(); } else if (value instanceof Instant || value instanceof OffsetDateTime || value instanceof ZonedDateTime) { @@ -86,6 +86,16 @@ private static Instant toInstant(Object value, String vectorTimeZone) { return Instant.ofEpochMilli((long) value); } + /** + * Parses the time zone of a timestamp vector, which is either the name of a time zone, such as + * {@code America/New_York}, or an offset from UTC, such as {@code +03:00}. + */ + private static ZoneId toZoneId(String vectorTimeZone) { + // TimeZone.getTimeZone is not used here, because it turns offsets such as "+03:00" and unknown + // names into GMT instead of failing + return ZoneId.of(vectorTimeZone, ZoneId.SHORT_IDS); + } + private static long toVectorValue(Instant instant, ArrowType.Timestamp type) { final long seconds = instant.getEpochSecond(); final long nanos = instant.getNano(); @@ -105,8 +115,14 @@ private static long toVectorValue(Instant instant, ArrowType.Timestamp type) { private static long toUnit(long seconds, long nanos, long unitsPerSecond) { // Fails with ArithmeticException instead of binding a wrong value if it does not fit in a long - return Math.addExact( - Math.multiplyExact(seconds, unitsPerSecond), nanos / (NANOS_PER_SECOND / unitsPerSecond)); + final long fraction = nanos / (NANOS_PER_SECOND / unitsPerSecond); + if (seconds < 0 && fraction > 0) { + // Count back from the next second, so that a result close to Long.MIN_VALUE does not overflow + // in the multiplication before the fraction is added + return Math.addExact( + Math.multiplyExact(seconds + 1, unitsPerSecond), fraction - unitsPerSecond); + } + return Math.addExact(Math.multiplyExact(seconds, unitsPerSecond), fraction); } @Override 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 39ee5ee949..7bafc4e0f6 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 @@ -20,6 +20,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.time.DateTimeException; import java.time.Instant; import java.time.LocalDateTime; import java.time.OffsetDateTime; @@ -145,6 +146,23 @@ public void testBindLocalDateTime(TimeUnit unit, long unitsPerSecond, long fract assertEquals(NEW_YORK_EPOCH_SECOND * unitsPerSecond + fraction, bind(unit, NEW_YORK, value)); } + @Test + public void testBindLocalDateTimeToVectorWithOffsetTimeZone() { + TypedValue value = objectValue(LocalDateTime.parse("2024-03-15T12:00:00")); + long noon = Instant.parse("2024-03-15T12:00:00Z").getEpochSecond(); + + assertEquals(noon - 3 * 60 * 60, bind(TimeUnit.SECOND, "+03:00", value)); + assertEquals(noon + 5 * 60 * 60 + 30 * 60, bind(TimeUnit.SECOND, "-05:30", value)); + assertEquals(noon, bind(TimeUnit.SECOND, "+00:00", value)); + } + + @Test + public void testBindLocalDateTimeToVectorWithUnknownTimeZone() { + TypedValue value = objectValue(LOCAL_DATE_TIME); + + assertThrows(DateTimeException.class, () -> bind(TimeUnit.SECOND, "Not/AZone", value)); + } + @ParameterizedTest @MethodSource("units") public void testBindInstantTypesToVectorWithTimeZone( @@ -187,6 +205,44 @@ public void testBindValueThatDoesNotFitInVectorUnit() { assertThrows(ArithmeticException.class, () -> bind(TimeUnit.NANOSECOND, null, value)); } + /** Time unit and the number of nanoseconds in it. */ + public static Stream subSecondUnits() { + return Stream.of( + Arguments.of(TimeUnit.MILLISECOND, 1_000_000L), + Arguments.of(TimeUnit.MICROSECOND, 1_000L), + Arguments.of(TimeUnit.NANOSECOND, 1L)); + } + + @ParameterizedTest + @MethodSource("subSecondUnits") + public void testBindSmallestAndLargestValueOfVectorUnit(TimeUnit unit, long nanosPerUnit) { + long unitsPerSecond = 1_000_000_000L / nanosPerUnit; + for (long value : new long[] {Long.MIN_VALUE, Long.MIN_VALUE + 1, -1L, Long.MAX_VALUE}) { + Instant instant = + Instant.ofEpochSecond( + Math.floorDiv(value, unitsPerSecond), + Math.floorMod(value, unitsPerSecond) * nanosPerUnit); + + assertEquals(value, bind(unit, "UTC", objectValue(instant))); + } + + Instant smallest = + Instant.ofEpochSecond( + Math.floorDiv(Long.MIN_VALUE, unitsPerSecond), + Math.floorMod(Long.MIN_VALUE, unitsPerSecond) * nanosPerUnit); + Instant largest = + Instant.ofEpochSecond( + Math.floorDiv(Long.MAX_VALUE, unitsPerSecond), + Math.floorMod(Long.MAX_VALUE, unitsPerSecond) * nanosPerUnit); + // One unit outside of the range of a long + assertThrows( + ArithmeticException.class, + () -> bind(unit, "UTC", objectValue(smallest.minusNanos(nanosPerUnit)))); + assertThrows( + ArithmeticException.class, + () -> bind(unit, "UTC", objectValue(largest.plusNanos(nanosPerUnit)))); + } + @Test public void testBindUnsupportedValue() { assertThrows(