You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2023/04/11 18:06:56 UTC
[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #5811: Scalar arithmetic should return error when overflows.
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