Skip to content

Commit b0e6ae0

Browse files
adriangbclaude
andcommitted
test: add unit tests for Interval::intersect/union/contains with mismatched Decimal128 types
Per PR review feedback, lock in the comparison-coercion behavior at the API layer with focused tests asserting exact bounds and the TRUE / FALSE / TRUE_OR_FALSE outcomes for `contains`. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 3cbe183 commit b0e6ae0

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

‎datafusion/expr-common/src/interval_arithmetic.rs‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3869,6 +3869,101 @@ mod tests {
38693869
Ok(())
38703870
}
38713871

3872+
#[test]
3873+
fn test_intersect_mismatched_decimal_types() -> Result<()> {
3874+
// Regression test: previously `Interval::intersect` asserted that both
3875+
// operands had identical data types. Now it coerces via
3876+
// `comparison_coercion`, which for `Decimal128(38, 10)` and
3877+
// `Decimal128(20, 0)` produces `Decimal128(38, 10)`.
3878+
3879+
// Overlapping intervals: [0.0, 10.0] ∩ [5, 20] = [5.0, 10.0]
3880+
let lhs = Interval::try_new(
3881+
ScalarValue::Decimal128(Some(0), 38, 10),
3882+
ScalarValue::Decimal128(Some(100_000_000_000), 38, 10), // 10.0
3883+
)?;
3884+
let rhs = Interval::try_new(
3885+
ScalarValue::Decimal128(Some(5), 20, 0),
3886+
ScalarValue::Decimal128(Some(20), 20, 0),
3887+
)?;
3888+
let intersected = lhs.intersect(&rhs)?.expect("intervals overlap");
3889+
let expected = Interval::try_new(
3890+
ScalarValue::Decimal128(Some(50_000_000_000), 38, 10), // 5.0
3891+
ScalarValue::Decimal128(Some(100_000_000_000), 38, 10), // 10.0
3892+
)?;
3893+
assert_eq!(intersected, expected);
3894+
assert_eq!(intersected.data_type(), DataType::Decimal128(38, 10));
3895+
3896+
// Disjoint intervals across mismatched precisions: [0.0, 3.0] ∩ [5, 20] = ∅
3897+
let lhs_disjoint = Interval::try_new(
3898+
ScalarValue::Decimal128(Some(0), 38, 10),
3899+
ScalarValue::Decimal128(Some(30_000_000_000), 38, 10), // 3.0
3900+
)?;
3901+
assert_eq!(lhs_disjoint.intersect(&rhs)?, None);
3902+
3903+
Ok(())
3904+
}
3905+
3906+
#[test]
3907+
fn test_union_mismatched_decimal_types() -> Result<()> {
3908+
// [0.0, 3.0] ∪ [5, 20] (mismatched precision/scale) = [0.0, 20.0]
3909+
let lhs = Interval::try_new(
3910+
ScalarValue::Decimal128(Some(0), 38, 10),
3911+
ScalarValue::Decimal128(Some(30_000_000_000), 38, 10), // 3.0
3912+
)?;
3913+
let rhs = Interval::try_new(
3914+
ScalarValue::Decimal128(Some(5), 20, 0),
3915+
ScalarValue::Decimal128(Some(20), 20, 0),
3916+
)?;
3917+
let unioned = lhs.union(&rhs)?;
3918+
let expected = Interval::try_new(
3919+
ScalarValue::Decimal128(Some(0), 38, 10),
3920+
ScalarValue::Decimal128(Some(200_000_000_000), 38, 10), // 20.0
3921+
)?;
3922+
assert_eq!(unioned, expected);
3923+
assert_eq!(unioned.data_type(), DataType::Decimal128(38, 10));
3924+
3925+
Ok(())
3926+
}
3927+
3928+
#[test]
3929+
fn test_contains_mismatched_decimal_types() -> Result<()> {
3930+
// `contains` should return TRUE when the lhs is a strict superset of
3931+
// rhs after coercion, TRUE_OR_FALSE when they merely overlap, and
3932+
// FALSE when they are disjoint — even with mismatched Decimal128
3933+
// precision/scale.
3934+
let rhs = Interval::try_new(
3935+
ScalarValue::Decimal128(Some(5), 20, 0),
3936+
ScalarValue::Decimal128(Some(10), 20, 0),
3937+
)?;
3938+
3939+
// Superset: [0.0, 20.0] ⊇ [5, 10] → TRUE
3940+
let superset = Interval::try_new(
3941+
ScalarValue::Decimal128(Some(0), 38, 10),
3942+
ScalarValue::Decimal128(Some(200_000_000_000), 38, 10), // 20.0
3943+
)?;
3944+
assert_eq!(superset.contains(&rhs)?, Interval::TRUE);
3945+
3946+
// Overlap (not superset): [0.0, 7.0] ∩ [5, 10] = [5.0, 7.0] → TRUE_OR_FALSE
3947+
let overlap = Interval::try_new(
3948+
ScalarValue::Decimal128(Some(0), 38, 10),
3949+
ScalarValue::Decimal128(Some(70_000_000_000), 38, 10), // 7.0
3950+
)?;
3951+
assert_eq!(overlap.contains(&rhs)?, Interval::TRUE_OR_FALSE);
3952+
3953+
// Disjoint: [0.0, 3.0] ∩ [5, 10] = ∅ → FALSE
3954+
let disjoint = Interval::try_new(
3955+
ScalarValue::Decimal128(Some(0), 38, 10),
3956+
ScalarValue::Decimal128(Some(30_000_000_000), 38, 10), // 3.0
3957+
)?;
3958+
assert_eq!(disjoint.contains(&rhs)?, Interval::FALSE);
3959+
3960+
// Cross-type with Int64: [0.0, 20.0] ⊇ [5, 10] → TRUE
3961+
let int_rhs = Interval::make(Some(5_i64), Some(10_i64))?;
3962+
assert_eq!(superset.contains(&int_rhs)?, Interval::TRUE);
3963+
3964+
Ok(())
3965+
}
3966+
38723967
#[test]
38733968
fn test_overflow_handling() -> Result<()> {
38743969
// Test integer overflow handling:

0 commit comments

Comments
 (0)