You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "vranes (via GitHub)" <gi...@apache.org> on 2026/07/21 12:53:37 UTC

[PR] [SPARK-58237][SQL] Fix BIN BY multi-day civil-time bin boundaries to use the absolute grid [spark]

vranes opened a new pull request, #57401:
URL: https://github.com/apache/spark/pull/57401

   ### What changes were proposed in this pull request?
   
   For `TIMESTAMP` (LTZ) inputs in a non-UTC session, `BinByExec` placed multi-day civil-time bin boundaries by forward-walking (each bin's end reused as the next bin's start) rather than computing every boundary independently from the origin on the absolute grid `bin_start(k) = timestampAddDayTime(origin, k * width)` that `time_bucket` uses. This PR changes `BinByExec` to compute every boundary on that absolute grid.
   
   - `DateTimeUtils.timeBucketDTInterval` is factored into two reusable pieces, and defined in terms of them:
     - `timeBucketIndexDTInterval(width, ts, origin, zone)`: the index of the bucket containing `ts`.
     - `timeBucketBoundaryDTInterval(width, k, origin, zone)`: the grid boundary of bucket index `k`.
     - `timeBucketDTInterval` becomes `boundary(index(ts))`, so it is behavior-preserving for `time_bucket` (its only other caller); the existing `DateTimeUtilsSuite` cases are the regression guard.
   - `BinByExec` advances the bucket index and reads each boundary off the grid via `timeBucketBoundaryDTInterval`, instead of walking with `timestampAddDayTime`. The zero-length-range path computes `bin_end` as the next grid boundary for the same reason.
   
   ### Why are the changes needed?
   
   `timestampAddDayTime` adds the width's calendar-day part as calendar days (which absorb DST: a calendar day is 23h or 25h of real time across a transition) and the sub-day remainder as real microseconds, so it is not associative across a DST or offset transition:
   
   ```
   addDayTime(addDayTime(origin, k*W), W) != addDayTime(origin, (k+1)*W)
   ```
   
   The forward walk (left side) therefore drifts off the grid (right side) by the transition hour. For a width with a non-whole-day part (for example `INTERVAL '36' HOUR`) whose bins span a DST transition, boundaries are wrong from the second emitted bin onward:
   
   - The same calendar bin is reported with a different `bin_start` / `bin_end` depending on where a row's range began (a row that walked into bin N vs. a row that started in bin N), so `GROUP BY bin_start` fragments one bin into two groups an hour apart.
   - At a whole-day zone skip (for example `Pacific/Apia` on 2011-12-30, which does not exist in that zone), two consecutive grid boundaries collapse to the same instant.
   
   Whole-day widths (`INTERVAL '1' DAY`, `'2' DAY`) were unaffected because the walk and the grid agree when the width has no sub-day remainder; sub-day widths use the UTC fast path and are already grid-aligned; `TIMESTAMP_NTZ` uses UTC arithmetic and is unaffected. The defect lived only in the emission of the second and later bins of a row, a path `time_bucket` does not have (it returns one bucket per row), so the shared `time_bucket` tests could not surface it, and the prior `BinBySuite` DST tests all used whole-day widths.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, a bug fix for a not-yet-released feature (BIN BY is gated off by default and has no released version). For a `TIMESTAMP` (LTZ) input in a non-UTC session with a bin width that has a non-whole-day part (e.g. 36h), the `bin_start` / `bin_end` of the second and later bins of a range spanning a DST transition now land on the absolute grid (matching `time_bucket`) instead of drifting by the transition hour. Whole-day widths, sub-day widths, and `TIMESTAMP_NTZ` inputs are unchanged.
   
   ### How was this patch tested?
   
   - `DateTimeUtilsSuite`: the existing `timeBucketDTInterval` cases are unchanged (regression guard proving the factoring is behavior-preserving), plus a new round-trip test that `boundary(index(ts)) == timeBucketDTInterval(ts)` across sub-day, 36h spring-forward, and fall-back cases, and a whole-day `Pacific/Apia` skip where two consecutive boundaries collapse.
   - `BinBySuite`: new tests that (1) 36h boundaries across a spring-forward spanning several bins land on the grid, (2) two rows reaching the same bin from different starts report the identical boundary, (3) a whole-day `Pacific/Apia` skip emits the collapsed zero-width ratio-0 bin while the real bins still tile the range, and (4) a zero-length range under a 36h width places `bin_end` on the grid. The multi-bin and row-independence tests were confirmed to fail on the previous forward-walk code and pass after the fix.
   - `SQLQueryTestSuite -- -z bin-by`: the golden file is unchanged (its cases are UTC sub-day, unaffected by this fix).
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Anthropic)
   


-- 
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-58237][SQL] Fix BIN BY multi-day civil-time bin boundaries to use the absolute grid [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #57401:
URL: https://github.com/apache/spark/pull/57401#discussion_r3630614608


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala:
##########
@@ -1266,47 +1266,78 @@ object DateTimeUtils extends SparkDateTimeUtils {
   }
 
   /**
-   * DayTimeInterval bucketing: bucket k starts at
-   * `timestampAddDayTime(originMicros, k * bucketMicros, zoneId)`, matching the instant that
-   * `originMicros + INTERVAL '<k * bucketSize>'` would produce. For sub-day buckets the
-   * calendar-day component is zero, so the result is pure UTC-microsecond floor division
-   * and `zoneId` has no effect.
+   * Start boundary of DayTimeInterval bucket index `k`, on the fixed grid anchored at
+   * `originMicros`. Sub-day buckets use pure UTC arithmetic; multi-day buckets add the calendar-day
+   * part in `zoneId`.
    *
    * `bucketMicros` must be positive; `TimeBucket.checkInputDataTypes` enforces this at
    * analysis time.
    *
    * @param bucketMicros bucket size in microseconds.
-   * @param tsMicros     timestamp to bucket, in microseconds since the epoch (UTC).
+   * @param k            bucket index (may be negative).
    * @param originMicros grid alignment anchor, in microseconds since the epoch (UTC).
    * @param zoneId       zone in which calendar-day arithmetic is performed.
    */
-  def timeBucketDTInterval(
-      bucketMicros: Long, tsMicros: Long, originMicros: Long, zoneId: ZoneId): Long = {
-    val bucketDays = bucketMicros / MICROS_PER_DAY
+  def timeBucketFromIndexDTInterval(
+      bucketMicros: Long, k: Long, originMicros: Long, zoneId: ZoneId): Long = {
+    val bucketOffset = MathUtils.multiplyExact(k, bucketMicros)
 
+    if (bucketMicros / MICROS_PER_DAY != 0) {
+      timestampAddDayTime(originMicros, bucketOffset, zoneId)
+    } else {
+      // Sub-day bucket stays zone-independent even when k*bucketMicros spans days.
+      MathUtils.addExact(originMicros, bucketOffset)
+    }
+  }
+
+  /**
+   * The DayTimeInterval bucket containing `tsMicros`, as `(index, startBoundary)`. The index is
+   * returned so callers walking to the next bucket need not search again.
+   *
+   * `bucketMicros` must be positive; `TimeBucket.checkInputDataTypes` enforces this at
+   * analysis time.
+   *
+   * @param bucketMicros bucket size in microseconds.
+   * @param tsMicros     timestamp to bucket, in microseconds since the epoch (UTC).
+   * @param originMicros grid alignment anchor, in microseconds since the epoch (UTC).
+   * @param zoneId       zone in which calendar-day arithmetic is performed.
+   */
+  def timeBucketFromTimestampDTInterval(
+      bucketMicros: Long, tsMicros: Long, originMicros: Long, zoneId: ZoneId): (Long, Long) = {
     val diff = MathUtils.subtractExact(tsMicros, originMicros)
-    var k = MathUtils.floorDiv(diff, bucketMicros)
+    val k = MathUtils.floorDiv(diff, bucketMicros)
 
-    if (bucketDays == 0) {
-      val bucketOffset = MathUtils.multiplyExact(k, bucketMicros)
-      MathUtils.addExact(originMicros, bucketOffset)
-    } else {
-      // bucketMicros >= MICROS_PER_DAY, so DST offset shifts (a few hours at most) can
-      // move candidate(k) within one bucket of `origin + k*bucketMicros` but no further.
-      // One +/-1 step recovers the correct k.
-      def candidate(kk: Long): Long =
-        timestampAddDayTime(originMicros, MathUtils.multiplyExact(kk, bucketMicros), zoneId)
-
-      var c = candidate(k)
-      if (c > tsMicros) {
-        k -= 1
-        c = candidate(k)
-      } else {
-        val cNext = candidate(MathUtils.addExact(k, 1L))
-        if (cNext <= tsMicros) c = cNext
+    def boundary(kk: Long): Long =
+      timeBucketFromIndexDTInterval(bucketMicros, kk, originMicros, zoneId)
+
+    val bucketStart = boundary(k)
+    if (bucketMicros / MICROS_PER_DAY != 0) {
+      // Multi-day: a DST offset shift moves the boundary by less than one bucket, so one +/-1 step
+      // recovers the correct index.
+      if (bucketStart > tsMicros) {
+        return (k - 1, boundary(k - 1))
+      }
+      val nextStart = boundary(MathUtils.addExact(k, 1L))
+      if (nextStart <= tsMicros) {

Review Comment:
   The containing-bucket helper must establish `boundary(k) <= ts < boundary(k + 1)`, but a single correction step does not always do that. For `Pacific/Apia` with a one-day bucket, origin `1900-01-01 00:00`, and timestamp `2012-01-02 00:00`, the raw index is 40906 and both boundaries 40907 and 40908 are at or before the timestamp, so this returns the previous civil day. Please adjust until the full containment invariant holds and add this distant-origin case as a regression test.



-- 
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-58237][SQL] Fix BIN BY multi-day civil-time bin boundaries to use the absolute grid [spark]

Posted by "vranes (via GitHub)" <gi...@apache.org>.
vranes commented on code in PR #57401:
URL: https://github.com/apache/spark/pull/57401#discussion_r3634478824


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala:
##########
@@ -1266,47 +1266,78 @@ object DateTimeUtils extends SparkDateTimeUtils {
   }
 
   /**
-   * DayTimeInterval bucketing: bucket k starts at
-   * `timestampAddDayTime(originMicros, k * bucketMicros, zoneId)`, matching the instant that
-   * `originMicros + INTERVAL '<k * bucketSize>'` would produce. For sub-day buckets the
-   * calendar-day component is zero, so the result is pure UTC-microsecond floor division
-   * and `zoneId` has no effect.
+   * Start boundary of DayTimeInterval bucket index `k`, on the fixed grid anchored at
+   * `originMicros`. Sub-day buckets use pure UTC arithmetic; multi-day buckets add the calendar-day
+   * part in `zoneId`.
    *
    * `bucketMicros` must be positive; `TimeBucket.checkInputDataTypes` enforces this at
    * analysis time.
    *
    * @param bucketMicros bucket size in microseconds.
-   * @param tsMicros     timestamp to bucket, in microseconds since the epoch (UTC).
+   * @param k            bucket index (may be negative).
    * @param originMicros grid alignment anchor, in microseconds since the epoch (UTC).
    * @param zoneId       zone in which calendar-day arithmetic is performed.
    */
-  def timeBucketDTInterval(
-      bucketMicros: Long, tsMicros: Long, originMicros: Long, zoneId: ZoneId): Long = {
-    val bucketDays = bucketMicros / MICROS_PER_DAY
+  def timeBucketFromIndexDTInterval(
+      bucketMicros: Long, k: Long, originMicros: Long, zoneId: ZoneId): Long = {
+    val bucketOffset = MathUtils.multiplyExact(k, bucketMicros)
 
+    if (bucketMicros / MICROS_PER_DAY != 0) {
+      timestampAddDayTime(originMicros, bucketOffset, zoneId)
+    } else {
+      // Sub-day bucket stays zone-independent even when k*bucketMicros spans days.
+      MathUtils.addExact(originMicros, bucketOffset)
+    }
+  }
+
+  /**
+   * The DayTimeInterval bucket containing `tsMicros`, as `(index, startBoundary)`. The index is
+   * returned so callers walking to the next bucket need not search again.
+   *
+   * `bucketMicros` must be positive; `TimeBucket.checkInputDataTypes` enforces this at
+   * analysis time.
+   *
+   * @param bucketMicros bucket size in microseconds.
+   * @param tsMicros     timestamp to bucket, in microseconds since the epoch (UTC).
+   * @param originMicros grid alignment anchor, in microseconds since the epoch (UTC).
+   * @param zoneId       zone in which calendar-day arithmetic is performed.
+   */
+  def timeBucketFromTimestampDTInterval(
+      bucketMicros: Long, tsMicros: Long, originMicros: Long, zoneId: ZoneId): (Long, Long) = {
     val diff = MathUtils.subtractExact(tsMicros, originMicros)
-    var k = MathUtils.floorDiv(diff, bucketMicros)
+    val k = MathUtils.floorDiv(diff, bucketMicros)
 
-    if (bucketDays == 0) {
-      val bucketOffset = MathUtils.multiplyExact(k, bucketMicros)
-      MathUtils.addExact(originMicros, bucketOffset)
-    } else {
-      // bucketMicros >= MICROS_PER_DAY, so DST offset shifts (a few hours at most) can
-      // move candidate(k) within one bucket of `origin + k*bucketMicros` but no further.
-      // One +/-1 step recovers the correct k.
-      def candidate(kk: Long): Long =
-        timestampAddDayTime(originMicros, MathUtils.multiplyExact(kk, bucketMicros), zoneId)
-
-      var c = candidate(k)
-      if (c > tsMicros) {
-        k -= 1
-        c = candidate(k)
-      } else {
-        val cNext = candidate(MathUtils.addExact(k, 1L))
-        if (cNext <= tsMicros) c = cNext
+    def boundary(kk: Long): Long =
+      timeBucketFromIndexDTInterval(bucketMicros, kk, originMicros, zoneId)
+
+    val bucketStart = boundary(k)
+    if (bucketMicros / MICROS_PER_DAY != 0) {
+      // Multi-day: a DST offset shift moves the boundary by less than one bucket, so one +/-1 step
+      // recovers the correct index.
+      if (bucketStart > tsMicros) {
+        return (k - 1, boundary(k - 1))
+      }
+      val nextStart = boundary(MathUtils.addExact(k, 1L))
+      if (nextStart <= tsMicros) {

Review Comment:
   Good catch, fixed. Replaced the single step with a loop that walks until boundary(k) <= ts < boundary(k + 1), and added Apia regression tests for epoch and 1900 origins.



-- 
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-58237][SQL] Fix BIN BY multi-day civil-time bin boundaries to use the absolute grid [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #57401:
URL: https://github.com/apache/spark/pull/57401#issuecomment-5059250800

   **Merge Summary:**
   - merged into master https://github.com/apache/spark/commit/07144c0bfcb2a80b77a93631121ab31a743675de
   - merged into branch-4.x https://github.com/apache/spark/commit/8dfbdd75ca26d8b60b6e92b25d2275fe56211f8b
   
   *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-58237][SQL] Fix BIN BY multi-day civil-time bin boundaries to use the absolute grid [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #57401: [SPARK-58237][SQL] Fix BIN BY multi-day civil-time bin boundaries to use the absolute grid
URL: https://github.com/apache/spark/pull/57401


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