Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct for min/max of i128? It seems like the minimum value of Decimal128(precision, scale) would be the minimum value for the precision and scale separately rather than the min value of the overall i128 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, that makes sense. These huge arrays remind me it should be all of 9s...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sadly the decimal 256 version is not public 😢 let me find a workaround

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe get_extreme_value should be part of arrow?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sadly the decimal 256 version is not public 😢 let me find a workaround

I recommend

  1. copy/pasting the value into DataFusion for now
  2. File a ticket in arrow-rs to make it public (or maybe even a PR!)
  3. leave a comment next to the copy in DataFUsion referencing the upstream ticket

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I filed a PR in arrow to expose these constants:

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