You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/06/29 01:21:25 UTC

[GitHub] [spark] beliefer commented on a diff in pull request #37013: [SPARK-39339][SQL][FOLLOWUP] Fix bug TimestampNTZ type in JDBC data source is incorrect

beliefer commented on code in PR #37013:
URL: https://github.com/apache/spark/pull/37013#discussion_r909117947


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala:
##########
@@ -599,10 +610,13 @@ object JdbcUtils extends Logging with SQLConfHelper {
           stmt.setTimestamp(pos + 1, toJavaTimestamp(instantToMicros(row.getAs[Instant](pos))))
       } else {
         (stmt: PreparedStatement, row: Row, pos: Int) =>
-          stmt.setTimestamp(
-            pos + 1,
-            toJavaTimestamp(localDateTimeToMicros(row.getAs[java.time.LocalDateTime](pos)))
-          )
+          val micros = localDateTimeToMicros(row.getAs[java.time.LocalDateTime](pos))
+          val seconds = Math.floorDiv(micros, DateTimeConstants.MICROS_PER_SECOND)
+          val nanos = (micros - seconds * DateTimeConstants.MICROS_PER_SECOND) *
+            DateTimeConstants.NANOS_PER_MICROS
+          val result = new java.sql.Timestamp(seconds * DateTimeConstants.MILLIS_PER_SECOND)

Review Comment:
   Because toJavaTimestamp based on the JVM system time zone, we must keep it without time zone.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala:
##########
@@ -599,10 +610,13 @@ object JdbcUtils extends Logging with SQLConfHelper {
           stmt.setTimestamp(pos + 1, toJavaTimestamp(instantToMicros(row.getAs[Instant](pos))))
       } else {
         (stmt: PreparedStatement, row: Row, pos: Int) =>
-          stmt.setTimestamp(
-            pos + 1,
-            toJavaTimestamp(localDateTimeToMicros(row.getAs[java.time.LocalDateTime](pos)))
-          )
+          val micros = localDateTimeToMicros(row.getAs[java.time.LocalDateTime](pos))
+          val seconds = Math.floorDiv(micros, DateTimeConstants.MICROS_PER_SECOND)
+          val nanos = (micros - seconds * DateTimeConstants.MICROS_PER_SECOND) *
+            DateTimeConstants.NANOS_PER_MICROS
+          val result = new java.sql.Timestamp(seconds * DateTimeConstants.MILLIS_PER_SECOND)

Review Comment:
   Because `toJavaTimestamp` based on the JVM system time zone, we must keep it without time zone.



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org