-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-34357][SQL] Map JDBC SQL TIME type to TimestampType with time portion fixed regardless of timezone #31473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,12 +19,14 @@ package org.apache.spark.sql.execution.datasources.jdbc | |
|
|
||
| import java.sql.{Connection, Driver, JDBCType, PreparedStatement, ResultSet, ResultSetMetaData, SQLException, SQLFeatureNotSupportedException} | ||
| import java.time.{Instant, LocalDate} | ||
| import java.util.Locale | ||
| import java.util.{Locale, TimeZone} | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| import scala.util.Try | ||
| import scala.util.control.NonFatal | ||
|
|
||
| import sun.util.calendar.ZoneInfo | ||
|
|
||
| import org.apache.spark.TaskContext | ||
| import org.apache.spark.executor.InputMetrics | ||
| import org.apache.spark.internal.Logging | ||
|
|
@@ -230,7 +232,7 @@ object JdbcUtils extends Logging { | |
| case java.sql.Types.SMALLINT => IntegerType | ||
| case java.sql.Types.SQLXML => StringType | ||
| case java.sql.Types.STRUCT => StringType | ||
| case java.sql.Types.TIME => IntegerType | ||
| case java.sql.Types.TIME => TimestampType | ||
| case java.sql.Types.TIME_WITH_TIMEZONE | ||
| => null | ||
| case java.sql.Types.TIMESTAMP => TimestampType | ||
|
|
@@ -421,23 +423,6 @@ object JdbcUtils extends Logging { | |
| (rs: ResultSet, row: InternalRow, pos: Int) => | ||
| row.setFloat(pos, rs.getFloat(pos + 1)) | ||
|
|
||
|
|
||
| // SPARK-33888 - sql TIME type represents as physical int in millis | ||
| // Represents a time of day, with no reference to a particular calendar, | ||
| // time zone or date, with a precision of one millisecond. | ||
| // It stores the number of milliseconds after midnight, 00:00:00.000. | ||
| case IntegerType if metadata.contains("logical_time_type") => | ||
| (rs: ResultSet, row: InternalRow, pos: Int) => { | ||
| val rawTime = rs.getTime(pos + 1) | ||
| if (rawTime != null) { | ||
| val rawTimeInNano = rawTime.toLocalTime().toNanoOfDay() | ||
| val timeInMillis = Math.toIntExact(TimeUnit.NANOSECONDS.toMillis(rawTimeInNano)) | ||
| row.setInt(pos, timeInMillis) | ||
| } else { | ||
| row.update(pos, null) | ||
| } | ||
| } | ||
|
|
||
| case IntegerType => | ||
| (rs: ResultSet, row: InternalRow, pos: Int) => | ||
| row.setInt(pos, rs.getInt(pos + 1)) | ||
|
|
@@ -470,6 +455,27 @@ object JdbcUtils extends Logging { | |
| // TODO(davies): use getBytes for better performance, if the encoding is UTF-8 | ||
| row.update(pos, UTF8String.fromString(rs.getString(pos + 1))) | ||
|
|
||
| // SPARK-34357 - sql TIME type represents as zero epoch timestamp. | ||
| // It is represented as Spark TimestampType but fixed at 1970-01-01 for day, | ||
| // time portion is time of day, with no reference to a particular calendar, | ||
| // time zone or date, with a precision till microseconds. | ||
| // It stores the number of milliseconds after midnight, 00:00:00.000000 | ||
| case TimestampType if metadata.contains("logical_time_type") => | ||
| (rs: ResultSet, row: InternalRow, pos: Int) => { | ||
| val rawTime = rs.getTime(pos + 1) | ||
| if (rawTime != null) { | ||
| val localTimeMicro = TimeUnit.NANOSECONDS.toMicros(rawTime.toLocalTime().toNanoOfDay()) | ||
| val localTimeMillis = DateTimeUtils.microsToMillis(localTimeMicro) | ||
| val timeZoneOffset = TimeZone.getDefault match { | ||
| case zoneInfo: ZoneInfo => zoneInfo.getOffsetsByWall(localTimeMillis, null) | ||
| case timeZone: TimeZone => timeZone.getOffset(localTimeMillis - timeZone.getRawOffset) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems we can just do
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Superb! This is really more elegant. Thanks for this suggestion! |
||
| row.setLong(pos, localTimeMicro - DateTimeUtils.millisToMicros(timeZoneOffset)) | ||
| } else { | ||
| row.update(pos, null) | ||
| } | ||
| } | ||
|
|
||
| case TimestampType => | ||
| (rs: ResultSet, row: InternalRow, pos: Int) => | ||
| val t = rs.getTimestamp(pos + 1) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In most cases, session timezone should be the same as JVM default timezone, but ideally we should use session timezone for datetime operations:
SQLConf.get.sessionLocalTimeZone