From 34088b5847b306994d88dcab7c2a672cd908a202 Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Tue, 14 Jan 2025 21:02:39 +0800 Subject: [PATCH] fix: add support for Decimal128 and Decimal256 types in interval arithmetic Signed-off-by: Ruihang Xia --- .../expr-common/src/interval_arithmetic.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/datafusion/expr-common/src/interval_arithmetic.rs b/datafusion/expr-common/src/interval_arithmetic.rs index ffaa32f08075..0323d6121212 100644 --- a/datafusion/expr-common/src/interval_arithmetic.rs +++ b/datafusion/expr-common/src/interval_arithmetic.rs @@ -76,6 +76,14 @@ macro_rules! get_extreme_value { DataType::Interval(IntervalUnit::MonthDayNano) => { ScalarValue::IntervalMonthDayNano(Some(IntervalMonthDayNano::$extreme)) } + DataType::Decimal128(precision, scale) => { + ScalarValue::Decimal128(Some(i128::$extreme), *precision, *scale) + } + DataType::Decimal256(precision, scale) => ScalarValue::Decimal256( + Some(arrow::datatypes::i256::$extreme), + *precision, + *scale, + ), _ => unreachable!(), } }; @@ -1008,17 +1016,20 @@ fn handle_overflow( lhs: &ScalarValue, rhs: &ScalarValue, ) -> ScalarValue { - let zero = ScalarValue::new_zero(dt).unwrap(); + let lhs_zero = ScalarValue::new_zero(&lhs.data_type()).unwrap(); + let rhs_zero = ScalarValue::new_zero(&rhs.data_type()).unwrap(); let positive_sign = match op { Operator::Multiply | Operator::Divide => { - lhs.lt(&zero) && rhs.lt(&zero) || lhs.gt(&zero) && rhs.gt(&zero) + lhs.lt(&lhs_zero) && rhs.lt(&rhs_zero) + || lhs.gt(&lhs_zero) && rhs.gt(&rhs_zero) } - Operator::Plus => lhs.ge(&zero), + Operator::Plus => lhs.ge(&lhs_zero), Operator::Minus => lhs.ge(rhs), _ => { unreachable!() } }; + match (UPPER, positive_sign) { (true, true) | (false, false) => ScalarValue::try_from(dt).unwrap(), (true, false) => {