xxlaykxx commented on code in PR #1542:
URL: https://github.com/apache/arrow-java/pull/1542#discussion_r4781759696
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverter.java:
##########
@@ -16,54 +16,97 @@
*/
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;
+import org.apache.calcite.avatica.ColumnMetaData;
import org.apache.calcite.avatica.remote.TypedValue;
/** 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);
+ 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;
}
- 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();
Review Comment:
Good catch, fixed in the latest push. The vector time zone is now parsed
with `ZoneId.of(vectorTimeZone, ZoneId.SHORT_IDS)`, so `+03:00` and `-05:30`
are applied (I added both as test cases, with your `2024-03-15T12:00` input).
One side effect: an unknown zone name now fails with a `DateTimeException`
rather than silently binding as GMT.
The read side
(`ArrowFlightJdbcTimeStampVectorAccessor.getTimeZoneForVector`) still uses
`TimeZone.getTimeZone` and has the same problem with numeric offsets. I left it
out of this PR since it is not part of parameter binding, and can open a
separate issue for it.
##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverter.java:
##########
@@ -16,54 +16,97 @@
*/
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;
+import org.apache.calcite.avatica.ColumnMetaData;
import org.apache.calcite.avatica.remote.TypedValue;
/** 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);
+ 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;
}
- 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));
Review Comment:
Fixed in the latest push, using the form you suggested: for negative seconds
with a non-zero fraction the value is computed as `(seconds + 1) *
unitsPerSecond + (fraction - unitsPerSecond)` with the exact arithmetic
helpers. The new test binds `Long.MIN_VALUE`, `Long.MIN_VALUE + 1`, `-1` and
`Long.MAX_VALUE` for milliseconds, microseconds and nanoseconds, and checks
that one unit outside either bound still throws `ArithmeticException`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]