You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "aiguofer (via GitHub)" <gi...@apache.org> on 2023/08/09 07:41:04 UTC

[GitHub] [arrow] aiguofer opened a new pull request, #37088: GH-37021 [Java][arrow-jdbc] Support converting JDBC TIMESTAMP_WITH_TIMEZONE to Arrow

aiguofer opened a new pull request, #37088:
URL: https://github.com/apache/arrow/pull/37088

   ### Rationale for this change
   
   We want to support converting `TIMESTAMP_WITH_TIMEZONE` JDBC fields into Arrow.
   
   ### What changes are included in this PR?
   
   Convert `TIMESTAMP_WITH_TIMEZONE` fields into Arrow, and include the provided timezone information.
   Ensure timezone information is not included in arrow object for regular `TIMESTAMP` fields.
   
   ### Are there any user-facing changes?
   
   Potentially, depending on how users are configuring the calendar.
   
   
   This might also be related to https://github.com/apache/arrow/pull/36519 since we need to ensure handling of timestamps is consistent when converting either jdbc -> arrow or arrow -> jdbc.  


-- 
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-37118 [Java][arrow-jdbc] Support converting JDBC TIMESTAMP_WITH_TIMEZONE to Arrow [arrow]

Posted by "thisisnic (via GitHub)" <gi...@apache.org>.
thisisnic closed pull request #37088: GH-37118 [Java][arrow-jdbc] Support converting JDBC TIMESTAMP_WITH_TIMEZONE to Arrow
URL: https://github.com/apache/arrow/pull/37088


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


[GitHub] [arrow] lidavidm commented on a diff in pull request #37088: GH-37021 [Java][arrow-jdbc] Support converting JDBC TIMESTAMP_WITH_TIMEZONE to Arrow

Posted by "lidavidm (via GitHub)" <gi...@apache.org>.
lidavidm commented on code in PR #37088:
URL: https://github.com/apache/arrow/pull/37088#discussion_r1288415073


##########
java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowUtils.java:
##########
@@ -188,12 +188,9 @@ public static ArrowType getArrowTypeFromJdbcType(final JdbcFieldInfo fieldInfo,
       case Types.TIME:
         return new ArrowType.Time(TimeUnit.MILLISECOND, 32);
       case Types.TIMESTAMP:
-        final String timezone;
-        if (calendar != null) {
-          timezone = calendar.getTimeZone().getID();
-        } else {
-          timezone = null;
-        }
+        return new ArrowType.Timestamp(TimeUnit.MILLISECOND);
+      case Types.TIMESTAMP_WITH_TIMEZONE:
+        final String timezone = calendar == null ? null : calendar.getTimeZone().getID();

Review Comment:
   Hmm, there is still an underlying timezone, right? I guess we just don't know without writing database-specific code. It might be better to error in this case?



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


[GitHub] [arrow] lidavidm commented on a diff in pull request #37088: GH-37021 [Java][arrow-jdbc] Support converting JDBC TIMESTAMP_WITH_TIMEZONE to Arrow

Posted by "lidavidm (via GitHub)" <gi...@apache.org>.
lidavidm commented on code in PR #37088:
URL: https://github.com/apache/arrow/pull/37088#discussion_r1290072459


##########
java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowUtils.java:
##########
@@ -188,12 +188,9 @@ public static ArrowType getArrowTypeFromJdbcType(final JdbcFieldInfo fieldInfo,
       case Types.TIME:
         return new ArrowType.Time(TimeUnit.MILLISECOND, 32);
       case Types.TIMESTAMP:
-        final String timezone;
-        if (calendar != null) {
-          timezone = calendar.getTimeZone().getID();
-        } else {
-          timezone = null;
-        }
+        return new ArrowType.Timestamp(TimeUnit.MILLISECOND);
+      case Types.TIMESTAMP_WITH_TIMEZONE:
+        final String timezone = calendar == null ? null : calendar.getTimeZone().getID();

Review Comment:
   Ok, so Snowflake:
   
   > TIMESTAMP_LTZ internally stores UTC time with a specified precision. However, all operations are performed in the current session’s time zone, controlled by the [TIMEZONE](https://docs.snowflake.com/en/sql-reference/parameters.html#label-timezone) session parameter.
   
   > TIMESTAMP_TZ internally stores UTC time together with an associated time zone offset. When a time zone is not provided, the session time zone offset is used. All operations are performed with the time zone offset specific to each record.
   
   Postgres:
   
   > All timezone-aware dates and times are stored internally in UTC.
   
   So the underlying value should be UTC. I think you'll have to look at what their JDBC driver specifically does, though: it might localize the value for you. I think you may need database-specific converters?



##########
java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowUtils.java:
##########
@@ -188,12 +188,9 @@ public static ArrowType getArrowTypeFromJdbcType(final JdbcFieldInfo fieldInfo,
       case Types.TIME:
         return new ArrowType.Time(TimeUnit.MILLISECOND, 32);
       case Types.TIMESTAMP:
-        final String timezone;
-        if (calendar != null) {
-          timezone = calendar.getTimeZone().getID();
-        } else {
-          timezone = null;
-        }
+        return new ArrowType.Timestamp(TimeUnit.MILLISECOND);
+      case Types.TIMESTAMP_WITH_TIMEZONE:
+        final String timezone = calendar == null ? null : calendar.getTimeZone().getID();

Review Comment:
   Ok, so Snowflake:
   
   > TIMESTAMP_LTZ internally stores UTC time with a specified precision. However, all operations are performed in the current session’s time zone, controlled by the [TIMEZONE](https://docs.snowflake.com/en/sql-reference/parameters.html#label-timezone) session parameter.
   
   > TIMESTAMP_TZ internally stores UTC time together with an associated time zone offset. When a time zone is not provided, the session time zone offset is used. All operations are performed with the time zone offset specific to each record.
   
   Postgres:
   
   > All timezone-aware dates and times are stored internally in UTC.
   
   So the underlying value should be UTC. I think you'll have to look at what their JDBC driver specifically does, though: it might localize the value for you. I think you may need database-specific converters?



-- 
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-37118 [Java][arrow-jdbc] Support converting JDBC TIMESTAMP_WITH_TIMEZONE to Arrow [arrow]

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

   Thank you for your contribution. Unfortunately, this pull request has been marked as stale because it has had no activity in the past 365 days. Please remove the stale label or comment below, or this PR will be closed in 14 days.  Feel free to re-open this if it has been closed in error. If you do not have repository permissions to reopen the PR, please tag a maintainer.


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


[GitHub] [arrow] lidavidm commented on a diff in pull request #37088: GH-37021 [Java][arrow-jdbc] Support converting JDBC TIMESTAMP_WITH_TIMEZONE to Arrow

Posted by "lidavidm (via GitHub)" <gi...@apache.org>.
lidavidm commented on code in PR #37088:
URL: https://github.com/apache/arrow/pull/37088#discussion_r1290558064


##########
java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowUtils.java:
##########
@@ -188,12 +188,9 @@ public static ArrowType getArrowTypeFromJdbcType(final JdbcFieldInfo fieldInfo,
       case Types.TIME:
         return new ArrowType.Time(TimeUnit.MILLISECOND, 32);
       case Types.TIMESTAMP:
-        final String timezone;
-        if (calendar != null) {
-          timezone = calendar.getTimeZone().getID();
-        } else {
-          timezone = null;
-        }
+        return new ArrowType.Timestamp(TimeUnit.MILLISECOND);
+      case Types.TIMESTAMP_WITH_TIMEZONE:
+        final String timezone = calendar == null ? null : calendar.getTimeZone().getID();

Review Comment:
   I ran into the same issue: https://github.com/apache/arrow/issues/35916 
   
   So agreed, we need more info than what JdbcFieldInfo has. (ADBC's Java adapter already has a workaround to add the extra fields.) And yeah, I think trying to get OffsetDateTime or similar would be better.
   
   That's part of why I want to vendor this into the ADBC driver, iterate on it + test it against actual databases, and then maybe send the changes back...



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


[GitHub] [arrow] aiguofer commented on a diff in pull request #37088: GH-37021 [Java][arrow-jdbc] Support converting JDBC TIMESTAMP_WITH_TIMEZONE to Arrow

Posted by "aiguofer (via GitHub)" <gi...@apache.org>.
aiguofer commented on code in PR #37088:
URL: https://github.com/apache/arrow/pull/37088#discussion_r1288901368


##########
java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowUtils.java:
##########
@@ -188,12 +188,9 @@ public static ArrowType getArrowTypeFromJdbcType(final JdbcFieldInfo fieldInfo,
       case Types.TIME:
         return new ArrowType.Time(TimeUnit.MILLISECOND, 32);
       case Types.TIMESTAMP:
-        final String timezone;
-        if (calendar != null) {
-          timezone = calendar.getTimeZone().getID();
-        } else {
-          timezone = null;
-        }
+        return new ArrowType.Timestamp(TimeUnit.MILLISECOND);
+      case Types.TIMESTAMP_WITH_TIMEZONE:
+        final String timezone = calendar == null ? null : calendar.getTimeZone().getID();

Review Comment:
   Yeah this one is tough. As I understand it, `TIMESTAMP_WITH_TIMEZONE` means that each record includes its own timezone as a UTC offset (or at least, that seems to be the case with Snowflake). Based on the JDBC spec, `ResultSet.getTimestamp(int columnIndex, Calendar cal)` does the following `This method uses the given calendar to construct an appropriate millisecond value for the timestamp if the underlying database does not store timezone information.`. This leads me to believe that it parses out the offset from the underlying db and uses that to convert to UTC. If no offset is available, it assumes the timestamp is in w/e is passed in the `calendar` and then converts it to UTC. Here's an interesting SO on the topic: https://stackoverflow.com/a/63078938/1815486
   
   From my understanding, `ArrowType.Timestamp` does not support per record timezones and it does not do any conversions on its own. It seems like the timezone is associated with the Vector itself and all records are expected to be in that same timezone. This means we must convert `TIMESTAMP_WITH_TIMEZONE` values into one specific TZ before we add them to the vector. It's also not exactly clear what it means when the TZ is `null` (I'm assuming this means it's just a "wall clock" time).
   
   This all leaves some questions:
   - What is the TZ associated with `calendar` supposed to represent for these functions? 
     - The TZ to assume the underlying values are in
     - The TZ to report on the ArrowType
     - The TZ that we should convert values to
     
   There's some important connotations because it depends on whether one is using the arrow-jdbc library to convert values server side, where it's generally safe to use/assume UTC for a lot of things, or client side, where they might want to express values in the user's timezone. 



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


[GitHub] [arrow] lidavidm commented on a diff in pull request #37088: GH-37021 [Java][arrow-jdbc] Support converting JDBC TIMESTAMP_WITH_TIMEZONE to Arrow

Posted by "lidavidm (via GitHub)" <gi...@apache.org>.
lidavidm commented on code in PR #37088:
URL: https://github.com/apache/arrow/pull/37088#discussion_r1288960693


##########
java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowUtils.java:
##########
@@ -188,12 +188,9 @@ public static ArrowType getArrowTypeFromJdbcType(final JdbcFieldInfo fieldInfo,
       case Types.TIME:
         return new ArrowType.Time(TimeUnit.MILLISECOND, 32);
       case Types.TIMESTAMP:
-        final String timezone;
-        if (calendar != null) {
-          timezone = calendar.getTimeZone().getID();
-        } else {
-          timezone = null;
-        }
+        return new ArrowType.Timestamp(TimeUnit.MILLISECOND);
+      case Types.TIMESTAMP_WITH_TIMEZONE:
+        final String timezone = calendar == null ? null : calendar.getTimeZone().getID();

Review Comment:
   In Arrow, the underlying representation for a timestamp with timezone is always UTC. So if the driver is giving us UTC and converting it, we should always bypass their conversion and construct a `timestamp[ms, UTC]`.
   
   And yes, if there's no timezone, then it's a wall-clock time.



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


[GitHub] [arrow] aiguofer commented on a diff in pull request #37088: GH-37021 [Java][arrow-jdbc] Support converting JDBC TIMESTAMP_WITH_TIMEZONE to Arrow

Posted by "aiguofer (via GitHub)" <gi...@apache.org>.
aiguofer commented on code in PR #37088:
URL: https://github.com/apache/arrow/pull/37088#discussion_r1290470152


##########
java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowUtils.java:
##########
@@ -188,12 +188,9 @@ public static ArrowType getArrowTypeFromJdbcType(final JdbcFieldInfo fieldInfo,
       case Types.TIME:
         return new ArrowType.Time(TimeUnit.MILLISECOND, 32);
       case Types.TIMESTAMP:
-        final String timezone;
-        if (calendar != null) {
-          timezone = calendar.getTimeZone().getID();
-        } else {
-          timezone = null;
-        }
+        return new ArrowType.Timestamp(TimeUnit.MILLISECOND);
+      case Types.TIMESTAMP_WITH_TIMEZONE:
+        final String timezone = calendar == null ? null : calendar.getTimeZone().getID();

Review Comment:
   Hmmm maybe we can change the consumer to do something smarter than `getTimestamp`. [This SO](https://stackoverflow.com/a/73092871/1815486) suggests `OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ; `.
   
   So for TZ aware timestamps we could use ^ and if no offset is available assume UTC. I think that would work for most things, but it'd get tricky for something like `TIMESTAMP_LTZ`. I wonder how that even comes across through the JDBC driver? I imagine it's still of type `TIMESTAMP_WITH_TIMEZONE` just like `TIMESTAMP_TZ`. We might want to add the value from `rsmd.getColumnTypeName` to the `JdbcFieldInfo` as well so users can disambiguate between the two if they need to write a custom converter.



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


[GitHub] [arrow] lidavidm commented on a diff in pull request #37088: GH-37021 [Java][arrow-jdbc] Support converting JDBC TIMESTAMP_WITH_TIMEZONE to Arrow

Posted by "lidavidm (via GitHub)" <gi...@apache.org>.
lidavidm commented on code in PR #37088:
URL: https://github.com/apache/arrow/pull/37088#discussion_r1290716134


##########
java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowUtils.java:
##########
@@ -188,12 +188,9 @@ public static ArrowType getArrowTypeFromJdbcType(final JdbcFieldInfo fieldInfo,
       case Types.TIME:
         return new ArrowType.Time(TimeUnit.MILLISECOND, 32);
       case Types.TIMESTAMP:
-        final String timezone;
-        if (calendar != null) {
-          timezone = calendar.getTimeZone().getID();
-        } else {
-          timezone = null;
-        }
+        return new ArrowType.Timestamp(TimeUnit.MILLISECOND);
+      case Types.TIMESTAMP_WITH_TIMEZONE:
+        final String timezone = calendar == null ? null : calendar.getTimeZone().getID();

Review Comment:
   Yeah. Though realistically, I don't have the time to do that right now so thank you for fixing up these things :sweat_smile: 



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


[GitHub] [arrow] github-actions[bot] commented on pull request #37088: GH-37118 [Java][arrow-jdbc] Support converting JDBC TIMESTAMP_WITH_TIMEZONE to Arrow

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

   :warning: GitHub issue #37118 **has been automatically assigned in GitHub** to PR creator.


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


[GitHub] [arrow] aiguofer commented on a diff in pull request #37088: GH-37021 [Java][arrow-jdbc] Support converting JDBC TIMESTAMP_WITH_TIMEZONE to Arrow

Posted by "aiguofer (via GitHub)" <gi...@apache.org>.
aiguofer commented on code in PR #37088:
URL: https://github.com/apache/arrow/pull/37088#discussion_r1290470152


##########
java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowUtils.java:
##########
@@ -188,12 +188,9 @@ public static ArrowType getArrowTypeFromJdbcType(final JdbcFieldInfo fieldInfo,
       case Types.TIME:
         return new ArrowType.Time(TimeUnit.MILLISECOND, 32);
       case Types.TIMESTAMP:
-        final String timezone;
-        if (calendar != null) {
-          timezone = calendar.getTimeZone().getID();
-        } else {
-          timezone = null;
-        }
+        return new ArrowType.Timestamp(TimeUnit.MILLISECOND);
+      case Types.TIMESTAMP_WITH_TIMEZONE:
+        final String timezone = calendar == null ? null : calendar.getTimeZone().getID();

Review Comment:
   Hmmm maybe we can change the consumer to do something smarter than `getTimestamp`. [This SO](https://stackoverflow.com/a/73092871/1815486) suggests `OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ; `.
   
   So for TZ aware timestamps we could use ^ and if no offset is available assume UTC. I think that would work for most things, but it'd get tricky for something like `TIMESTAMP_LTZ`. I wonder how that even comes across through the JDBC driver? I imagine it's still of type `TIMESTAMP_WITH_TIMEZONE` just like `TIMESTAMP_TZ`. We might want to add the value from `rsmd.getColumnTypeName` to the `JdbcFieldInfo` as well so users can disambiguate between the two if they need to write a custom converter.



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


[GitHub] [arrow] aiguofer commented on a diff in pull request #37088: GH-37021 [Java][arrow-jdbc] Support converting JDBC TIMESTAMP_WITH_TIMEZONE to Arrow

Posted by "aiguofer (via GitHub)" <gi...@apache.org>.
aiguofer commented on code in PR #37088:
URL: https://github.com/apache/arrow/pull/37088#discussion_r1290714028


##########
java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowUtils.java:
##########
@@ -188,12 +188,9 @@ public static ArrowType getArrowTypeFromJdbcType(final JdbcFieldInfo fieldInfo,
       case Types.TIME:
         return new ArrowType.Time(TimeUnit.MILLISECOND, 32);
       case Types.TIMESTAMP:
-        final String timezone;
-        if (calendar != null) {
-          timezone = calendar.getTimeZone().getID();
-        } else {
-          timezone = null;
-        }
+        return new ArrowType.Timestamp(TimeUnit.MILLISECOND);
+      case Types.TIMESTAMP_WITH_TIMEZONE:
+        final String timezone = calendar == null ? null : calendar.getTimeZone().getID();

Review Comment:
   Cool makes sense. I'll try to modify the consumer to do something smarter and correctly convert values into UTC.
   
   I'll make a separate PR to add more data into `JdbcFieldInfo`.
   
   WRT `I want to vendor this into the ADBC driver... and then maybe send the changes back` you mean redesign the JDBC to Arrow conversion within ADBC and eventually pull that back out into the `arrow-jdbc` package?



-- 
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-37118 [Java][arrow-jdbc] Support converting JDBC TIMESTAMP_WITH_TIMEZONE to Arrow [arrow]

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

   Closing this now that the Java implementation has moved to: https://github.com/apache/arrow-java


-- 
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-37118 [Java][arrow-jdbc] Support converting JDBC TIMESTAMP_WITH_TIMEZONE to Arrow [arrow]

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

   :warning: GitHub issue #37118 **has no components**, please add labels for components.


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