From d0bef9a8770c938ba9f0ad84190e6ea535a98dde Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Tue, 14 Jan 2025 21:18:41 +0800 Subject: [PATCH] add unit test Signed-off-by: Ruihang Xia --- .../expr-common/src/interval_arithmetic.rs | 71 ++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/datafusion/expr-common/src/interval_arithmetic.rs b/datafusion/expr-common/src/interval_arithmetic.rs index 0323d6121212..9923d79d1c5c 100644 --- a/datafusion/expr-common/src/interval_arithmetic.rs +++ b/datafusion/expr-common/src/interval_arithmetic.rs @@ -1843,7 +1843,12 @@ impl NullableInterval { #[cfg(test)] mod tests { - use crate::interval_arithmetic::{next_value, prev_value, satisfy_greater, Interval}; + use crate::{ + interval_arithmetic::{ + handle_overflow, next_value, prev_value, satisfy_greater, Interval, + }, + operator::Operator, + }; use arrow::datatypes::DataType; use datafusion_common::{Result, ScalarValue}; @@ -3119,6 +3124,70 @@ mod tests { Ok(()) } + #[test] + fn test_overflow_handling() -> Result<()> { + // Test integer overflow handling: + let dt = DataType::Int32; + let op = Operator::Plus; + let lhs = ScalarValue::Int32(Some(i32::MAX)); + let rhs = ScalarValue::Int32(Some(1)); + let result = handle_overflow::(&dt, op, &lhs, &rhs); + assert_eq!(result, ScalarValue::Int32(None)); + let result = handle_overflow::(&dt, op, &lhs, &rhs); + assert_eq!(result, ScalarValue::Int32(Some(i32::MAX))); + + // Test float overflow handling: + let dt = DataType::Float32; + let op = Operator::Multiply; + let lhs = ScalarValue::Float32(Some(f32::MAX)); + let rhs = ScalarValue::Float32(Some(2.0)); + let result = handle_overflow::(&dt, op, &lhs, &rhs); + assert_eq!(result, ScalarValue::Float32(None)); + let result = handle_overflow::(&dt, op, &lhs, &rhs); + assert_eq!(result, ScalarValue::Float32(Some(f32::MAX))); + + // Test float underflow handling: + let lhs = ScalarValue::Float32(Some(f32::MIN)); + let rhs = ScalarValue::Float32(Some(2.0)); + let result = handle_overflow::(&dt, op, &lhs, &rhs); + assert_eq!(result, ScalarValue::Float32(Some(f32::MIN))); + let result = handle_overflow::(&dt, op, &lhs, &rhs); + assert_eq!(result, ScalarValue::Float32(None)); + + // Test integer underflow handling: + let dt = DataType::Int64; + let op = Operator::Minus; + let lhs = ScalarValue::Int64(Some(i64::MIN)); + let rhs = ScalarValue::Int64(Some(1)); + let result = handle_overflow::(&dt, op, &lhs, &rhs); + assert_eq!(result, ScalarValue::Int64(Some(i64::MIN))); + let result = handle_overflow::(&dt, op, &lhs, &rhs); + assert_eq!(result, ScalarValue::Int64(None)); + + // Test unsigned integer handling: + let dt = DataType::UInt32; + let op = Operator::Minus; + let lhs = ScalarValue::UInt32(Some(0)); + let rhs = ScalarValue::UInt32(Some(1)); + let result = handle_overflow::(&dt, op, &lhs, &rhs); + assert_eq!(result, ScalarValue::UInt32(Some(0))); + let result = handle_overflow::(&dt, op, &lhs, &rhs); + assert_eq!(result, ScalarValue::UInt32(None)); + + // Test decimal handling: + let dt = DataType::Decimal128(38, 35); + let op = Operator::Plus; + let lhs = + ScalarValue::Decimal128(Some(54321543215432154321543215432154321), 35, 35); + let rhs = ScalarValue::Decimal128(Some(10000), 20, 0); + let result = handle_overflow::(&dt, op, &lhs, &rhs); + assert_eq!(result, ScalarValue::Decimal128(None, 38, 35)); + let result = handle_overflow::(&dt, op, &lhs, &rhs); + assert_eq!(result, ScalarValue::Decimal128(Some(i128::MAX), 38, 35)); + + Ok(()) + } + #[test] fn test_cardinality_of_intervals() -> Result<()> { // In IEEE 754 standard for floating-point arithmetic, if we keep the sign and exponent fields same,