choksi2212 opened a new pull request, #11611:
URL: https://github.com/apache/arrow-rs/pull/11611

   # Which issue does this PR close?
   
   - Closes #11600
   
   # Rationale for this change
   
   `Date32Type`/`Date64Type`::`subtract_year_months_opt` and 
`subtract_month_day_nano_opt` negate the interval months with a unary minus 
before handing them to `add_months_date`. When the months are `i32::MIN` the 
negation itself overflows, so in debug builds the `_opt` helpers panic instead 
of returning `None`:
   
   ```
   thread panicked at arrow-array/src/types.rs:1143:55:
   attempt to negate with overflow
   ```
   
   Every other out-of-range date arithmetic returns `None` from the `_opt` 
helpers, which `date_op` surfaces as `Date arithmetic overflow`. The timestamp 
kernels already handle `i32::MIN` months (`sub_months_datetime` subtracts 
through `unsigned_abs`), so only these four date helpers were affected.
   
   # What changes are included in this PR?
   
   The four negations now use `checked_neg()`, so the negation overflow returns 
`None` and the kernels report the same `Date arithmetic overflow` error as any 
other interval that moves the date out of range:
   
   - `Date32Type::subtract_year_months_opt`
   - `Date32Type::subtract_month_day_nano_opt`
   - `Date64Type::subtract_year_months_opt`
   - `Date64Type::subtract_month_day_nano_opt`
   
   `add_months_date` is untouched — it already handles months of any magnitude 
via `unsigned_abs`.
   
   # Are there any user-facing changes?
   
   No API changes. Subtracting an `i32::MIN`-months interval from a 
`Date32`/`Date64` array no longer panics in debug builds; it returns the 
existing `Date arithmetic overflow` error, matching the already-correct 
behavior of every other out-of-range interval.
   
   One note on release builds (overflow checks off): the wrapped `-i32::MIN` 
still lands in `add_months_date`'s `unsigned_abs` branch, which returns `None` 
for a ~179-million-year shift, so release builds were already reporting the 
overflow error. That part is reasoned from the code; everything below is from 
actual runs.
   
   # Testing
   
   - New kernel-level test `test_date_sub_interval_min_months` covers the repro 
from the issue for all four type/interval combinations and asserts the `Date 
arithmetic overflow` error. On unpatched `main` it panics with `attempt to 
negate with overflow` at `arrow-array/src/types.rs:1143:55`; with this patch it 
passes.
   - `test_year_month_ops!` and `test_month_day_nano_ops!` gain `i32::MIN` 
cases for both `Date32Type` and `Date64Type`: subtract must return `None`, and 
the corresponding `add` with `i32::MIN` months is pinned as the out-of-range 
control.
   - `cargo test -p arrow-arith`: 256 passed, 0 failed. `cargo test -p 
arrow-array`: 946 passed, 0 failed. `cargo fmt --all -- --check` and `cargo 
clippy -p arrow-arith -p arrow-array --all-targets` clean (rustc 1.99.0, Linux).
   


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

Reply via email to