tustvold commented on code in PR #2770:
URL: https://github.com/apache/arrow-rs/pull/2770#discussion_r978435034


##########
arrow/src/compute/kernels/arithmetic.rs:
##########
@@ -1605,6 +1605,168 @@ where
     unary_dyn::<_, T>(array, |value| value.div_wrapping(divisor))
 }
 
+/// Creates an Decimal128Array the same size as `left`,
+/// by applying `op` to all non-null elements of left and right
+fn arith_decimal<F>(
+    left: &Decimal128Array,
+    right: &Decimal128Array,
+    op: F,
+) -> Result<Decimal128Array>
+where
+    F: Fn(i128, i128) -> Result<i128>,
+{
+    left.iter()
+        .zip(right.iter())
+        .map(|(left, right)| {
+            if let (Some(left), Some(right)) = (left, right) {
+                Some(op(left.as_i128(), right.as_i128())).transpose()
+            } else {
+                Ok(None)
+            }
+        })
+        .collect()
+}
+
+fn arith_decimal_scalar<F>(
+    left: &Decimal128Array,
+    right: i128,
+    op: F,
+) -> Result<Decimal128Array>
+where
+    F: Fn(i128, i128) -> Result<i128>,
+{
+    left.iter()
+        .map(|left| {
+            if let Some(left) = left {
+                Some(op(left.as_i128(), right)).transpose()
+            } else {
+                Ok(None)
+            }
+        })
+        .collect()
+}
+
+pub fn add_decimal(
+    left: &Decimal128Array,
+    right: &Decimal128Array,
+) -> Result<Decimal128Array> {
+    let array = arith_decimal(left, right, |left, right| Ok(left + right))?
+        .with_precision_and_scale(left.precision(), left.scale())?;
+    Ok(array)
+}
+
+pub fn add_decimal_scalar(
+    left: &Decimal128Array,
+    right: i128,
+) -> Result<Decimal128Array> {
+    let array = arith_decimal_scalar(left, right, |left, right| Ok(left + 
right))?
+        .with_precision_and_scale(left.precision(), left.scale())?;
+    Ok(array)
+}
+
+pub fn subtract_decimal(
+    left: &Decimal128Array,
+    right: &Decimal128Array,
+) -> Result<Decimal128Array> {
+    let array = arith_decimal(left, right, |left, right| Ok(left - right))?
+        .with_precision_and_scale(left.precision(), left.scale())?;
+    Ok(array)
+}
+
+pub fn subtract_decimal_scalar(
+    left: &Decimal128Array,
+    right: i128,
+) -> Result<Decimal128Array> {
+    let array = arith_decimal_scalar(left, right, |left, right| Ok(left - 
right))?
+        .with_precision_and_scale(left.precision(), left.scale())?;
+    Ok(array)
+}
+
+pub fn multiply_decimal(
+    left: &Decimal128Array,
+    right: &Decimal128Array,
+) -> Result<Decimal128Array> {
+    let divide = 10_i128.pow(left.scale() as u32);
+    let array = arith_decimal(left, right, |left, right| {
+        let result = BigInt::from(left) * BigInt::from(right) / divide;
+        Ok(result.to_i128().unwrap())
+    })?
+    .with_precision_and_scale(left.precision(), left.scale())?;
+    Ok(array)
+}
+
+pub fn multiply_decimal_scalar(
+    left: &Decimal128Array,
+    right: i128,
+) -> Result<Decimal128Array> {
+    let divide = 10_i128.pow(left.scale() as u32);
+    let array = arith_decimal_scalar(left, right, |left, right| {
+        let result = BigInt::from(left) * BigInt::from(right) / divide;

Review Comment:
   Why can't we use i128 for this? The performance should be better than a 
variable width encoding?



-- 
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]

Reply via email to