cloud-fan commented on code in PR #59561:
URL: https://github.com/apache/spark/pull/59561#discussion_r4582226974
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ToStringBase.scala:
##########
@@ -100,6 +100,8 @@ trait ToStringBase { self: UnaryExpression with
TimeZoneAwareExpression =>
acceptAny[Long](t => UTF8String.fromString(timestampFormatter.format(t)))
case TimestampNTZType =>
acceptAny[Long](t =>
UTF8String.fromString(timestampNTZFormatter.format(t)))
+ case VariantType =>
+ acceptAny[VariantVal](v => UTF8String.fromString(v.toJson(zoneId)))
Review Comment:
**Blocking (P1):** This arm makes the string form of a nested VARIANT depend
on the cast's time zone, but `Cast.needsTimeZoneToString` (Cast.scala:456-461)
still has no `VariantType` case. So `Cast.needsTimeZone` stays false for a cast
to STRING of `ARRAY<VARIANT>`, `MAP<_, VARIANT>`, or `STRUCT<v: VARIANT>`. Its
ScalaDoc (Cast.scala:451-454) still says a nested VARIANT renders at UTC, and
the SPARK-60179 test in CastSuiteBase.scala (lines 2738-2746) still asserts
that these casts do not need a time zone.
Because of that, `Cast.canonicalized` drops the zone, so two casts that
differ only in session time zone count as the same result for `sameResult`.
Reading the code, `SELECT CAST(array(v) AS STRING) FROM t`, cached under UTC
and run again after `SET spark.sql.session.timeZone=America/Los_Angeles`, is
served from the cache as `["2020-01-01 00:00:00+00:00"]`, while the uncached
query returns `["2019-12-31 16:00:00-08:00"]`. This is the pattern SPARK-60179
fixed for nested TIMESTAMP (CachedTableSuite.scala:2854-2882), and it also
covers exchange reuse and sessions that share the cache manager. In addition,
`Cast.resolved` accepts such a cast without a zone, and evaluating one now
fails with `None.get` because this arm reads `zoneId`.
The new tests cannot catch this: `checkEvaluation` assigns the session zone
before evaluating, and the stale CastSuiteBase assertion keeps passing.
**Recommended change:** Add a VariantType case returning true to
Cast.needsTimeZoneToString in Cast.scala, and rewrite its ScalaDoc so it says
that a VARIANT at any depth renders its TIMESTAMP values in the cast's time
zone. In the Cast expression tests, move ARRAY<VARIANT>, MAP<INT, VARIANT>, and
STRUCT<v: VARIANT> from the zone-independent list of the SPARK-60179
needsTimeZone test to its zone-dependent list and correct its comment, and give
the complex-to-string resolution test a nested-variant case. In the
cached-table tests, cover a column that casts an array containing a variant
TIMESTAMP to string across session time zones.
**Why this works:** With the new case, Cast.needsTimeZone(from, STRING) is
true whenever the source contains a VARIANT. Cast.resolved then requires a
zone, Cast.canonicalized keeps it, contextIndependentFoldable reports false,
and the zone-gated builders attach the session zone. Two such casts in
different zones then canonicalize differently, so QueryPlan.sameResult no
longer matches them and the cache manager no longer serves text rendered in one
zone to a query in another.
**Compatibility:** The text that ToStringBase, ToPrettyString, and
HiveResult produce for variants is unchanged, as is the classification of the
top-level VARIANT cast and of every cast whose source contains no VARIANT.
**Risks:** The classification is per type, so a cast to string of a struct,
array, or map containing a VARIANT becomes zone-dependent even when its
variants hold no TIMESTAMP value. Such plans are then no longer reused from a
cache or exchange computed in another session time zone, and are no longer
folded as context-independent constants, for example into a pushed-down Data
Source V2 literal. Nested TIMESTAMP columns already work this way, and
reverting the one case undoes it. Such casts also count as zone-dependent in
the CHECK constraint write-time check and in Hive metastore partition pruning,
which then handle them like other zone-dependent casts.
**Constraints:** Leave every other case of Cast.needsTimeZone unchanged,
including the top-level (VariantType, _) arm; only casts to string whose source
contains a nested VARIANT change classification. Keep the new case
unconditional even if a legacy setting is later added to restore UTC rendering,
because declaring a zone-independent cast zone-dependent is safe while the
reverse returns wrong results.
**Exclusions:**
- Whether the new rendering needs a migration-guide entry or a legacy
setting is the separate decision about shipping the change ungated and is not
part of this repair.
**Verification:**
- **Behavior:** Cast.needsTimeZone reports a cast to string as
zone-dependent when its array, map, or struct source contains a VARIANT at any
depth, and still reports the TIMESTAMP_NTZ, DATE, and TIME controls as
zone-independent.
- **Behavior:** A complex-to-string cast whose source contains a VARIANT is
unresolved without a time zone, resolved with one, and not semantically equal
when the only difference is UTC versus America/Los_Angeles.
- **Regression:** Caching a query that casts an array containing a variant
TIMESTAMP to string under UTC and rerunning it under America/Los_Angeles
returns the -08:00 rendering instead of the cached +00:00 text.
--
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]