Skip to content

fix(arrow-arith): return zeros when the required scale is far below the product scale - #12459

Open
VanshajPoonia wants to merge 2 commits into
apache:mainfrom
VanshajPoonia:fix/11605-multiply-fixed-point-scale
Open

VanshajPoonia wants to merge 2 commits into
apache:mainfrom
VanshajPoonia:fix/11605-multiply-fixed-point-scale

Conversation

@VanshajPoonia

@VanshajPoonia VanshajPoonia commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

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

…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.
@boring-cyborg boring-cyborg Bot added the arrow-kernels Crates that transform arrow data (e.g. compute, cast) label Nov 25, 2026
@shihchengwei-lab

Copy link
Copy Markdown

On 8e910c77, multiply_fixed_point_checked succeeds and returns [0] in a release build when both input scales are -80 and the requested scale is -100. The product scale is -160, so the requested scale is greater than the product scale and the API should return an error.

Save this as arrow-arith/tests/scale_sum.rs and run cargo test --release -p arrow-arith --test scale_sum.

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 i8, so -160 wraps to 96 before the subtraction. The parent commit panics on this input. The PR reaches the new zero-result branch and returns success.

Could you convert both input scales to i16 before adding them and include this case in the regression test?

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.
@VanshajPoonia

Copy link
Copy Markdown
Contributor Author

Thanks, good catch. Fixed in 13dd80a. As get_fixed_point_info now adds the two input scales as an i16, two scale -80 inputs give a product scale of -160, and a required scale of -100 returns the "greater than product scale" error. I added your case to test_decimal_multiply_fixed_point_required_scale_far_below_product_scale for both functions. Without the change it panics on the overflow in a debug build and returns zeros in a release build. With it the arithmetic tests pass in both, and cargo test -p arrow-arith, fmt and clippy are clean.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow-kernels Crates that transform arrow data (e.g. compute, cast)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

multiply_fixed_point returns wrong values or panics when the required scale is more than 76 below the product scale

2 participants