Skip to content

Commit

Permalink
fix: add support for Decimal128 and Decimal256 types in interval arit…
Browse files Browse the repository at this point in the history
…hmetic

Signed-off-by: Ruihang Xia <[email protected]>
  • Loading branch information
waynexia committed Jan 14, 2025
1 parent 888df6a commit 34088b5
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions datafusion/expr-common/src/interval_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
}
};
Expand Down Expand Up @@ -1008,17 +1016,20 @@ fn handle_overflow<const UPPER: bool>(
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) => {
Expand Down

0 comments on commit 34088b5

Please sign in to comment.