You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "hhr293 (via GitHub)" <gi...@apache.org> on 2026/10/12 08:17:46 UTC
[PR] [SPARK-60179][SQL] Treat a cast to STRING as time-zone dependent when… [spark]
hhr293 opened a new pull request, #59394:
URL: https://github.com/apache/spark/pull/59394
### What changes were proposed in this pull request?
`Cast.needsTimeZone` recurses into ARRAY/MAP/STRUCT only when the target has the same shape as
the source, so a cast that collapses a complex type to a single STRING falls through to the
`case _ => false` fallback. `castToString` (and `castToStringCode`) does recurse into those
containers and formats every LTZ timestamp leaf with the session `zoneId`, so the classification
contradicts the implementation it is documented to mirror:
| cast | before | after |
| --- | --- | --- |
| `TIMESTAMP -> STRING` | true | true |
| `ARRAY<TIMESTAMP> -> ARRAY<STRING>` | true | true |
| `ARRAY<TIMESTAMP> -> STRING` | **false** | true |
| `MAP<INT, TIMESTAMP> -> STRING` | **false** | true |
| `STRUCT<t: TIMESTAMP> -> STRING` | **false** | true |
| `ARRAY<ARRAY<TIMESTAMP>> -> ARRAY<STRING>` | **false** | true |
| `ARRAY<TIMESTAMP_NTZ> -> STRING` | false | false |
This PR adds one case to `Cast.needsTimeZone`: a cast whose target is STRING needs the session
time zone when the source type recursively contains an LTZ timestamp leaf. The nested
`ARRAY<ARRAY<TIMESTAMP>> -> ARRAY<STRING>` shape is covered by the existing complex-to-complex
recursion, which now bottoms out in the new case. TIMESTAMP_NTZ, DATE and TIME render on a fixed
grid and stay zone-independent, as in the scalar cases.
A Python UDT is included because `castToString` renders it through its `sqlType`; a Scala UDT is
not, because it goes through the user's `stringifyValue`.
### Why are the changes needed?
Three consumers of the predicate are affected.
1. `Cast.resolved` only demands a `timeZoneId` when `needsTimeZone` is true, so the cast was
reported as resolved without one and then failed in `TimeZoneAwareExpression.zoneId`:
```scala
val c = Cast(Literal.create(Seq(0L), ArrayType(TimestampType)), StringType)
c.resolved // true
c.eval(null) // java.util.NoSuchElementException: None.get
```
2. `Cast.canonicalized` replaces the zone with `null` when `!needsTimeZone`, so two casts carrying
different session time zones compared equal.
3. Because `CacheManager` looks up cached plans with `sameResult`, a result cached under one
session time zone was served to a query running under another, returning a stale rendering.
With a table holding `ts TIMESTAMP` and `ats ARRAY<TIMESTAMP>` at the same instant:
```scala
// q = SELECT CAST(<col> AS STRING) AS v FROM t
spark.conf.set("spark.sql.session.timeZone", "UTC")
val cached = sql(q); cached.cache(); cached.collect()
spark.conf.set("spark.sql.session.timeZone", "America/Los_Angeles")
sql(q).collect()
```
```
col = ts (needsTimeZone = true) -> [1969-12-31 16:00:00] correct, cache not reused
col = ats (needsTimeZone = false) -> [[1970-01-01 00:00:00]] wrong, UTC rendering reused
col = ats, after unpersist -> [[1969-12-31 16:00:00]] correct
```
The classification has had no complex-to-STRING case since branch-3.5, so this is not a
regression.
### Does this PR introduce _any_ user-facing change?
Yes. A query that renders a complex type containing an LTZ timestamp as a string no longer
receives a result cached under a different session time zone, as shown above.
The stricter classification also means such a cast now requires a `timeZoneId` to be resolved.
`ResolveTimeZone` assigns the session zone unconditionally, so plans built by the analyzer are
unaffected. Reporting these casts as zone-dependent additionally makes
`Cast.contextIndependentFoldable` false for them, which can forgo a V2 pushdown or a constant
folding opportunity that was previously taken with a zone-dependent value.
### How was this patch tested?
New tests:
* `CastSuiteBase` - a positive/negative matrix for `needsTimeZone` over ARRAY/MAP/STRUCT, nested
containers, LTZ nanos, VARIANT and a Python UDT, each also checked one level further out as
`ARRAY<...> -> ARRAY<STRING>`; TIMESTAMP_NTZ, DATE, TIME and an NTZ-backed Python UDT as the
negative controls. Run by `CastSuite`, `CastWithAnsiOnSuite` and `TryCastSuite`.
* `CastSuiteBase` - a complex-to-string cast is unresolved without a zone, resolved with one, not
semantically equal across two zones, and renders differently per zone (`checkEvaluation` covers
the interpreted and the codegen path).
* `CachedTableSuite` - the cross-time-zone cache case above, over `ARRAY<TIMESTAMP>`,
`ARRAY<ARRAY<TIMESTAMP>>` and a scalar `TIMESTAMP` control.
Verified that each new test fails without the production change: reverting the classification
turns both the `ARRAY<TIMESTAMP>` and the `ARRAY<ARRAY<TIMESTAMP>>` cache cases red while the
scalar control stays green, and dropping only the Python UDT branch fails exactly that entry of
the matrix.
Regression: `CastSuite` + `CastWithAnsiOnSuite` + `TryCastSuite` (276 tests) and the full
`CachedTableSuite` (107 tests) pass.
### Was this patch authored or co-authored using generative AI tooling?
Yes. Co-authored-by: Claude Code (Claude Opus 5)
--
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-60179][SQL] Treat a cast to STRING as time-zone dependent when… [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #59394:
URL: https://github.com/apache/spark/pull/59394#discussion_r4348196469
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -429,6 +431,22 @@ object Cast extends QueryErrorsBase {
case (fromField, toField) =>
needsTimeZone(fromField.dataType, toField.dataType)
}
+ // `castToString` recurses into ARRAY/MAP/STRUCT and formats each LTZ leaf with `zoneId`, while
+ // the complex-to-complex cases above only match a target of the same shape.
+ case (from, _: StringType) => from.existsRecursively(needsTimeZoneToString)
+ case _ => false
+ }
+
+ /**
+ * Whether rendering a value of this type as a string reads the session time zone. Only the LTZ
Review Comment:
**Nit (P3):** This reads as a whole-type contract, but the helper is a per-node check. `needsTimeZoneToString(ArrayType(TimestampType))` returns false, because ARRAY/MAP/STRUCT nodes fall through to `case _ => false`, yet rendering an ARRAY<TIMESTAMP> reads `zoneId`. The whole-type answer comes only from the caller's `from.existsRecursively(needsTimeZoneToString)`. The current call site is correct, but a future direct call on a container type would misclassify the cast. Could the first sentence say it is a per-node check? For example:
```
* Whether this type node, taken as a leaf, reads the session time zone when rendered as a
* string. ARRAY/MAP/STRUCT nodes return false; apply it through `existsRecursively` to cover
* nested types.
```
The rest of the comment, which explains why TIMESTAMP_NTZ, DATE, TIME, and a nested VARIANT are zone-independent, can stay as is.
**Verification:**
- **Inspection:** The reworded comment says the helper classifies one type node (TIMESTAMP and TIMESTAMP_LTZ(p) leaves, and Python UDTs through their sqlType), that container traversal comes from `existsRecursively`, and it still explains why TIMESTAMP_NTZ, DATE, TIME, and a nested VARIANT are zone-independent.
--
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-60179][SQL] Treat a cast to STRING as time-zone dependent when… [spark]
Posted by "hhr293 (via GitHub)" <gi...@apache.org>.
hhr293 commented on PR #59394:
URL: https://github.com/apache/spark/pull/59394#issuecomment-6337307945
Hi, @sunchao @cloud-fan could you please take a look at this PR when you have a chance? Thanks a lot.
--
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-60179][SQL] Treat a cast to STRING as time-zone dependent when… [spark]
Posted by "hhr293 (via GitHub)" <gi...@apache.org>.
hhr293 commented on code in PR #59394:
URL: https://github.com/apache/spark/pull/59394#discussion_r4268479883
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -429,6 +431,21 @@ object Cast extends QueryErrorsBase {
case (fromField, toField) =>
needsTimeZone(fromField.dataType, toField.dataType)
}
+ // `castToString` recurses into ARRAY/MAP/STRUCT and formats each LTZ leaf with `zoneId`, while
+ // the complex-to-complex cases above only match a target of the same shape.
+ case (from, _: StringType) => from.existsRecursively(needsTimeZoneToString)
+ case _ => false
+ }
+
+ /**
+ * Whether rendering a value of this type as a string reads the session time zone. Only the LTZ
+ * families carry an absolute instant that has to be localized; TIMESTAMP_NTZ, DATE and TIME
+ * render on a fixed grid. VARIANT may hold a timestamp at runtime, as in `needsTimeZone`.
+ */
+ private def needsTimeZoneToString(dt: DataType): Boolean = dt match {
+ case TimestampType | _: TimestampLTZNanosType | VariantType => true
Review Comment:
Thanks for the detailed review! You're right — nested VARIANT-to-STRING rendering uses UTC and does not depend on the session time zone.
I'll remove VariantType from the helper, update the comment, and adjust the tests to cover both nested and element-wise VARIANT casts.
--
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-60179][SQL] Treat a cast to STRING as time-zone dependent when the source type contains a nested TIMESTAMP_LTZ [spark]
Posted by "sunchao (via GitHub)" <gi...@apache.org>.
sunchao commented on PR #59394:
URL: https://github.com/apache/spark/pull/59394#issuecomment-6368378500
**Merge Summary:**
- merged into master https://github.com/apache/spark/commit/11e2e6c3700f97aa42a001067e1872bcde7b03e4
- merged into branch-4.x https://github.com/apache/spark/commit/f7c391791d4093929532b8813900b86c47fafad1
*Posted by `merge_spark_pr.py`*
--
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-60179][SQL] Treat a cast to STRING as time-zone dependent when… [spark]
Posted by "hhr293 (via GitHub)" <gi...@apache.org>.
hhr293 commented on PR #59394:
URL: https://github.com/apache/spark/pull/59394#issuecomment-6206200666
@HyukjinKwon Hi, could you please take a look at this PR when you have a chance? It fixes incorrect time-zone handling for complex-to-STRING casts. Your feedback would be greatly appreciated. Thanks!
--
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-60179][SQL] Treat a cast to STRING as time-zone dependent when the source type contains a nested TIMESTAMP_LTZ [spark]
Posted by "sunchao (via GitHub)" <gi...@apache.org>.
sunchao closed pull request #59394: [SPARK-60179][SQL] Treat a cast to STRING as time-zone dependent when the source type contains a nested TIMESTAMP_LTZ
URL: https://github.com/apache/spark/pull/59394
--
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-60179][SQL] Treat a cast to STRING as time-zone dependent when… [spark]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #59394:
URL: https://github.com/apache/spark/pull/59394#discussion_r4268420127
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -429,6 +431,21 @@ object Cast extends QueryErrorsBase {
case (fromField, toField) =>
needsTimeZone(fromField.dataType, toField.dataType)
}
+ // `castToString` recurses into ARRAY/MAP/STRUCT and formats each LTZ leaf with `zoneId`, while
+ // the complex-to-complex cases above only match a target of the same shape.
+ case (from, _: StringType) => from.existsRecursively(needsTimeZoneToString)
+ case _ => false
+ }
+
+ /**
+ * Whether rendering a value of this type as a string reads the session time zone. Only the LTZ
+ * families carry an absolute instant that has to be localized; TIMESTAMP_NTZ, DATE and TIME
+ * render on a fixed grid. VARIANT may hold a timestamp at runtime, as in `needsTimeZone`.
+ */
+ private def needsTimeZoneToString(dt: DataType): Boolean = dt match {
+ case TimestampType | _: TimestampLTZNanosType | VariantType => true
Review Comment:
**Nit (P3):** This also marks a nested `VARIANT` as zone-dependent, but the nested path never reads the zone. Only a top-level VARIANT source goes through `VariantGet.cast(v, to, castArgs)` and renders with `castArgs.zoneId`, which is what the existing `(VariantType, _)` arm covers. A VARIANT under ARRAY/MAP/STRUCT is rendered by `ToStringBase.castToString` instead. `TypeApiOps(VariantType, ...)` is `None`, so it falls to the default `o.toString` (codegen: `String.valueOf($c)`). That is `VariantVal.toString`, i.e. `toJson(ZoneOffset.UTC)`.
So `CAST(array(CAST(ts AS VARIANT)) AS STRING)` returns the same string in every session zone. With this change it now needs a zone to resolve, keeps the zone in `canonicalized` (no cache reuse or `semanticEquals` across zones), and is no longer `contextIndependentFoldable`. That breaks the `needsTimeZone` contract ("exactly when its `castTo*` / `castTo*Code` path reads `zoneId`"). Results are still correct; the classification is just over-conservative.
Could we drop `VariantType` here and keep the top-level `(VariantType, _)` arm? That would also mean rewording the "VARIANT may hold a timestamp at runtime" sentence above and moving `ArrayType(VariantType)` in the new test to the zone-independent list. `ARRAY<VARIANT> -> ARRAY<STRING>` would still be zone-dependent through the top-level arm.
```suggestion
case TimestampType | _: TimestampLTZNanosType => true
```
**Verification:**
- **Behavior:** The `needsTimeZone` classification test asserts nested VARIANT under ARRAY (and at least one of MAP or STRUCT) is zone-independent when cast to STRING, while top-level VARIANT -> STRING and ARRAY<VARIANT> -> ARRAY<STRING> remain zone-dependent.
- **Regression:** The existing nested-LTZ positives, NTZ/DATE/TIME negatives, the resolution and semantic-equality checks, and the cross-time-zone cache-reuse test still pass unchanged.
--
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-60179][SQL] Treat a cast to STRING as time-zone dependent when the source type contains a nested TIMESTAMP_LTZ [spark]
Posted by "hhr293 (via GitHub)" <gi...@apache.org>.
hhr293 commented on PR #59394:
URL: https://github.com/apache/spark/pull/59394#issuecomment-6356017660
Thanks @cloud-fan and @sunchao for the reviews and approvals! I've addressed the documentation nit and updated the PR title.
--
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-60179][SQL] Treat a cast to STRING as time-zone dependent when… [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #59394:
URL: https://github.com/apache/spark/pull/59394#issuecomment-6340538796
Thanks for the ping, @hhr293. I've taken a look; please see my review.
<!-- SPARK_DEV_REVIEW_REPLY {"feedback_id":"conversation:6337307945","thread_id":"conversation:6337307945","verdict_sha256":"de69bfaba10d27bcfbd4dba35a7e17709efa53ad84e2208196a023510609a278"} -->
--
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