xxlaykxx commented on code in PR #1542:
URL: https://github.com/apache/arrow-java/pull/1542#discussion_r4758059096


##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverter.java:
##########
@@ -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);

Review Comment:
   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.



-- 
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]

Reply via email to