Skip to content

Apache DataFusion SQL Query Engine

perf: add in-place fast path for ScalarValue::add (#20959)

Kumar Ujjawal3bdcdf5114b6
diff --git a/datafusion/common/src/scalar/mod.rs b/datafusion/common/src/scalar/mod.rs
index 697794375c..e30c1e5bf8 100644
--- a/datafusion/common/src/scalar/mod.rs
+++ b/datafusion/common/src/scalar/mod.rs
@@ -2000,6 +2000,209 @@ impl ScalarValue {
}
}
+ #[inline]
+ fn can_use_direct_add(lhs: &ScalarValue, rhs: &ScalarValue) -> bool {
+ matches!(
+ (lhs, rhs),
+ (ScalarValue::Int8(_), ScalarValue::Int8(_))
+ | (ScalarValue::Int16(_), ScalarValue::Int16(_))
+ | (ScalarValue::Int32(_), ScalarValue::Int32(_))
+ | (ScalarValue::Int64(_), ScalarValue::Int64(_))
+ | (ScalarValue::UInt8(_), ScalarValue::UInt8(_))
+ | (ScalarValue::UInt16(_), ScalarValue::UInt16(_))
+ | (ScalarValue::UInt32(_), ScalarValue::UInt32(_))
+ | (ScalarValue::UInt64(_), ScalarValue::UInt64(_))
+ | (ScalarValue::Float16(_), ScalarValue::Float16(_))
+ | (ScalarValue::Float32(_), ScalarValue::Float32(_))
+ | (ScalarValue::Float64(_), ScalarValue::Float64(_))
+ | (
+ ScalarValue::Decimal32(_, _, _),
+ ScalarValue::Decimal32(_, _, _)
+ )
+ | (
+ ScalarValue::Decimal64(_, _, _),
+ ScalarValue::Decimal64(_, _, _)
+ )
+ | (
+ ScalarValue::Decimal128(_, _, _),
+ ScalarValue::Decimal128(_, _, _),
+ )
+ | (
+ ScalarValue::Decimal256(_, _, _),
+ ScalarValue::Decimal256(_, _, _),
+ )
+ )
+ }
+
+ #[inline]
+ fn add_optional<T: ArrowNativeTypeOp>(
+ lhs: &mut Option<T>,
+ rhs: Option<T>,
+ checked: bool,
+ ) -> Result<()> {
+ match rhs {
+ Some(rhs) => {
+ if let Some(lhs) = lhs.as_mut() {
+ *lhs = if checked {
+ lhs.add_checked(rhs).map_err(|e| arrow_datafusion_err!(e))?
+ } else {
+ lhs.add_wrapping(rhs)
+ };
+ }
+ }
+ None => *lhs = None,
+ }
+ Ok(())
+ }
+
+ #[inline]
+ fn add_decimal_values<T: DecimalType>(
+ lhs_value: &mut Option<T::Native>,
+ lhs_precision: &mut u8,
+ lhs_scale: &mut i8,
+ rhs_value: Option<T::Native>,
+ rhs_precision: u8,
+ rhs_scale: i8,
+ ) -> Result<()>
+ where
+ T::Native: ArrowNativeTypeOp,
+ {
+ Self::validate_decimal_or_internal_err::<T>(*lhs_precision, *lhs_scale)?;
+ Self::validate_decimal_or_internal_err::<T>(rhs_precision, rhs_scale)?;
+
+ let result_scale = (*lhs_scale).max(rhs_scale);
+ // Decimal scales can be negative, so use a wider signed type for the
+ // intermediate precision arithmetic.
+ let lhs_precision_delta = i16::from(*lhs_precision) - i16::from(*lhs_scale);
+ let rhs_precision_delta = i16::from(rhs_precision) - i16::from(rhs_scale);
+ let result_precision =
+ (i16::from(result_scale) + lhs_precision_delta.max(rhs_precision_delta) + 1)
+ .min(i16::from(T::MAX_PRECISION)) as u8;
+
+ Self::validate_decimal_or_internal_err::<T>(result_precision, result_scale)?;
+
+ let lhs_mul = T::Native::usize_as(10)
+ .pow_checked((result_scale - *lhs_scale) as u32)
+ .map_err(|e| arrow_datafusion_err!(e))?;
+ let rhs_mul = T::Native::usize_as(10)
+ .pow_checked((result_scale - rhs_scale) as u32)
+ .map_err(|e| arrow_datafusion_err!(e))?;
+
+ let result_value = match (*lhs_value, rhs_value) {
+ (Some(lhs_value), Some(rhs_value)) => Some(
+ lhs_value
+ .mul_checked(lhs_mul)
+ .and_then(|lhs| {
+ rhs_value
+ .mul_checked(rhs_mul)
+ .and_then(|rhs| lhs.add_checked(rhs))
+ })
+ .map_err(|e| arrow_datafusion_err!(e))?,
+ ),
+ _ => None,
+ };
+
+ *lhs_value = result_value;
+ *lhs_precision = result_precision;
+ *lhs_scale = result_scale;
+
+ Ok(())
+ }
+
+ #[inline]
+ fn try_add_in_place_impl(
+ &mut self,
+ other: &ScalarValue,
+ checked: bool,
+ ) -> Result<bool> {
+ match (self, other) {
+ (ScalarValue::Int8(lhs), ScalarValue::Int8(rhs)) => {
+ Self::add_optional(lhs, *rhs, checked)?;
+ }
+ (ScalarValue::Int16(lhs), ScalarValue::Int16(rhs)) => {
+ Self::add_optional(lhs, *rhs, checked)?;
+ }
+ (ScalarValue::Int32(lhs), ScalarValue::Int32(rhs)) => {
+ Self::add_optional(lhs, *rhs, checked)?;
+ }
+ (ScalarValue::Int64(lhs), ScalarValue::Int64(rhs)) => {
+ Self::add_optional(lhs, *rhs, checked)?;
+ }
+ (ScalarValue::UInt8(lhs), ScalarValue::UInt8(rhs)) => {
+ Self::add_optional(lhs, *rhs, checked)?;
+ }
+ (ScalarValue::UInt16(lhs), ScalarValue::UInt16(rhs)) => {
+ Self::add_optional(lhs, *rhs, checked)?;
+ }
+ (ScalarValue::UInt32(lhs), ScalarValue::UInt32(rhs)) => {
+ Self::add_optional(lhs, *rhs, checked)?;
+ }
+ (ScalarValue::UInt64(lhs), ScalarValue::UInt64(rhs)) => {
+ Self::add_optional(lhs, *rhs, checked)?;
+ }
+ (ScalarValue::Float16(lhs), ScalarValue::Float16(rhs)) => {
+ Self::add_optional(lhs, *rhs, checked)?;
+ }
+ (ScalarValue::Float32(lhs), ScalarValue::Float32(rhs)) => {
+ Self::add_optional(lhs, *rhs, checked)?;
+ }
+ (ScalarValue::Float64(lhs), ScalarValue::Float64(rhs)) => {
+ Self::add_optional(lhs, *rhs, checked)?;
+ }
+ (
+ ScalarValue::Decimal32(lhs, p, s),
+ ScalarValue::Decimal32(rhs, rhs_p, rhs_s),
+ ) => {
+ Self::add_decimal_values::<Decimal32Type>(
+ lhs, p, s, *rhs, *rhs_p, *rhs_s,
+ )?;
+ }
+ (
+ ScalarValue::Decimal64(lhs, p, s),
+ ScalarValue::Decimal64(rhs, rhs_p, rhs_s),
+ ) => {
+ Self::add_decimal_values::<Decimal64Type>(
+ lhs, p, s, *rhs, *rhs_p, *rhs_s,
+ )?;
+ }
+ (
+ ScalarValue::Decimal128(lhs, p, s),
+ ScalarValue::Decimal128(rhs, rhs_p, rhs_s),
+ ) => {
+ Self::add_decimal_values::<Decimal128Type>(
+ lhs, p, s, *rhs, *rhs_p, *rhs_s,
+ )?;
+ }
+ (
+ ScalarValue::Decimal256(lhs, p, s),
+ ScalarValue::Decimal256(rhs, rhs_p, rhs_s),
+ ) => {
+ Self::add_decimal_values::<Decimal256Type>(
+ lhs, p, s, *rhs, *rhs_p, *rhs_s,
+ )?;
+ }
+ _ => return Ok(false),
+ }
+
+ Ok(true)
+ }
+
+ #[inline]
+ pub(crate) fn try_add_wrapping_in_place(
+ &mut self,
+ other: &ScalarValue,
+ ) -> Result<bool> {
+ self.try_add_in_place_impl(other, false)
+ }
+
+ #[inline]
+ pub(crate) fn try_add_checked_in_place(
+ &mut self,
+ other: &ScalarValue,
+ ) -> Result<bool> {
+ self.try_add_in_place_impl(other, true)
+ }
+
/// Calculate arithmetic negation for a scalar value
pub fn arithmetic_negate(&self) -> Result<Self> {
fn neg_checked_with_ctx<T: ArrowNativeTypeOp>(
@@ -2135,7 +2338,16 @@ impl ScalarValue {
/// NB: operating on `ScalarValue` directly is not efficient, performance sensitive code
/// should operate on Arrays directly, using vectorized array kernels
pub fn add<T: Borrow<ScalarValue>>(&self, other: T) -> Result<ScalarValue> {
- let r = add_wrapping(&self.to_scalar()?, &other.borrow().to_scalar()?)?;
+ let other = other.borrow();
+ if Self::can_use_direct_add(self, other) {
+ let mut result = self.clone();
+ if result.try_add_wrapping_in_place(other)? {
+ return Ok(result);
+ }
+ debug_assert!(false, "fast-path eligibility drifted from implementation");
+ }
+
+ let r = add_wrapping(&self.to_scalar()?, &other.to_scalar()?)?;
Self::try_from_array(r.as_ref(), 0)
}
@@ -2144,7 +2356,16 @@ impl ScalarValue {
/// NB: operating on `ScalarValue` directly is not efficient, performance sensitive code
/// should operate on Arrays directly, using vectorized array kernels
pub fn add_checked<T: Borrow<ScalarValue>>(&self, other: T) -> Result<ScalarValue> {
- let r = add(&self.to_scalar()?, &other.borrow().to_scalar()?)?;
+ let other = other.borrow();
+ if Self::can_use_direct_add(self, other) {
+ let mut result = self.clone();
+ if result.try_add_checked_in_place(other)? {
+ return Ok(result);
+ }
+ debug_assert!(false, "fast-path eligibility drifted from implementation");
+ }
+
+ let r = add(&self.to_scalar()?, &other.to_scalar()?)?;
Self::try_from_array(r.as_ref(), 0)
}
@@ -5943,6 +6164,68 @@ mod tests {
Ok(())
}
+ #[test]
+ fn scalar_add_trait_null_test() -> Result<()> {
+ let int_value = ScalarValue::Int32(Some(42));
+
+ assert_eq!(
+ int_value.add(ScalarValue::Int32(None))?,
+ ScalarValue::Int32(None)
+ );
+
+ Ok(())
+ }
+
+ #[test]
+ fn scalar_add_trait_wrapping_overflow_test() -> Result<()> {
+ let int_value = ScalarValue::Int32(Some(i32::MAX));
+ let one = ScalarValue::Int32(Some(1));
+
+ assert_eq!(int_value.add(one)?, ScalarValue::Int32(Some(i32::MIN)));
+
+ Ok(())
+ }
+
+ #[test]
+ fn scalar_add_trait_decimal_scale_test() -> Result<()> {
+ let decimal = ScalarValue::Decimal128(Some(123), 10, 2);
+ let decimal_2 = ScalarValue::Decimal128(Some(4), 9, 1);
+
+ assert_eq!(
+ decimal.add(decimal_2)?,
+ ScalarValue::Decimal128(Some(163), 11, 2)
+ );
+
+ Ok(())
+ }
+
+ #[test]
+ fn scalar_add_trait_decimal256_scale_test() -> Result<()> {
+ let decimal = ScalarValue::Decimal256(Some(i256::from(123)), 10, 2);
+ let decimal_2 = ScalarValue::Decimal256(Some(i256::from(4)), 9, 1);
+
+ assert_eq!(
+ decimal.add(decimal_2)?,
+ ScalarValue::Decimal256(Some(i256::from(163)), 11, 2)
+ );
+
+ Ok(())
+ }
+
+ #[test]
+ fn scalar_add_trait_decimal_negative_scale_test() -> Result<()> {
+ let decimal = ScalarValue::Decimal128(Some(1), DECIMAL128_MAX_PRECISION, i8::MIN);
+ let decimal_2 =
+ ScalarValue::Decimal128(Some(1), DECIMAL128_MAX_PRECISION, i8::MIN);
+
+ assert_eq!(
+ decimal.add(decimal_2)?,
+ ScalarValue::Decimal128(Some(2), DECIMAL128_MAX_PRECISION, i8::MIN)
+ );
+
+ Ok(())
+ }
+
#[test]
fn scalar_sub_trait_test() -> Result<()> {
let float_value = ScalarValue::Float64(Some(123.));
@@ -6042,6 +6325,43 @@ mod tests {
Ok(())
}
+ #[test]
+ fn scalar_decimal_add_overflow_test() {
+ check_scalar_decimal_add_overflow::<Decimal128Type>(
+ ScalarValue::Decimal128(Some(i128::MAX), DECIMAL128_MAX_PRECISION, 0),
+ ScalarValue::Decimal128(Some(1), DECIMAL128_MAX_PRECISION, 0),
+ );
+ check_scalar_decimal_add_overflow::<Decimal256Type>(
+ ScalarValue::Decimal256(Some(i256::MAX), DECIMAL256_MAX_PRECISION, 0),
+ ScalarValue::Decimal256(Some(i256::ONE), DECIMAL256_MAX_PRECISION, 0),
+ );
+ }
+
+ #[test]
+ fn scalar_decimal_in_place_add_error_preserves_lhs() {
+ let mut lhs =
+ ScalarValue::Decimal128(Some(i128::MAX), DECIMAL128_MAX_PRECISION, 0);
+ let original = lhs.clone();
+
+ let err = lhs
+ .try_add_checked_in_place(&ScalarValue::Decimal128(
+ Some(1),
+ DECIMAL128_MAX_PRECISION,
+ 0,
+ ))
+ .unwrap_err()
+ .strip_backtrace();
+
+ assert_eq!(
+ err,
+ format!(
+ "Arrow error: Arithmetic overflow: Overflow happened on: {} + 1",
+ i128::MAX
+ )
+ );
+ assert_eq!(lhs, original);
+ }
+
// Verifies that ScalarValue has the same behavior with compute kernel when it overflows.
fn check_scalar_add_overflow<T>(left: ScalarValue, right: ScalarValue)
where
@@ -6058,6 +6378,22 @@ mod tests {
assert_eq!(scalar_result.is_ok(), arrow_result.is_ok());
}
+ // Verifies the decimal fast path preserves the same overflow behavior as Arrow kernels.
+ fn check_scalar_decimal_add_overflow<T>(left: ScalarValue, right: ScalarValue)
+ where
+ T: ArrowPrimitiveType,
+ {
+ let scalar_result = left.add(&right);
+
+ let left_array = left.to_array().expect("Failed to convert to array");
+ let right_array = right.to_array().expect("Failed to convert to array");
+ let arrow_left_array = left_array.as_primitive::<T>();
+ let arrow_right_array = right_array.as_primitive::<T>();
+ let arrow_result = add_wrapping(arrow_left_array, arrow_right_array);
+
+ assert_eq!(scalar_result.is_ok(), arrow_result.is_ok());
+ }
+
#[test]
fn test_interval_add_timestamp() -> Result<()> {
let interval = ScalarValue::IntervalMonthDayNano(Some(IntervalMonthDayNano {
diff --git a/datafusion/common/src/stats.rs b/datafusion/common/src/stats.rs
index f263c905fa..7d3d511ca7 100644
--- a/datafusion/common/src/stats.rs
+++ b/datafusion/common/src/stats.rs
@@ -671,9 +671,9 @@ impl Statistics {
.collect();
// Accumulate all statistics in a single pass.
- // Uses precision_add for sum (avoids the expensive
- // ScalarValue::add round-trip through Arrow arrays), and
- // Precision::min/max which use cheap PartialOrd comparison.
+ // Uses precision_add for sum (reuses the lhs accumulator for
+ // direct numeric addition), while preserving the NDV update
+ // ordering required by estimate_ndv_with_overlap.
for stat in items.iter().skip(1) {
for (col_idx, col_stats) in column_statistics.iter_mut().enumerate() {
let item_cs = &stat.column_statistics[col_idx];
@@ -691,11 +691,9 @@ impl Statistics {
),
_ => Precision::Absent,
};
-
col_stats.min_value = col_stats.min_value.min(&item_cs.min_value);
col_stats.max_value = col_stats.max_value.max(&item_cs.max_value);
- col_stats.sum_value =
- precision_add(&col_stats.sum_value, &item_cs.sum_value);
+ precision_add(&mut col_stats.sum_value, &item_cs.sum_value);
col_stats.byte_size = col_stats.byte_size.add(&item_cs.byte_size);
}
}
diff --git a/datafusion/common/src/utils/aggregate.rs b/datafusion/common/src/utils/aggregate.rs
index 43bc0676b2..783ec665f3 100644
--- a/datafusion/common/src/utils/aggregate.rs
+++ b/datafusion/common/src/utils/aggregate.rs
@@ -17,133 +17,116 @@
//! Scalar-level aggregation utilities for statistics merging.
//!
-//! Provides a cheap pairwise [`ScalarValue`] addition that directly
-//! extracts inner primitive values, avoiding the expensive
-//! `ScalarValue::add` path (which round-trips through Arrow arrays).
-use arrow::datatypes::i256;
+//! Provides in-place accumulation helpers that reuse the existing
+//! [`ScalarValue`] accumulator when possible.
use crate::stats::Precision;
use crate::{Result, ScalarValue};
-/// Saturating addition for [`i256`] (which lacks a built-in
-/// `saturating_add`). Returns `i256::MAX` on positive overflow and
-/// `i256::MIN` on negative overflow.
-#[inline]
-fn i256_saturating_add(a: i256, b: i256) -> i256 {
- match a.checked_add(b) {
- Some(sum) => sum,
- None => {
- // If b is non-negative the overflow is positive, otherwise
- // negative.
- if b >= i256::ZERO {
- i256::MAX
- } else {
- i256::MIN
- }
- }
+/// Adds `rhs` into `lhs`, mutating the accumulator in place when
+/// possible and otherwise falling back to `ScalarValue::add_checked`.
+pub(crate) fn scalar_add(lhs: &mut ScalarValue, rhs: &ScalarValue) -> Result<()> {
+ if lhs.try_add_checked_in_place(rhs)? {
+ return Ok(());
}
+
+ *lhs = lhs.add_checked(rhs)?;
+ Ok(())
}
-/// Add two [`ScalarValue`]s by directly extracting and adding their
-/// inner primitive values.
-///
-/// This avoids `ScalarValue::add` which converts both operands to
-/// single-element Arrow arrays, runs the `add_wrapping` kernel, and
-/// converts the result back — 3 heap allocations per call.
+/// [`Precision`]-aware sum that mutates `lhs` in place when possible.
///
-/// For non-primitive types, falls back to `ScalarValue::add`.
-pub(crate) fn scalar_add(lhs: &ScalarValue, rhs: &ScalarValue) -> Result<ScalarValue> {
- macro_rules! add_int {
- ($lhs:expr, $rhs:expr, $VARIANT:ident) => {
- match ($lhs, $rhs) {
- (ScalarValue::$VARIANT(Some(a)), ScalarValue::$VARIANT(Some(b))) => {
- Ok(ScalarValue::$VARIANT(Some(a.saturating_add(*b))))
- }
- (ScalarValue::$VARIANT(None), other)
- | (other, ScalarValue::$VARIANT(None)) => Ok(other.clone()),
- _ => unreachable!(),
- }
- };
+/// Mirrors the semantics of `Precision<ScalarValue>::add`, including
+/// checked overflow handling, but avoids allocating a fresh
+/// [`ScalarValue`] for the common numeric fast path.
+pub(crate) fn precision_add(
+ lhs: &mut Precision<ScalarValue>,
+ rhs: &Precision<ScalarValue>,
+) {
+ let (mut lhs_value, lhs_is_exact) = match std::mem::take(lhs) {
+ Precision::Exact(value) => (value, true),
+ Precision::Inexact(value) => (value, false),
+ Precision::Absent => return,
+ };
+
+ let (rhs_value, rhs_is_exact) = match rhs {
+ Precision::Exact(value) => (value, true),
+ Precision::Inexact(value) => (value, false),
+ Precision::Absent => return,
+ };
+
+ if scalar_add(&mut lhs_value, rhs_value).is_err() {
+ return;
}
- macro_rules! add_decimal {
- ($lhs:expr, $rhs:expr, $VARIANT:ident) => {
- match ($lhs, $rhs) {
- (
- ScalarValue::$VARIANT(Some(a), p, s),
- ScalarValue::$VARIANT(Some(b), _, _),
- ) => Ok(ScalarValue::$VARIANT(Some(a.saturating_add(*b)), *p, *s)),
- (ScalarValue::$VARIANT(None, _, _), other)
- | (other, ScalarValue::$VARIANT(None, _, _)) => Ok(other.clone()),
- _ => unreachable!(),
- }
- };
+ *lhs = if lhs_is_exact && rhs_is_exact {
+ Precision::Exact(lhs_value)
+ } else {
+ Precision::Inexact(lhs_value)
+ };
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_scalar_add_null_propagates() -> Result<()> {
+ let mut lhs = ScalarValue::Int32(Some(42));
+
+ scalar_add(&mut lhs, &ScalarValue::Int32(None))?;
+
+ assert_eq!(lhs, ScalarValue::Int32(None));
+ Ok(())
}
- macro_rules! add_float {
- ($lhs:expr, $rhs:expr, $VARIANT:ident) => {
- match ($lhs, $rhs) {
- (ScalarValue::$VARIANT(Some(a)), ScalarValue::$VARIANT(Some(b))) => {
- Ok(ScalarValue::$VARIANT(Some(*a + *b)))
- }
- (ScalarValue::$VARIANT(None), other)
- | (other, ScalarValue::$VARIANT(None)) => Ok(other.clone()),
- _ => unreachable!(),
- }
- };
+ #[test]
+ fn test_scalar_add_overflow_returns_error() {
+ let mut lhs = ScalarValue::Int32(Some(i32::MAX));
+
+ let err = scalar_add(&mut lhs, &ScalarValue::Int32(Some(1)))
+ .unwrap_err()
+ .strip_backtrace();
+
+ assert_eq!(
+ err,
+ "Arrow error: Arithmetic overflow: Overflow happened on: 2147483647 + 1"
+ );
}
- match lhs {
- ScalarValue::Int8(_) => add_int!(lhs, rhs, Int8),
- ScalarValue::Int16(_) => add_int!(lhs, rhs, Int16),
- ScalarValue::Int32(_) => add_int!(lhs, rhs, Int32),
- ScalarValue::Int64(_) => add_int!(lhs, rhs, Int64),
- ScalarValue::UInt8(_) => add_int!(lhs, rhs, UInt8),
- ScalarValue::UInt16(_) => add_int!(lhs, rhs, UInt16),
- ScalarValue::UInt32(_) => add_int!(lhs, rhs, UInt32),
- ScalarValue::UInt64(_) => add_int!(lhs, rhs, UInt64),
- ScalarValue::Float16(_) => add_float!(lhs, rhs, Float16),
- ScalarValue::Float32(_) => add_float!(lhs, rhs, Float32),
- ScalarValue::Float64(_) => add_float!(lhs, rhs, Float64),
- ScalarValue::Decimal32(_, _, _) => add_decimal!(lhs, rhs, Decimal32),
- ScalarValue::Decimal64(_, _, _) => add_decimal!(lhs, rhs, Decimal64),
- ScalarValue::Decimal128(_, _, _) => add_decimal!(lhs, rhs, Decimal128),
- ScalarValue::Decimal256(_, _, _) => match (lhs, rhs) {
- (
- ScalarValue::Decimal256(Some(a), p, s),
- ScalarValue::Decimal256(Some(b), _, _),
- ) => Ok(ScalarValue::Decimal256(
- Some(i256_saturating_add(*a, *b)),
- *p,
- *s,
- )),
- (ScalarValue::Decimal256(None, _, _), other)
- | (other, ScalarValue::Decimal256(None, _, _)) => Ok(other.clone()),
- _ => unreachable!(),
- },
- // Fallback: use the existing ScalarValue::add
- _ => lhs.add(rhs),
+ #[test]
+ fn test_precision_add_null_propagates() {
+ let mut lhs = Precision::Exact(ScalarValue::Int32(Some(42)));
+
+ precision_add(&mut lhs, &Precision::Exact(ScalarValue::Int32(None)));
+
+ assert_eq!(lhs, Precision::Exact(ScalarValue::Int32(None)));
}
-}
-/// [`Precision`]-aware sum of two [`ScalarValue`] precisions using
-/// cheap direct addition via [`scalar_add`].
-///
-/// Mirrors the semantics of `Precision<ScalarValue>::add` but avoids
-/// the expensive `ScalarValue::add` round-trip through Arrow arrays.
-pub(crate) fn precision_add(
- lhs: &Precision<ScalarValue>,
- rhs: &Precision<ScalarValue>,
-) -> Precision<ScalarValue> {
- match (lhs, rhs) {
- (Precision::Exact(a), Precision::Exact(b)) => scalar_add(a, b)
- .map(Precision::Exact)
- .unwrap_or(Precision::Absent),
- (Precision::Inexact(a), Precision::Exact(b))
- | (Precision::Exact(a), Precision::Inexact(b))
- | (Precision::Inexact(a), Precision::Inexact(b)) => scalar_add(a, b)
- .map(Precision::Inexact)
- .unwrap_or(Precision::Absent),
- (_, _) => Precision::Absent,
+ #[test]
+ fn test_precision_add_overflow_becomes_absent() {
+ let mut lhs = Precision::Exact(ScalarValue::Int32(Some(i32::MAX)));
+
+ precision_add(&mut lhs, &Precision::Exact(ScalarValue::Int32(Some(1))));
+
+ assert_eq!(lhs, Precision::Absent);
+ }
+
+ #[test]
+ fn test_precision_add_rhs_absent_absorbs() {
+ let mut lhs = Precision::Exact(ScalarValue::Int32(Some(42)));
+
+ precision_add(&mut lhs, &Precision::Absent);
+
+ assert_eq!(lhs, Precision::Absent);
+ }
+
+ #[test]
+ fn test_precision_add_mixed_exactness() {
+ let mut lhs = Precision::Exact(ScalarValue::Int32(Some(10)));
+
+ precision_add(&mut lhs, &Precision::Inexact(ScalarValue::Int32(Some(5))));
+
+ assert_eq!(lhs, Precision::Inexact(ScalarValue::Int32(Some(15))));
}
}