Subodh-17 opened a new pull request, #12389:
URL: https://github.com/apache/arrow-rs/pull/12389

   # Which issue does this PR close?
   
   Closes #11589
   
   # Rationale for this change
   
   When casting decimals with an extreme scale increase where `output_scale - 
input_scale > O::MAX_PRECISION`, `make_upscaler` returns `None` because the 
scale delta exceeds the precomputed power-of-ten table 
`O::MAX_FOR_EACH_PRECISION`.
   
   Previously, `convert_to_bigger_or_equal_scale_decimal` unconditionally 
returned an unhandled `ArrowError::CastError("... Value overflows for output 
scale")` when `make_upscaler` returned `None`. This caused two correctness 
issues:
   1. Safe casts (`CastOptions { safe: true, .. }`) failed with an `Err` 
instead of returning `None` (null) for overflowing elements.
   2. Individual representable values such as `0` (`0 * 10^k = 0`) failed with 
an error even though zero never overflows any precision or scale.
   
   Similarly, in `rescale_decimal`, when upscaling exceeded the precision 
table, it returned `None` even for `0`, while downscaling (`input_scale > 
output_scale`) already had fallback logic returning `(value == 
ZERO).then_some(ZERO)`.
   
   # What changes are included in this PR?
   
   1. **Array Casting (`convert_to_bigger_or_equal_scale_decimal`)**:
      When `make_upscaler` returns `None`, construct a fallible mapping closure 
`move |x| (x == I::Native::ZERO).then_some(O::Native::ZERO)` and route through 
`apply_decimal_cast`.
      - When `safe: true`, non-zero elements map to `None` (null slots in the 
output array), while zero values are preserved.
      - When `safe: false`, non-zero elements produce the expected 
`ArrowError::CastError("Cannot cast to ... Overflowing on <x>")`.
   2. **Scalar Rescaling (`rescale_decimal`)**:
      When `make_upscaler` returns `None`, return `(value == 
I::Native::ZERO).then_some(O::Native::ZERO)`, providing symmetry with the 
existing downscaling fallback path.
   3. **Docstrings**:
      Updated docstrings on `make_upscaler` and `rescale_decimal` to note that 
zero values remain representable when scaling exceeds precision tables.
   
   # Are these changes tested?
   
   Yes:
   - **Unit test**: Added `test_rescale_decimal_upscale_large_delta` in 
`arrow-cast/src/cast/decimal.rs` verifying scalar upscaling with large scale 
delta for `Decimal128` and `Decimal32`.
   - **Integration tests**:
     - Added `test_cast_decimal_extreme_scale_increase` in 
`arrow-cast/tests/cast/decimal.rs` covering:
       - Decimal128 ($\Delta s = 40$), Decimal32 ($\Delta s = 19$), Decimal64 
($\Delta s = 33$), Decimal256 ($\Delta s = 152$).
       - Cross-type decimal cast (`Decimal32` to `Decimal64`).
       - Array topologies: mixed elements, all-zeros, all-nulls, and empty 
arrays.
       - Precision table boundary transitions ($\Delta s = 38$ vs $\Delta s = 
39$).
     - Updated `test_cast_decimal256_extreme_scale_difference` to verify null 
outputs under `safe: true`.
   
   Verified locally:
   - `cargo fmt --check` (clean)
   - `cargo clippy -p arrow-cast -- -D warnings` (clean, 0 warnings)
   - `cargo test -p arrow-cast --lib decimal` (21 passed)
   - `cargo test -p arrow-cast --test cast decimal` (92 passed)
   - `cargo test -p parquet-variant-compute` (369 unit tests + 15 doctests 
passed)
   
   # Are there any significant user-facing changes?
   
   No breaking changes. Safe decimal casts with scale increases exceeding 
destination precision tables now correctly return nulls instead of returning an 
unhandled error, and zero values are preserved.
   


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