From 8e910c77e99bda400011bed8cd847773a51c305d Mon Sep 17 00:00:00 2001 From: Vanshaj Poonia Date: Wed, 25 Nov 2026 04:45:39 +0530 Subject: [PATCH 1/2] fix(arrow-arith): return zeros when the required scale is far below the 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. --- arrow-arith/src/arithmetic.rs | 65 +++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/arrow-arith/src/arithmetic.rs b/arrow-arith/src/arithmetic.rs index 4592cc799429..3ff3b66ba42f 100644 --- a/arrow-arith/src/arithmetic.rs +++ b/arrow-arith/src/arithmetic.rs @@ -17,13 +17,19 @@ use arrow_schema::*; use std::cmp::min; use std::sync::Arc; +/// The largest `n` for which `10^n` fits in an `i256`. +const MAX_I256_POW10: i16 = 76; + /// Returns the precision and scale of the result of a multiplication of two decimal types, /// and the divisor for fixed point multiplication. +/// +/// The divisor is `None` when it does not fit in an `i256`. A product of two `i128` values is +/// below `10^77 / 2`, so every product then rounds to zero. fn get_fixed_point_info( left: (u8, i8), right: (u8, i8), required_scale: i8, -) -> Result<(u8, i8, i256), ArrowError> { +) -> Result<(u8, i8, Option), ArrowError> { let product_scale = left.1 + right.1; let precision = min(left.0 + right.0 + 1, DECIMAL128_MAX_PRECISION); @@ -33,7 +39,9 @@ fn get_fixed_point_info( ))); } - let divisor = i256::from_i128(10).pow_wrapping((product_scale - required_scale) as u32); + let exponent = i16::from(product_scale) - i16::from(required_scale); + let divisor = + (exponent <= MAX_I256_POW10).then(|| i256::from_i128(10).pow_wrapping(exponent as u32)); Ok((precision, product_scale, divisor)) } @@ -46,6 +54,7 @@ fn get_fixed_point_info( /// will be rounded to the required scale. /// /// If the required scale is greater than the product scale, an error is returned. +/// If it is more than 76 below the product scale, every product rounds to zero. /// /// This doesn't detect overflow. Once overflowing, the result will wrap around. /// @@ -80,6 +89,7 @@ pub fn multiply_fixed_point_dyn( /// will be rounded to the required scale. /// /// If the required scale is greater than the product scale, an error is returned. +/// If it is more than 76 below the product scale, every product rounds to zero. /// /// It is implemented for compatibility with precision loss `multiply` function provided by /// other data processing engines. For multiplication with precision loss detection, use @@ -100,6 +110,11 @@ pub fn multiply_fixed_point_checked( .with_precision_and_scale(precision, required_scale); } + let Some(divisor) = divisor else { + return try_binary::<_, _, _, Decimal128Type>(left, right, |_, _| Ok(0))? + .with_precision_and_scale(precision, required_scale); + }; + let round = divide_and_round(divisor); let product = try_binary::<_, _, _, Decimal128Type>(left, right, |a, b| { let a = i256::from_i128(a); @@ -122,6 +137,7 @@ pub fn multiply_fixed_point_checked( /// will be rounded to the required scale. /// /// If the required scale is greater than the product scale, an error is returned. +/// If it is more than 76 below the product scale, every product rounds to zero. /// /// This doesn't detect overflow. Once overflowing, the result will wrap around. /// For an overflow-checking variant, use `multiply_fixed_point_checked` instead. @@ -145,6 +161,10 @@ pub fn multiply_fixed_point( .with_precision_and_scale(precision, required_scale); } + let Some(divisor) = divisor else { + return binary(left, right, |_, _| 0)?.with_precision_and_scale(precision, required_scale); + }; + let round = divide_and_round(divisor); let product = binary::<_, _, _, Decimal128Type>(left, right, |a, b| { let a = i256::from_i128(a); @@ -372,4 +392,45 @@ mod tests { assert_eq!(multiply_fixed_point_checked(&a, &b, 0).unwrap(), expected); } } + + #[test] + fn test_decimal_multiply_fixed_point_required_scale_far_below_product_scale() { + let a = Decimal128Array::from(vec![Some(0), Some(5), Some(5), Some(i128::MAX), None]) + .with_precision_and_scale(38, 0) + .unwrap(); + let b = Decimal128Array::from(vec![Some(0), Some(5), Some(-5), Some(i128::MAX), Some(1)]) + .with_precision_and_scale(38, 0) + .unwrap(); + + // 10^76 is the largest power of ten that fits in an i256. Beyond it every product is zero. + let at_limit = Decimal128Array::from(vec![Some(0), Some(0), Some(0), Some(3), None]) + .with_precision_and_scale(38, -76) + .unwrap(); + assert_eq!(multiply_fixed_point(&a, &b, -76).unwrap(), at_limit); + assert_eq!(multiply_fixed_point_checked(&a, &b, -76).unwrap(), at_limit); + + for required_scale in [-77, -100, -128] { + let expected = Decimal128Array::from(vec![Some(0), Some(0), Some(0), Some(0), None]) + .with_precision_and_scale(38, required_scale) + .unwrap(); + assert_eq!( + multiply_fixed_point(&a, &b, required_scale).unwrap(), + expected + ); + assert_eq!( + multiply_fixed_point_checked(&a, &b, required_scale).unwrap(), + expected + ); + } + + // The exponent 76 - -90 does not fit in an i8 + let c = Decimal128Array::from(vec![Some(i128::MIN), None]) + .with_precision_and_scale(38, 38) + .unwrap(); + let expected = Decimal128Array::from(vec![Some(0), None]) + .with_precision_and_scale(38, -90) + .unwrap(); + assert_eq!(multiply_fixed_point(&c, &c, -90).unwrap(), expected); + assert_eq!(multiply_fixed_point_checked(&c, &c, -90).unwrap(), expected); + } } From 13dd80add000f66dc0b1630cac654c2a3879cf46 Mon Sep 17 00:00:00 2001 From: Vanshaj Poonia Date: Fri, 27 Nov 2026 11:54:13 +0530 Subject: [PATCH 2/2] fix(arrow-arith): add the input scales as i16 in multiply_fixed_point 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. --- arrow-arith/src/arithmetic.rs | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/arrow-arith/src/arithmetic.rs b/arrow-arith/src/arithmetic.rs index 3ff3b66ba42f..d3a98fffb507 100644 --- a/arrow-arith/src/arithmetic.rs +++ b/arrow-arith/src/arithmetic.rs @@ -29,17 +29,18 @@ fn get_fixed_point_info( left: (u8, i8), right: (u8, i8), required_scale: i8, -) -> Result<(u8, i8, Option), ArrowError> { - let product_scale = left.1 + right.1; +) -> Result<(u8, i16, Option), ArrowError> { + // Two i8 scales can add up to a value outside i8, such as -80 + -80 + let product_scale = i16::from(left.1) + i16::from(right.1); let precision = min(left.0 + right.0 + 1, DECIMAL128_MAX_PRECISION); - if required_scale > product_scale { + if i16::from(required_scale) > product_scale { return Err(ArrowError::ComputeError(format!( "Required scale {required_scale} is greater than product scale {product_scale}", ))); } - let exponent = i16::from(product_scale) - i16::from(required_scale); + let exponent = product_scale - i16::from(required_scale); let divisor = (exponent <= MAX_I256_POW10).then(|| i256::from_i128(10).pow_wrapping(exponent as u32)); @@ -105,7 +106,7 @@ pub fn multiply_fixed_point_checked( required_scale, )?; - if required_scale == product_scale { + if i16::from(required_scale) == product_scale { return try_binary::<_, _, _, Decimal128Type>(left, right, |a, b| a.mul_checked(b))? .with_precision_and_scale(precision, required_scale); } @@ -156,7 +157,7 @@ pub fn multiply_fixed_point( required_scale, )?; - if required_scale == product_scale { + if i16::from(required_scale) == product_scale { return binary(left, right, |a, b| a.mul_wrapping(b))? .with_precision_and_scale(precision, required_scale); } @@ -432,5 +433,21 @@ mod tests { .unwrap(); assert_eq!(multiply_fixed_point(&c, &c, -90).unwrap(), expected); assert_eq!(multiply_fixed_point_checked(&c, &c, -90).unwrap(), expected); + + // The product scale -80 + -80 does not fit in an i8, and every required scale is above it + let d = Decimal128Array::from(vec![1_i128]) + .with_precision_and_scale(1, -80) + .unwrap(); + for result in [ + multiply_fixed_point(&d, &d, -100), + multiply_fixed_point_checked(&d, &d, -100), + ] { + assert!( + result + .unwrap_err() + .to_string() + .contains("Required scale -100 is greater than product scale -160") + ); + } } }