MaxGekk commented on code in PR #56577:
URL: https://github.com/apache/spark/pull/56577#discussion_r3437272448
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1969,9 +2027,24 @@ case class Cast(
case TimestampNTZType =>
(c, evPrim, evNull) =>
code"$evPrim = TimestampNanosVal.fromParts($c, (short) 0);"
+ case TimestampType =>
+ val zoneIdClass = classOf[ZoneId]
+ val zid = JavaCode.global(
+ ctx.addReferenceObj("zoneId", zoneId, zoneIdClass.getName),
+ zoneIdClass)
+ (c, evPrim, evNull) =>
+ code"$evPrim = TimestampNanosVal.fromParts(" +
+ code"$dateTimeUtilsCls.convertTz($c, java.time.ZoneOffset.UTC,
$zid), (short) 0);"
case _: TimestampNTZNanosType =>
(c, evPrim, evNull) =>
code"$evPrim = $dateTimeUtilsCls.truncateTimestampNanosToPrecision($c,
$precision);"
+ case _: TimestampLTZNanosType =>
+ val zoneIdClass = classOf[ZoneId]
+ val zid = JavaCode.global(
+ ctx.addReferenceObj("zoneId", zoneId, zoneIdClass.getName),
+ zoneIdClass)
+ (c, evPrim, evNull) =>
+ code"$evPrim = $dateTimeUtilsCls.timestampLTZNanosToNTZNanos($c, $zid,
$precision);"
Review Comment:
Good point, done in 0f1682c. Extracted the repeated
`JavaCode.global(ctx.addReferenceObj("zoneId", ...))` boilerplate (14 call
sites across the timestamp/date codegen arms) into a single helper:
```scala
private[this] def zoneIdValue(ctx: CodegenContext): GlobalValue = {
val zoneIdClass = classOf[ZoneId]
JavaCode.global(ctx.addReferenceObj("zoneId", zoneId,
zoneIdClass.getName), zoneIdClass)
}
```
Each arm now just calls `val zid = zoneIdValue(ctx)` (-52/+22 lines). I kept
it as a per-arm call rather than a single pre-`match` val on purpose: since
`addReferenceObj` doesn't dedup, hoisting it before the `match` would register
an unused `zoneId` reference for the zone-independent arms (string NTZ,
integral, DATE -> UTC). This way the reference is still only added for casts
that actually need the session time zone, and at runtime only one arm fires per
cast so there's exactly one reference as before.
--
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]