MaxGekk commented on code in PR #56848:
URL: https://github.com/apache/spark/pull/56848#discussion_r3490150016


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala:
##########
@@ -1287,3 +1298,60 @@ object DateTimeUtils extends SparkDateTimeUtils {
     c
   }
 }
+
+/**
+ * Per-task memoization of a zone's UTC offset, used by the 
[[DateTimeUtils.truncTimestamp]] hot
+ * path. The session zone is constant for a query and the offset is 
piecewise-constant between DST
+ * transitions, so consecutive rows almost always resolve to the same offset. 
The cache holds the
+ * half-open epoch-second interval `[lo, hi)` on which the offset is provably 
constant -- derived
+ * from the surrounding zone transitions -- so a lookup that falls in the 
interval reduces to two
+ * comparisons instead of a transition-array binary search.
+ *
+ * Not thread-safe by design: a fresh instance is created per task (codegen 
mutable state) and used
+ * single-threaded, mirroring how stateful per-row helpers are scoped in 
generated code.
+ */
+class ZoneOffsetCache(val zoneId: ZoneId) {

Review Comment:
   Nice optimization, and the `hi-1` anchoring to dodge the on-transition 
off-by-one is well reasoned. One gap: `ZoneOffsetCache` has no direct unit 
test. The existing `DateTimeUtilsSuite` truncation tests all go through the 
one-shot `truncTimestamp(micros, level, zoneId)` overload, which builds a 
*fresh* cache per call — so they confirm the offset *values* near DST 
transitions, but never exercise (a) the persistent cross-row reuse the codegen 
per-task cache depends on, nor (b) the on-transition interval boundary directly 
(the subtle part).
   
   Since the class is public, a small focused test would lock in the reasoning: 
consecutive `offsetSeconds` lookups within one window (hit), a lookup 
straddling a transition (miss + window reset), and a lookup with `epochSec` 
exactly on a transition instant (the `hi-1` anchor). Non-blocking.



-- 
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]

Reply via email to