aleksandar-trajkovic-db commented on code in PR #60609:
URL: https://github.com/apache/spark/pull/60609#discussion_r4600276612
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala:
##########
@@ -480,7 +481,9 @@ object JdbcUtils extends Logging with SQLConfHelper {
case _: StringType => JDBCValueGetter.StringGetter
case TimestampType if metadata.contains("logical_time_type") =>
JDBCValueGetter.LogicalTimeGetter
- case TimestampType => JDBCValueGetter.TimestampGetter(dialect)
+ case TimestampType =>
+ JDBCValueGetter.TimestampGetter(
+ dialect, options.flatMap(_.timestampCalendarTimeZone).map(tz =>
Calendar.getInstance(tz)))
Review Comment:
**Blocking (P1):** With `timestampCalendarTimeZone` set, this getter reads
the database wall clock as an instant in the option's zone. Predicate pushdown
still renders timestamp literals without it. `JDBCSQLBuilder.visitLiteral`
calls `JdbcDialect.compileValue`, which renders a `java.sql.Timestamp` with
`toString` in the JVM default zone and an `Instant` in the session zone.
`JDBCRelation.unhandledFilters` and the V2 `JDBCScanBuilder.pushPredicates`
then report every compiled filter as handled, so Spark does not re-evaluate it
after the scan.
By my reading of the code: take a MySQL session at `+00:00` storing
`2026-10-10 11:03:07`, a JVM in `Asia/Kolkata`, the Java 8 API off, and the
option set to `UTC`. An unfiltered read returns `2026-10-10T11:03:07Z`.
Filtering on that same value pushes `WHERE t = '2026-10-10 16:33:07.0'`, so the
database returns no row. Equality filters drop matching rows, and range filters
drop or admit rows near the boundary. Both V1 and V2 reads are affected.
Without the option, reads and literals both use the JVM zone, so this mismatch
does not arise there.
**Recommended change:** When timestampCalendarTimeZone is set, do not push
down any expression that contains a timestamp literal whose type the option
governs (TimestampType). In V2, add a walker next to containsUnsupportedCharPad
and apply it wherever that guard is applied: predicates, aggregates and
group-bys, join conditions, and sort orders. In V1, report such filters from
unhandledFilters and leave them out of the predicates buildScan pushes, so
Spark evaluates them after the scan. Add JDBC data source tests and a sentence
in the option's doc row saying such filters are evaluated by Spark.
**Why this works:** Spark evaluates a declined predicate against the values
the getter produced in the option's zone, so filtered and unfiltered reads
agree. Expressions without timestamp literals, and every read without the
option, keep their current pushdown.
**Scope:** Add an option-gated decline guard for timestamp-literal
expressions to the V1 and V2 JDBC pushdown decisions, with tests and a doc
sentence.
**Compatibility:** Without timestampCalendarTimeZone, pushdown decisions and
the SQL sent to the database are unchanged.
**Risks:** Reads that set the option and filter on timestamp literals
transfer more rows from the database and lose join, aggregate, and top-N
pushdown when those expressions hold timestamp literals; the cost applies only
when the option is set, and unsetting the option restores current pushdown.
**Constraints:** If the nanosecond TIMESTAMP_LTZ repair makes those columns
read in the option's zone, the guard must also cover nanosecond LTZ timestamp
literals.
**Exclusions:**
- Rendering pushed timestamp literals in the option's zone, which would keep
pushdown but needs a dialect API change, is left as a possible follow-up.
- Partition stride bounds are left unchanged because they only affect how
rows are split across partitions, not which rows are returned.
**Success:** With timestampCalendarTimeZone set, a filter comparing a
TIMESTAMP column with a timestamp literal returns the same rows as the same
filter applied to an unfiltered read, on V1 and V2 JDBC reads. Without the
option, pushdown of every predicate, aggregate, join condition, and sort order
is unchanged. With the option set, expressions with no timestamp literal are
still pushed.
**Verification:**
- **Regression:** With timestampCalendarTimeZone set to a zone different
from the JVM default and session zones, equality and range filters on a
TIMESTAMP column using a value from an unfiltered read return that row, through
both the V1 data source path and the V2 catalog path.
- **Behavior:** With the option set, the pushed predicates in the scan's
plan do not include the timestamp-literal filter, while a filter on a
non-timestamp column of the same scan is still pushed.
- **Compatibility:** Without the option, a timestamp-literal filter is still
pushed to the database as before.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JDBCOptions.scala:
##########
@@ -283,6 +284,14 @@ class JDBCOptions(
.map(_.toBoolean)
.getOrElse(dialectTimestampNTZAsWallClock)
+ // Time zone the source renders TimestampType values in. When set, reads and
writes pass a
+ // calendar in this zone to ResultSet.getTimestamp and
PreparedStatement.setTimestamp, so
+ // offset-less driver values are not interpreted in the JVM default time
zone.
+ val timestampCalendarTimeZone: Option[TimeZone] =
+ parameters
+ .get(JDBC_TIMESTAMP_CALENDAR_TIME_ZONE)
+ .map(id => TimeZone.getTimeZone(ZoneId.of(id)))
Review Comment:
**Nit (P3):** `ZoneId.of(id)` accepts fewer zone strings than Spark's other
zone settings, and fails with a different error. `spark.sql.session.timeZone`,
the CSV/JSON `timeZone` option and JDBC partition-bound parsing all go through
`DateTimeUtils.getZoneId`. That helper accepts short IDs such as `PST` and the
legacy `+5:30` style offsets, and wraps failures in the `INVALID_TIMEZONE`
error. Here, the same strings fail with a bare `java.time.DateTimeException`.
`DateTimeUtils.getTimeZone(id)` already returns a `TimeZone` through the shared
parser, so it can replace this expression; the invalid-zone test would then
check the `INVALID_TIMEZONE` error.
**Verification:**
- **Behavior:** Setting the option to a short zone ID and to a
single-digit-hour offset reads timestamps as wall clocks in that zone.
- **Regression:** An invalid zone ID raises the INVALID_TIMEZONE error
condition with the given value.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala:
##########
@@ -480,7 +481,9 @@ object JdbcUtils extends Logging with SQLConfHelper {
case _: StringType => JDBCValueGetter.StringGetter
case TimestampType if metadata.contains("logical_time_type") =>
JDBCValueGetter.LogicalTimeGetter
- case TimestampType => JDBCValueGetter.TimestampGetter(dialect)
+ case TimestampType =>
Review Comment:
**Non-blocking (P2):** The calendar is only passed to the microsecond
`TimestampGetter` here, and to the matching `TimestampType` setter. When
`spark.sql.timestampNanosTypes.enabled` and `preferTimestampNanos` are on,
`resolveTimestampType` maps a TIMESTAMP column with scale 7-9 to
`TimestampLTZNanosType`. Its `TimestampLTZNanosGetter` still calls
`rs.getTimestamp(pos + 1)` with no calendar. The `TimestampLTZNanosType` setter
still calls `stmt.setTimestamp(pos + 1, ts)`, even though its comment says it
matches the `TimestampType` setter.
The doc row says the option applies to TIMESTAMP columns except
TIMESTAMP_NTZ. A user who enables nanosecond timestamps and sets the option
therefore still gets those columns shifted by the JVM-zone difference on reads,
and shifted the other way on writes, with no error. TIMESTAMP(6) columns in the
same table are corrected. Passing an optional calendar to
`TimestampLTZNanosGetter` and the nanos setter, as this PR does for
`TimestampGetter`, would close the gap.
**Verification:**
- **Regression:** With nanosecond timestamp types and preferTimestampNanos
enabled and the option set, a scale-9 TIMESTAMP column reads as the stored wall
clock in the option's zone under several JVM default zones, with its
nanoseconds intact.
- **Behavior:** A nanosecond TIMESTAMP_LTZ value written with the option set
is stored as the wall clock in the option's zone and reads back to the same
instant.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]