Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 84 additions & 6 deletions arrow-arith/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,32 @@ 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> {
let product_scale = left.1 + right.1;
) -> Result<(u8, i16, Option<i256>), 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 divisor = i256::from_i128(10).pow_wrapping((product_scale - required_scale) as u32);
let exponent = 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))
}
Expand All @@ -46,6 +55,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.
///
Expand Down Expand Up @@ -80,6 +90,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
Expand All @@ -95,11 +106,16 @@ 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);
}

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);
Expand All @@ -122,6 +138,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.
Expand All @@ -140,11 +157,15 @@ 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);
}

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);
Expand Down Expand Up @@ -372,4 +393,61 @@ 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);

// 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")
);
}
}
}