You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "dmitry-chirkov-dremio (via GitHub)" <gi...@apache.org> on 2026/03/21 00:25:28 UTC

[PR] GH-838: [FlightSQL][JDBC] Fix timestamp unit conversion in PreparedStatement parameter binding [arrow-java]

dmitry-chirkov-dremio opened a new pull request, #1081:
URL: https://github.com/apache/arrow-java/pull/1081

   The FlightSQL JDBC driver's `TimestampAvaticaParameterConverter` writes raw epoch millisecond values (received from Avatica) directly into Arrow timestamp vectors regardless of the vector's time unit. This causes data corruption when the server uses SECOND, MICROSECOND, or NANOSECOND precision - the millisecond value is misinterpreted as the target unit.
   
   ## What's Changed
   
   - Modified `TimestampAvaticaParameterConverter` to store the `ArrowType.Timestamp` from its constructor and use the target time unit to scale the epoch millisecond value before writing to the vector:
     - SECOND: divide by 1,000
     - MILLISECOND: pass through
     - MICROSECOND: multiply by 1,000
     - NANOSECOND: multiply by 1,000,000
   - Added unit tests covering all 8 timestamp vector types (4 units × with/without timezone)
   
   **This contains breaking changes.**
   Yes. `PreparedStatement#setTimestamp()` now correctly converts timestamp values for non-millisecond precision columns. Previously, timestamps inserted via the FlightSQL JDBC driver into SECOND, MICROSECOND, or NANOSECOND columns were silently corrupted.
   
   **This PR contains a "Critical Fix".** The bug caused incorrect data to be stored on the server when using `PreparedStatement#setTimestamp()` with any timestamp precision other than milliseconds.
   
   Closes #838 
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-838: [FlightSQL][JDBC] Fix timestamp unit conversion and sub-millisecond precision in PreparedStatement parameter binding [arrow-java]

Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on PR #1081:
URL: https://github.com/apache/arrow-java/pull/1081#issuecomment-5033598805

   @vandop I'm on it. 


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-838: [FlightSQL][JDBC] Fix timestamp unit conversion and sub-millisecond precision in PreparedStatement parameter binding [arrow-java]

Posted by "vandop (via GitHub)" <gi...@apache.org>.
vandop commented on PR #1081:
URL: https://github.com/apache/arrow-java/pull/1081#issuecomment-5033151770

   @dmitry-chirkov-dremio / @xborder are we still blocked on this one? can we get it going?


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-838: [FlightSQL][JDBC] Fix timestamp unit conversion and sub-millisecond precision in PreparedStatement parameter binding [arrow-java]

Posted by "dmitry-chirkov-dremio (via GitHub)" <gi...@apache.org>.
dmitry-chirkov-dremio commented on code in PR #1081:
URL: https://github.com/apache/arrow-java/pull/1081#discussion_r2969799814


##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverter.java:
##########
@@ -33,11 +35,79 @@
 /** AvaticaParameterConverter for Timestamp Arrow types. */
 public class TimestampAvaticaParameterConverter extends BaseAvaticaParameterConverter {
 
-  public TimestampAvaticaParameterConverter(ArrowType.Timestamp type) {}
+  private final ArrowType.Timestamp type;
+
+  public TimestampAvaticaParameterConverter(ArrowType.Timestamp type) {
+    this.type = type;
+  }
+
+  /**
+   * Converts a raw java.sql.Timestamp to the value in the target Arrow time unit, preserving
+   * sub-millisecond precision from Timestamp.getNanos().
+   */
+  private long convertFromTimestamp(Timestamp ts) {
+    // Timestamp.getTime() returns epoch millis (truncated, no sub-ms precision).
+    // Timestamp.getNanos() returns the fractional-second component in nanoseconds (0..999_999_999).
+    // We reconstruct the full-precision value from epoch seconds + nanos to avoid double-counting.
+    long epochSeconds = Math.floorDiv(ts.getTime(), 1_000L);
+    int nanos = ts.getNanos(); // 0..999_999_999, full fractional second
+    switch (type.getUnit()) {
+      case SECOND:
+        return epochSeconds;
+      case MILLISECOND:
+        return epochSeconds * 1_000L + nanos / 1_000_000;
+      case MICROSECOND:
+        return epochSeconds * 1_000_000L + nanos / 1_000;
+      case NANOSECOND:
+        return epochSeconds * 1_000_000_000L + nanos;
+      default:
+        throw new UnsupportedOperationException("Unsupported time unit: " + type.getUnit());
+    }
+  }
+
+  /** Converts an epoch millisecond value from Avatica to the target time unit. */
+  private long convertFromMillis(long epochMillis) {
+    switch (type.getUnit()) {
+      case SECOND:
+        return epochMillis / 1_000L;

Review Comment:
   @jbonofre @xborder addressed your feedback in 5th commit



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-838: [FlightSQL][JDBC] Fix timestamp unit conversion and sub-millisecond precision in PreparedStatement parameter binding [arrow-java]

Posted by "xborder (via GitHub)" <gi...@apache.org>.
xborder commented on PR #1081:
URL: https://github.com/apache/arrow-java/pull/1081#issuecomment-4126081049

   I think there's still a risk of having stale parameters.
   Example:
   ```
   Statement st = Statement.createStatement("SELECT * FROM table WHERE ts = ?")
   Timestamp ts = new Timestamp(TEST_EPOCH_MILLIS_WITH_FRACTIONAL_SECONDS);
       ts.setNanos(869885001);
   st.setTimestamp(1, ts);
   st.Long(1, 1774261392);
   ```
   
   The final query should be `SELECT * FROM table WHERE ts = 1774261392` and I think this will fetch the raw timestamps instead.
   
   The issue is mainly with the approach taken. Having a map of index to timestamp means that there are other scenarios where the index can be updated but the map isn't unless we override all the setters functions to keep it up-to-date. I'm not sure what is the best approach here.
   In the long run fixing the precision in Avatica would be ideal (I believe that is where the root-cause is)


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-838: [FlightSQL][JDBC] Fix timestamp unit conversion and sub-millisecond precision in PreparedStatement parameter binding [arrow-java]

Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #1081:
URL: https://github.com/apache/arrow-java/pull/1081#discussion_r2969142430


##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverter.java:
##########
@@ -33,11 +35,79 @@
 /** AvaticaParameterConverter for Timestamp Arrow types. */
 public class TimestampAvaticaParameterConverter extends BaseAvaticaParameterConverter {
 
-  public TimestampAvaticaParameterConverter(ArrowType.Timestamp type) {}
+  private final ArrowType.Timestamp type;
+
+  public TimestampAvaticaParameterConverter(ArrowType.Timestamp type) {
+    this.type = type;
+  }
+
+  /**
+   * Converts a raw java.sql.Timestamp to the value in the target Arrow time unit, preserving
+   * sub-millisecond precision from Timestamp.getNanos().
+   */
+  private long convertFromTimestamp(Timestamp ts) {
+    // Timestamp.getTime() returns epoch millis (truncated, no sub-ms precision).
+    // Timestamp.getNanos() returns the fractional-second component in nanoseconds (0..999_999_999).
+    // We reconstruct the full-precision value from epoch seconds + nanos to avoid double-counting.
+    long epochSeconds = Math.floorDiv(ts.getTime(), 1_000L);
+    int nanos = ts.getNanos(); // 0..999_999_999, full fractional second
+    switch (type.getUnit()) {
+      case SECOND:
+        return epochSeconds;
+      case MILLISECOND:
+        return epochSeconds * 1_000L + nanos / 1_000_000;
+      case MICROSECOND:
+        return epochSeconds * 1_000_000L + nanos / 1_000;
+      case NANOSECOND:
+        return epochSeconds * 1_000_000_000L + nanos;
+      default:
+        throw new UnsupportedOperationException("Unsupported time unit: " + type.getUnit());
+    }
+  }
+
+  /** Converts an epoch millisecond value from Avatica to the target time unit. */
+  private long convertFromMillis(long epochMillis) {
+    switch (type.getUnit()) {
+      case SECOND:
+        return epochMillis / 1_000L;

Review Comment:
   `epochMillis / 1_000L` truncates toward zero. It means that -999ms will end as 0 instead of -1.
   This differs from the `convertFromTimestamp` method which uses `Math.floorDiv`.
   
   I suggest to use `Math.floorDiv(epochMillis, 1_000L)` here.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-838: [FlightSQL][JDBC] Fix timestamp unit conversion and sub-millisecond precision in PreparedStatement parameter binding [arrow-java]

Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #1081:
URL: https://github.com/apache/arrow-java/pull/1081#discussion_r2969807911


##########
flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/TimestampAvaticaParameterConverter.java:
##########
@@ -33,11 +35,79 @@
 /** AvaticaParameterConverter for Timestamp Arrow types. */
 public class TimestampAvaticaParameterConverter extends BaseAvaticaParameterConverter {
 
-  public TimestampAvaticaParameterConverter(ArrowType.Timestamp type) {}
+  private final ArrowType.Timestamp type;
+
+  public TimestampAvaticaParameterConverter(ArrowType.Timestamp type) {
+    this.type = type;
+  }
+
+  /**
+   * Converts a raw java.sql.Timestamp to the value in the target Arrow time unit, preserving
+   * sub-millisecond precision from Timestamp.getNanos().
+   */
+  private long convertFromTimestamp(Timestamp ts) {
+    // Timestamp.getTime() returns epoch millis (truncated, no sub-ms precision).
+    // Timestamp.getNanos() returns the fractional-second component in nanoseconds (0..999_999_999).
+    // We reconstruct the full-precision value from epoch seconds + nanos to avoid double-counting.
+    long epochSeconds = Math.floorDiv(ts.getTime(), 1_000L);
+    int nanos = ts.getNanos(); // 0..999_999_999, full fractional second
+    switch (type.getUnit()) {
+      case SECOND:
+        return epochSeconds;
+      case MILLISECOND:
+        return epochSeconds * 1_000L + nanos / 1_000_000;
+      case MICROSECOND:
+        return epochSeconds * 1_000_000L + nanos / 1_000;
+      case NANOSECOND:
+        return epochSeconds * 1_000_000_000L + nanos;
+      default:
+        throw new UnsupportedOperationException("Unsupported time unit: " + type.getUnit());
+    }
+  }
+
+  /** Converts an epoch millisecond value from Avatica to the target time unit. */
+  private long convertFromMillis(long epochMillis) {
+    switch (type.getUnit()) {
+      case SECOND:
+        return epochMillis / 1_000L;

Review Comment:
   @dmitry-chirkov-dremio thanks. I do a new pass.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-838: [FlightSQL][JDBC] Fix timestamp unit conversion and sub-millisecond precision in PreparedStatement parameter binding [arrow-java]

Posted by "dmitry-chirkov-dremio (via GitHub)" <gi...@apache.org>.
dmitry-chirkov-dremio commented on PR #1081:
URL: https://github.com/apache/arrow-java/pull/1081#issuecomment-4102193211

   There might be a preexisting lack of timezone offset handling somewhere near this path however its not relevant to the issue nor the fix.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-838: [FlightSQL][JDBC] Fix timestamp unit conversion in PreparedStatement parameter binding [arrow-java]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #1081:
URL: https://github.com/apache/arrow-java/pull/1081#issuecomment-4101599008

   
   <!-- label_helper_comment -->
   Thank you for opening a pull request!
   
   Please label the PR with one or more of:
   
   - bug-fix
   - chore
   - dependencies
   - documentation
   - enhancement
   
   Also, add the 'breaking-change' label if appropriate.
   
   See [CONTRIBUTING.md](https://github.com/apache/arrow-java/blob/main/CONTRIBUTING.md) for details.
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-838: [FlightSQL][JDBC] Fix timestamp unit conversion and sub-millisecond precision in PreparedStatement parameter binding [arrow-java]

Posted by "dmitry-chirkov-dremio (via GitHub)" <gi...@apache.org>.
dmitry-chirkov-dremio commented on PR #1081:
URL: https://github.com/apache/arrow-java/pull/1081#issuecomment-4136149069

   Putting this back to draft as @xborder and I are looking into synchronization/staleness issues introduced by this approach.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org