tustvold commented on code in PR #3690:
URL: https://github.com/apache/arrow-rs/pull/3690#discussion_r1136292121
##########
arrow-arith/src/arithmetic.rs:
##########
@@ -1165,6 +1167,77 @@ 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.
+///
+/// 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<ArrayRef, ArrowError> {
+ let precision = left.precision();
+ let product_scale = left.scale() + right.scale();
+
+ try_binary::<_, _, _, Decimal128Type>(left, right, |a, b| {
+ let a = i256::from_i128(a);
+ let b = i256::from_i128(b);
+
+ a.checked_mul(b)
+ .map(|mut a| {
+ if required_scale < product_scale {
Review Comment:
What happens if required_scale is greater than product_scale, should we just
assert?
##########
arrow-arith/src/arithmetic.rs:
##########
@@ -1165,6 +1167,77 @@ 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.
+///
+/// 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<ArrayRef, ArrowError> {
+ let precision = left.precision();
+ let product_scale = left.scale() + right.scale();
+
+ try_binary::<_, _, _, Decimal128Type>(left, right, |a, b| {
+ let a = i256::from_i128(a);
+ let b = i256::from_i128(b);
+
+ a.checked_mul(b)
Review Comment:
Does this need to be checked, I don't think it can overflow?
##########
arrow-arith/src/arithmetic.rs:
##########
@@ -1165,6 +1167,77 @@ 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.
+///
+/// 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<ArrayRef, ArrowError> {
+ let precision = left.precision();
+ let product_scale = left.scale() + right.scale();
+
+ try_binary::<_, _, _, Decimal128Type>(left, right, |a, b| {
+ let a = i256::from_i128(a);
+ let b = i256::from_i128(b);
+
+ a.checked_mul(b)
+ .map(|mut a| {
+ if required_scale < product_scale {
+ let divisor = i256::from_i128(10)
Review Comment:
I think computing this could be lifted out of the try_unary?
##########
arrow-arith/src/arity.rs:
##########
@@ -77,7 +77,7 @@ pub fn try_unary<I, F, O>(
where
I: ArrowPrimitiveType,
O: ArrowPrimitiveType,
- F: Fn(I::Native) -> Result<O::Native, ArrowError>,
+ F: FnMut(I::Native) -> Result<O::Native, ArrowError>,
Review Comment:
Do we still need these FnMut changes?
##########
arrow-arith/src/arithmetic.rs:
##########
@@ -1165,6 +1167,77 @@ 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.
+///
+/// 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<ArrayRef, ArrowError> {
+ let precision = left.precision();
+ let product_scale = left.scale() + right.scale();
+
+ try_binary::<_, _, _, Decimal128Type>(left, right, |a, b| {
+ let a = i256::from_i128(a);
+ let b = i256::from_i128(b);
+
+ a.checked_mul(b)
+ .map(|mut a| {
+ if required_scale < product_scale {
+ let divisor = i256::from_i128(10)
+ .pow_wrapping((product_scale - required_scale) as u32);
+ a = divide_and_round::<Decimal256Type>(a, divisor);
+ }
+ a
+ })
+ .ok_or_else(|| {
+ ArrowError::ComputeError(format!(
+ "Overflow happened on: {:?} * {:?}, {:?}",
+ a,
+ b,
+ a.checked_mul(b)
+ ))
+ })
+ .and_then(|a| {
Review Comment:
The ? operator may be more legible than chaining and_then
--
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]