Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,113 @@
*/
package org.apache.arrow.driver.jdbc.converter.impl;

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 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(toZoneId(vectorTimeZone)).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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This fallback also catches values bound with setLong and setObject(Long), not only java.sql.Timestamp and java.util.Date. Avatica passes those as Rep.LONG. On main, they were written to the vector unchanged, so setLong was the only way to bind a correct second, microsecond or nanosecond value. With this change they are read as epoch millis:

Parameter Call main This PR
Timestamp(MICROSECOND) setLong(1, 1_710_506_096_123_456L) 1710506096123456 1710506096123456000 (year 56173)
Timestamp(SECOND) setLong(1, 1_710_506_096L) 1710506096 1710506 (1970-01-20)
Timestamp(NANOSECOND) setLong(1, 1_710_506_096_123_456_789L) stored unchanged ArithmeticException: long overflow

Clients using setLong as a workaround for #1540 would silently start writing wrong values for the first two.

I would suggest keeping longs unchanged, since they are already in the vector's unit, and only converting the date/time types. In bindParameter, before toInstant:

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;
}

A test that binds TypedValue.ofJdbc(ColumnMetaData.Rep.LONG, ...) to each unit would cover it. If you would rather keep the epoc-millis reading for longs, which matches Avatica's own setObject(Long, Types.TIMESTAMP), that works for me too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch — fixed in the latest push. Rep.LONG values are now written to the vector unchanged, as on main, and only the date/time types are converted. I added a test that binds a long to each unit, with and without a timezone, and noted it in the PR description.

}

/**
* 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();
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
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
Expand Down
Loading
Loading