You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "zhzy0077 (via GitHub)" <gi...@apache.org> on 2023/03/31 11:24:02 UTC

[GitHub] [arrow-datafusion] zhzy0077 opened a new pull request, #5811: Scalar arithmetic should return error when overflows.

zhzy0077 opened a new pull request, #5811:
URL: https://github.com/apache/arrow-datafusion/pull/5811

   # 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 #5810.
   
   # 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.  
   -->
   Repro in the bug itself. ScalarValue returns an error for many arithmetic errors, but not overflows. 
   
   # 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.
   -->
   Use `checked_*` and check results.
   
   # Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   Tests added.
   
   # Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   No.
   
   <!--
   If there are any breaking changes to public APIs, please add the `api 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-datafusion] zhzy0077 commented on pull request #5811: Scalar arithmetic should return error when overflows.

Posted by "zhzy0077 (via GitHub)" <gi...@apache.org>.
zhzy0077 commented on PR #5811:
URL: https://github.com/apache/arrow-datafusion/pull/5811#issuecomment-1503071986

   UT failure fixed.


-- 
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-datafusion] comphead commented on a diff in pull request #5811: Scalar arithmetic should return error when overflows.

Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #5811:
URL: https://github.com/apache/arrow-datafusion/pull/5811#discussion_r1155153400


##########
datafusion/common/src/scalar.rs:
##########
@@ -573,12 +573,43 @@ macro_rules! primitive_op {
         match ($LEFT, $RIGHT) {
             (lhs, None) => Ok(ScalarValue::$SCALAR(*lhs)),
             #[allow(unused_variables)]
-            (None, Some(b)) => { primitive_right!(*b, $OPERATION, $SCALAR) },
-            (Some(a), Some(b)) => Ok(ScalarValue::$SCALAR(Some(*a $OPERATION *b))),
+            (None, Some(b)) => {
+                primitive_right!(*b, $OPERATION, $SCALAR)
+            }
+            (Some(a), Some(b)) => {
+                if let Some(value) = primitive_checked_op!(a, b, $OPERATION, $SCALAR) {
+                    Ok(ScalarValue::$SCALAR(Some(value)))
+                } else {
+                    Err(DataFusionError::Execution(
+                        "Overflow while calculating ScalarValue.".to_string(),
+                    ))
+                }
+            }
         }
     };
 }
 
+macro_rules! primitive_checked_op {
+    ($LEFT:expr, $RIGHT:expr, $OPERATION:tt, Float64) => {

Review Comment:
   cool!



-- 
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-datafusion] zhzy0077 commented on pull request #5811: Scalar arithmetic should return error when overflows.

Posted by "zhzy0077 (via GitHub)" <gi...@apache.org>.
zhzy0077 commented on PR #5811:
URL: https://github.com/apache/arrow-datafusion/pull/5811#issuecomment-1493245241

   > Thank you @zhzy0077 -- this looks like a great improvement to me
   > 
   > Can you also please add a test that ensures the overflow behavior is consistent with the arrow compute kernels (I can't remember if they error on overflow). Perhaps following the model of:
   > 
   > https://github.com/apache/arrow-datafusion/blob/ab2ad35e821e754c5ba20e92da0f6cacf205b831/datafusion/common/src/scalar.rs#L4896-L4913
   
   Thank you. Added a few tests in `scalar_add_overflow_test`. Not sure if this covers all your concerns.


-- 
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-datafusion] zhzy0077 commented on pull request #5811: Scalar arithmetic should return error when overflows.

Posted by "zhzy0077 (via GitHub)" <gi...@apache.org>.
zhzy0077 commented on PR #5811:
URL: https://github.com/apache/arrow-datafusion/pull/5811#issuecomment-1494321376

   Thank you for the detailed explanation @tustvold. What do you think if we introduce the other set of operands, say "add_checked", "mul_checked", etc.?


-- 
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-datafusion] waynexia commented on pull request #5811: Scalar arithmetic should return error when overflows.

Posted by "waynexia (via GitHub)" <gi...@apache.org>.
waynexia commented on PR #5811:
URL: https://github.com/apache/arrow-datafusion/pull/5811#issuecomment-1501732283

   Test error:
   
   > ---- scalar::tests::scalar_sub_trait_int64_overflow_test stdout ----
   thread 'scalar::tests::scalar_sub_trait_int64_overflow_test' panicked at 'attempt to subtract with overflow', datafusion/common/src/scalar.rs:1934:9
   
   It looks like the methods called in unit tests should also be `*_checked`


-- 
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-datafusion] alamb commented on a diff in pull request #5811: Scalar arithmetic should return error when overflows.

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #5811:
URL: https://github.com/apache/arrow-datafusion/pull/5811#discussion_r1163165327


##########
datafusion/common/src/scalar.rs:
##########
@@ -3664,6 +3746,100 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn scalar_sub_trait_int32_test() -> Result<()> {
+        let int_value = ScalarValue::Int32(Some(42));
+        let int_value_2 = ScalarValue::Int32(Some(100));
+        assert_eq!(int_value.sub(&int_value_2)?, ScalarValue::Int32(Some(-58)));
+        assert_eq!(int_value_2.sub(int_value)?, ScalarValue::Int32(Some(58)));
+        Ok(())
+    }
+
+    #[test]
+    fn scalar_sub_trait_int32_overflow_test() -> Result<()> {
+        let int_value = ScalarValue::Int32(Some(i32::MAX));
+        let int_value_2 = ScalarValue::Int32(Some(i32::MIN));
+        assert!(matches!(
+            int_value.sub_checked(&int_value_2),
+            Err(DataFusionError::Execution(msg)) if msg == "Overflow while calculating ScalarValue."
+        ));
+        Ok(())
+    }
+
+    #[test]
+    fn scalar_sub_trait_int64_test() -> Result<()> {
+        let int_value = ScalarValue::Int64(Some(42));
+        let int_value_2 = ScalarValue::Int64(Some(100));
+        assert_eq!(int_value.sub(&int_value_2)?, ScalarValue::Int64(Some(-58)));
+        assert_eq!(int_value_2.sub(int_value)?, ScalarValue::Int64(Some(58)));
+        Ok(())
+    }
+
+    #[test]
+    fn scalar_sub_trait_int64_overflow_test() -> Result<()> {
+        let int_value = ScalarValue::Int64(Some(i64::MAX));
+        let int_value_2 = ScalarValue::Int64(Some(i64::MIN));
+        assert!(matches!(
+            int_value.sub_checked(&int_value_2),
+            Err(DataFusionError::Execution(msg)) if msg == "Overflow while calculating ScalarValue."
+        ));
+        Ok(())
+    }
+
+    #[test]
+    fn scalar_add_overflow_test() -> Result<()> {
+        check_scalar_add_overflow::<Int8Type>(
+            ScalarValue::Int8(Some(i8::MAX)),
+            ScalarValue::Int8(Some(i8::MAX)),
+        );
+        check_scalar_add_overflow::<UInt8Type>(
+            ScalarValue::UInt8(Some(u8::MAX)),
+            ScalarValue::UInt8(Some(u8::MAX)),
+        );
+        check_scalar_add_overflow::<Int16Type>(
+            ScalarValue::Int16(Some(i16::MAX)),
+            ScalarValue::Int16(Some(i16::MAX)),
+        );
+        check_scalar_add_overflow::<UInt16Type>(
+            ScalarValue::UInt16(Some(u16::MAX)),
+            ScalarValue::UInt16(Some(u16::MAX)),
+        );
+        check_scalar_add_overflow::<Int32Type>(
+            ScalarValue::Int32(Some(i32::MAX)),
+            ScalarValue::Int32(Some(i32::MAX)),
+        );
+        check_scalar_add_overflow::<UInt32Type>(
+            ScalarValue::UInt32(Some(u32::MAX)),
+            ScalarValue::UInt32(Some(u32::MAX)),
+        );
+        check_scalar_add_overflow::<Int64Type>(
+            ScalarValue::Int64(Some(i64::MAX)),
+            ScalarValue::Int64(Some(i64::MAX)),
+        );
+        check_scalar_add_overflow::<UInt64Type>(
+            ScalarValue::UInt64(Some(u64::MAX)),
+            ScalarValue::UInt64(Some(u64::MAX)),
+        );
+
+        Ok(())
+    }
+
+    // Verifies that ScalarValue has the same behavior with compute kernal when it overflows.
+    fn check_scalar_add_overflow<T>(left: ScalarValue, right: ScalarValue)

Review Comment:
   Thank you for this test



-- 
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-datafusion] alamb merged pull request #5811: Scalar arithmetic should return error when overflows.

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #5811:
URL: https://github.com/apache/arrow-datafusion/pull/5811


-- 
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-datafusion] alamb commented on pull request #5811: Scalar arithmetic should return error when overflows.

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #5811:
URL: https://github.com/apache/arrow-datafusion/pull/5811#issuecomment-1494290528

   @tustvold  can you give this one a review (mostly for consistency with the semantics of arrow-rs)?


-- 
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-datafusion] tustvold commented on pull request #5811: Scalar arithmetic should return error when overflows.

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #5811:
URL: https://github.com/apache/arrow-datafusion/pull/5811#issuecomment-1494309259

   > mostly for consistency with the semantics of arrow-rs
   
   So this is not consistent with the arrow kernels that DataFusion makes use of, in particular, DataFusion is currently using the unchecked arithmetic kernels, which do not return an error on overflow. So this PR will introduce inconsistency between ScalarValue arithmetic, and any arithmetic involving arrays.
   
   The major reason I'm a little apprehensive about changing this is that the checked kernels are at least an order of magnitude slower in the presence of nulls, as LLVM can't vectorise them correctly
   
   ```
   add(0.1)                time:   [7.4481 µs 7.4539 µs 7.4608 µs]
   Found 6 outliers among 100 measurements (6.00%)
     2 (2.00%) high mild
     4 (4.00%) high severe
   
   add_checked(0.1)        time:   [101.62 µs 106.31 µs 111.40 µs]
   ```
   
   I definitely think whatever semantics we settle on should be consistent and well documented, but I don't have a strong opinion what it should be. However, I do feel that changing the current semantics warrants some communication due to the major performance regression it would entail.


-- 
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-datafusion] zhzy0077 commented on pull request #5811: Scalar arithmetic should return error when overflows.

Posted by "zhzy0077 (via GitHub)" <gi...@apache.org>.
zhzy0077 commented on PR #5811:
URL: https://github.com/apache/arrow-datafusion/pull/5811#issuecomment-1498970860

   @alamb @tustvold PR is updated. `add/sub` methods are no longer touched and `add_checked/sub_checked` are added. Could you help to take another look?


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