tustvold commented on code in PR #3690:
URL: https://github.com/apache/arrow-rs/pull/3690#discussion_r1136889146
##########
arrow-arith/src/arithmetic.rs:
##########
@@ -1165,6 +1168,78 @@ pub fn multiply_dyn_checked(
}
}
+/// Perform `left * right` operation on two decimal arrays. If either left or
right value is
+/// null then the result is also null.
+///
+/// This performs decimal multiplication which allows precision loss if an
exact representation
+/// is not possible for the result, according to the required scale. In the
case, the result
+/// will be rounded to the required scale.
+///
+/// If the required scale is greater than the product scale, an error is
returned.
+///
+/// It is implemented for compatibility with precision loss `multiply`
function provided by
+/// other data processing engines. For multiplication with precision loss
detection, use
+/// `multiply` or `multiply_checked` instead.
+pub fn mul_fixed_point_checked(
Review Comment:
```suggestion
pub fn multiply_fixed_point_checked(
```
##########
arrow-arith/src/arithmetic.rs:
##########
@@ -1165,6 +1168,78 @@ pub fn multiply_dyn_checked(
}
}
+/// Perform `left * right` operation on two decimal arrays. If either left or
right value is
+/// null then the result is also null.
+///
+/// This performs decimal multiplication which allows precision loss if an
exact representation
+/// is not possible for the result, according to the required scale. In the
case, the result
+/// will be rounded to the required scale.
+///
+/// If the required scale is greater than the product scale, an error is
returned.
+///
+/// It is implemented for compatibility with precision loss `multiply`
function provided by
+/// other data processing engines. For multiplication with precision loss
detection, use
+/// `multiply` or `multiply_checked` instead.
+pub fn mul_fixed_point_checked(
+ left: &PrimitiveArray<Decimal128Type>,
+ right: &PrimitiveArray<Decimal128Type>,
+ required_scale: i8,
+) -> Result<PrimitiveArray<Decimal128Type>, ArrowError> {
+ let product_scale = left.scale() + right.scale();
+
+ if required_scale == product_scale {
+ return multiply_checked(left, right);
+ }
+
+ if required_scale > product_scale {
+ return Err(ArrowError::ComputeError(format!(
+ "Required scale {} is greater than product scale {}",
+ required_scale, product_scale
+ )));
+ }
+
+ let precision = min(
+ left.precision() + right.precision() + 1,
+ DECIMAL128_MAX_PRECISION,
+ );
+ let divisor =
+ i256::from_i128(10).pow_wrapping((product_scale - required_scale) as
u32);
+
+ try_binary::<_, _, _, Decimal128Type>(left, right, |a, b| {
+ let a = i256::from_i128(a);
+ let b = i256::from_i128(b);
+
+ let mut mul = a.wrapping_mul(b);
+ if required_scale < product_scale {
+ mul = divide_and_round::<Decimal256Type>(mul, divisor);
+ }
Review Comment:
```suggestion
mul = divide_and_round::<Decimal256Type>(mul, divisor);
```
I believe this check is redundant as it is checked above
##########
arrow-arith/src/arithmetic.rs:
##########
@@ -1165,6 +1168,78 @@ pub fn multiply_dyn_checked(
}
}
+/// Perform `left * right` operation on two decimal arrays. If either left or
right value is
+/// null then the result is also null.
+///
+/// This performs decimal multiplication which allows precision loss if an
exact representation
+/// is not possible for the result, according to the required scale. In the
case, the result
+/// will be rounded to the required scale.
+///
+/// If the required scale is greater than the product scale, an error is
returned.
+///
+/// It is implemented for compatibility with precision loss `multiply`
function provided by
+/// other data processing engines. For multiplication with precision loss
detection, use
+/// `multiply` or `multiply_checked` instead.
+pub fn mul_fixed_point_checked(
+ left: &PrimitiveArray<Decimal128Type>,
+ right: &PrimitiveArray<Decimal128Type>,
+ required_scale: i8,
+) -> Result<PrimitiveArray<Decimal128Type>, ArrowError> {
+ let product_scale = left.scale() + right.scale();
+
+ if required_scale == product_scale {
+ return multiply_checked(left, right);
Review Comment:
```suggestion
return multiply_checked(left,
right)?.with_precision_and_scale(precision, required_scale);
```
I think because of https://github.com/apache/arrow-rs/issues/3307
And perhaps a test to confirm
##########
arrow-arith/src/arithmetic.rs:
##########
@@ -3231,4 +3306,91 @@ mod tests {
assert_eq!(&expected, &result);
}
+
+ #[test]
+ fn test_decimal_multiply_allow_precision_loss() {
+ // Overflow happening as i128 cannot hold multiplying result.
+ let a = Decimal128Array::from(vec![123456789000000000000000000])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let b = Decimal128Array::from(vec![10000000000000000000])
Review Comment:
```suggestion
// [10]
let b = Decimal128Array::from(vec![10000000000000000000])
```
##########
arrow-arith/src/arithmetic.rs:
##########
@@ -3231,4 +3306,91 @@ mod tests {
assert_eq!(&expected, &result);
}
+
+ #[test]
+ fn test_decimal_multiply_allow_precision_loss() {
+ // Overflow happening as i128 cannot hold multiplying result.
+ let a = Decimal128Array::from(vec![123456789000000000000000000])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let b = Decimal128Array::from(vec![10000000000000000000])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let err = multiply_dyn_checked(&a, &b).unwrap_err();
+ assert!(err.to_string().contains(
+ "Overflow happened on: 123456789000000000000000000 *
10000000000000000000"
+ ));
+
+ // Allow precision loss.
+ let result = mul_fixed_point_checked(&a, &b, 28).unwrap();
+ let expected =
Review Comment:
```suggestion
// [1234567890]
let expected =
```
##########
arrow-arith/src/arithmetic.rs:
##########
@@ -3231,4 +3306,91 @@ mod tests {
assert_eq!(&expected, &result);
}
+
+ #[test]
+ fn test_decimal_multiply_allow_precision_loss() {
+ // Overflow happening as i128 cannot hold multiplying result.
+ let a = Decimal128Array::from(vec![123456789000000000000000000])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let b = Decimal128Array::from(vec![10000000000000000000])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let err = multiply_dyn_checked(&a, &b).unwrap_err();
+ assert!(err.to_string().contains(
+ "Overflow happened on: 123456789000000000000000000 *
10000000000000000000"
+ ));
+
+ // Allow precision loss.
+ let result = mul_fixed_point_checked(&a, &b, 28).unwrap();
+ let expected =
+ Decimal128Array::from(vec![12345678900000000000000000000000000000])
+ .with_precision_and_scale(38, 28)
+ .unwrap();
+
+ assert_eq!(&expected, &result);
+ assert_eq!(
+ result.value_as_string(0),
+ "1234567890.0000000000000000000000000000"
+ );
+
+ // Rounding case
+ let a = Decimal128Array::from(vec![
Review Comment:
```suggestion
[0.000000000000000001, 123456789.555555555555555555,
1.555555555555555555]
let a = Decimal128Array::from(vec![
```
##########
arrow-arith/src/arithmetic.rs:
##########
@@ -3231,4 +3306,91 @@ mod tests {
assert_eq!(&expected, &result);
}
+
+ #[test]
+ fn test_decimal_multiply_allow_precision_loss() {
+ // Overflow happening as i128 cannot hold multiplying result.
+ let a = Decimal128Array::from(vec![123456789000000000000000000])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let b = Decimal128Array::from(vec![10000000000000000000])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let err = multiply_dyn_checked(&a, &b).unwrap_err();
+ assert!(err.to_string().contains(
+ "Overflow happened on: 123456789000000000000000000 *
10000000000000000000"
+ ));
+
+ // Allow precision loss.
+ let result = mul_fixed_point_checked(&a, &b, 28).unwrap();
+ let expected =
+ Decimal128Array::from(vec![12345678900000000000000000000000000000])
+ .with_precision_and_scale(38, 28)
+ .unwrap();
+
+ assert_eq!(&expected, &result);
+ assert_eq!(
+ result.value_as_string(0),
+ "1234567890.0000000000000000000000000000"
+ );
+
+ // Rounding case
+ let a = Decimal128Array::from(vec![
+ 1,
+ 123456789555555555555555555,
+ 1555555555555555555,
+ ])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let b = Decimal128Array::from(vec![1555555555555555555,
11222222222222222222, 1])
Review Comment:
```suggestion
// [1.555555555555555555, 11.222222222222222222,
0.000000000000000001]
let b = Decimal128Array::from(vec![1555555555555555555,
11222222222222222222, 1])
```
##########
arrow-arith/src/arithmetic.rs:
##########
@@ -3231,4 +3306,91 @@ mod tests {
assert_eq!(&expected, &result);
}
+
+ #[test]
+ fn test_decimal_multiply_allow_precision_loss() {
+ // Overflow happening as i128 cannot hold multiplying result.
+ let a = Decimal128Array::from(vec![123456789000000000000000000])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let b = Decimal128Array::from(vec![10000000000000000000])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let err = multiply_dyn_checked(&a, &b).unwrap_err();
+ assert!(err.to_string().contains(
+ "Overflow happened on: 123456789000000000000000000 *
10000000000000000000"
+ ));
+
+ // Allow precision loss.
+ let result = mul_fixed_point_checked(&a, &b, 28).unwrap();
+ let expected =
+ Decimal128Array::from(vec![12345678900000000000000000000000000000])
+ .with_precision_and_scale(38, 28)
+ .unwrap();
+
+ assert_eq!(&expected, &result);
+ assert_eq!(
+ result.value_as_string(0),
+ "1234567890.0000000000000000000000000000"
+ );
+
+ // Rounding case
+ let a = Decimal128Array::from(vec![
+ 1,
+ 123456789555555555555555555,
+ 1555555555555555555,
+ ])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let b = Decimal128Array::from(vec![1555555555555555555,
11222222222222222222, 1])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let result = mul_fixed_point_checked(&a, &b, 28).unwrap();
+ let expected = Decimal128Array::from(vec![
+ 15555555556,
+ 13854595272345679012071330528765432099,
+ 15555555556,
+ ])
+ .with_precision_and_scale(38, 28)
+ .unwrap();
+
+ assert_eq!(&expected, &result);
+
+ // Rounded the value "1385459527.234567901207133052876543209876543210".
+ assert_eq!(
+ result.value_as_string(1),
+ "1385459527.2345679012071330528765432099"
+ );
+ assert_eq!(result.value_as_string(0),
"0.0000000000000000015555555556");
+ assert_eq!(result.value_as_string(2),
"0.0000000000000000015555555556");
+
+ let a = Decimal128Array::from(vec![1230])
+ .with_precision_and_scale(4, 2)
+ .unwrap();
+
+ let b = Decimal128Array::from(vec![1000])
+ .with_precision_and_scale(4, 2)
+ .unwrap();
+
+ // Required scale is same as the product of the input scales. Behavior
is same as multiply.
+ let result = mul_fixed_point_checked(&a, &b, 4)
+ .unwrap()
+ .with_precision_and_scale(9, 4)
+ .unwrap();
+ let expected = multiply_checked(&a, &b)
+ .unwrap()
+ .with_precision_and_scale(9, 4)
+ .unwrap();
+ assert_eq!(&expected, &result);
Review Comment:
```suggestion
assert_eq!(&expected, &result);
assert_eq(result.precision(), 9);
assert_eq(result.scale(), 9);
```
##########
arrow-arith/src/arithmetic.rs:
##########
@@ -3231,4 +3306,91 @@ mod tests {
assert_eq!(&expected, &result);
}
+
+ #[test]
+ fn test_decimal_multiply_allow_precision_loss() {
+ // Overflow happening as i128 cannot hold multiplying result.
+ let a = Decimal128Array::from(vec![123456789000000000000000000])
Review Comment:
```suggestion
// [123456789]
let a = Decimal128Array::from(vec![123456789000000000000000000])
```
##########
arrow-arith/src/arithmetic.rs:
##########
@@ -3231,4 +3306,91 @@ mod tests {
assert_eq!(&expected, &result);
}
+
+ #[test]
+ fn test_decimal_multiply_allow_precision_loss() {
+ // Overflow happening as i128 cannot hold multiplying result.
+ let a = Decimal128Array::from(vec![123456789000000000000000000])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let b = Decimal128Array::from(vec![10000000000000000000])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let err = multiply_dyn_checked(&a, &b).unwrap_err();
+ assert!(err.to_string().contains(
+ "Overflow happened on: 123456789000000000000000000 *
10000000000000000000"
+ ));
+
+ // Allow precision loss.
+ let result = mul_fixed_point_checked(&a, &b, 28).unwrap();
+ let expected =
+ Decimal128Array::from(vec![12345678900000000000000000000000000000])
+ .with_precision_and_scale(38, 28)
+ .unwrap();
+
+ assert_eq!(&expected, &result);
+ assert_eq!(
+ result.value_as_string(0),
+ "1234567890.0000000000000000000000000000"
+ );
+
+ // Rounding case
+ let a = Decimal128Array::from(vec![
+ 1,
+ 123456789555555555555555555,
+ 1555555555555555555,
+ ])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let b = Decimal128Array::from(vec![1555555555555555555,
11222222222222222222, 1])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let result = mul_fixed_point_checked(&a, &b, 28).unwrap();
+ let expected = Decimal128Array::from(vec![
+ 15555555556,
+ 13854595272345679012071330528765432099,
+ 15555555556,
+ ])
+ .with_precision_and_scale(38, 28)
+ .unwrap();
+
+ assert_eq!(&expected, &result);
+
+ // Rounded the value "1385459527.234567901207133052876543209876543210".
Review Comment:
:+1:
##########
arrow-arith/src/arithmetic.rs:
##########
@@ -3231,4 +3306,91 @@ mod tests {
assert_eq!(&expected, &result);
}
+
+ #[test]
+ fn test_decimal_multiply_allow_precision_loss() {
+ // Overflow happening as i128 cannot hold multiplying result.
+ let a = Decimal128Array::from(vec![123456789000000000000000000])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let b = Decimal128Array::from(vec![10000000000000000000])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let err = multiply_dyn_checked(&a, &b).unwrap_err();
+ assert!(err.to_string().contains(
+ "Overflow happened on: 123456789000000000000000000 *
10000000000000000000"
+ ));
+
+ // Allow precision loss.
+ let result = mul_fixed_point_checked(&a, &b, 28).unwrap();
+ let expected =
+ Decimal128Array::from(vec![12345678900000000000000000000000000000])
+ .with_precision_and_scale(38, 28)
+ .unwrap();
+
+ assert_eq!(&expected, &result);
+ assert_eq!(
+ result.value_as_string(0),
+ "1234567890.0000000000000000000000000000"
+ );
+
+ // Rounding case
+ let a = Decimal128Array::from(vec![
+ 1,
+ 123456789555555555555555555,
+ 1555555555555555555,
+ ])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let b = Decimal128Array::from(vec![1555555555555555555,
11222222222222222222, 1])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let result = mul_fixed_point_checked(&a, &b, 28).unwrap();
+ let expected = Decimal128Array::from(vec![
Review Comment:
```suggestion
// [
// 0.0000000000000000015555555556,
// 1385459527.2345679012071330528765432099,
// 0.0000000000000000015555555556
// ]
let expected = Decimal128Array::from(vec![
```
##########
arrow-arith/src/arithmetic.rs:
##########
@@ -3231,4 +3306,91 @@ mod tests {
assert_eq!(&expected, &result);
}
+
+ #[test]
+ fn test_decimal_multiply_allow_precision_loss() {
+ // Overflow happening as i128 cannot hold multiplying result.
+ let a = Decimal128Array::from(vec![123456789000000000000000000])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let b = Decimal128Array::from(vec![10000000000000000000])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let err = multiply_dyn_checked(&a, &b).unwrap_err();
+ assert!(err.to_string().contains(
+ "Overflow happened on: 123456789000000000000000000 *
10000000000000000000"
+ ));
+
+ // Allow precision loss.
+ let result = mul_fixed_point_checked(&a, &b, 28).unwrap();
+ let expected =
+ Decimal128Array::from(vec![12345678900000000000000000000000000000])
+ .with_precision_and_scale(38, 28)
+ .unwrap();
+
+ assert_eq!(&expected, &result);
+ assert_eq!(
+ result.value_as_string(0),
+ "1234567890.0000000000000000000000000000"
+ );
+
+ // Rounding case
+ let a = Decimal128Array::from(vec![
+ 1,
+ 123456789555555555555555555,
+ 1555555555555555555,
+ ])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let b = Decimal128Array::from(vec![1555555555555555555,
11222222222222222222, 1])
+ .with_precision_and_scale(38, 18)
+ .unwrap();
+
+ let result = mul_fixed_point_checked(&a, &b, 28).unwrap();
+ let expected = Decimal128Array::from(vec![
+ 15555555556,
+ 13854595272345679012071330528765432099,
+ 15555555556,
+ ])
+ .with_precision_and_scale(38, 28)
+ .unwrap();
+
+ assert_eq!(&expected, &result);
+
+ // Rounded the value "1385459527.234567901207133052876543209876543210".
+ assert_eq!(
+ result.value_as_string(1),
+ "1385459527.2345679012071330528765432099"
+ );
+ assert_eq!(result.value_as_string(0),
"0.0000000000000000015555555556");
+ assert_eq!(result.value_as_string(2),
"0.0000000000000000015555555556");
+
+ let a = Decimal128Array::from(vec![1230])
+ .with_precision_and_scale(4, 2)
+ .unwrap();
+
+ let b = Decimal128Array::from(vec![1000])
+ .with_precision_and_scale(4, 2)
+ .unwrap();
+
+ // Required scale is same as the product of the input scales. Behavior
is same as multiply.
+ let result = mul_fixed_point_checked(&a, &b, 4)
+ .unwrap()
+ .with_precision_and_scale(9, 4)
+ .unwrap();
+ let expected = multiply_checked(&a, &b)
+ .unwrap()
+ .with_precision_and_scale(9, 4)
+ .unwrap();
+ assert_eq!(&expected, &result);
Review Comment:
```suggestion
assert_eq!(&expected, &result);
assert_eq(result.precision(), 9);
assert_eq(result.scale(), 4);
```
--
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]