Repository navigation
fix(arrow-arith): return zeros when the required scale is far below the product scale - #12459
VanshajPoonia wants to merge 2 commits into
Conversation
…he product scale multiply_fixed_point and multiply_fixed_point_checked wrapped the divisor 10^(product_scale - required_scale) once the exponent passed 76, and the exponent itself overflowed an i8 past 127. Every product of two i128 values rounds to zero at that point, so return zeros.
|
On Save this as use arrow_arith::arithmetic::multiply_fixed_point_checked;
use arrow_array::Decimal128Array;
#[test]
fn rejects_required_scale_above_product_scale() {
let a = Decimal128Array::from(vec![1_i128])
.with_precision_and_scale(1, -80)
.unwrap();
assert!(multiply_fixed_point_checked(&a, &a, -100).is_err());
}The input scales are still added as Could you convert both input scales to I used AI assistance for the review, test, and wording, and verified the behavior on both commits in an isolated environment. |
The two input scales were added as i8. With both at -80 the sum -160 wrapped to 96 in a release build, so a required scale of -100 passed the "greater than product scale" check and returned zeros. A debug build panicked on the overflow. The product scale is now an i16, and the regression test covers that case.
|
Thanks, good catch. Fixed in 13dd80a. As |
Which issue does this PR close?
multiply_fixed_pointreturns wrong values or panics when the required scale is more than 76 below the product scale #11605Rationale for this change
See #11605. When product_scale minus required_scale is 77 or more, 10^exponent does not fit in an i256 and wraps, so a zero product comes back as 1. Past 127 the exponent overflows an i8 and panics in debug builds.
What changes are included in this PR?
get_fixed_point_info computes the exponent as an i16 and returns the divisor only when it is at most 76. Above that both functions return zeros. Nulls stay null. The doc comments say so. It also adds the two input scales as an i16. Because of that, inputs at scale -80 give a product scale of -160 and a required scale of -100 is rejected, instead of the sum wrapping to 96.
Are these changes tested?
test_decimal_multiply_fixed_point_required_scale_far_below_product_scale covers scale -76 (rounds to 3), -77, -100, -128, the i8 overflow case, two scale -80 inputs with a required scale of -100, a null slot and both functions. It fails on main. cargo test -p arrow-arith, fmt, clippy and doc pass. I did not run typos or the workspace tests.
Are there any significant user-facing changes?
Calls with a required scale far below the product scale now return zeros instead of wrong values or a panic.
AI usage disclosure
AI usage: I used Claude Code to draft the change and this description. I have gone through every line manually and ran the checks above.