You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "shrirangmhalgi (via GitHub)" <gi...@apache.org> on 2026/06/22 07:20:57 UTC
[PR] [SPARK-57555][SQL] Support TIME data type in built-in JDBC data source [spark]
shrirangmhalgi opened a new pull request, #56653:
URL: https://github.com/apache/spark/pull/56653
### What changes were proposed in this pull request?
Add native `TimeType` support to the built-in JDBC data source. `getCatalystType` maps `java.sql.Types.TIME` to `TimeType` (gated behind `spark.sql.timeType.enabled`), `getCommonJDBCType` maps `TimeType` to SQL `TIME`, and value read / write uses `java.time.LocalTime`. Per-dialect overrides are left for follow-up sub-tasks.
### Why are the changes needed?
JDBC currently maps `java.sql.Types.TIME` to `TimestampType`, losing time-only semantics. With TimeType now supported in Spark, the JDBC source should read and write TIME columns natively.
### Does this PR introduce any user-facing change?
Yes. When `spark.sql.timeType.enabled` is `true`, JDBC TIME columns are inferred as `TimeType` with driver-reported precision, and `TimeType` columns can be written as SQL TIME. When disabled (default), behavior is unchanged.
### How was this patch tested?
- Read, write round-trip, and sub-second precision tests against H2
- Legacy back-compat tests preserved with `timeType.enabled=false`
- All 6 TIME-related tests pass
### Was this patch authored or co-authored using generative AI tooling?
Co-Authored using Claude Opus 4.6
--
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
Re: [PR] [SPARK-57555][SQL] Support TIME data type in built-in JDBC data source [spark]
Posted by "shrirangmhalgi (via GitHub)" <gi...@apache.org>.
shrirangmhalgi commented on PR #56653:
URL: https://github.com/apache/spark/pull/56653#issuecomment-4775792155
CI fix: added DB2 dialect override to emit bare `TIME` (DB2 doesn't support `TIME(p)` syntax). Oracle CI failure is an infrastructure flake (Docker container timeout, unrelated to code change).
--
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
Re: [PR] [SPARK-57555][SQL] Support TIME data type in built-in JDBC data source [spark]
Posted by "shrirangmhalgi (via GitHub)" <gi...@apache.org>.
shrirangmhalgi commented on PR #56653:
URL: https://github.com/apache/spark/pull/56653#issuecomment-4773917927
Thank you @MaxGekk for the review. Addressed all the points in the latest commit:
- DDL now emits `TIME(p)` preserving precision.
- Round-trip test uses sub-second value (LocalTime.of(23, 59, 59, 123456000))
- `scale >= 0` so `TIME(0)` infers correctly.
- Re: getter storing full nanos — intentional, same as `TimestampType` which stores full micros regardless of source precision. The declared precision is metadata for display/comparison, not a storage constraint.
- Also fixed CI: integration tests wrapped with `timeType.enabled=false` to preserve legacy behavior.
--
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
Re: [PR] [SPARK-57555][SQL] Support TIME data type in built-in JDBC data source [spark]
Posted by "shrirangmhalgi (via GitHub)" <gi...@apache.org>.
shrirangmhalgi commented on PR #56653:
URL: https://github.com/apache/spark/pull/56653#issuecomment-4796450519
Thankyou @MaxGekk / @uros-b for the review. Will work the followup for all the 6 dialects
--
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
Re: [PR] [SPARK-57555][SQL] Support TIME data type in built-in JDBC data source [spark]
Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on code in PR #56653:
URL: https://github.com/apache/spark/pull/56653#discussion_r3450753948
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala:
##########
@@ -164,6 +164,7 @@ object JdbcUtils extends Logging with SQLConfHelper {
// Note that some dialects override this setting, e.g. as SQL Server.
case TimestampNTZType => Option(JdbcType("TIMESTAMP", java.sql.Types.TIMESTAMP))
case DateType => Option(JdbcType("DATE", java.sql.Types.DATE))
+ case _: TimeType => Option(JdbcType("TIME", java.sql.Types.TIME))
Review Comment:
This drops the type's precision — the column is created as bare `TIME`, which on SQL-standard databases (and H2, where `TIME` means `TIME(0)`) truncates sub-second values on write. `DecimalType` two lines down preserves precision the same way, so emit `TIME(p)`:
```suggestion
case t: TimeType => Option(JdbcType(s"TIME(${t.precision})", java.sql.Types.TIME))
```
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala:
##########
@@ -443,6 +450,15 @@ object JdbcUtils extends Logging with SQLConfHelper {
row.update(pos, null)
}
+ case _: TimeType =>
+ (rs: ResultSet, row: InternalRow, pos: Int) =>
+ val localTime = rs.getObject(pos + 1, classOf[java.time.LocalTime])
+ if (localTime != null) {
+ row.setLong(pos, localTime.toNanoOfDay)
Review Comment:
For a `TIME(7..9)` source (H2 allows `TIME(9)`), `getCatalystType` clamps the inferred type to `TimeType(6)`, but `toNanoOfDay` here stores the full nanosecond value — so the stored value can carry more precision than its declared type. Intentional, or should this truncate to the inferred precision?
##########
sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala:
##########
@@ -736,24 +736,79 @@ class JDBCSuite extends SharedSparkSession {
}
test("SPARK-34357: test TIME types") {
- val rows = spark.read.jdbc(
- urlWithUserAndPass, "TEST.TIMETYPES", new Properties()).collect()
- val cachedRows = spark.read.jdbc(urlWithUserAndPass, "TEST.TIMETYPES", new Properties())
- .cache().collect()
- val expectedTimeAtEpoch = java.sql.Timestamp.valueOf("1970-01-01 12:34:56.0")
- assert(rows(0).getAs[java.sql.Timestamp](0) === expectedTimeAtEpoch)
- assert(rows(1).getAs[java.sql.Timestamp](0) === expectedTimeAtEpoch)
- assert(cachedRows(0).getAs[java.sql.Timestamp](0) === expectedTimeAtEpoch)
+ withSQLConf(SQLConf.TIME_TYPE_ENABLED.key -> "false") {
+ val rows = spark.read.jdbc(
+ urlWithUserAndPass, "TEST.TIMETYPES", new Properties()).collect()
+ val cachedRows = spark.read.jdbc(urlWithUserAndPass, "TEST.TIMETYPES", new Properties())
+ .cache().collect()
+ val expectedTimeAtEpoch = java.sql.Timestamp.valueOf("1970-01-01 12:34:56.0")
+ assert(rows(0).getAs[java.sql.Timestamp](0) === expectedTimeAtEpoch)
+ assert(rows(1).getAs[java.sql.Timestamp](0) === expectedTimeAtEpoch)
+ assert(cachedRows(0).getAs[java.sql.Timestamp](0) === expectedTimeAtEpoch)
+ }
}
test("SPARK-47396: TIME WITHOUT TIME ZONE preferTimestampNTZ") {
- spark.catalog.clearCache()
- val df = spark.read.format("jdbc")
- .option("preferTimestampNTZ", true)
- .option("url", urlWithUserAndPass)
- .option("query", "SELECT A FROM TEST.TIMETYPES limit 1")
- .load()
- assert(df.head().get(0).isInstanceOf[LocalDateTime])
+ withSQLConf(SQLConf.TIME_TYPE_ENABLED.key -> "false") {
+ spark.catalog.clearCache()
+ val df = spark.read.format("jdbc")
+ .option("preferTimestampNTZ", true)
+ .option("url", urlWithUserAndPass)
+ .option("query", "SELECT A FROM TEST.TIMETYPES limit 1")
+ .load()
+ assert(df.head().get(0).isInstanceOf[LocalDateTime])
+ }
+ }
+
+ test("SPARK-57555: JDBC TIME maps to TimeType when timeType.enabled") {
+ val df = spark.read.jdbc(
+ urlWithUserAndPass, "TEST.TIMETYPES", new Properties())
+ // With timeType.enabled=true (default in tests), TIME column maps to TimeType
+ assert(df.schema("A").dataType.isInstanceOf[TimeType])
+ val rows = df.collect()
+ assert(rows(0).getAs[java.time.LocalTime](0) === java.time.LocalTime.of(12, 34, 56))
+ }
+
+ test("SPARK-57555: JDBC TIME write round-trip") {
+ val url = urlWithUserAndPass
+ val tableName = "TEST.TIME_ROUNDTRIP"
+ val time1 = java.time.LocalTime.of(9, 30, 0)
+ val time2 = java.time.LocalTime.of(23, 59, 59)
Review Comment:
These values have no fractional seconds, so this round-trip can't observe the precision drop at `JdbcUtils.scala:167` — write truncation is invisible when there's nothing below the second. Once the write DDL carries precision, give one of these a sub-second component (e.g. `LocalTime.of(1, 2, 3, 123456000)`) and assert it survives the round-trip; that assertion fails today.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala:
##########
@@ -227,7 +228,13 @@ object JdbcUtils extends Logging with SQLConfHelper {
case java.sql.Types.SMALLINT => IntegerType
case java.sql.Types.SQLXML => StringType
case java.sql.Types.STRUCT => StringType
- case java.sql.Types.TIME => getTimestampType(isTimestampNTZ)
+ case java.sql.Types.TIME =>
+ if (conf.isTimeTypeEnabled) {
+ // Use reported scale (fractional digits) as precision, default to 6 if not reported
+ val timePrecision = if (scale > 0 && scale <= TimeType.MAX_PRECISION) scale
Review Comment:
`scale == 0` is a valid `TIME(0)` column, but the `scale > 0` guard sends it to the `else` branch and infers `TimeType(6)` — the comment's "if not reported" conflates reported-zero with not-reported (`getScale()` returns 0 for both). A source `TIME(0)` becomes Spark `time(6)`. If you want `TIME(0)` to round-trip its precision, `scale >= 0` maps it correctly; the trade-off is drivers that report 0 for "unknown". Worth a deliberate call either way.
--
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
Re: [PR] [SPARK-57555][SQL] Support TIME data type in built-in JDBC data source [spark]
Posted by "uros-b (via GitHub)" <gi...@apache.org>.
uros-b closed pull request #56653: [SPARK-57555][SQL] Support TIME data type in built-in JDBC data source
URL: https://github.com/apache/spark/pull/56653
--
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
Re: [PR] [SPARK-57555][SQL] Support TIME data type in built-in JDBC data source [spark]
Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on code in PR #56653:
URL: https://github.com/apache/spark/pull/56653#discussion_r3459093115
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala:
##########
@@ -164,6 +164,7 @@ object JdbcUtils extends Logging with SQLConfHelper {
// Note that some dialects override this setting, e.g. as SQL Server.
case TimestampNTZType => Option(JdbcType("TIMESTAMP", java.sql.Types.TIMESTAMP))
case DateType => Option(JdbcType("DATE", java.sql.Types.DATE))
+ case t: TimeType => Option(JdbcType(s"TIME(${t.precision})", java.sql.Types.TIME))
Review Comment:
Non-blocking, for the per-dialect follow-ups: `TimeType.precision` can be up to 9 (`MAX_PRECISION`), so this emits `TIME(7)`..`TIME(9)` for high-precision columns — but PostgreSQL, MySQL, and SQL Server cap fractional-second precision at 6 and reject that DDL. It fails loudly rather than silently corrupting, and the tests use H2 (which supports `TIME(9)`), so the round-trip tests don't surface it. Clamping to the dialect's max precision belongs naturally in the per-dialect overrides you've scoped as follow-ups — flagging so it's tracked. (`DecimalType`, the pattern this mirrors, never emits out-of-range DDL within its own max of 38.)
##########
connector/docker-integration-tests/src/test/scala/org/apache/spark/sql/jdbc/PostgresIntegrationSuite.scala:
##########
@@ -186,8 +186,9 @@ class PostgresIntegrationSuite extends SharedJDBCIntegrationSuite {
}
test("Type mapping for various types") {
- val df = spark.read.jdbc(jdbcUrl, "bar", new Properties)
- val rows = df.collect().sortBy(_.toString())
+ withSQLConf(SQLConf.TIME_TYPE_ENABLED.key -> "false") {
+ val df = spark.read.jdbc(jdbcUrl, "bar", new Properties)
+ val rows = df.collect().sortBy(_.toString())
Review Comment:
Nit: the body of this new `withSQLConf { ... }` block isn't re-indented — `val df`/`val rows` got the extra two spaces but everything from `assert(rows.length == 2)` down stays at the outer indent, so the block reads as if it's outside the `withSQLConf`. Same pattern in `DB2IntegrationSuite` ("Date types") and `MySQLIntegrationSuite` ("Date types" / "SPARK-47406").
--
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
Re: [PR] [SPARK-57555][SQL] Support TIME data type in built-in JDBC data source [spark]
Posted by "shrirangmhalgi (via GitHub)" <gi...@apache.org>.
shrirangmhalgi commented on PR #56653:
URL: https://github.com/apache/spark/pull/56653#issuecomment-4780703130
Thanks @MaxGekk. All items addressed in the latest commit.
- Fixed indentation inside `withSQLConf` blocks (`Postgres`, `DB2`, `MySQL`)
- `TIME(7-9)` precision clamping tracked for per-dialect follow-ups
- Also updated PR description to document the DB2 dialect override.
--
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
Re: [PR] [SPARK-57555][SQL] Support TIME data type in built-in JDBC data source [spark]
Posted by "shrirangmhalgi (via GitHub)" <gi...@apache.org>.
shrirangmhalgi commented on code in PR #56653:
URL: https://github.com/apache/spark/pull/56653#discussion_r3460794359
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala:
##########
@@ -164,6 +164,7 @@ object JdbcUtils extends Logging with SQLConfHelper {
// Note that some dialects override this setting, e.g. as SQL Server.
case TimestampNTZType => Option(JdbcType("TIMESTAMP", java.sql.Types.TIMESTAMP))
case DateType => Option(JdbcType("DATE", java.sql.Types.DATE))
+ case t: TimeType => Option(JdbcType(s"TIME(${t.precision})", java.sql.Types.TIME))
Review Comment:
Ack. Thanks for flagging. I agree - clamping to each dialect's max precision belongs in the per-dialect follow-ups. Will make sure to follow this
--
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
Re: [PR] [SPARK-57555][SQL] Support TIME data type in built-in JDBC data source [spark]
Posted by "shrirangmhalgi (via GitHub)" <gi...@apache.org>.
shrirangmhalgi commented on code in PR #56653:
URL: https://github.com/apache/spark/pull/56653#discussion_r3460794359
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala:
##########
@@ -164,6 +164,7 @@ object JdbcUtils extends Logging with SQLConfHelper {
// Note that some dialects override this setting, e.g. as SQL Server.
case TimestampNTZType => Option(JdbcType("TIMESTAMP", java.sql.Types.TIMESTAMP))
case DateType => Option(JdbcType("DATE", java.sql.Types.DATE))
+ case t: TimeType => Option(JdbcType(s"TIME(${t.precision})", java.sql.Types.TIME))
Review Comment:
Ack. Thanks for flagging. I agree. clamping to each dialect's max precision belongs in the per-dialect follow-ups.
--
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
Re: [PR] [SPARK-57555][SQL] Support TIME data type in built-in JDBC data source [spark]
Posted by "uros-b (via GitHub)" <gi...@apache.org>.
uros-b commented on PR #56653:
URL: https://github.com/apache/spark/pull/56653#issuecomment-4796398043
Merging to master/4.x
--
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
Re: [PR] [SPARK-57555][SQL] Support TIME data type in built-in JDBC data source [spark]
Posted by "shrirangmhalgi (via GitHub)" <gi...@apache.org>.
shrirangmhalgi commented on PR #56653:
URL: https://github.com/apache/spark/pull/56653#issuecomment-4765874285
@MaxGekk could you please review? This adds native `TimeType` support to the JDBC data source with backward compatibility gating and precision-aware schema inference.
Per-dialect overrides scoped as follow-ups per the JIRA suggestion. Thankyou.
--
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
Re: [PR] [SPARK-57555][SQL] Support TIME data type in built-in JDBC data source [spark]
Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on PR #56653:
URL: https://github.com/apache/spark/pull/56653#issuecomment-4767876690
@milastdbx Could you review this PR, please.
--
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
Re: [PR] [SPARK-57555][SQL] Support TIME data type in built-in JDBC data source [spark]
Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk commented on PR #56653:
URL: https://github.com/apache/spark/pull/56653#issuecomment-4789067530
Waiting for CI. @shrirangmhalgi could you re-run only the failed [Run / Run Docker integration tests](https://github.com/shrirangmhalgi/spark/actions/runs/28036371420/job/82994645796#logs), please.
--
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