andygrove opened a new issue, #5094:
URL: https://github.com/apache/datafusion-comet/issues/5094

   ### What is the problem the feature request solves?
   
   `native/spark-expr/src/math_funcs/internal/decimal_rescale_check.rs` 
hand-rolls a fused Decimal128 rescale plus precision check: checked multiply on 
scale-up, add-half-then-divide HALF_UP on scale-down, bounds check against 
`10^p - 1`, an `i128::MAX` sentinel plus `null_if_overflow_precision` two-pass 
dance in legacy mode, and an error in ANSI mode.
   
   Arrow's decimal-to-decimal `cast_with_options` implements the same thing in 
one pass: it rescales, rounds half-away-from-zero on scale reduction (matching 
HALF_UP for both signs), validates target precision, and either nulls (`safe: 
true`) or errors (`safe: false`) on overflow, with no sentinel needed.
   
   ### Describe the potential solution
   
   Replace `rescale_and_check` with `cast_with_options(&array, 
&Decimal128(p_out, s_out), &CastOptions { safe: !fail_on_error, .. })`, keeping 
a thin wrapper to map the ANSI error (the current implementation also raises a 
generic compute error there, so no fidelity is lost).
   
   ### Additional context
   
   - The file exists as a deliberate fusion of `CheckOverflow(Cast(...))` into 
one pass, but arrow's cast kernel is itself a single pass doing both jobs; 
benchmark before switching (`spark-expr/benches/decimal_rescale.rs` exists).
   - The `i128::MAX` sentinel aliasing is not user-visible today (a legitimate 
value of `i128::MAX` always exceeds precision 38 bounds and would be nulled 
anyway), but removing the sentinel simplifies reasoning.
   
   Found during an audit of native code that replicates existing arrow-rs 
kernels.
   


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