marko-k0 opened a new pull request, #27796:
URL: https://github.com/apache/datafusion/pull/27796

   ## Which issue does this PR close?
   
   - Closes #24894.
   
   ## Rationale for this change
   
   `DurationAvgAccumulator` accumulated its running sum with plain `+=` / `-`, 
so
   once the running sum exceeds the `i64` range the aggregate panics with
   "attempt to add with overflow" in debug builds (and silently wraps in release
   builds). Every other `avg` accumulator, the `sum` aggregate, and
   `arrow::compute::sum` wrap on overflow (see the docs on `add_avg_sum`), so 
this
   was the only accumulator whose behaviour depended on the build profile.
   
   Repro before the change (debug build):
   
   ```sql
   SELECT avg(x) OVER () FROM (VALUES (arrow_cast(9223372036854775807, 
'Duration(Second)'))) t(x);
   -- panicked at datafusion/functions-aggregate/src/average.rs:889: attempt to 
add with overflow
   ```
   
   ## What changes are included in this PR?
   
   - `update_batch` and `merge_batch` use `add_wrapping` instead of `+=`.
   - `retract_batch` uses `sub_wrapping`, and `get_or_insert(0)` replaces an
     `unwrap()` that could panic independently of overflow.
   - Regression tests for the update, merge, and retract underflow paths, plus a
     guard test for ordinary sliding-window retraction.
   
   ## What is the testing strategy for this PR?
   
   Unit tests in `average.rs`'s `mod tests`:
   
   - `avg_duration_accumulator_wraps_on_overflow` (`update_batch`)
   - `avg_duration_accumulator_merge_wraps_on_overflow` (`merge_batch`)
   - `avg_duration_accumulator_retract_wraps_on_underflow` (`retract_batch`)
   
   All three panic on the pre-fix code in debug builds and assert the wrapped
   result afterwards (`i64::MAX + i64::MAX` wraps to `-2`, average `-1`).
   `avg_duration_accumulator_retract_updates_average` guards that normal
   retraction math is unchanged.
   
   ## Are there any user-facing changes?
   
   No public API changes. `avg` over `Duration` no longer panics in debug 
builds on
   overflow; it wraps, matching `sum`.
   


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