You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/09/22 15:42:33 UTC

[GitHub] [arrow-rs] kmitchener opened a new pull request, #2770: add arithmetic and comparison kernels from DF + small modifications

kmitchener opened a new pull request, #2770:
URL: https://github.com/apache/arrow-rs/pull/2770

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #1200, #2766.
   
   # Rationale for this change
    
   <!--
   Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
   Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
   -->
   
   Per issues, these kernels belong in this project, not in DF.
   
   # What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   * Copies the decimal arithmetic and comparison kernels 
   * Small (but useful) changes to the decimal division so that it uses the `num` crate's BigInt to do division, instead of f64, which aren't suitable for decimal division.
   
   
   # Are there any user-facing changes?
   
   addition of these kernels to the public API
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!---
   If there are any breaking changes to public APIs, please add the `breaking change` label.
   -->
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] liukun4515 commented on pull request #2770: add arithmetic and comparison kernels from DF + small modifications

Posted by GitBox <gi...@apache.org>.
liukun4515 commented on PR #2770:
URL: https://github.com/apache/arrow-rs/pull/2770#issuecomment-1256141091

   I will review it tomorrow, please hold this pr.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on pull request #2770: add arithmetic and comparison kernels from DF + small modifications

Posted by GitBox <gi...@apache.org>.
tustvold commented on PR #2770:
URL: https://github.com/apache/arrow-rs/pull/2770#issuecomment-1257677789

   My personal preference would to hold off on this if the consensus is to move forward with making DecimalArray a PrimitiveArray. However, this may not be ready in time for this weeks release, so if there is urgency to add this functionality I don't wish to block it.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #2770: add arithmetic and comparison kernels from DF + small modifications

Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #2770:
URL: https://github.com/apache/arrow-rs/pull/2770#discussion_r978418843


##########
arrow/src/compute/kernels/arithmetic.rs:
##########
@@ -2898,4 +3060,176 @@ mod tests {
         let division_by_zero = divide_scalar_opt_dyn::<Int32Type>(&a, 0);
         assert_eq!(&expected, &division_by_zero.unwrap());
     }
+
+    // TODO, cast, arithmetic, and comparison all have this function
+    fn create_decimal_array(
+        array: Vec<Option<i128>>,
+        precision: u8,
+        scale: u8,
+    ) -> Result<Decimal128Array> {
+        array
+            .into_iter()
+            .collect::<Decimal128Array>()
+            .with_precision_and_scale(precision, scale)
+    }
+
+    #[test]
+    fn arithmetic_decimal_op_test() -> Result<()> {
+        let value_i128: i128 = 123;
+        let left_decimal_array = create_decimal_array(
+            vec![
+                Some(value_i128),
+                None,
+                Some(value_i128 - 1),
+                Some(value_i128 + 1),
+            ],
+            25,
+            3,
+        )?;
+        let right_decimal_array = create_decimal_array(
+            vec![
+                Some(value_i128),
+                Some(value_i128),
+                Some(value_i128),
+                Some(value_i128),
+            ],
+            25,
+            3,
+        )?;
+        // add
+        let result = add_decimal(&left_decimal_array, &right_decimal_array)?;
+        let expect =
+            create_decimal_array(vec![Some(246), None, Some(245), Some(247)], 25, 3)?;
+        assert_eq!(expect, result);
+        let result = add_decimal_scalar(&left_decimal_array, 10)?;
+        let expect =
+            create_decimal_array(vec![Some(133), None, Some(132), Some(134)], 25, 3)?;
+        assert_eq!(expect, result);
+        // subtract
+        let result = subtract_decimal(&left_decimal_array, &right_decimal_array)?;
+        let expect = create_decimal_array(vec![Some(0), None, Some(-1), Some(1)], 25, 3)?;
+        assert_eq!(expect, result);
+        let result = subtract_decimal_scalar(&left_decimal_array, 10)?;
+        let expect =
+            create_decimal_array(vec![Some(113), None, Some(112), Some(114)], 25, 3)?;
+        assert_eq!(expect, result);
+        // multiply
+        let result = multiply_decimal(&left_decimal_array, &right_decimal_array)?;
+        let expect =
+            create_decimal_array(vec![Some(15), None, Some(15), Some(15)], 25, 3)?;
+        assert_eq!(expect, result);
+        let result = multiply_decimal_scalar(&left_decimal_array, 10)?;
+        let expect = create_decimal_array(vec![Some(1), None, Some(1), Some(1)], 25, 3)?;
+        assert_eq!(expect, result);
+        // divide
+        let left_decimal_array = create_decimal_array(
+            vec![
+                Some(1234567),
+                None,
+                Some(1234567),
+                Some(1234567),
+                Some(1234567),
+            ],
+            25,
+            3,
+        )?;
+        let right_decimal_array = create_decimal_array(
+            vec![Some(10), Some(100), Some(55), Some(-123), None],
+            3,
+            0,
+        )?;
+        let result = divide_opt_decimal(&left_decimal_array, &right_decimal_array)?;
+        let expect = create_decimal_array(
+            vec![Some(123456700), None, Some(22446672), Some(-10037130), None],
+            25,
+            3,
+        )?;
+        assert_eq!(expect, result);
+        let result = divide_decimal_scalar(&left_decimal_array, 10)?;
+        let expect = create_decimal_array(
+            vec![
+                Some(123456700),
+                None,
+                Some(123456700),
+                Some(123456700),
+                Some(123456700),
+            ],
+            25,
+            3,
+        )?;
+        assert_eq!(expect, result);
+        // modulus
+        let result = modulus_decimal(&left_decimal_array, &right_decimal_array)?;
+        let expect =
+            create_decimal_array(vec![Some(7), None, Some(37), Some(16), None], 25, 3)?;
+        assert_eq!(expect, result);
+        let result = modulus_decimal_scalar(&left_decimal_array, 10)?;
+        let expect =
+            create_decimal_array(vec![Some(7), None, Some(7), Some(7), Some(7)], 25, 3)?;
+        assert_eq!(expect, result);
+        Ok(())
+    }
+
+    #[test]
+    fn arithmetic_decimal_divide_by_zero() -> Result<()> {
+        let left_decimal_array = create_decimal_array(vec![Some(101)], 10, 1)?;
+        let right_decimal_array = create_decimal_array(vec![Some(0)], 1, 1)?;
+
+        let err =
+            divide_opt_decimal(&left_decimal_array, &right_decimal_array).unwrap_err();
+        assert_eq!("Divide by zero error", err.to_string());
+        let err = divide_decimal_scalar(&left_decimal_array, 0).unwrap_err();
+        assert_eq!("Divide by zero error", err.to_string());
+        let err = modulus_decimal(&left_decimal_array, &right_decimal_array).unwrap_err();
+        assert_eq!("Divide by zero error", err.to_string());
+        let err = modulus_decimal_scalar(&left_decimal_array, 0).unwrap_err();
+        assert_eq!("Divide by zero error", err.to_string());
+        Ok(())
+    }
+
+    // #[test]

Review Comment:
   Why are these tests commented out, I presume because they aren't working as expected?



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on pull request #2770: add arithmetic and comparison kernels from DF + small modifications

Posted by GitBox <gi...@apache.org>.
tustvold commented on PR #2770:
URL: https://github.com/apache/arrow-rs/pull/2770#issuecomment-1255966313

   I think this is only part of https://github.com/apache/arrow-rs/issues/1200 as it doesn't hook up the kernels in the dyn kernels


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] kmitchener closed pull request #2770: add arithmetic and comparison kernels from DF + small modifications

Posted by GitBox <gi...@apache.org>.
kmitchener closed pull request #2770: add arithmetic and comparison kernels from DF + small modifications
URL: https://github.com/apache/arrow-rs/pull/2770


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #2770: add arithmetic and comparison kernels from DF + small modifications

Posted by GitBox <gi...@apache.org>.
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] kmitchener commented on pull request #2770: add arithmetic and comparison kernels from DF + small modifications

Posted by GitBox <gi...@apache.org>.
kmitchener commented on PR #2770:
URL: https://github.com/apache/arrow-rs/pull/2770#issuecomment-1255218612

   I'll note that I seem to have picked the worst time to try to move these kernels here, as things aren't working correctly right now. However, I tested this change with DF and this revision of arrow-rs https://github.com/apache/arrow-rs/tree/5e83ef9cc7e426171f4cb9451fa004c55c7c95be and everything compiled and all tests passed in both DF and arrow-rs.
   
   @liukun4515 for your review. I'll add a similar draft PR for DataFusion so you can see both sides of the move.
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] liukun4515 commented on pull request #2770: add arithmetic and comparison kernels from DF + small modifications

Posted by GitBox <gi...@apache.org>.
liukun4515 commented on PR #2770:
URL: https://github.com/apache/arrow-rs/pull/2770#issuecomment-1257323503

   > > Is there a way to make DecimalArray a PrimitiveArray?
   > 
   > Yes (see #2637), and this would definitely be the best path forward if it is tractable. I can try to find some time over the weekend to have a play around with this. I have some ideas, but I'm loathe to subject someone else to my potentially half-baked ideas 😅
   
   Do we need to hold this pr and implement them by the proposal in https://github.com/apache/arrow-rs/issues/2637?


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] kmitchener commented on pull request #2770: add arithmetic and comparison kernels from DF + small modifications

Posted by GitBox <gi...@apache.org>.
kmitchener commented on PR #2770:
URL: https://github.com/apache/arrow-rs/pull/2770#issuecomment-1256178980

   As it is, I've only copied over what was in DataFusion, but I think that's not suitable for arrow-rs's public APIs. Specifically, the math takes rust primitives (i128) with no checks on precision or scalar or any indication what the precision or scalar might or should be for the scalar. 
   
   Also, as a user of arrow-rs, you'd expect to be able to use the same functions to do math on decimals as you do all other number types, but you can't, because they're fundamentally different types here.
   
   Is there a way to make DecimalArray a PrimitiveArray?
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] kmitchener commented on pull request #2770: add arithmetic and comparison kernels from DF + small modifications

Posted by GitBox <gi...@apache.org>.
kmitchener commented on PR #2770:
URL: https://github.com/apache/arrow-rs/pull/2770#issuecomment-1255378815

   Never mind, everything is working fine. I've moved all the DF decimal kernel code here and copied the tests over into the correct locations. Ready for review.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on pull request #2770: add arithmetic and comparison kernels from DF + small modifications

Posted by GitBox <gi...@apache.org>.
tustvold commented on PR #2770:
URL: https://github.com/apache/arrow-rs/pull/2770#issuecomment-1256282350

   > Is there a way to make DecimalArray a PrimitiveArray?
   
   Yes (see #2637), and this would definitely be the best path forward if it is tractable. I can try to find some time to have a play around with this. I have some ideas, but I'm loathe to subject someone else to my potentially half-baked ideas 😅


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] kmitchener commented on pull request #2770: add arithmetic and comparison kernels from DF + small modifications

Posted by GitBox <gi...@apache.org>.
kmitchener commented on PR #2770:
URL: https://github.com/apache/arrow-rs/pull/2770#issuecomment-1257937422

   No, there's no urgency in that way. I'll close this PR in favor of the more complete plan for decimal implementation in the other issue.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org