1use std::borrow::Borrow;
8use std::fmt::{self, Display, Formatter};
9use std::ops::{AddAssign, SubAssign};
10
11use crate::operator::Operator;
12use crate::type_coercion::binary::{BinaryTypeCoercer, comparison_coercion};
13
14use arrow::compute::{CastOptions, cast_with_options};
15use arrow::datatypes::{
16 DataType, IntervalDayTime, IntervalMonthDayNano, IntervalUnit,
17 MAX_DECIMAL128_FOR_EACH_PRECISION, MAX_DECIMAL256_FOR_EACH_PRECISION,
18 MIN_DECIMAL128_FOR_EACH_PRECISION, MIN_DECIMAL256_FOR_EACH_PRECISION, TimeUnit,
19};
20use datafusion_common::rounding::{alter_fp_rounding_mode, next_down, next_up};
21use datafusion_common::{
22 DataFusionError, Result, ScalarValue, assert_eq_or_internal_err,
23 assert_or_internal_err, internal_err,
24};
25
26macro_rules! get_extreme_value {
27 ($extreme:ident, $DECIMAL128_ARRAY:ident, $DECIMAL256_ARRAY:ident, $value:expr) => {
28 match $value {
29 DataType::UInt8 => ScalarValue::UInt8(Some(u8::$extreme)),
30 DataType::UInt16 => ScalarValue::UInt16(Some(u16::$extreme)),
31 DataType::UInt32 => ScalarValue::UInt32(Some(u32::$extreme)),
32 DataType::UInt64 => ScalarValue::UInt64(Some(u64::$extreme)),
33 DataType::Int8 => ScalarValue::Int8(Some(i8::$extreme)),
34 DataType::Int16 => ScalarValue::Int16(Some(i16::$extreme)),
35 DataType::Int32 => ScalarValue::Int32(Some(i32::$extreme)),
36 DataType::Int64 => ScalarValue::Int64(Some(i64::$extreme)),
37 DataType::Float32 => ScalarValue::Float32(Some(f32::$extreme)),
38 DataType::Float64 => ScalarValue::Float64(Some(f64::$extreme)),
39 DataType::Date32 => ScalarValue::Date32(Some(i32::$extreme)),
40 DataType::Date64 => ScalarValue::Date64(Some(i64::$extreme)),
41 DataType::Duration(TimeUnit::Second) => {
42 ScalarValue::DurationSecond(Some(i64::$extreme))
43 }
44 DataType::Duration(TimeUnit::Millisecond) => {
45 ScalarValue::DurationMillisecond(Some(i64::$extreme))
46 }
47 DataType::Duration(TimeUnit::Microsecond) => {
48 ScalarValue::DurationMicrosecond(Some(i64::$extreme))
49 }
50 DataType::Duration(TimeUnit::Nanosecond) => {
51 ScalarValue::DurationNanosecond(Some(i64::$extreme))
52 }
53 DataType::Timestamp(TimeUnit::Second, _) => {
54 ScalarValue::TimestampSecond(Some(i64::$extreme), None)
55 }
56 DataType::Timestamp(TimeUnit::Millisecond, _) => {
57 ScalarValue::TimestampMillisecond(Some(i64::$extreme), None)
58 }
59 DataType::Timestamp(TimeUnit::Microsecond, _) => {
60 ScalarValue::TimestampMicrosecond(Some(i64::$extreme), None)
61 }
62 DataType::Timestamp(TimeUnit::Nanosecond, _) => {
63 ScalarValue::TimestampNanosecond(Some(i64::$extreme), None)
64 }
65 DataType::Interval(IntervalUnit::YearMonth) => {
66 ScalarValue::IntervalYearMonth(Some(i32::$extreme))
67 }
68 DataType::Interval(IntervalUnit::DayTime) => {
69 ScalarValue::IntervalDayTime(Some(IntervalDayTime::$extreme))
70 }
71 DataType::Interval(IntervalUnit::MonthDayNano) => {
72 ScalarValue::IntervalMonthDayNano(Some(IntervalMonthDayNano::$extreme))
73 }
74 DataType::Decimal128(precision, scale) => ScalarValue::Decimal128(
75 Some($DECIMAL128_ARRAY[*precision as usize]),
76 *precision,
77 *scale,
78 ),
79 DataType::Decimal256(precision, scale) => ScalarValue::Decimal256(
80 Some($DECIMAL256_ARRAY[*precision as usize]),
81 *precision,
82 *scale,
83 ),
84 _ => unreachable!(),
85 }
86 };
87}
88
89macro_rules! value_transition {
90 ($bound:ident, $direction:expr, $value:expr) => {
91 match $value {
92 UInt8(Some(value)) if value == u8::$bound => UInt8(None),
93 UInt16(Some(value)) if value == u16::$bound => UInt16(None),
94 UInt32(Some(value)) if value == u32::$bound => UInt32(None),
95 UInt64(Some(value)) if value == u64::$bound => UInt64(None),
96 Int8(Some(value)) if value == i8::$bound => Int8(None),
97 Int16(Some(value)) if value == i16::$bound => Int16(None),
98 Int32(Some(value)) if value == i32::$bound => Int32(None),
99 Int64(Some(value)) if value == i64::$bound => Int64(None),
100 Float32(Some(value)) if value == f32::$bound => Float32(None),
101 Float64(Some(value)) if value == f64::$bound => Float64(None),
102 DurationSecond(Some(value)) if value == i64::$bound => DurationSecond(None),
103 DurationMillisecond(Some(value)) if value == i64::$bound => {
104 DurationMillisecond(None)
105 }
106 DurationMicrosecond(Some(value)) if value == i64::$bound => {
107 DurationMicrosecond(None)
108 }
109 DurationNanosecond(Some(value)) if value == i64::$bound => {
110 DurationNanosecond(None)
111 }
112 TimestampSecond(Some(value), tz) if value == i64::$bound => {
113 TimestampSecond(None, tz)
114 }
115 TimestampMillisecond(Some(value), tz) if value == i64::$bound => {
116 TimestampMillisecond(None, tz)
117 }
118 TimestampMicrosecond(Some(value), tz) if value == i64::$bound => {
119 TimestampMicrosecond(None, tz)
120 }
121 TimestampNanosecond(Some(value), tz) if value == i64::$bound => {
122 TimestampNanosecond(None, tz)
123 }
124 IntervalYearMonth(Some(value)) if value == i32::$bound => {
125 IntervalYearMonth(None)
126 }
127 IntervalDayTime(Some(value))
128 if value == arrow::datatypes::IntervalDayTime::$bound =>
129 {
130 IntervalDayTime(None)
131 }
132 IntervalMonthDayNano(Some(value))
133 if value == arrow::datatypes::IntervalMonthDayNano::$bound =>
134 {
135 IntervalMonthDayNano(None)
136 }
137 _ => next_value_helper::<$direction>($value),
138 }
139 };
140}
141
142#[derive(Debug, Clone, PartialEq, Eq)]
165pub struct Interval {
166 lower: ScalarValue,
167 upper: ScalarValue,
168}
169
170macro_rules! handle_float_intervals {
184 ($scalar_type:ident, $primitive_type:ident, $lower:expr, $upper:expr) => {{
185 let lower = match $lower {
186 ScalarValue::$scalar_type(Some(l_val))
187 if l_val == $primitive_type::NEG_INFINITY || l_val.is_nan() =>
188 {
189 ScalarValue::$scalar_type(None)
190 }
191 ScalarValue::$scalar_type(Some(l_val))
192 if l_val == $primitive_type::INFINITY =>
193 {
194 ScalarValue::$scalar_type(Some($primitive_type::MAX))
195 }
196 value @ ScalarValue::$scalar_type(Some(_)) => value,
197 _ => ScalarValue::$scalar_type(None),
198 };
199
200 let upper = match $upper {
201 ScalarValue::$scalar_type(Some(r_val))
202 if r_val == $primitive_type::INFINITY || r_val.is_nan() =>
203 {
204 ScalarValue::$scalar_type(None)
205 }
206 ScalarValue::$scalar_type(Some(r_val))
207 if r_val == $primitive_type::NEG_INFINITY =>
208 {
209 ScalarValue::$scalar_type(Some($primitive_type::MIN))
210 }
211 value @ ScalarValue::$scalar_type(Some(_)) => value,
212 _ => ScalarValue::$scalar_type(None),
213 };
214
215 Interval { lower, upper }
216 }};
217}
218
219macro_rules! map_floating_point_order {
229 ($value:expr, $ty:ty) => {{
230 let num_bits = std::mem::size_of::<$ty>() * 8;
231 let sign_bit = 1 << (num_bits - 1);
232 if $value & sign_bit == sign_bit {
233 !$value
235 } else {
236 $value | sign_bit
238 }
239 }};
240}
241
242impl Interval {
243 pub fn try_new(lower: ScalarValue, upper: ScalarValue) -> Result<Self> {
255 assert_eq_or_internal_err!(
256 lower.data_type(),
257 upper.data_type(),
258 "Endpoints of an Interval should have the same type"
259 );
260
261 let interval = Self::new(lower, upper);
262
263 assert_or_internal_err!(
264 interval.lower.is_null()
265 || interval.upper.is_null()
266 || interval.lower <= interval.upper,
267 "Interval's lower bound {} is greater than the upper bound {}",
268 interval.lower,
269 interval.upper
270 );
271 Ok(interval)
272 }
273
274 fn new(lower: ScalarValue, upper: ScalarValue) -> Self {
280 if let ScalarValue::Boolean(lower_bool) = lower {
281 let ScalarValue::Boolean(upper_bool) = upper else {
282 unreachable!();
284 };
285 return Self {
287 lower: ScalarValue::Boolean(Some(lower_bool.unwrap_or(false))),
288 upper: ScalarValue::Boolean(Some(upper_bool.unwrap_or(true))),
289 };
290 }
291 match lower.data_type() {
292 DataType::Float32 => handle_float_intervals!(Float32, f32, lower, upper),
294 DataType::Float64 => handle_float_intervals!(Float64, f64, lower, upper),
295 DataType::UInt8 if lower.is_null() => Self {
297 lower: ScalarValue::UInt8(Some(0)),
298 upper,
299 },
300 DataType::UInt16 if lower.is_null() => Self {
301 lower: ScalarValue::UInt16(Some(0)),
302 upper,
303 },
304 DataType::UInt32 if lower.is_null() => Self {
305 lower: ScalarValue::UInt32(Some(0)),
306 upper,
307 },
308 DataType::UInt64 if lower.is_null() => Self {
309 lower: ScalarValue::UInt64(Some(0)),
310 upper,
311 },
312 _ => Self { lower, upper },
314 }
315 }
316
317 pub fn make<T>(lower: Option<T>, upper: Option<T>) -> Result<Self>
321 where
322 ScalarValue: From<Option<T>>,
323 {
324 Self::try_new(ScalarValue::from(lower), ScalarValue::from(upper))
325 }
326
327 pub fn make_zero(data_type: &DataType) -> Result<Self> {
329 let zero_endpoint = ScalarValue::new_zero(data_type)?;
330 Ok(Self::new(zero_endpoint.clone(), zero_endpoint))
331 }
332
333 pub fn make_unbounded(data_type: &DataType) -> Result<Self> {
335 let unbounded_endpoint = ScalarValue::try_from(data_type)?;
336 Ok(Self::new(unbounded_endpoint.clone(), unbounded_endpoint))
337 }
338
339 pub fn make_symmetric_unit_interval(data_type: &DataType) -> Result<Self> {
341 Self::try_new(
342 ScalarValue::new_negative_one(data_type)?,
343 ScalarValue::new_one(data_type)?,
344 )
345 }
346
347 pub fn make_symmetric_pi_interval(data_type: &DataType) -> Result<Self> {
349 Self::try_new(
350 ScalarValue::new_negative_pi_lower(data_type)?,
351 ScalarValue::new_pi_upper(data_type)?,
352 )
353 }
354
355 pub fn make_symmetric_half_pi_interval(data_type: &DataType) -> Result<Self> {
357 Self::try_new(
358 ScalarValue::new_neg_frac_pi_2_lower(data_type)?,
359 ScalarValue::new_frac_pi_2_upper(data_type)?,
360 )
361 }
362
363 pub fn make_non_negative_infinity_interval(data_type: &DataType) -> Result<Self> {
365 Self::try_new(
366 ScalarValue::new_zero(data_type)?,
367 ScalarValue::try_from(data_type)?,
368 )
369 }
370
371 pub fn lower(&self) -> &ScalarValue {
373 &self.lower
374 }
375
376 pub fn upper(&self) -> &ScalarValue {
378 &self.upper
379 }
380
381 pub fn into_bounds(self) -> (ScalarValue, ScalarValue) {
384 (self.lower, self.upper)
385 }
386
387 pub fn data_type(&self) -> DataType {
389 let lower_type = self.lower.data_type();
390 let upper_type = self.upper.data_type();
391
392 debug_assert!(
395 lower_type == upper_type,
396 "Interval bounds have different types: {lower_type} != {upper_type}"
397 );
398 lower_type
399 }
400
401 pub fn is_unbounded(&self) -> bool {
403 self.lower.is_null() || self.upper.is_null()
404 }
405
406 pub fn cast_to(
410 &self,
411 data_type: &DataType,
412 cast_options: &CastOptions,
413 ) -> Result<Self> {
414 let bound_options = CastOptions {
416 safe: true,
417 ..cast_options.clone()
418 };
419 let cast_options = if self.data_type().is_numeric() && data_type.is_numeric() {
420 &bound_options
421 } else {
422 cast_options
423 };
424 Self::try_new(
425 cast_scalar_value(&self.lower, data_type, cast_options)?,
426 cast_scalar_value(&self.upper, data_type, cast_options)?,
427 )
428 }
429
430 pub const FALSE: Self = Self {
432 lower: ScalarValue::Boolean(Some(false)),
433 upper: ScalarValue::Boolean(Some(false)),
434 };
435
436 #[deprecated(since = "52.0.0", note = "Use `FALSE` instead")]
437 pub const CERTAINLY_FALSE: Self = Self::FALSE;
438
439 pub const TRUE_OR_FALSE: Self = Self {
441 lower: ScalarValue::Boolean(Some(false)),
442 upper: ScalarValue::Boolean(Some(true)),
443 };
444
445 #[deprecated(since = "52.0.0", note = "Use `TRUE_OR_FALSE` instead")]
446 pub const UNCERTAIN: Self = Self::TRUE_OR_FALSE;
447
448 pub const TRUE: Self = Self {
450 lower: ScalarValue::Boolean(Some(true)),
451 upper: ScalarValue::Boolean(Some(true)),
452 };
453
454 #[deprecated(since = "52.0.0", note = "Use `TRUE` instead")]
455 pub const CERTAINLY_TRUE: Self = Self::TRUE;
456
457 pub fn gt<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
465 let rhs = other.borrow();
466 let lhs_type = self.data_type();
467 let rhs_type = rhs.data_type();
468 assert_eq_or_internal_err!(
469 lhs_type,
470 rhs_type,
471 "Only intervals with the same data type are comparable, lhs:{}, rhs:{}",
472 self.data_type(),
473 rhs.data_type()
474 );
475 if !(self.upper.is_null() || rhs.lower.is_null()) && self.upper <= rhs.lower {
476 Ok(Self::FALSE)
479 } else if !(self.lower.is_null() || rhs.upper.is_null())
480 && (self.lower > rhs.upper)
481 {
482 Ok(Self::TRUE)
485 } else {
486 Ok(Self::TRUE_OR_FALSE)
488 }
489 }
490
491 pub fn gt_eq<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
499 let rhs = other.borrow();
500 let lhs_type = self.data_type();
501 let rhs_type = rhs.data_type();
502 assert_eq_or_internal_err!(
503 lhs_type,
504 rhs_type,
505 "Only intervals with the same data type are comparable, lhs:{}, rhs:{}",
506 self.data_type(),
507 rhs.data_type()
508 );
509 if !(self.lower.is_null() || rhs.upper.is_null()) && self.lower >= rhs.upper {
510 Ok(Self::TRUE)
513 } else if !(self.upper.is_null() || rhs.lower.is_null())
514 && (self.upper < rhs.lower)
515 {
516 Ok(Self::FALSE)
519 } else {
520 Ok(Self::TRUE_OR_FALSE)
522 }
523 }
524
525 pub fn lt<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
533 other.borrow().gt(self)
534 }
535
536 pub fn lt_eq<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
544 other.borrow().gt_eq(self)
545 }
546
547 pub fn equal<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
555 let rhs = other.borrow();
556 let types_compatible =
557 BinaryTypeCoercer::new(&self.data_type(), &Operator::Eq, &rhs.data_type())
558 .get_result_type()
559 .is_ok();
560 assert_or_internal_err!(
561 types_compatible,
562 "Interval data types must be compatible for equality checks, lhs:{}, rhs:{}",
563 self.data_type(),
564 rhs.data_type()
565 );
566 if !self.lower.is_null()
567 && (self.lower == self.upper)
568 && (rhs.lower == rhs.upper)
569 && (self.lower == rhs.lower)
570 {
571 Ok(Self::TRUE)
572 } else if self.intersect(rhs)?.is_none() {
573 Ok(Self::FALSE)
574 } else {
575 Ok(Self::TRUE_OR_FALSE)
576 }
577 }
578
579 pub fn and<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
582 let rhs = other.borrow();
583 match (&self.lower, &self.upper, &rhs.lower, &rhs.upper) {
584 (
585 &ScalarValue::Boolean(Some(self_lower)),
586 &ScalarValue::Boolean(Some(self_upper)),
587 &ScalarValue::Boolean(Some(other_lower)),
588 &ScalarValue::Boolean(Some(other_upper)),
589 ) => {
590 let lower = self_lower && other_lower;
591 let upper = self_upper && other_upper;
592
593 Ok(Self {
594 lower: ScalarValue::Boolean(Some(lower)),
595 upper: ScalarValue::Boolean(Some(upper)),
596 })
597 }
598
599 _ => Ok(Self::TRUE_OR_FALSE),
601 }
602 }
603
604 pub fn or<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
607 let rhs = other.borrow();
608 match (&self.lower, &self.upper, &rhs.lower, &rhs.upper) {
609 (
610 &ScalarValue::Boolean(Some(self_lower)),
611 &ScalarValue::Boolean(Some(self_upper)),
612 &ScalarValue::Boolean(Some(other_lower)),
613 &ScalarValue::Boolean(Some(other_upper)),
614 ) => {
615 let lower = self_lower || other_lower;
616 let upper = self_upper || other_upper;
617
618 Ok(Self {
619 lower: ScalarValue::Boolean(Some(lower)),
620 upper: ScalarValue::Boolean(Some(upper)),
621 })
622 }
623
624 _ => Ok(Self::TRUE_OR_FALSE),
626 }
627 }
628
629 pub fn not(&self) -> Result<Self> {
631 assert_eq_or_internal_err!(
632 self.data_type(),
633 DataType::Boolean,
634 "Cannot apply logical negation to a non-boolean interval"
635 );
636 if self == &Self::TRUE {
637 Ok(Self::FALSE)
638 } else if self == &Self::FALSE {
639 Ok(Self::TRUE)
640 } else {
641 Ok(Self::TRUE_OR_FALSE)
642 }
643 }
644
645 pub fn intersect<T: Borrow<Self>>(&self, other: T) -> Result<Option<Self>> {
652 let rhs = other.borrow();
653 let (lhs_owned, rhs_owned) = coerce_for_comparison(self, rhs)?;
654 let lhs = lhs_owned.as_ref().unwrap_or(self);
655 let rhs = rhs_owned.as_ref().unwrap_or(rhs);
656
657 if (!(lhs.lower.is_null() || rhs.upper.is_null()) && lhs.lower > rhs.upper)
660 || (!(lhs.upper.is_null() || rhs.lower.is_null()) && lhs.upper < rhs.lower)
661 {
662 return Ok(None);
663 }
664
665 let lower = max_lower_bound(&lhs.lower, &rhs.lower);
666 let upper = min_upper_bound(&lhs.upper, &rhs.upper);
667
668 debug_assert!(
670 (lower.is_null() || upper.is_null() || (lower <= upper)),
671 "The intersection of two intervals can not be an invalid interval"
672 );
673
674 Ok(Some(Self { lower, upper }))
675 }
676
677 pub fn union<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
683 let rhs = other.borrow();
684 let (lhs_owned, rhs_owned) = coerce_for_comparison(self, rhs)?;
685 let lhs = lhs_owned.as_ref().unwrap_or(self);
686 let rhs = rhs_owned.as_ref().unwrap_or(rhs);
687
688 let lower = min_lower_bound(&lhs.lower, &rhs.lower);
689 let upper = max_upper_bound(&lhs.upper, &rhs.upper);
690
691 debug_assert!(
693 (lower.is_null() || upper.is_null() || (lower <= upper)),
694 "The union of two intervals can not be an invalid interval"
695 );
696
697 Ok(Self { lower, upper })
698 }
699
700 pub fn contains_value<T: Borrow<ScalarValue>>(&self, other: T) -> Result<bool> {
702 let rhs = other.borrow();
703
704 let (lhs_lower, lhs_upper, rhs_value) = if self.data_type().eq(&rhs.data_type()) {
705 (self.lower.clone(), self.upper.clone(), rhs.clone())
706 } else {
707 let maybe_common_type =
708 comparison_coercion(&self.data_type(), &rhs.data_type());
709 assert_or_internal_err!(
710 maybe_common_type.is_some(),
711 "Data types must be compatible for containment checks, lhs:{}, rhs:{}",
712 self.data_type(),
713 rhs.data_type()
714 );
715 let common_type = maybe_common_type.expect("checked for Some");
716 (
717 self.lower.cast_to(&common_type)?,
718 self.upper.cast_to(&common_type)?,
719 rhs.cast_to(&common_type)?,
720 )
721 };
722
723 Ok(lhs_lower <= rhs_value && (lhs_upper.is_null() || rhs_value <= lhs_upper))
726 }
727
728 pub fn contains<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
736 let rhs = other.borrow();
737 let (lhs_owned, rhs_owned) = coerce_for_comparison(self, rhs)?;
738 let lhs = lhs_owned.as_ref().unwrap_or(self);
739 let rhs = rhs_owned.as_ref().unwrap_or(rhs);
740
741 match lhs.intersect(rhs)? {
742 Some(intersection) => {
743 if &intersection == rhs {
744 Ok(Self::TRUE)
745 } else {
746 Ok(Self::TRUE_OR_FALSE)
747 }
748 }
749 None => Ok(Self::FALSE),
750 }
751 }
752
753 pub fn is_superset(&self, other: &Interval, strict: bool) -> Result<bool> {
760 Ok(!(strict && self.eq(other)) && (self.contains(other)? == Interval::TRUE))
761 }
762
763 pub fn add<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
768 let rhs = other.borrow();
769 let dt =
770 BinaryTypeCoercer::new(&self.data_type(), &Operator::Plus, &rhs.data_type())
771 .get_result_type()?;
772
773 Ok(Self::new(
774 add_bounds::<false>(&dt, &self.lower, &rhs.lower),
775 add_bounds::<true>(&dt, &self.upper, &rhs.upper),
776 ))
777 }
778
779 pub fn sub<T: Borrow<Interval>>(&self, other: T) -> Result<Self> {
785 let rhs = other.borrow();
786 let dt =
787 BinaryTypeCoercer::new(&self.data_type(), &Operator::Minus, &rhs.data_type())
788 .get_result_type()?;
789
790 Ok(Self::new(
791 sub_bounds::<false>(&dt, &self.lower, &rhs.upper),
792 sub_bounds::<true>(&dt, &self.upper, &rhs.lower),
793 ))
794 }
795
796 pub fn mul<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
805 let rhs = other.borrow();
806 let (lhs_owned, rhs_owned, dt) = coerce_operands(self, rhs, &Operator::Multiply)?;
807 let lhs_ref = lhs_owned.as_ref().unwrap_or(self);
808 let rhs_ref = rhs_owned.as_ref().unwrap_or(rhs);
809
810 let zero = ScalarValue::new_zero(&dt)?;
811
812 let result = match (
813 lhs_ref.contains_value(&zero)?,
814 rhs_ref.contains_value(&zero)?,
815 dt.is_unsigned_integer(),
816 ) {
817 (true, true, false) => mul_helper_multi_zero_inclusive(&dt, lhs_ref, rhs_ref),
818 (true, false, false) => {
819 mul_helper_single_zero_inclusive(&dt, lhs_ref, rhs_ref, &zero)
820 }
821 (false, true, false) => {
822 mul_helper_single_zero_inclusive(&dt, rhs_ref, lhs_ref, &zero)
823 }
824 _ => mul_helper_zero_exclusive(&dt, lhs_ref, rhs_ref, &zero),
825 };
826 Ok(result)
827 }
828
829 pub fn div<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
841 let rhs = other.borrow();
842 let (lhs_owned, rhs_owned, dt) = coerce_operands(self, rhs, &Operator::Divide)?;
843 let lhs_ref = lhs_owned.as_ref().unwrap_or(self);
844 let rhs_ref = rhs_owned.as_ref().unwrap_or(rhs);
845
846 let zero = ScalarValue::new_zero(&dt)?;
847 let zero_point = match &dt {
849 DataType::Float32 | DataType::Float64 => {
850 Self::new(zero.clone(), zero.clone())
851 }
852 _ => Self::new(prev_value(zero.clone()), next_value(zero.clone())),
853 };
854
855 if rhs_ref.contains(&zero_point)? == Self::TRUE && !dt.is_unsigned_integer() {
859 Self::make_unbounded(&dt)
860 }
861 else if lhs_ref.contains(&zero_point)? == Self::TRUE
864 && !dt.is_unsigned_integer()
865 {
866 Ok(div_helper_lhs_zero_inclusive(&dt, lhs_ref, rhs_ref, &zero))
867 } else {
868 Ok(div_helper_zero_exclusive(&dt, lhs_ref, rhs_ref, &zero))
869 }
870 }
871
872 pub fn width(&self) -> Result<ScalarValue> {
877 let dt = self.data_type();
878 let width_dt =
879 BinaryTypeCoercer::new(&dt, &Operator::Minus, &dt).get_result_type()?;
880 Ok(sub_bounds::<true>(&width_dt, &self.upper, &self.lower))
881 }
882
883 pub fn cardinality(&self) -> Option<u64> {
891 let data_type = self.data_type();
892 if data_type.is_integer()
893 || matches!(
894 data_type,
895 DataType::Date32
896 | DataType::Date64
897 | DataType::Timestamp(_, _)
898 | DataType::Decimal32(_, _)
899 | DataType::Decimal64(_, _)
900 | DataType::Decimal128(_, _)
901 | DataType::Decimal256(_, _)
902 )
903 {
904 self.upper.distance_u64(&self.lower)
905 } else if data_type.is_floating() {
906 match (&self.lower, &self.upper) {
910 (
913 ScalarValue::Float32(Some(lower)),
914 ScalarValue::Float32(Some(upper)),
915 ) => {
916 let lower_bits = map_floating_point_order!(lower.to_bits(), u32);
917 let upper_bits = map_floating_point_order!(upper.to_bits(), u32);
918 Some((upper_bits - lower_bits) as u64)
919 }
920 (
921 ScalarValue::Float64(Some(lower)),
922 ScalarValue::Float64(Some(upper)),
923 ) => {
924 let lower_bits = map_floating_point_order!(lower.to_bits(), u64);
925 let upper_bits = map_floating_point_order!(upper.to_bits(), u64);
926 let count = upper_bits - lower_bits;
927 (count != u64::MAX).then_some(count)
928 }
929 _ => None,
930 }
931 } else {
932 None
934 }
935 .and_then(|result| result.checked_add(1))
936 }
937
938 pub fn arithmetic_negate(&self) -> Result<Self> {
944 Ok(Self {
945 lower: self.upper.arithmetic_negate()?,
946 upper: self.lower.arithmetic_negate()?,
947 })
948 }
949}
950
951impl Display for Interval {
952 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
953 write!(f, "[{}, {}]", self.lower, self.upper)
954 }
955}
956
957impl From<ScalarValue> for Interval {
958 fn from(value: ScalarValue) -> Self {
959 Self::new(value.clone(), value)
960 }
961}
962
963impl From<&ScalarValue> for Interval {
964 fn from(value: &ScalarValue) -> Self {
965 Self::new(value.to_owned(), value.to_owned())
966 }
967}
968
969fn coerce_for_comparison(
976 lhs: &Interval,
977 rhs: &Interval,
978) -> Result<(Option<Interval>, Option<Interval>)> {
979 let lhs_type = lhs.data_type();
980 let rhs_type = rhs.data_type();
981 if lhs_type == rhs_type {
982 return Ok((None, None));
983 }
984 let maybe_common = comparison_coercion(&lhs_type, &rhs_type);
985 assert_or_internal_err!(
986 maybe_common.is_some(),
987 "Data types must be compatible for interval comparison, lhs:{}, rhs:{}",
988 lhs_type,
989 rhs_type
990 );
991 let common = maybe_common.expect("checked for Some");
992 let cast_options = CastOptions::default();
993 let new_lhs = (lhs_type != common)
994 .then(|| lhs.cast_to(&common, &cast_options))
995 .transpose()?;
996 let new_rhs = (rhs_type != common)
997 .then(|| rhs.cast_to(&common, &cast_options))
998 .transpose()?;
999 Ok((new_lhs, new_rhs))
1000}
1001
1002fn coerce_operands(
1012 lhs: &Interval,
1013 rhs: &Interval,
1014 op: &Operator,
1015) -> Result<(Option<Interval>, Option<Interval>, DataType)> {
1016 let lhs_type = lhs.data_type();
1017 let rhs_type = rhs.data_type();
1018 if lhs_type == rhs_type {
1019 return Ok((None, None, lhs_type));
1020 }
1021 let common_type =
1022 BinaryTypeCoercer::new(&lhs_type, op, &rhs_type).get_result_type()?;
1023 let cast_options = CastOptions::default();
1024 let new_lhs = (lhs_type != common_type)
1025 .then(|| lhs.cast_to(&common_type, &cast_options))
1026 .transpose()?;
1027 let new_rhs = (rhs_type != common_type)
1028 .then(|| rhs.cast_to(&common_type, &cast_options))
1029 .transpose()?;
1030 Ok((new_lhs, new_rhs, common_type))
1031}
1032
1033pub fn apply_operator(op: &Operator, lhs: &Interval, rhs: &Interval) -> Result<Interval> {
1035 match *op {
1036 Operator::Eq => lhs.equal(rhs),
1037 Operator::NotEq => lhs.equal(rhs)?.not(),
1038 Operator::Gt => lhs.gt(rhs),
1039 Operator::GtEq => lhs.gt_eq(rhs),
1040 Operator::Lt => lhs.lt(rhs),
1041 Operator::LtEq => lhs.lt_eq(rhs),
1042 Operator::And => lhs.and(rhs),
1043 Operator::Or => lhs.or(rhs),
1044 Operator::Plus => lhs.add(rhs),
1045 Operator::Minus => lhs.sub(rhs),
1046 Operator::Multiply => lhs.mul(rhs),
1047 Operator::Divide => lhs.div(rhs),
1048 _ => internal_err!("Interval arithmetic does not support the operator {op}"),
1049 }
1050}
1051
1052fn add_bounds<const UPPER: bool>(
1060 dt: &DataType,
1061 lhs: &ScalarValue,
1062 rhs: &ScalarValue,
1063) -> ScalarValue {
1064 if lhs.is_null() || rhs.is_null() {
1065 return ScalarValue::try_from(dt).unwrap();
1066 }
1067
1068 match dt {
1069 DataType::Float64 | DataType::Float32 => {
1070 alter_fp_rounding_mode::<UPPER, _>(lhs, rhs, |lhs, rhs| lhs.add_checked(rhs))
1071 }
1072 _ => lhs.add_checked(rhs),
1073 }
1074 .unwrap_or_else(|_| handle_overflow::<UPPER>(dt, Operator::Plus, lhs, rhs))
1075}
1076
1077fn sub_bounds<const UPPER: bool>(
1085 dt: &DataType,
1086 lhs: &ScalarValue,
1087 rhs: &ScalarValue,
1088) -> ScalarValue {
1089 if lhs.is_null() || rhs.is_null() {
1090 return ScalarValue::try_from(dt).unwrap();
1091 }
1092
1093 match dt {
1094 DataType::Float64 | DataType::Float32 => {
1095 alter_fp_rounding_mode::<UPPER, _>(lhs, rhs, |lhs, rhs| lhs.sub_checked(rhs))
1096 }
1097 _ => lhs.sub_checked(rhs),
1098 }
1099 .unwrap_or_else(|_| handle_overflow::<UPPER>(dt, Operator::Minus, lhs, rhs))
1100}
1101
1102fn mul_bounds<const UPPER: bool>(
1110 dt: &DataType,
1111 lhs: &ScalarValue,
1112 rhs: &ScalarValue,
1113) -> ScalarValue {
1114 if lhs.is_null() || rhs.is_null() {
1115 return ScalarValue::try_from(dt).unwrap();
1116 }
1117
1118 match dt {
1119 DataType::Float64 | DataType::Float32 => {
1120 alter_fp_rounding_mode::<UPPER, _>(lhs, rhs, |lhs, rhs| lhs.mul_checked(rhs))
1121 }
1122 _ => lhs.mul_checked(rhs),
1123 }
1124 .unwrap_or_else(|_| handle_overflow::<UPPER>(dt, Operator::Multiply, lhs, rhs))
1125}
1126
1127fn div_bounds<const UPPER: bool>(
1135 dt: &DataType,
1136 lhs: &ScalarValue,
1137 rhs: &ScalarValue,
1138) -> ScalarValue {
1139 let zero = ScalarValue::new_zero(dt).unwrap();
1140
1141 if (lhs.is_null() || rhs.eq(&zero)) || (dt.is_unsigned_integer() && rhs.is_null()) {
1142 return ScalarValue::try_from(dt).unwrap();
1143 } else if rhs.is_null() {
1144 return zero;
1145 }
1146
1147 match dt {
1148 DataType::Float64 | DataType::Float32 => {
1149 alter_fp_rounding_mode::<UPPER, _>(lhs, rhs, |lhs, rhs| lhs.div(rhs))
1150 }
1151 _ => lhs.div(rhs),
1152 }
1153 .unwrap_or_else(|_| handle_overflow::<UPPER>(dt, Operator::Divide, lhs, rhs))
1154}
1155
1156fn handle_overflow<const UPPER: bool>(
1174 dt: &DataType,
1175 op: Operator,
1176 lhs: &ScalarValue,
1177 rhs: &ScalarValue,
1178) -> ScalarValue {
1179 let lhs_zero = ScalarValue::new_zero(&lhs.data_type()).unwrap();
1180 let rhs_zero = ScalarValue::new_zero(&rhs.data_type()).unwrap();
1181 let positive_sign = match op {
1182 Operator::Multiply | Operator::Divide => {
1183 lhs.lt(&lhs_zero) && rhs.lt(&rhs_zero)
1184 || lhs.gt(&lhs_zero) && rhs.gt(&rhs_zero)
1185 }
1186 Operator::Plus => lhs.ge(&lhs_zero),
1187 Operator::Minus => lhs.ge(rhs),
1188 _ => {
1189 unreachable!()
1190 }
1191 };
1192
1193 match (UPPER, positive_sign) {
1194 (true, true) | (false, false) => ScalarValue::try_from(dt).unwrap(),
1195 (true, false) => {
1196 get_extreme_value!(
1197 MIN,
1198 MIN_DECIMAL128_FOR_EACH_PRECISION,
1199 MIN_DECIMAL256_FOR_EACH_PRECISION,
1200 dt
1201 )
1202 }
1203 (false, true) => {
1204 get_extreme_value!(
1205 MAX,
1206 MAX_DECIMAL128_FOR_EACH_PRECISION,
1207 MAX_DECIMAL256_FOR_EACH_PRECISION,
1208 dt
1209 )
1210 }
1211 }
1212}
1213
1214fn next_value(value: ScalarValue) -> ScalarValue {
1217 use ScalarValue::*;
1218 value_transition!(MAX, true, value)
1219}
1220
1221fn prev_value(value: ScalarValue) -> ScalarValue {
1224 use ScalarValue::*;
1225 value_transition!(MIN, false, value)
1226}
1227
1228trait OneTrait: Sized + std::ops::Add + std::ops::Sub {
1229 fn one() -> Self;
1230}
1231macro_rules! impl_OneTrait{
1232 ($($m:ty),*) => {$( impl OneTrait for $m { fn one() -> Self { 1 as $m } })*}
1233}
1234impl_OneTrait! {u8, u16, u32, u64, i8, i16, i32, i64, i128}
1235
1236impl OneTrait for IntervalDayTime {
1237 fn one() -> Self {
1238 IntervalDayTime {
1239 days: 0,
1240 milliseconds: 1,
1241 }
1242 }
1243}
1244
1245impl OneTrait for IntervalMonthDayNano {
1246 fn one() -> Self {
1247 IntervalMonthDayNano {
1248 months: 0,
1249 days: 0,
1250 nanoseconds: 1,
1251 }
1252 }
1253}
1254
1255fn increment_decrement<const INC: bool, T: OneTrait + SubAssign + AddAssign>(
1258 mut value: T,
1259) -> T {
1260 if INC {
1261 value.add_assign(T::one());
1262 } else {
1263 value.sub_assign(T::one());
1264 }
1265 value
1266}
1267
1268fn next_value_helper<const INC: bool>(value: ScalarValue) -> ScalarValue {
1271 use ScalarValue::*;
1272 match value {
1273 Float32(Some(val)) => {
1275 debug_assert!(val.is_finite(), "Non-standardized floating point usage");
1276 Float32(Some(if INC { next_up(val) } else { next_down(val) }))
1277 }
1278 Float64(Some(val)) => {
1279 debug_assert!(val.is_finite(), "Non-standardized floating point usage");
1280 Float64(Some(if INC { next_up(val) } else { next_down(val) }))
1281 }
1282 Int8(Some(val)) => Int8(Some(increment_decrement::<INC, i8>(val))),
1283 Int16(Some(val)) => Int16(Some(increment_decrement::<INC, i16>(val))),
1284 Int32(Some(val)) => Int32(Some(increment_decrement::<INC, i32>(val))),
1285 Int64(Some(val)) => Int64(Some(increment_decrement::<INC, i64>(val))),
1286 UInt8(Some(val)) => UInt8(Some(increment_decrement::<INC, u8>(val))),
1287 UInt16(Some(val)) => UInt16(Some(increment_decrement::<INC, u16>(val))),
1288 UInt32(Some(val)) => UInt32(Some(increment_decrement::<INC, u32>(val))),
1289 UInt64(Some(val)) => UInt64(Some(increment_decrement::<INC, u64>(val))),
1290 DurationSecond(Some(val)) => {
1291 DurationSecond(Some(increment_decrement::<INC, i64>(val)))
1292 }
1293 DurationMillisecond(Some(val)) => {
1294 DurationMillisecond(Some(increment_decrement::<INC, i64>(val)))
1295 }
1296 DurationMicrosecond(Some(val)) => {
1297 DurationMicrosecond(Some(increment_decrement::<INC, i64>(val)))
1298 }
1299 DurationNanosecond(Some(val)) => {
1300 DurationNanosecond(Some(increment_decrement::<INC, i64>(val)))
1301 }
1302 TimestampSecond(Some(val), tz) => {
1303 TimestampSecond(Some(increment_decrement::<INC, i64>(val)), tz)
1304 }
1305 TimestampMillisecond(Some(val), tz) => {
1306 TimestampMillisecond(Some(increment_decrement::<INC, i64>(val)), tz)
1307 }
1308 TimestampMicrosecond(Some(val), tz) => {
1309 TimestampMicrosecond(Some(increment_decrement::<INC, i64>(val)), tz)
1310 }
1311 TimestampNanosecond(Some(val), tz) => {
1312 TimestampNanosecond(Some(increment_decrement::<INC, i64>(val)), tz)
1313 }
1314 IntervalYearMonth(Some(val)) => {
1315 IntervalYearMonth(Some(increment_decrement::<INC, i32>(val)))
1316 }
1317 IntervalDayTime(Some(val)) => IntervalDayTime(Some(increment_decrement::<
1318 INC,
1319 arrow::datatypes::IntervalDayTime,
1320 >(val))),
1321 IntervalMonthDayNano(Some(val)) => {
1322 IntervalMonthDayNano(Some(increment_decrement::<
1323 INC,
1324 arrow::datatypes::IntervalMonthDayNano,
1325 >(val)))
1326 }
1327 _ => value, }
1329}
1330
1331fn min_lower_bound(first: &ScalarValue, second: &ScalarValue) -> ScalarValue {
1333 if first.is_null() || (!second.is_null() && first <= second) {
1334 first.clone()
1335 } else {
1336 second.clone()
1337 }
1338}
1339
1340fn max_lower_bound(first: &ScalarValue, second: &ScalarValue) -> ScalarValue {
1342 if !first.is_null() && (second.is_null() || first >= second) {
1343 first.clone()
1344 } else {
1345 second.clone()
1346 }
1347}
1348
1349fn min_upper_bound(first: &ScalarValue, second: &ScalarValue) -> ScalarValue {
1351 if !first.is_null() && (second.is_null() || first <= second) {
1352 first.clone()
1353 } else {
1354 second.clone()
1355 }
1356}
1357
1358fn max_upper_bound(first: &ScalarValue, second: &ScalarValue) -> ScalarValue {
1360 if first.is_null() || (!second.is_null() && first >= second) {
1361 first.clone()
1362 } else {
1363 second.clone()
1364 }
1365}
1366
1367pub fn satisfy_greater(
1396 left: &Interval,
1397 right: &Interval,
1398 strict: bool,
1399) -> Result<Option<(Interval, Interval)>> {
1400 let lhs_type = left.data_type();
1401 let rhs_type = right.data_type();
1402 assert_eq_or_internal_err!(
1403 lhs_type.clone(),
1404 rhs_type.clone(),
1405 "Intervals must have the same data type, lhs:{}, rhs:{}",
1406 lhs_type,
1407 rhs_type
1408 );
1409
1410 if !left.upper.is_null() && left.upper <= right.lower {
1411 if !strict && left.upper == right.lower {
1412 return Ok(Some((
1414 Interval::new(left.upper.clone(), left.upper.clone()),
1415 Interval::new(left.upper.clone(), left.upper.clone()),
1416 )));
1417 } else {
1418 return Ok(None);
1422 }
1423 }
1424
1425 let new_left_lower = if left.lower.is_null() || left.lower <= right.lower {
1428 if strict {
1429 next_value(right.lower.clone())
1430 } else {
1431 right.lower.clone()
1432 }
1433 } else {
1434 left.lower.clone()
1435 };
1436 let new_right_upper = if right.upper.is_null()
1439 || (!left.upper.is_null() && left.upper <= right.upper)
1440 {
1441 if strict {
1442 prev_value(left.upper.clone())
1443 } else {
1444 left.upper.clone()
1445 }
1446 } else {
1447 right.upper.clone()
1448 };
1449 Ok(Some((
1451 Interval::new(new_left_lower, left.upper.clone()),
1452 Interval::new(right.lower.clone(), new_right_upper),
1453 )))
1454}
1455
1456fn mul_helper_multi_zero_inclusive(
1472 dt: &DataType,
1473 lhs: &Interval,
1474 rhs: &Interval,
1475) -> Interval {
1476 if lhs.lower.is_null()
1477 || lhs.upper.is_null()
1478 || rhs.lower.is_null()
1479 || rhs.upper.is_null()
1480 {
1481 return Interval::make_unbounded(dt).unwrap();
1482 }
1483 let lower = min_lower_bound(
1487 &mul_bounds::<false>(dt, &lhs.lower, &rhs.upper),
1488 &mul_bounds::<false>(dt, &rhs.lower, &lhs.upper),
1489 );
1490 let upper = max_upper_bound(
1491 &mul_bounds::<true>(dt, &lhs.upper, &rhs.upper),
1492 &mul_bounds::<true>(dt, &lhs.lower, &rhs.lower),
1493 );
1494 Interval::new(lower, upper)
1496}
1497
1498fn mul_helper_single_zero_inclusive(
1520 dt: &DataType,
1521 lhs: &Interval,
1522 rhs: &Interval,
1523 zero: &ScalarValue,
1524) -> Interval {
1525 if rhs.upper <= *zero && !rhs.upper.is_null() {
1527 let lower = mul_bounds::<false>(dt, &lhs.upper, &rhs.lower);
1530 let upper = mul_bounds::<true>(dt, &lhs.lower, &rhs.lower);
1531 Interval::new(lower, upper)
1532 } else {
1533 let lower = mul_bounds::<false>(dt, &lhs.lower, &rhs.upper);
1536 let upper = mul_bounds::<true>(dt, &lhs.upper, &rhs.upper);
1537 Interval::new(lower, upper)
1538 }
1539}
1540
1541fn mul_helper_zero_exclusive(
1572 dt: &DataType,
1573 lhs: &Interval,
1574 rhs: &Interval,
1575 zero: &ScalarValue,
1576) -> Interval {
1577 let (lower, upper) = match (
1578 lhs.upper <= *zero && !lhs.upper.is_null(),
1579 rhs.upper <= *zero && !rhs.upper.is_null(),
1580 ) {
1581 (true, true) => (
1583 mul_bounds::<false>(dt, &lhs.upper, &rhs.upper),
1586 mul_bounds::<true>(dt, &lhs.lower, &rhs.lower),
1587 ),
1588 (true, false) => (
1589 mul_bounds::<false>(dt, &lhs.lower, &rhs.upper),
1592 mul_bounds::<true>(dt, &lhs.upper, &rhs.lower),
1593 ),
1594 (false, true) => (
1595 mul_bounds::<false>(dt, &rhs.lower, &lhs.upper),
1598 mul_bounds::<true>(dt, &rhs.upper, &lhs.lower),
1599 ),
1600 (false, false) => (
1601 mul_bounds::<false>(dt, &lhs.lower, &rhs.lower),
1604 mul_bounds::<true>(dt, &lhs.upper, &rhs.upper),
1605 ),
1606 };
1607 Interval::new(lower, upper)
1608}
1609
1610fn div_helper_lhs_zero_inclusive(
1632 dt: &DataType,
1633 lhs: &Interval,
1634 rhs: &Interval,
1635 zero: &ScalarValue,
1636) -> Interval {
1637 if rhs.upper <= *zero && !rhs.upper.is_null() {
1639 let lower = div_bounds::<false>(dt, &lhs.upper, &rhs.upper);
1642 let upper = div_bounds::<true>(dt, &lhs.lower, &rhs.upper);
1643 Interval::new(lower, upper)
1644 } else {
1645 let lower = div_bounds::<false>(dt, &lhs.lower, &rhs.lower);
1648 let upper = div_bounds::<true>(dt, &lhs.upper, &rhs.lower);
1649 Interval::new(lower, upper)
1650 }
1651}
1652
1653fn div_helper_zero_exclusive(
1685 dt: &DataType,
1686 lhs: &Interval,
1687 rhs: &Interval,
1688 zero: &ScalarValue,
1689) -> Interval {
1690 let (lower, upper) = match (
1691 lhs.upper <= *zero && !lhs.upper.is_null(),
1692 rhs.upper <= *zero && !rhs.upper.is_null(),
1693 ) {
1694 (true, true) => (
1696 div_bounds::<false>(dt, &lhs.upper, &rhs.lower),
1699 div_bounds::<true>(dt, &lhs.lower, &rhs.upper),
1700 ),
1701 (true, false) => (
1702 div_bounds::<false>(dt, &lhs.lower, &rhs.lower),
1705 div_bounds::<true>(dt, &lhs.upper, &rhs.upper),
1706 ),
1707 (false, true) => (
1708 div_bounds::<false>(dt, &lhs.upper, &rhs.upper),
1711 div_bounds::<true>(dt, &lhs.lower, &rhs.lower),
1712 ),
1713 (false, false) => (
1714 div_bounds::<false>(dt, &lhs.lower, &rhs.upper),
1717 div_bounds::<true>(dt, &lhs.upper, &rhs.lower),
1718 ),
1719 };
1720 Interval::new(lower, upper)
1721}
1722
1723pub fn cardinality_ratio(initial_interval: &Interval, final_interval: &Interval) -> f64 {
1728 match (final_interval.cardinality(), initial_interval.cardinality()) {
1729 (Some(final_interval), Some(initial_interval)) => {
1730 (final_interval as f64) / (initial_interval as f64)
1731 }
1732 _ => 1.0,
1733 }
1734}
1735
1736fn cast_scalar_value(
1738 value: &ScalarValue,
1739 data_type: &DataType,
1740 cast_options: &CastOptions,
1741) -> Result<ScalarValue> {
1742 let cast_array = cast_with_options(&value.to_array()?, data_type, cast_options)?;
1743 ScalarValue::try_from_array(&cast_array, 0)
1744}
1745
1746#[derive(Debug, Clone, PartialEq, Eq)]
1782pub enum NullableInterval {
1783 Null { datatype: DataType },
1786 MaybeNull { values: Interval },
1789 NotNull { values: Interval },
1791}
1792
1793impl Display for NullableInterval {
1794 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1795 match self {
1796 Self::Null { .. } => write!(f, "NullableInterval: {{NULL}}"),
1797 Self::MaybeNull { values } => {
1798 write!(f, "NullableInterval: {values} U {{NULL}}")
1799 }
1800 Self::NotNull { values } => write!(f, "NullableInterval: {values}"),
1801 }
1802 }
1803}
1804
1805impl From<ScalarValue> for NullableInterval {
1806 fn from(value: ScalarValue) -> Self {
1808 if value.is_null() {
1809 Self::Null {
1810 datatype: value.data_type(),
1811 }
1812 } else {
1813 Self::NotNull {
1814 values: Interval {
1815 lower: value.clone(),
1816 upper: value,
1817 },
1818 }
1819 }
1820 }
1821}
1822
1823impl NullableInterval {
1824 pub const FALSE: Self = NullableInterval::NotNull {
1827 values: Interval::FALSE,
1828 };
1829
1830 pub const TRUE: Self = NullableInterval::NotNull {
1833 values: Interval::TRUE,
1834 };
1835
1836 pub const UNKNOWN: Self = NullableInterval::Null {
1838 datatype: DataType::Boolean,
1839 };
1840
1841 pub const TRUE_OR_FALSE: Self = NullableInterval::NotNull {
1844 values: Interval::TRUE_OR_FALSE,
1845 };
1846
1847 pub const TRUE_OR_UNKNOWN: Self = NullableInterval::MaybeNull {
1849 values: Interval::TRUE,
1850 };
1851
1852 pub const FALSE_OR_UNKNOWN: Self = NullableInterval::MaybeNull {
1854 values: Interval::FALSE,
1855 };
1856
1857 pub const ANY_TRUTH_VALUE: Self = NullableInterval::MaybeNull {
1859 values: Interval::TRUE_OR_FALSE,
1860 };
1861
1862 pub fn values(&self) -> Option<&Interval> {
1864 match self {
1865 Self::Null { .. } => None,
1866 Self::MaybeNull { values } | Self::NotNull { values } => Some(values),
1867 }
1868 }
1869
1870 pub fn data_type(&self) -> DataType {
1872 match self {
1873 Self::Null { datatype } => datatype.clone(),
1874 Self::MaybeNull { values } | Self::NotNull { values } => values.data_type(),
1875 }
1876 }
1877
1878 pub fn is_certainly_true(&self) -> bool {
1880 self == &Self::TRUE
1881 }
1882
1883 pub fn is_true(&self) -> Result<Self> {
1887 let (t, f, u) = self.is_true_false_unknown()?;
1888
1889 match (t, f, u) {
1890 (true, false, false) => Ok(Self::TRUE),
1891 (true, _, _) => Ok(Self::TRUE_OR_FALSE),
1892 (false, _, _) => Ok(Self::FALSE),
1893 }
1894 }
1895
1896 pub fn is_certainly_false(&self) -> bool {
1898 self == &Self::FALSE
1899 }
1900
1901 pub fn is_false(&self) -> Result<Self> {
1905 let (t, f, u) = self.is_true_false_unknown()?;
1906
1907 match (t, f, u) {
1908 (false, true, false) => Ok(Self::TRUE),
1909 (_, true, _) => Ok(Self::TRUE_OR_FALSE),
1910 (_, false, _) => Ok(Self::FALSE),
1911 }
1912 }
1913
1914 pub fn is_certainly_unknown(&self) -> bool {
1916 self == &Self::UNKNOWN
1917 }
1918
1919 pub fn is_unknown(&self) -> Result<Self> {
1923 let (t, f, u) = self.is_true_false_unknown()?;
1924
1925 match (t, f, u) {
1926 (false, false, true) => Ok(Self::TRUE),
1927 (_, _, true) => Ok(Self::TRUE_OR_FALSE),
1928 (_, _, false) => Ok(Self::FALSE),
1929 }
1930 }
1931
1932 fn is_true_false_unknown(&self) -> Result<(bool, bool, bool), DataFusionError> {
1935 Ok(match self {
1936 NullableInterval::Null { .. } => (false, false, true),
1937 NullableInterval::MaybeNull { values } => (
1938 values.contains_value(ScalarValue::Boolean(Some(true)))?,
1939 values.contains_value(ScalarValue::Boolean(Some(false)))?,
1940 true,
1941 ),
1942 NullableInterval::NotNull { values } => (
1943 values.contains_value(ScalarValue::Boolean(Some(true)))?,
1944 values.contains_value(ScalarValue::Boolean(Some(false)))?,
1945 false,
1946 ),
1947 })
1948 }
1949
1950 pub fn not(&self) -> Result<Self> {
1963 match self {
1964 Self::Null { datatype } => {
1965 assert_eq_or_internal_err!(
1966 datatype,
1967 &DataType::Boolean,
1968 "Cannot apply logical negation to a non-boolean interval"
1969 );
1970 Ok(Self::UNKNOWN)
1971 }
1972 Self::MaybeNull { values } => Ok(Self::MaybeNull {
1973 values: values.not()?,
1974 }),
1975 Self::NotNull { values } => Ok(Self::NotNull {
1976 values: values.not()?,
1977 }),
1978 }
1979 }
1980
1981 pub fn and<T: Borrow<Self>>(&self, rhs: T) -> Result<Self> {
1996 if self == &Self::FALSE || rhs.borrow() == &Self::FALSE {
1997 return Ok(Self::FALSE);
1998 }
1999
2000 match (self.values(), rhs.borrow().values()) {
2001 (Some(l), Some(r)) => {
2002 let values = l.and(r)?;
2003 match (self, rhs.borrow()) {
2004 (Self::NotNull { .. }, Self::NotNull { .. }) => {
2005 Ok(Self::NotNull { values })
2006 }
2007 _ => Ok(Self::MaybeNull { values }),
2008 }
2009 }
2010 (Some(v), None) | (None, Some(v)) => {
2011 if v.contains_value(ScalarValue::Boolean(Some(false)))? {
2012 Ok(Self::FALSE_OR_UNKNOWN)
2013 } else {
2014 Ok(Self::UNKNOWN)
2015 }
2016 }
2017 _ => Ok(Self::UNKNOWN),
2018 }
2019 }
2020
2021 pub fn or<T: Borrow<Self>>(&self, rhs: T) -> Result<Self> {
2036 if self == &Self::TRUE || rhs.borrow() == &Self::TRUE {
2037 return Ok(Self::TRUE);
2038 }
2039
2040 match (self.values(), rhs.borrow().values()) {
2041 (Some(l), Some(r)) => {
2042 let values = l.or(r)?;
2043 match (self, rhs.borrow()) {
2044 (Self::NotNull { .. }, Self::NotNull { .. }) => {
2045 Ok(Self::NotNull { values })
2046 }
2047 _ => Ok(Self::MaybeNull { values }),
2048 }
2049 }
2050 (Some(v), None) | (None, Some(v)) => {
2051 if v.contains_value(ScalarValue::Boolean(Some(true)))? {
2052 Ok(Self::TRUE_OR_UNKNOWN)
2053 } else {
2054 Ok(Self::UNKNOWN)
2055 }
2056 }
2057 _ => Ok(Self::UNKNOWN),
2058 }
2059 }
2060
2061 pub fn apply_operator(&self, op: &Operator, rhs: &Self) -> Result<Self> {
2118 match op {
2119 Operator::IsDistinctFrom => {
2120 let values = match (self, rhs) {
2121 (Self::Null { .. }, Self::Null { .. }) => Interval::FALSE,
2123 (Self::NotNull { .. }, _) | (_, Self::NotNull { .. }) => {
2126 let lhs_values = self.values();
2127 let rhs_values = rhs.values();
2128 match (lhs_values, rhs_values) {
2129 (Some(lhs_values), Some(rhs_values)) => {
2130 lhs_values.equal(rhs_values)?.not()?
2131 }
2132 (Some(_), None) | (None, Some(_)) => Interval::TRUE,
2133 (None, None) => unreachable!("Null case handled above"),
2134 }
2135 }
2136 _ => Interval::TRUE_OR_FALSE,
2137 };
2138 Ok(Self::NotNull { values })
2140 }
2141 Operator::IsNotDistinctFrom => self
2142 .apply_operator(&Operator::IsDistinctFrom, rhs)
2143 .map(|i| i.not())?,
2144 Operator::And => self.and(rhs),
2145 Operator::Or => self.or(rhs),
2146 _ => {
2147 if let (Some(left_values), Some(right_values)) =
2148 (self.values(), rhs.values())
2149 {
2150 let values = apply_operator(op, left_values, right_values)?;
2151 match (self, rhs) {
2152 (Self::NotNull { .. }, Self::NotNull { .. }) => {
2153 Ok(Self::NotNull { values })
2154 }
2155 _ => Ok(Self::MaybeNull { values }),
2156 }
2157 } else if op.supports_propagation() {
2158 Ok(Self::Null {
2159 datatype: DataType::Boolean,
2160 })
2161 } else {
2162 Ok(Self::Null {
2163 datatype: self.data_type(),
2164 })
2165 }
2166 }
2167 }
2168 }
2169
2170 pub fn contains<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
2178 let rhs = other.borrow();
2179 if let (Some(left_values), Some(right_values)) = (self.values(), rhs.values()) {
2180 left_values
2181 .contains(right_values)
2182 .map(|values| match (self, rhs) {
2183 (Self::NotNull { .. }, Self::NotNull { .. }) => {
2184 Self::NotNull { values }
2185 }
2186 _ => Self::MaybeNull { values },
2187 })
2188 } else {
2189 Ok(Self::Null {
2190 datatype: DataType::Boolean,
2191 })
2192 }
2193 }
2194
2195 pub fn contains_value<T: Borrow<ScalarValue>>(&self, value: T) -> Result<bool> {
2197 match value.borrow() {
2198 ScalarValue::Null => match self {
2199 NullableInterval::Null { .. } | NullableInterval::MaybeNull { .. } => {
2200 Ok(true)
2201 }
2202 NullableInterval::NotNull { .. } => Ok(false),
2203 },
2204 s if s.is_null() => match self {
2205 NullableInterval::Null { datatype } => Ok(datatype.eq(&s.data_type())),
2206 NullableInterval::MaybeNull { values } => {
2207 Ok(values.data_type().eq(&s.data_type()))
2208 }
2209 NullableInterval::NotNull { .. } => Ok(false),
2210 },
2211 s => match self {
2212 NullableInterval::Null { .. } => Ok(false),
2213 NullableInterval::MaybeNull { values }
2214 | NullableInterval::NotNull { values } => values.contains_value(s),
2215 },
2216 }
2217 }
2218
2219 pub fn single_value(&self) -> Option<ScalarValue> {
2245 match self {
2246 Self::Null { datatype } => {
2247 Some(ScalarValue::try_from(datatype).unwrap_or(ScalarValue::Null))
2248 }
2249 Self::MaybeNull { values } | Self::NotNull { values }
2250 if values.lower == values.upper && !values.lower.is_null() =>
2251 {
2252 Some(values.lower.clone())
2253 }
2254 _ => None,
2255 }
2256 }
2257}
2258
2259#[cfg(test)]
2260mod tests {
2261 use crate::{
2262 interval_arithmetic::{
2263 Interval, cast_scalar_value, handle_overflow, next_value, prev_value,
2264 satisfy_greater,
2265 },
2266 operator::Operator,
2267 };
2268
2269 use crate::interval_arithmetic::NullableInterval;
2270 use arrow::compute::CastOptions;
2271 use arrow::datatypes::DataType;
2272 use datafusion_common::rounding::{next_down, next_up};
2273 use datafusion_common::{Result, ScalarValue};
2274
2275 #[test]
2276 fn test_numeric_cast_out_of_range_bounds() {
2277 for safe in [false, true] {
2278 let options = CastOptions {
2279 safe,
2280 ..Default::default()
2281 };
2282 for source in [
2283 DataType::Int64,
2284 DataType::Float64,
2285 DataType::Decimal128(10, 0),
2286 ] {
2287 for (lower, upper, expected_lower, expected_upper) in [
2288 (Some(-129i64), Some(42), None, Some(42i8)),
2289 (Some(-42), Some(128), Some(-42), None),
2290 (Some(-129), Some(128), None, None),
2291 (Some(-128), Some(127), Some(-128i8), Some(127i8)),
2292 (Some(128), Some(129), None, None),
2293 (Some(-130), Some(-129), None, None),
2294 (None, Some(42), None, Some(42)),
2295 (Some(-42), None, Some(-42), None),
2296 ] {
2297 let input = Interval::make(lower, upper)
2298 .unwrap()
2299 .cast_to(&source, &options)
2300 .unwrap();
2301 assert_eq!(
2302 input.cast_to(&DataType::Int8, &options).unwrap(),
2303 Interval::make(expected_lower, expected_upper).unwrap(),
2304 "{source}: {input}, safe={safe}"
2305 );
2306 }
2307 }
2308 }
2309 }
2310
2311 #[test]
2312 fn test_numeric_cast_bounds_across_types() {
2313 for safe in [false, true] {
2314 let options = CastOptions {
2315 safe,
2316 ..Default::default()
2317 };
2318 for source in [
2320 DataType::Int64,
2321 DataType::UInt64,
2322 DataType::Float64,
2323 DataType::Decimal128(10, 2),
2324 DataType::Decimal256(30, 3),
2325 ] {
2326 let input = Interval::make(Some(42i64), Some(1000))
2327 .unwrap()
2328 .cast_to(&source, &options)
2329 .unwrap();
2330 for target in
2331 [DataType::Int8, DataType::UInt8, DataType::Decimal128(3, 1)]
2332 {
2333 let lower = ScalarValue::Int64(Some(42)).cast_to(&target).unwrap();
2334 assert_eq!(
2335 input.cast_to(&target, &options).unwrap(),
2336 Interval::try_new(lower, ScalarValue::try_from(&target).unwrap())
2337 .unwrap(),
2338 "{source} -> {target}, safe={safe}"
2339 );
2340 }
2341 }
2342 assert_eq!(
2343 Interval::make(Some(-1i64), Some(42))
2344 .unwrap()
2345 .cast_to(&DataType::UInt8, &options)
2346 .unwrap(),
2347 Interval::make(Some(0u8), Some(42)).unwrap()
2348 );
2349
2350 for lower in [-129.5f64, -42.5] {
2352 for source in [DataType::Float64, DataType::Decimal128(10, 2)] {
2353 let input = Interval::make(Some(lower), Some(42.5))
2354 .unwrap()
2355 .cast_to(&source, &options)
2356 .unwrap();
2357 let expected_lower = if lower < -128.0 { None } else { Some(-42i8) };
2358 assert_eq!(
2359 input.cast_to(&DataType::Int8, &options).unwrap(),
2360 Interval::make(expected_lower, Some(42)).unwrap()
2361 );
2362 }
2363 }
2364 assert_eq!(
2366 Interval::make(Some(0.0f64), Some(f64::MAX))
2367 .unwrap()
2368 .cast_to(&DataType::Float32, &options)
2369 .unwrap(),
2370 Interval::make(Some(0.0f32), None).unwrap()
2371 );
2372 }
2373 }
2374
2375 #[test]
2376 fn test_numeric_cast_bounds_contain_values() {
2377 let types = [
2378 DataType::Int8,
2379 DataType::Int16,
2380 DataType::Int32,
2381 DataType::Int64,
2382 DataType::UInt8,
2383 DataType::UInt16,
2384 DataType::UInt32,
2385 DataType::UInt64,
2386 DataType::Float32,
2387 DataType::Float64,
2388 DataType::Decimal128(3, 0),
2389 DataType::Decimal128(10, 2),
2390 DataType::Decimal128(20, -2),
2391 DataType::Decimal256(30, 3),
2392 ];
2393 let samples = [
2394 i64::MIN,
2395 -16777217,
2396 -65537,
2397 -129,
2398 -128,
2399 -1,
2400 0,
2401 1,
2402 42,
2403 127,
2404 128,
2405 255,
2406 256,
2407 65536,
2408 16777217,
2409 i64::MAX,
2410 ];
2411 let safe_options = CastOptions {
2412 safe: true,
2413 ..Default::default()
2414 };
2415 let cast = |value: &ScalarValue, target: &DataType| {
2416 cast_scalar_value(value, target, &safe_options)
2417 };
2418 for source in &types {
2419 let mut values = samples
2420 .iter()
2421 .map(|value| cast(&ScalarValue::from(*value), source))
2422 .collect::<Result<Vec<_>>>()
2423 .unwrap();
2424 values.retain(|value| !value.is_null());
2425 for target in &types {
2426 let converted = values
2427 .iter()
2428 .map(|value| cast(value, target))
2429 .collect::<Result<Vec<_>>>()
2430 .unwrap();
2431 for safe in [false, true] {
2432 let options = CastOptions {
2433 safe,
2434 ..Default::default()
2435 };
2436 for start in 0..values.len() {
2437 for end in start..values.len() {
2438 let input = Interval::try_new(
2439 values[start].clone(),
2440 values[end].clone(),
2441 )
2442 .unwrap();
2443 let bounds = input.cast_to(target, &options).unwrap();
2444 for value in &converted[start..=end] {
2445 if !value.is_null() {
2446 assert!(
2447 (bounds.lower.is_null()
2448 || bounds.lower <= *value)
2449 && (bounds.upper.is_null()
2450 || *value <= bounds.upper),
2451 "{source} -> {target}, {input} -> {bounds}, value={value}, safe={safe}"
2452 );
2453 }
2454 }
2455 }
2456 }
2457 }
2458 }
2459 }
2460 }
2461
2462 #[test]
2463 fn test_non_numeric_cast_retains_error_policy() {
2464 let strict = CastOptions {
2465 safe: false,
2466 ..Default::default()
2467 };
2468 let safe = CastOptions {
2469 safe: true,
2470 ..strict.clone()
2471 };
2472 for (lower, upper, expected_lower, expected_upper) in [
2474 ("not a number", "not a number", None, None),
2475 ("1", "not a number", Some(1i8), None),
2476 ("!", "42", None, Some(42i8)),
2477 ] {
2478 let input = Interval::try_new(
2479 ScalarValue::Utf8(Some(lower.into())),
2480 ScalarValue::Utf8(Some(upper.into())),
2481 )
2482 .unwrap();
2483 assert!(input.cast_to(&DataType::Int8, &strict).is_err());
2484 assert_eq!(
2485 input.cast_to(&DataType::Int8, &safe).unwrap(),
2486 Interval::make(expected_lower, expected_upper).unwrap()
2487 );
2488 }
2489 let numeric = Interval::make(Some(0i64), Some(1)).unwrap();
2490 assert!(
2491 numeric
2492 .cast_to(&DataType::Struct(Default::default()), &safe)
2493 .is_err()
2494 );
2495 }
2496
2497 #[test]
2498 fn test_next_prev_value() -> Result<()> {
2499 let zeros = vec![
2500 ScalarValue::new_zero(&DataType::UInt8)?,
2501 ScalarValue::new_zero(&DataType::UInt16)?,
2502 ScalarValue::new_zero(&DataType::UInt32)?,
2503 ScalarValue::new_zero(&DataType::UInt64)?,
2504 ScalarValue::new_zero(&DataType::Int8)?,
2505 ScalarValue::new_zero(&DataType::Int16)?,
2506 ScalarValue::new_zero(&DataType::Int32)?,
2507 ScalarValue::new_zero(&DataType::Int64)?,
2508 ];
2509 let ones = vec![
2510 ScalarValue::new_one(&DataType::UInt8)?,
2511 ScalarValue::new_one(&DataType::UInt16)?,
2512 ScalarValue::new_one(&DataType::UInt32)?,
2513 ScalarValue::new_one(&DataType::UInt64)?,
2514 ScalarValue::new_one(&DataType::Int8)?,
2515 ScalarValue::new_one(&DataType::Int16)?,
2516 ScalarValue::new_one(&DataType::Int32)?,
2517 ScalarValue::new_one(&DataType::Int64)?,
2518 ];
2519 zeros.into_iter().zip(ones).for_each(|(z, o)| {
2520 assert_eq!(next_value(z.clone()), o);
2521 assert_eq!(prev_value(o), z);
2522 });
2523
2524 let values = vec![
2525 ScalarValue::new_zero(&DataType::Float32)?,
2526 ScalarValue::new_zero(&DataType::Float64)?,
2527 ];
2528 let eps = vec![
2529 ScalarValue::Float32(Some(1e-6)),
2530 ScalarValue::Float64(Some(1e-6)),
2531 ];
2532 values.into_iter().zip(eps).for_each(|(value, eps)| {
2533 assert!(
2534 next_value(value.clone())
2535 .sub(value.clone())
2536 .unwrap()
2537 .lt(&eps)
2538 );
2539 assert!(value.sub(prev_value(value.clone())).unwrap().lt(&eps));
2540 assert_ne!(next_value(value.clone()), value);
2541 assert_ne!(prev_value(value.clone()), value);
2542 });
2543
2544 let min_max = vec![
2545 (
2546 ScalarValue::UInt64(Some(u64::MIN)),
2547 ScalarValue::UInt64(Some(u64::MAX)),
2548 ),
2549 (
2550 ScalarValue::Int8(Some(i8::MIN)),
2551 ScalarValue::Int8(Some(i8::MAX)),
2552 ),
2553 (
2554 ScalarValue::Float32(Some(f32::MIN)),
2555 ScalarValue::Float32(Some(f32::MAX)),
2556 ),
2557 (
2558 ScalarValue::Float64(Some(f64::MIN)),
2559 ScalarValue::Float64(Some(f64::MAX)),
2560 ),
2561 ];
2562 let inf = vec![
2563 ScalarValue::UInt64(None),
2564 ScalarValue::Int8(None),
2565 ScalarValue::Float32(None),
2566 ScalarValue::Float64(None),
2567 ];
2568 min_max.into_iter().zip(inf).for_each(|((min, max), inf)| {
2569 assert_eq!(next_value(max.clone()), inf);
2570 assert_ne!(prev_value(max.clone()), max);
2571 assert_ne!(prev_value(max), inf);
2572
2573 assert_eq!(prev_value(min.clone()), inf);
2574 assert_ne!(next_value(min.clone()), min);
2575 assert_ne!(next_value(min), inf);
2576
2577 assert_eq!(next_value(inf.clone()), inf);
2578 assert_eq!(prev_value(inf.clone()), inf);
2579 });
2580
2581 Ok(())
2582 }
2583
2584 #[test]
2585 fn test_new_interval() -> Result<()> {
2586 use ScalarValue::*;
2587
2588 let cases = vec![
2589 (
2590 (Boolean(None), Boolean(Some(false))),
2591 Boolean(Some(false)),
2592 Boolean(Some(false)),
2593 ),
2594 (
2595 (Boolean(Some(false)), Boolean(None)),
2596 Boolean(Some(false)),
2597 Boolean(Some(true)),
2598 ),
2599 (
2600 (Boolean(Some(false)), Boolean(Some(true))),
2601 Boolean(Some(false)),
2602 Boolean(Some(true)),
2603 ),
2604 (
2605 (UInt16(Some(u16::MAX)), UInt16(None)),
2606 UInt16(Some(u16::MAX)),
2607 UInt16(None),
2608 ),
2609 (
2610 (Int16(None), Int16(Some(-1000))),
2611 Int16(None),
2612 Int16(Some(-1000)),
2613 ),
2614 (
2615 (Float32(Some(f32::MAX)), Float32(Some(f32::MAX))),
2616 Float32(Some(f32::MAX)),
2617 Float32(Some(f32::MAX)),
2618 ),
2619 (
2620 (Float32(Some(f32::NAN)), Float32(Some(f32::MIN))),
2621 Float32(None),
2622 Float32(Some(f32::MIN)),
2623 ),
2624 (
2625 (
2626 Float64(Some(f64::NEG_INFINITY)),
2627 Float64(Some(f64::INFINITY)),
2628 ),
2629 Float64(None),
2630 Float64(None),
2631 ),
2632 ];
2633 for (inputs, lower, upper) in cases {
2634 let result = Interval::try_new(inputs.0, inputs.1)?;
2635 assert_eq!(result.clone().lower(), &lower);
2636 assert_eq!(result.upper(), &upper);
2637 }
2638
2639 let invalid_intervals = vec![
2640 (Float32(Some(f32::INFINITY)), Float32(Some(100_f32))),
2641 (Float64(Some(0_f64)), Float64(Some(f64::NEG_INFINITY))),
2642 (Boolean(Some(true)), Boolean(Some(false))),
2643 (Int32(Some(1000)), Int32(Some(-2000))),
2644 (UInt64(Some(1)), UInt64(Some(0))),
2645 ];
2646 for (lower, upper) in invalid_intervals {
2647 Interval::try_new(lower, upper).expect_err(
2648 "Given parameters should have given an invalid interval error",
2649 );
2650 }
2651
2652 Ok(())
2653 }
2654
2655 #[test]
2656 fn test_make_unbounded() -> Result<()> {
2657 use ScalarValue::*;
2658
2659 let unbounded_cases = vec![
2660 (DataType::Boolean, Boolean(Some(false)), Boolean(Some(true))),
2661 (DataType::UInt8, UInt8(Some(0)), UInt8(None)),
2662 (DataType::UInt16, UInt16(Some(0)), UInt16(None)),
2663 (DataType::UInt32, UInt32(Some(0)), UInt32(None)),
2664 (DataType::UInt64, UInt64(Some(0)), UInt64(None)),
2665 (DataType::Int8, Int8(None), Int8(None)),
2666 (DataType::Int16, Int16(None), Int16(None)),
2667 (DataType::Int32, Int32(None), Int32(None)),
2668 (DataType::Int64, Int64(None), Int64(None)),
2669 (DataType::Float32, Float32(None), Float32(None)),
2670 (DataType::Float64, Float64(None), Float64(None)),
2671 ];
2672 for (dt, lower, upper) in unbounded_cases {
2673 let inf = Interval::make_unbounded(&dt)?;
2674 assert_eq!(inf.clone().lower(), &lower);
2675 assert_eq!(inf.upper(), &upper);
2676 }
2677
2678 Ok(())
2679 }
2680
2681 #[test]
2682 fn gt_lt_test() -> Result<()> {
2683 let exactly_gt_cases = vec![
2684 (
2685 Interval::make(Some(1000_i64), None)?,
2686 Interval::make(None, Some(999_i64))?,
2687 ),
2688 (
2689 Interval::make(Some(1000_i64), Some(1000_i64))?,
2690 Interval::make(None, Some(999_i64))?,
2691 ),
2692 (
2693 Interval::make(Some(501_i64), Some(1000_i64))?,
2694 Interval::make(Some(500_i64), Some(500_i64))?,
2695 ),
2696 (
2697 Interval::make(Some(-1000_i64), Some(1000_i64))?,
2698 Interval::make(None, Some(-1500_i64))?,
2699 ),
2700 (
2701 Interval::try_new(
2702 next_value(ScalarValue::Float32(Some(0.0))),
2703 next_value(ScalarValue::Float32(Some(0.0))),
2704 )?,
2705 Interval::make(Some(0.0_f32), Some(0.0_f32))?,
2706 ),
2707 (
2708 Interval::make(Some(-1.0_f32), Some(-1.0_f32))?,
2709 Interval::try_new(
2710 prev_value(ScalarValue::Float32(Some(-1.0))),
2711 prev_value(ScalarValue::Float32(Some(-1.0))),
2712 )?,
2713 ),
2714 ];
2715 for (first, second) in exactly_gt_cases {
2716 assert_eq!(first.gt(second.clone())?, Interval::TRUE);
2717 assert_eq!(second.lt(first)?, Interval::TRUE);
2718 }
2719
2720 let possibly_gt_cases = vec![
2721 (
2722 Interval::make(Some(1000_i64), Some(2000_i64))?,
2723 Interval::make(Some(1000_i64), Some(1000_i64))?,
2724 ),
2725 (
2726 Interval::make(Some(500_i64), Some(1000_i64))?,
2727 Interval::make(Some(500_i64), Some(1000_i64))?,
2728 ),
2729 (
2730 Interval::make(Some(1000_i64), None)?,
2731 Interval::make(Some(1000_i64), None)?,
2732 ),
2733 (
2734 Interval::make::<i64>(None, None)?,
2735 Interval::make::<i64>(None, None)?,
2736 ),
2737 (
2738 Interval::try_new(
2739 ScalarValue::Float32(Some(0.0_f32)),
2740 next_value(ScalarValue::Float32(Some(0.0_f32))),
2741 )?,
2742 Interval::make(Some(0.0_f32), Some(0.0_f32))?,
2743 ),
2744 (
2745 Interval::make(Some(-1.0_f32), Some(-1.0_f32))?,
2746 Interval::try_new(
2747 prev_value(ScalarValue::Float32(Some(-1.0_f32))),
2748 ScalarValue::Float32(Some(-1.0_f32)),
2749 )?,
2750 ),
2751 ];
2752 for (first, second) in possibly_gt_cases {
2753 assert_eq!(first.gt(second.clone())?, Interval::TRUE_OR_FALSE);
2754 assert_eq!(second.lt(first)?, Interval::TRUE_OR_FALSE);
2755 }
2756
2757 let not_gt_cases = vec![
2758 (
2759 Interval::make(Some(1000_i64), Some(1000_i64))?,
2760 Interval::make(Some(1000_i64), Some(1000_i64))?,
2761 ),
2762 (
2763 Interval::make(Some(500_i64), Some(1000_i64))?,
2764 Interval::make(Some(1000_i64), None)?,
2765 ),
2766 (
2767 Interval::make(None, Some(1000_i64))?,
2768 Interval::make(Some(1000_i64), Some(1500_i64))?,
2769 ),
2770 (
2771 Interval::make(Some(0_u8), Some(0_u8))?,
2772 Interval::make::<u8>(None, None)?,
2773 ),
2774 (
2775 Interval::try_new(
2776 prev_value(ScalarValue::Float32(Some(0.0_f32))),
2777 ScalarValue::Float32(Some(0.0_f32)),
2778 )?,
2779 Interval::make(Some(0.0_f32), Some(0.0_f32))?,
2780 ),
2781 (
2782 Interval::make(Some(-1.0_f32), Some(-1.0_f32))?,
2783 Interval::try_new(
2784 ScalarValue::Float32(Some(-1.0_f32)),
2785 next_value(ScalarValue::Float32(Some(-1.0_f32))),
2786 )?,
2787 ),
2788 ];
2789 for (first, second) in not_gt_cases {
2790 assert_eq!(first.gt(second.clone())?, Interval::FALSE);
2791 assert_eq!(second.lt(first)?, Interval::FALSE);
2792 }
2793
2794 Ok(())
2795 }
2796
2797 #[test]
2798 fn gteq_lteq_test() -> Result<()> {
2799 let exactly_gteq_cases = vec![
2800 (
2801 Interval::make(Some(1000_i64), None)?,
2802 Interval::make(None, Some(1000_i64))?,
2803 ),
2804 (
2805 Interval::make(Some(1000_i64), Some(1000_i64))?,
2806 Interval::make(None, Some(1000_i64))?,
2807 ),
2808 (
2809 Interval::make(Some(500_i64), Some(1000_i64))?,
2810 Interval::make(Some(500_i64), Some(500_i64))?,
2811 ),
2812 (
2813 Interval::make(Some(-1000_i64), Some(1000_i64))?,
2814 Interval::make(None, Some(-1500_i64))?,
2815 ),
2816 (
2817 Interval::make::<u64>(None, None)?,
2818 Interval::make(Some(0_u64), Some(0_u64))?,
2819 ),
2820 (
2821 Interval::make(Some(0.0_f32), Some(0.0_f32))?,
2822 Interval::make(Some(0.0_f32), Some(0.0_f32))?,
2823 ),
2824 (
2825 Interval::try_new(
2826 ScalarValue::Float32(Some(-1.0)),
2827 next_value(ScalarValue::Float32(Some(-1.0))),
2828 )?,
2829 Interval::try_new(
2830 prev_value(ScalarValue::Float32(Some(-1.0))),
2831 ScalarValue::Float32(Some(-1.0)),
2832 )?,
2833 ),
2834 ];
2835 for (first, second) in exactly_gteq_cases {
2836 assert_eq!(first.gt_eq(second.clone())?, Interval::TRUE);
2837 assert_eq!(second.lt_eq(first)?, Interval::TRUE);
2838 }
2839
2840 let possibly_gteq_cases = vec![
2841 (
2842 Interval::make(Some(999_i64), Some(2000_i64))?,
2843 Interval::make(Some(1000_i64), Some(1000_i64))?,
2844 ),
2845 (
2846 Interval::make(Some(500_i64), Some(1000_i64))?,
2847 Interval::make(Some(500_i64), Some(1001_i64))?,
2848 ),
2849 (
2850 Interval::make(Some(0_i64), None)?,
2851 Interval::make(Some(1000_i64), None)?,
2852 ),
2853 (
2854 Interval::make::<i64>(None, None)?,
2855 Interval::make::<i64>(None, None)?,
2856 ),
2857 (
2858 Interval::try_new(
2859 prev_value(ScalarValue::Float32(Some(0.0))),
2860 ScalarValue::Float32(Some(0.0)),
2861 )?,
2862 Interval::make(Some(0.0_f32), Some(0.0_f32))?,
2863 ),
2864 (
2865 Interval::make(Some(-1.0_f32), Some(-1.0_f32))?,
2866 Interval::try_new(
2867 prev_value(ScalarValue::Float32(Some(-1.0_f32))),
2868 next_value(ScalarValue::Float32(Some(-1.0_f32))),
2869 )?,
2870 ),
2871 ];
2872 for (first, second) in possibly_gteq_cases {
2873 assert_eq!(first.gt_eq(second.clone())?, Interval::TRUE_OR_FALSE);
2874 assert_eq!(second.lt_eq(first)?, Interval::TRUE_OR_FALSE);
2875 }
2876
2877 let not_gteq_cases = vec![
2878 (
2879 Interval::make(Some(1000_i64), Some(1000_i64))?,
2880 Interval::make(Some(2000_i64), Some(2000_i64))?,
2881 ),
2882 (
2883 Interval::make(Some(500_i64), Some(999_i64))?,
2884 Interval::make(Some(1000_i64), None)?,
2885 ),
2886 (
2887 Interval::make(None, Some(1000_i64))?,
2888 Interval::make(Some(1001_i64), Some(1500_i64))?,
2889 ),
2890 (
2891 Interval::try_new(
2892 prev_value(ScalarValue::Float32(Some(0.0_f32))),
2893 prev_value(ScalarValue::Float32(Some(0.0_f32))),
2894 )?,
2895 Interval::make(Some(0.0_f32), Some(0.0_f32))?,
2896 ),
2897 (
2898 Interval::make(Some(-1.0_f32), Some(-1.0_f32))?,
2899 Interval::try_new(
2900 next_value(ScalarValue::Float32(Some(-1.0))),
2901 next_value(ScalarValue::Float32(Some(-1.0))),
2902 )?,
2903 ),
2904 ];
2905 for (first, second) in not_gteq_cases {
2906 assert_eq!(first.gt_eq(second.clone())?, Interval::FALSE);
2907 assert_eq!(second.lt_eq(first)?, Interval::FALSE);
2908 }
2909
2910 Ok(())
2911 }
2912
2913 #[test]
2914 fn equal_test() -> Result<()> {
2915 let exactly_eq_cases = vec![
2916 (
2917 Interval::make(Some(1000_i64), Some(1000_i64))?,
2918 Interval::make(Some(1000_i64), Some(1000_i64))?,
2919 ),
2920 (
2921 Interval::make(Some(0_u64), Some(0_u64))?,
2922 Interval::make(Some(0_u64), Some(0_u64))?,
2923 ),
2924 (
2925 Interval::make(Some(f32::MAX), Some(f32::MAX))?,
2926 Interval::make(Some(f32::MAX), Some(f32::MAX))?,
2927 ),
2928 (
2929 Interval::make(Some(f64::MIN), Some(f64::MIN))?,
2930 Interval::make(Some(f64::MIN), Some(f64::MIN))?,
2931 ),
2932 ];
2933 for (first, second) in exactly_eq_cases {
2934 assert_eq!(first.equal(second.clone())?, Interval::TRUE);
2935 assert_eq!(second.equal(first)?, Interval::TRUE);
2936 }
2937
2938 let possibly_eq_cases = vec![
2939 (
2940 Interval::make::<i64>(None, None)?,
2941 Interval::make::<i64>(None, None)?,
2942 ),
2943 (
2944 Interval::make(Some(0_i64), Some(0_i64))?,
2945 Interval::make(Some(0_i64), Some(1000_i64))?,
2946 ),
2947 (
2948 Interval::make(Some(0_i64), Some(0_i64))?,
2949 Interval::make(Some(0_i64), Some(1000_i64))?,
2950 ),
2951 (
2952 Interval::make(Some(100.0_f32), Some(200.0_f32))?,
2953 Interval::make(Some(0.0_f32), Some(1000.0_f32))?,
2954 ),
2955 (
2956 Interval::try_new(
2957 prev_value(ScalarValue::Float32(Some(0.0))),
2958 ScalarValue::Float32(Some(0.0)),
2959 )?,
2960 Interval::make(Some(0.0_f32), Some(0.0_f32))?,
2961 ),
2962 (
2963 Interval::make(Some(-1.0_f32), Some(-1.0_f32))?,
2964 Interval::try_new(
2965 prev_value(ScalarValue::Float32(Some(-1.0))),
2966 next_value(ScalarValue::Float32(Some(-1.0))),
2967 )?,
2968 ),
2969 ];
2970 for (first, second) in possibly_eq_cases {
2971 assert_eq!(first.equal(second.clone())?, Interval::TRUE_OR_FALSE);
2972 assert_eq!(second.equal(first)?, Interval::TRUE_OR_FALSE);
2973 }
2974
2975 let not_eq_cases = vec![
2976 (
2977 Interval::make(Some(1000_i64), Some(1000_i64))?,
2978 Interval::make(Some(2000_i64), Some(2000_i64))?,
2979 ),
2980 (
2981 Interval::make(Some(500_i64), Some(999_i64))?,
2982 Interval::make(Some(1000_i64), None)?,
2983 ),
2984 (
2985 Interval::make(None, Some(1000_i64))?,
2986 Interval::make(Some(1001_i64), Some(1500_i64))?,
2987 ),
2988 (
2989 Interval::try_new(
2990 prev_value(ScalarValue::Float32(Some(0.0))),
2991 prev_value(ScalarValue::Float32(Some(0.0))),
2992 )?,
2993 Interval::make(Some(0.0_f32), Some(0.0_f32))?,
2994 ),
2995 (
2996 Interval::make(Some(-1.0_f32), Some(-1.0_f32))?,
2997 Interval::try_new(
2998 next_value(ScalarValue::Float32(Some(-1.0))),
2999 next_value(ScalarValue::Float32(Some(-1.0))),
3000 )?,
3001 ),
3002 ];
3003 for (first, second) in not_eq_cases {
3004 assert_eq!(first.equal(second.clone())?, Interval::FALSE);
3005 assert_eq!(second.equal(first)?, Interval::FALSE);
3006 }
3007
3008 Ok(())
3009 }
3010
3011 #[test]
3012 fn and_test() -> Result<()> {
3013 let cases = vec![
3014 (Interval::TRUE_OR_FALSE, Interval::FALSE, Interval::FALSE),
3015 (
3016 Interval::TRUE_OR_FALSE,
3017 Interval::TRUE_OR_FALSE,
3018 Interval::TRUE_OR_FALSE,
3019 ),
3020 (
3021 Interval::TRUE_OR_FALSE,
3022 Interval::TRUE,
3023 Interval::TRUE_OR_FALSE,
3024 ),
3025 (Interval::FALSE, Interval::FALSE, Interval::FALSE),
3026 (Interval::FALSE, Interval::TRUE_OR_FALSE, Interval::FALSE),
3027 (Interval::FALSE, Interval::TRUE, Interval::FALSE),
3028 (Interval::TRUE, Interval::FALSE, Interval::FALSE),
3029 (
3030 Interval::TRUE,
3031 Interval::TRUE_OR_FALSE,
3032 Interval::TRUE_OR_FALSE,
3033 ),
3034 (Interval::TRUE, Interval::TRUE, Interval::TRUE),
3035 ];
3036
3037 for case in cases {
3038 assert_eq!(
3039 case.0.and(&case.1)?,
3040 case.2,
3041 "Failed for {} AND {}",
3042 case.0,
3043 case.1
3044 );
3045 }
3046 Ok(())
3047 }
3048
3049 #[test]
3050 fn or_test() -> Result<()> {
3051 let cases = vec![
3052 (
3053 Interval::TRUE_OR_FALSE,
3054 Interval::FALSE,
3055 Interval::TRUE_OR_FALSE,
3056 ),
3057 (
3058 Interval::TRUE_OR_FALSE,
3059 Interval::TRUE_OR_FALSE,
3060 Interval::TRUE_OR_FALSE,
3061 ),
3062 (Interval::TRUE_OR_FALSE, Interval::TRUE, Interval::TRUE),
3063 (Interval::FALSE, Interval::FALSE, Interval::FALSE),
3064 (
3065 Interval::FALSE,
3066 Interval::TRUE_OR_FALSE,
3067 Interval::TRUE_OR_FALSE,
3068 ),
3069 (Interval::FALSE, Interval::TRUE, Interval::TRUE),
3070 (Interval::TRUE, Interval::FALSE, Interval::TRUE),
3071 (Interval::TRUE, Interval::TRUE_OR_FALSE, Interval::TRUE),
3072 (Interval::TRUE, Interval::TRUE, Interval::TRUE),
3073 ];
3074
3075 for case in cases {
3076 assert_eq!(
3077 case.0.or(&case.1)?,
3078 case.2,
3079 "Failed for {} OR {}",
3080 case.0,
3081 case.1
3082 );
3083 }
3084 Ok(())
3085 }
3086
3087 #[test]
3088 fn not_test() -> Result<()> {
3089 let cases = vec![
3090 (Interval::TRUE_OR_FALSE, Interval::TRUE_OR_FALSE),
3091 (Interval::FALSE, Interval::TRUE),
3092 (Interval::TRUE, Interval::FALSE),
3093 ];
3094
3095 for case in cases {
3096 assert_eq!(case.0.not()?, case.1, "Failed for NOT {}", case.0);
3097 }
3098 Ok(())
3099 }
3100
3101 #[test]
3102 fn test_and_or_with_normalized_boolean_intervals() -> Result<()> {
3103 let from_nulls =
3105 Interval::try_new(ScalarValue::Boolean(None), ScalarValue::Boolean(None))?;
3106
3107 assert!(from_nulls.or(&Interval::TRUE).is_ok());
3108 assert!(from_nulls.and(&Interval::FALSE).is_ok());
3109
3110 Ok(())
3111 }
3112
3113 #[test]
3116 fn test_null_boolean_interval() {
3117 let null_interval =
3118 Interval::try_new(ScalarValue::Boolean(None), ScalarValue::Boolean(None))
3119 .unwrap();
3120
3121 assert_eq!(null_interval, Interval::TRUE_OR_FALSE);
3122 }
3123
3124 #[test]
3127 fn test_uncertain_boolean_interval() {
3128 assert!(
3129 Interval::TRUE_OR_FALSE
3130 .contains_value(ScalarValue::Boolean(Some(true)))
3131 .unwrap()
3132 );
3133 assert!(
3134 Interval::TRUE_OR_FALSE
3135 .contains_value(ScalarValue::Boolean(Some(false)))
3136 .unwrap()
3137 );
3138 assert!(
3139 !Interval::TRUE_OR_FALSE
3140 .contains_value(ScalarValue::Boolean(None))
3141 .unwrap()
3142 );
3143 assert!(
3144 !Interval::TRUE_OR_FALSE
3145 .contains_value(ScalarValue::Null)
3146 .unwrap()
3147 );
3148 }
3149
3150 #[test]
3151 fn test_and_uncertain_boolean_intervals() -> Result<()> {
3152 let and_result = Interval::TRUE_OR_FALSE.and(&Interval::FALSE)?;
3153 assert_eq!(and_result, Interval::FALSE);
3154
3155 let and_result = Interval::FALSE.and(&Interval::TRUE_OR_FALSE)?;
3156 assert_eq!(and_result, Interval::FALSE);
3157
3158 let and_result = Interval::TRUE_OR_FALSE.and(&Interval::TRUE)?;
3159 assert_eq!(and_result, Interval::TRUE_OR_FALSE);
3160
3161 let and_result = Interval::TRUE.and(&Interval::TRUE_OR_FALSE)?;
3162 assert_eq!(and_result, Interval::TRUE_OR_FALSE);
3163
3164 let and_result = Interval::TRUE_OR_FALSE.and(&Interval::TRUE_OR_FALSE)?;
3165 assert_eq!(and_result, Interval::TRUE_OR_FALSE);
3166
3167 Ok(())
3168 }
3169
3170 #[test]
3171 fn test_or_uncertain_boolean_intervals() -> Result<()> {
3172 let or_result = Interval::TRUE_OR_FALSE.or(&Interval::FALSE)?;
3173 assert_eq!(or_result, Interval::TRUE_OR_FALSE);
3174
3175 let or_result = Interval::FALSE.or(&Interval::TRUE_OR_FALSE)?;
3176 assert_eq!(or_result, Interval::TRUE_OR_FALSE);
3177
3178 let or_result = Interval::TRUE_OR_FALSE.or(&Interval::TRUE)?;
3179 assert_eq!(or_result, Interval::TRUE);
3180
3181 let or_result = Interval::TRUE.or(&Interval::TRUE_OR_FALSE)?;
3182 assert_eq!(or_result, Interval::TRUE);
3183
3184 let or_result = Interval::TRUE_OR_FALSE.or(&Interval::TRUE_OR_FALSE)?;
3185 assert_eq!(or_result, Interval::TRUE_OR_FALSE);
3186
3187 Ok(())
3188 }
3189
3190 #[test]
3191 fn intersect_test() -> Result<()> {
3192 let possible_cases = vec![
3193 (
3194 Interval::make(Some(1000_i64), None)?,
3195 Interval::make::<i64>(None, None)?,
3196 Interval::make(Some(1000_i64), None)?,
3197 ),
3198 (
3199 Interval::make(Some(1000_i64), None)?,
3200 Interval::make(None, Some(1000_i64))?,
3201 Interval::make(Some(1000_i64), Some(1000_i64))?,
3202 ),
3203 (
3204 Interval::make(Some(1000_i64), None)?,
3205 Interval::make(None, Some(2000_i64))?,
3206 Interval::make(Some(1000_i64), Some(2000_i64))?,
3207 ),
3208 (
3209 Interval::make(Some(1000_i64), Some(2000_i64))?,
3210 Interval::make(Some(1000_i64), None)?,
3211 Interval::make(Some(1000_i64), Some(2000_i64))?,
3212 ),
3213 (
3214 Interval::make(Some(1000_i64), Some(2000_i64))?,
3215 Interval::make(Some(1000_i64), Some(1500_i64))?,
3216 Interval::make(Some(1000_i64), Some(1500_i64))?,
3217 ),
3218 (
3219 Interval::make(Some(1000_i64), Some(2000_i64))?,
3220 Interval::make(Some(500_i64), Some(1500_i64))?,
3221 Interval::make(Some(1000_i64), Some(1500_i64))?,
3222 ),
3223 (
3224 Interval::make::<i64>(None, None)?,
3225 Interval::make::<i64>(None, None)?,
3226 Interval::make::<i64>(None, None)?,
3227 ),
3228 (
3229 Interval::make(None, Some(2000_u64))?,
3230 Interval::make(Some(500_u64), None)?,
3231 Interval::make(Some(500_u64), Some(2000_u64))?,
3232 ),
3233 (
3234 Interval::make(Some(0_u64), Some(0_u64))?,
3235 Interval::make(Some(0_u64), None)?,
3236 Interval::make(Some(0_u64), Some(0_u64))?,
3237 ),
3238 (
3239 Interval::make(Some(1000.0_f32), None)?,
3240 Interval::make(None, Some(1000.0_f32))?,
3241 Interval::make(Some(1000.0_f32), Some(1000.0_f32))?,
3242 ),
3243 (
3244 Interval::make(Some(1000.0_f32), Some(1500.0_f32))?,
3245 Interval::make(Some(0.0_f32), Some(1500.0_f32))?,
3246 Interval::make(Some(1000.0_f32), Some(1500.0_f32))?,
3247 ),
3248 (
3249 Interval::make(Some(-1000.0_f64), Some(1500.0_f64))?,
3250 Interval::make(Some(-1500.0_f64), Some(2000.0_f64))?,
3251 Interval::make(Some(-1000.0_f64), Some(1500.0_f64))?,
3252 ),
3253 (
3254 Interval::make(Some(16.0_f64), Some(32.0_f64))?,
3255 Interval::make(Some(32.0_f64), Some(64.0_f64))?,
3256 Interval::make(Some(32.0_f64), Some(32.0_f64))?,
3257 ),
3258 ];
3259 for (first, second, expected) in possible_cases {
3260 assert_eq!(first.intersect(second)?.unwrap(), expected)
3261 }
3262
3263 let empty_cases = vec![
3264 (
3265 Interval::make(Some(1000_i64), None)?,
3266 Interval::make(None, Some(0_i64))?,
3267 ),
3268 (
3269 Interval::make(Some(1000_i64), None)?,
3270 Interval::make(None, Some(999_i64))?,
3271 ),
3272 (
3273 Interval::make(Some(1500_i64), Some(2000_i64))?,
3274 Interval::make(Some(1000_i64), Some(1499_i64))?,
3275 ),
3276 (
3277 Interval::make(Some(0_i64), Some(1000_i64))?,
3278 Interval::make(Some(2000_i64), Some(3000_i64))?,
3279 ),
3280 (
3281 Interval::try_new(
3282 prev_value(ScalarValue::Float32(Some(1.0))),
3283 prev_value(ScalarValue::Float32(Some(1.0))),
3284 )?,
3285 Interval::make(Some(1.0_f32), Some(1.0_f32))?,
3286 ),
3287 (
3288 Interval::try_new(
3289 next_value(ScalarValue::Float32(Some(1.0))),
3290 next_value(ScalarValue::Float32(Some(1.0))),
3291 )?,
3292 Interval::make(Some(1.0_f32), Some(1.0_f32))?,
3293 ),
3294 ];
3295 for (first, second) in empty_cases {
3296 assert_eq!(first.intersect(second)?, None)
3297 }
3298
3299 Ok(())
3300 }
3301
3302 #[test]
3303 fn union_test() -> Result<()> {
3304 let possible_cases = vec![
3305 (
3306 Interval::make(Some(1000_i64), None)?,
3307 Interval::make::<i64>(None, None)?,
3308 Interval::make_unbounded(&DataType::Int64)?,
3309 ),
3310 (
3311 Interval::make(Some(1000_i64), None)?,
3312 Interval::make(None, Some(1000_i64))?,
3313 Interval::make_unbounded(&DataType::Int64)?,
3314 ),
3315 (
3316 Interval::make(Some(1000_i64), None)?,
3317 Interval::make(None, Some(2000_i64))?,
3318 Interval::make_unbounded(&DataType::Int64)?,
3319 ),
3320 (
3321 Interval::make(Some(1000_i64), Some(2000_i64))?,
3322 Interval::make(Some(1000_i64), None)?,
3323 Interval::make(Some(1000_i64), None)?,
3324 ),
3325 (
3326 Interval::make(Some(1000_i64), Some(2000_i64))?,
3327 Interval::make(Some(1000_i64), Some(1500_i64))?,
3328 Interval::make(Some(1000_i64), Some(2000_i64))?,
3329 ),
3330 (
3331 Interval::make(Some(1000_i64), Some(2000_i64))?,
3332 Interval::make(Some(500_i64), Some(1500_i64))?,
3333 Interval::make(Some(500_i64), Some(2000_i64))?,
3334 ),
3335 (
3336 Interval::make::<i64>(None, None)?,
3337 Interval::make::<i64>(None, None)?,
3338 Interval::make::<i64>(None, None)?,
3339 ),
3340 (
3341 Interval::make(Some(1000_i64), None)?,
3342 Interval::make(None, Some(0_i64))?,
3343 Interval::make_unbounded(&DataType::Int64)?,
3344 ),
3345 (
3346 Interval::make(Some(1000_i64), None)?,
3347 Interval::make(None, Some(999_i64))?,
3348 Interval::make_unbounded(&DataType::Int64)?,
3349 ),
3350 (
3351 Interval::make(Some(1500_i64), Some(2000_i64))?,
3352 Interval::make(Some(1000_i64), Some(1499_i64))?,
3353 Interval::make(Some(1000_i64), Some(2000_i64))?,
3354 ),
3355 (
3356 Interval::make(Some(0_i64), Some(1000_i64))?,
3357 Interval::make(Some(2000_i64), Some(3000_i64))?,
3358 Interval::make(Some(0_i64), Some(3000_i64))?,
3359 ),
3360 (
3361 Interval::make(None, Some(2000_u64))?,
3362 Interval::make(Some(500_u64), None)?,
3363 Interval::make(Some(0_u64), None)?,
3364 ),
3365 (
3366 Interval::make(Some(0_u64), Some(0_u64))?,
3367 Interval::make(Some(0_u64), None)?,
3368 Interval::make(Some(0_u64), None)?,
3369 ),
3370 (
3371 Interval::make(Some(1000.0_f32), None)?,
3372 Interval::make(None, Some(1000.0_f32))?,
3373 Interval::make_unbounded(&DataType::Float32)?,
3374 ),
3375 (
3376 Interval::make(Some(1000.0_f32), Some(1500.0_f32))?,
3377 Interval::make(Some(0.0_f32), Some(1500.0_f32))?,
3378 Interval::make(Some(0.0_f32), Some(1500.0_f32))?,
3379 ),
3380 (
3381 Interval::try_new(
3382 prev_value(ScalarValue::Float32(Some(1.0))),
3383 prev_value(ScalarValue::Float32(Some(1.0))),
3384 )?,
3385 Interval::make(Some(1.0_f32), Some(1.0_f32))?,
3386 Interval::try_new(
3387 prev_value(ScalarValue::Float32(Some(1.0))),
3388 ScalarValue::Float32(Some(1.0)),
3389 )?,
3390 ),
3391 (
3392 Interval::try_new(
3393 next_value(ScalarValue::Float32(Some(1.0))),
3394 next_value(ScalarValue::Float32(Some(1.0))),
3395 )?,
3396 Interval::make(Some(1.0_f32), Some(1.0_f32))?,
3397 Interval::try_new(
3398 ScalarValue::Float32(Some(1.0)),
3399 next_value(ScalarValue::Float32(Some(1.0))),
3400 )?,
3401 ),
3402 (
3403 Interval::make(Some(-1000.0_f64), Some(1500.0_f64))?,
3404 Interval::make(Some(-1500.0_f64), Some(2000.0_f64))?,
3405 Interval::make(Some(-1500.0_f64), Some(2000.0_f64))?,
3406 ),
3407 (
3408 Interval::make(Some(16.0_f64), Some(32.0_f64))?,
3409 Interval::make(Some(32.0_f64), Some(64.0_f64))?,
3410 Interval::make(Some(16.0_f64), Some(64.0_f64))?,
3411 ),
3412 ];
3413 for (first, second, expected) in possible_cases {
3414 println!("{first}");
3415 println!("{second}");
3416 assert_eq!(first.union(second)?, expected)
3417 }
3418
3419 Ok(())
3420 }
3421
3422 #[test]
3423 fn test_contains() -> Result<()> {
3424 let possible_cases = vec![
3425 (
3426 Interval::make::<i64>(None, None)?,
3427 Interval::make::<i64>(None, None)?,
3428 Interval::TRUE,
3429 ),
3430 (
3431 Interval::make(Some(1500_i64), Some(2000_i64))?,
3432 Interval::make(Some(1501_i64), Some(1999_i64))?,
3433 Interval::TRUE,
3434 ),
3435 (
3436 Interval::make(Some(1000_i64), None)?,
3437 Interval::make::<i64>(None, None)?,
3438 Interval::TRUE_OR_FALSE,
3439 ),
3440 (
3441 Interval::make(Some(1000_i64), Some(2000_i64))?,
3442 Interval::make(Some(500), Some(1500_i64))?,
3443 Interval::TRUE_OR_FALSE,
3444 ),
3445 (
3446 Interval::make(Some(16.0), Some(32.0))?,
3447 Interval::make(Some(32.0), Some(64.0))?,
3448 Interval::TRUE_OR_FALSE,
3449 ),
3450 (
3451 Interval::make(Some(1000_i64), None)?,
3452 Interval::make(None, Some(0_i64))?,
3453 Interval::FALSE,
3454 ),
3455 (
3456 Interval::make(Some(1500_i64), Some(2000_i64))?,
3457 Interval::make(Some(1000_i64), Some(1499_i64))?,
3458 Interval::FALSE,
3459 ),
3460 (
3461 Interval::try_new(
3462 prev_value(ScalarValue::Float32(Some(1.0))),
3463 prev_value(ScalarValue::Float32(Some(1.0))),
3464 )?,
3465 Interval::make(Some(1.0_f32), Some(1.0_f32))?,
3466 Interval::FALSE,
3467 ),
3468 (
3469 Interval::try_new(
3470 next_value(ScalarValue::Float32(Some(1.0))),
3471 next_value(ScalarValue::Float32(Some(1.0))),
3472 )?,
3473 Interval::make(Some(1.0_f32), Some(1.0_f32))?,
3474 Interval::FALSE,
3475 ),
3476 ];
3477 for (first, second, expected) in possible_cases {
3478 assert_eq!(first.contains(second)?, expected)
3479 }
3480
3481 Ok(())
3482 }
3483
3484 #[test]
3485 fn test_contains_value() -> Result<()> {
3486 let possible_cases = vec![
3487 (
3488 Interval::make(Some(0), Some(100))?,
3489 ScalarValue::Int32(Some(50)),
3490 true,
3491 ),
3492 (
3493 Interval::make(Some(0), Some(100))?,
3494 ScalarValue::Int32(Some(150)),
3495 false,
3496 ),
3497 (
3498 Interval::make(Some(0), Some(100))?,
3499 ScalarValue::Float64(Some(50.)),
3500 true,
3501 ),
3502 (
3503 Interval::make(Some(0), Some(100))?,
3504 ScalarValue::Float64(Some(next_down(100.))),
3505 true,
3506 ),
3507 (
3508 Interval::make(Some(0), Some(100))?,
3509 ScalarValue::Float64(Some(next_up(100.))),
3510 false,
3511 ),
3512 ];
3513
3514 for (interval, value, expected) in possible_cases {
3515 assert_eq!(interval.contains_value(value)?, expected)
3516 }
3517
3518 Ok(())
3519 }
3520
3521 #[test]
3522 fn test_add() -> Result<()> {
3523 let cases = vec![
3524 (
3525 Interval::make(Some(100_i64), Some(200_i64))?,
3526 Interval::make(None, Some(200_i64))?,
3527 Interval::make(None, Some(400_i64))?,
3528 ),
3529 (
3530 Interval::make(Some(100_i64), Some(200_i64))?,
3531 Interval::make(Some(200_i64), None)?,
3532 Interval::make(Some(300_i64), None)?,
3533 ),
3534 (
3535 Interval::make(None, Some(200_i64))?,
3536 Interval::make(Some(100_i64), Some(200_i64))?,
3537 Interval::make(None, Some(400_i64))?,
3538 ),
3539 (
3540 Interval::make(Some(200_i64), None)?,
3541 Interval::make(Some(100_i64), Some(200_i64))?,
3542 Interval::make(Some(300_i64), None)?,
3543 ),
3544 (
3545 Interval::make(Some(100_i64), Some(200_i64))?,
3546 Interval::make(Some(-300_i64), Some(150_i64))?,
3547 Interval::make(Some(-200_i64), Some(350_i64))?,
3548 ),
3549 (
3550 Interval::make(Some(f32::MAX), Some(f32::MAX))?,
3551 Interval::make(Some(11_f32), Some(11_f32))?,
3552 Interval::make(Some(f32::MAX), None)?,
3553 ),
3554 (
3555 Interval::make(Some(f32::MIN), Some(f32::MIN))?,
3556 Interval::make(Some(-10_f32), Some(10_f32))?,
3557 Interval::make(
3560 None,
3561 Some(-340282330000000000000000000000000000000.0_f32),
3562 )?,
3563 ),
3564 (
3565 Interval::make(Some(f32::MIN), Some(f32::MIN))?,
3566 Interval::make(Some(-10_f32), Some(-10_f32))?,
3567 Interval::make(None, Some(f32::MIN))?,
3568 ),
3569 (
3570 Interval::make(Some(1.0), Some(f32::MAX))?,
3571 Interval::make(Some(f32::MAX), Some(f32::MAX))?,
3572 Interval::make(Some(f32::MAX), None)?,
3573 ),
3574 (
3575 Interval::make(Some(f32::MIN), Some(f32::MIN))?,
3576 Interval::make(Some(f32::MAX), Some(f32::MAX))?,
3577 Interval::make(Some(-0.0_f32), Some(0.0_f32))?,
3578 ),
3579 (
3580 Interval::make(Some(100_f64), None)?,
3581 Interval::make(None, Some(200_f64))?,
3582 Interval::make::<i64>(None, None)?,
3583 ),
3584 (
3585 Interval::make(None, Some(100_f64))?,
3586 Interval::make(None, Some(200_f64))?,
3587 Interval::make(None, Some(300_f64))?,
3588 ),
3589 ];
3590 for case in cases {
3591 let result = case.0.add(case.1)?;
3592 if case.0.data_type().is_floating() {
3593 assert!(
3594 result.lower().is_null() && case.2.lower().is_null()
3595 || result.lower().le(case.2.lower())
3596 );
3597 assert!(
3598 result.upper().is_null() && case.2.upper().is_null()
3599 || result.upper().ge(case.2.upper())
3600 );
3601 } else {
3602 assert_eq!(result, case.2);
3603 }
3604 }
3605
3606 Ok(())
3607 }
3608
3609 #[test]
3610 fn test_sub() -> Result<()> {
3611 let cases = vec![
3612 (
3613 Interval::make(Some(i32::MAX), Some(i32::MAX))?,
3614 Interval::make(Some(11_i32), Some(11_i32))?,
3615 Interval::make(Some(i32::MAX - 11), Some(i32::MAX - 11))?,
3616 ),
3617 (
3618 Interval::make(Some(100_i64), Some(200_i64))?,
3619 Interval::make(None, Some(200_i64))?,
3620 Interval::make(Some(-100_i64), None)?,
3621 ),
3622 (
3623 Interval::make(Some(100_i64), Some(200_i64))?,
3624 Interval::make(Some(200_i64), None)?,
3625 Interval::make(None, Some(0_i64))?,
3626 ),
3627 (
3628 Interval::make(None, Some(200_i64))?,
3629 Interval::make(Some(100_i64), Some(200_i64))?,
3630 Interval::make(None, Some(100_i64))?,
3631 ),
3632 (
3633 Interval::make(Some(200_i64), None)?,
3634 Interval::make(Some(100_i64), Some(200_i64))?,
3635 Interval::make(Some(0_i64), None)?,
3636 ),
3637 (
3638 Interval::make(Some(100_i64), Some(200_i64))?,
3639 Interval::make(Some(-300_i64), Some(150_i64))?,
3640 Interval::make(Some(-50_i64), Some(500_i64))?,
3641 ),
3642 (
3643 Interval::make(Some(i64::MIN), Some(i64::MIN))?,
3644 Interval::make(Some(-10_i64), Some(-10_i64))?,
3645 Interval::make(Some(i64::MIN + 10), Some(i64::MIN + 10))?,
3646 ),
3647 (
3648 Interval::make(Some(1), Some(i64::MAX))?,
3649 Interval::make(Some(i64::MAX), Some(i64::MAX))?,
3650 Interval::make(Some(1 - i64::MAX), Some(0))?,
3651 ),
3652 (
3653 Interval::make(Some(i64::MIN), Some(i64::MIN))?,
3654 Interval::make(Some(i64::MAX), Some(i64::MAX))?,
3655 Interval::make(None, Some(i64::MIN))?,
3656 ),
3657 (
3658 Interval::make(Some(2_u32), Some(10_u32))?,
3659 Interval::make(Some(4_u32), Some(6_u32))?,
3660 Interval::make(None, Some(6_u32))?,
3661 ),
3662 (
3663 Interval::make(Some(2_u32), Some(10_u32))?,
3664 Interval::make(Some(20_u32), Some(30_u32))?,
3665 Interval::make(None, Some(0_u32))?,
3666 ),
3667 (
3668 Interval::make(Some(f32::MIN), Some(f32::MIN))?,
3669 Interval::make(Some(-10_f32), Some(10_f32))?,
3670 Interval::make(
3673 None,
3674 Some(-340282330000000000000000000000000000000.0_f32),
3675 )?,
3676 ),
3677 (
3678 Interval::make(Some(100_f64), None)?,
3679 Interval::make(None, Some(200_f64))?,
3680 Interval::make(Some(-100_f64), None)?,
3681 ),
3682 (
3683 Interval::make(None, Some(100_f64))?,
3684 Interval::make(None, Some(200_f64))?,
3685 Interval::make::<i64>(None, None)?,
3686 ),
3687 ];
3688 for case in cases {
3689 let result = case.0.sub(case.1)?;
3690 if case.0.data_type().is_floating() {
3691 assert!(
3692 result.lower().is_null() && case.2.lower().is_null()
3693 || result.lower().le(case.2.lower())
3694 );
3695 assert!(
3696 result.upper().is_null() && case.2.upper().is_null()
3697 || result.upper().ge(case.2.upper(),)
3698 );
3699 } else {
3700 assert_eq!(result, case.2);
3701 }
3702 }
3703
3704 Ok(())
3705 }
3706
3707 #[test]
3708 fn test_mul_zero_inclusive_overflow() -> Result<()> {
3709 let cases = [
3712 ((0_i8, 20_i8), (-3, 7), (Some(-60_i8), None)),
3713 ((-20, 0), (-7, 3), (Some(-60), None)),
3714 ((-20, 0), (-3, 7), (None, Some(60))),
3715 ((0, 20), (-7, 3), (None, Some(60))),
3716 ((-20, 20), (-7, 7), (None, None)),
3717 ((-3, 3), (-7, 7), (Some(-21), Some(21))),
3718 ((0, 0), (i8::MIN, i8::MAX), (Some(0), Some(0))),
3719 ];
3720 for ((lo, hi), (rlo, rhi), (expected_lo, expected_hi)) in cases {
3721 let lhs = Interval::make(Some(lo), Some(hi))?;
3722 let rhs = Interval::make(Some(rlo), Some(rhi))?;
3723 let expected = Interval::make(expected_lo, expected_hi)?;
3724 assert_eq!(lhs.mul(&rhs)?, expected, "{lhs:?} * {rhs:?}");
3725 assert_eq!(rhs.mul(&lhs)?, expected, "{rhs:?} * {lhs:?}");
3726 }
3727 Ok(())
3728 }
3729
3730 #[test]
3731 fn test_mul() -> Result<()> {
3732 let cases = vec![
3733 (
3734 Interval::make(Some(1_i64), Some(2_i64))?,
3735 Interval::make(None, Some(2_i64))?,
3736 Interval::make(None, Some(4_i64))?,
3737 ),
3738 (
3739 Interval::make(Some(1_i64), Some(2_i64))?,
3740 Interval::make(Some(2_i64), None)?,
3741 Interval::make(Some(2_i64), None)?,
3742 ),
3743 (
3744 Interval::make(None, Some(2_i64))?,
3745 Interval::make(Some(1_i64), Some(2_i64))?,
3746 Interval::make(None, Some(4_i64))?,
3747 ),
3748 (
3749 Interval::make(Some(2_i64), None)?,
3750 Interval::make(Some(1_i64), Some(2_i64))?,
3751 Interval::make(Some(2_i64), None)?,
3752 ),
3753 (
3754 Interval::make(Some(1_i64), Some(2_i64))?,
3755 Interval::make(Some(-3_i64), Some(15_i64))?,
3756 Interval::make(Some(-6_i64), Some(30_i64))?,
3757 ),
3758 (
3759 Interval::make(Some(-0.0), Some(0.0))?,
3760 Interval::make(None, Some(0.0))?,
3761 Interval::make::<i64>(None, None)?,
3762 ),
3763 (
3764 Interval::make(Some(f32::MIN), Some(f32::MIN))?,
3765 Interval::make(Some(-10_f32), Some(10_f32))?,
3766 Interval::make::<i64>(None, None)?,
3767 ),
3768 (
3769 Interval::make(Some(1_u32), Some(2_u32))?,
3770 Interval::make(Some(0_u32), Some(1_u32))?,
3771 Interval::make(Some(0_u32), Some(2_u32))?,
3772 ),
3773 (
3774 Interval::make(None, Some(2_u32))?,
3775 Interval::make(Some(0_u32), Some(1_u32))?,
3776 Interval::make(None, Some(2_u32))?,
3777 ),
3778 (
3779 Interval::make(None, Some(2_u32))?,
3780 Interval::make(Some(1_u32), Some(2_u32))?,
3781 Interval::make(None, Some(4_u32))?,
3782 ),
3783 (
3784 Interval::make(None, Some(2_u32))?,
3785 Interval::make(Some(1_u32), None)?,
3786 Interval::make::<u32>(None, None)?,
3787 ),
3788 (
3789 Interval::make::<u32>(None, None)?,
3790 Interval::make(Some(0_u32), None)?,
3791 Interval::make::<u32>(None, None)?,
3792 ),
3793 (
3794 Interval::make(Some(f32::MAX), Some(f32::MAX))?,
3795 Interval::make(Some(11_f32), Some(11_f32))?,
3796 Interval::make(Some(f32::MAX), None)?,
3797 ),
3798 (
3799 Interval::make(Some(f32::MIN), Some(f32::MIN))?,
3800 Interval::make(Some(-10_f32), Some(-10_f32))?,
3801 Interval::make(Some(f32::MAX), None)?,
3802 ),
3803 (
3804 Interval::make(Some(1.0), Some(f32::MAX))?,
3805 Interval::make(Some(f32::MAX), Some(f32::MAX))?,
3806 Interval::make(Some(f32::MAX), None)?,
3807 ),
3808 (
3809 Interval::make(Some(f32::MIN), Some(f32::MIN))?,
3810 Interval::make(Some(f32::MAX), Some(f32::MAX))?,
3811 Interval::make(None, Some(f32::MIN))?,
3812 ),
3813 (
3814 Interval::make(Some(-0.0_f32), Some(0.0_f32))?,
3815 Interval::make(Some(f32::MAX), None)?,
3816 Interval::make::<f32>(None, None)?,
3817 ),
3818 (
3819 Interval::make(Some(0.0_f32), Some(0.0_f32))?,
3820 Interval::make(Some(f32::MAX), None)?,
3821 Interval::make(Some(0.0_f32), None)?,
3822 ),
3823 (
3824 Interval::make(Some(1_f64), None)?,
3825 Interval::make(None, Some(2_f64))?,
3826 Interval::make::<f64>(None, None)?,
3827 ),
3828 (
3829 Interval::make(None, Some(1_f64))?,
3830 Interval::make(None, Some(2_f64))?,
3831 Interval::make::<f64>(None, None)?,
3832 ),
3833 (
3834 Interval::make(Some(-0.0_f64), Some(-0.0_f64))?,
3835 Interval::make(Some(1_f64), Some(2_f64))?,
3836 Interval::make(Some(-0.0_f64), Some(-0.0_f64))?,
3837 ),
3838 (
3839 Interval::make(Some(0.0_f64), Some(0.0_f64))?,
3840 Interval::make(Some(1_f64), Some(2_f64))?,
3841 Interval::make(Some(0.0_f64), Some(0.0_f64))?,
3842 ),
3843 (
3844 Interval::make(Some(-0.0_f64), Some(0.0_f64))?,
3845 Interval::make(Some(1_f64), Some(2_f64))?,
3846 Interval::make(Some(-0.0_f64), Some(0.0_f64))?,
3847 ),
3848 (
3849 Interval::make(Some(-0.0_f64), Some(1.0_f64))?,
3850 Interval::make(Some(1_f64), Some(2_f64))?,
3851 Interval::make(Some(-0.0_f64), Some(2.0_f64))?,
3852 ),
3853 (
3854 Interval::make(Some(0.0_f64), Some(1.0_f64))?,
3855 Interval::make(Some(1_f64), Some(2_f64))?,
3856 Interval::make(Some(0.0_f64), Some(2.0_f64))?,
3857 ),
3858 (
3859 Interval::make(Some(-0.0_f64), Some(1.0_f64))?,
3860 Interval::make(Some(-1_f64), Some(2_f64))?,
3861 Interval::make(Some(-1.0_f64), Some(2.0_f64))?,
3862 ),
3863 (
3864 Interval::make::<f64>(None, None)?,
3865 Interval::make(Some(-0.0_f64), Some(0.0_f64))?,
3866 Interval::make::<f64>(None, None)?,
3867 ),
3868 (
3869 Interval::make::<f64>(None, Some(10.0_f64))?,
3870 Interval::make(Some(-0.0_f64), Some(0.0_f64))?,
3871 Interval::make::<f64>(None, None)?,
3872 ),
3873 ];
3874 for case in cases {
3875 let result = case.0.mul(case.1)?;
3876 if case.0.data_type().is_floating() {
3877 assert!(
3878 result.lower().is_null() && case.2.lower().is_null()
3879 || result.lower().le(case.2.lower())
3880 );
3881 assert!(
3882 result.upper().is_null() && case.2.upper().is_null()
3883 || result.upper().ge(case.2.upper())
3884 );
3885 } else {
3886 assert_eq!(result, case.2);
3887 }
3888 }
3889
3890 Ok(())
3891 }
3892
3893 #[test]
3894 fn test_div_small_integer_intervals_contain_runtime_results() -> Result<()> {
3895 for lo in -3_i64..=3 {
3897 for hi in lo..=3 {
3898 for rlo in -3_i64..=3 {
3899 for rhi in rlo..=3 {
3900 let lhs = Interval::make(Some(lo), Some(hi))?;
3901 let rhs = Interval::make(Some(rlo), Some(rhi))?;
3902 let result = lhs.div(&rhs)?;
3903 for a in lo..=hi {
3904 for b in rlo..=rhi {
3905 if b != 0 {
3906 assert!(
3907 result.contains_value(ScalarValue::Int64(
3908 Some(a / b)
3909 ))?,
3910 "{lhs:?} / {rhs:?} = {result:?} excludes {a} / {b}",
3911 );
3912 }
3913 }
3914 }
3915 }
3916 }
3917 }
3918 }
3919 Ok(())
3920 }
3921
3922 #[test]
3923 fn test_div() -> Result<()> {
3924 let cases = vec![
3925 (
3926 Interval::make(Some(-3_i64), Some(0_i64))?,
3927 Interval::make(Some(-2_i64), Some(-1_i64))?,
3928 Interval::make(Some(0_i64), Some(3_i64))?,
3929 ),
3930 (
3931 Interval::make(Some(-3_i64), Some(0_i64))?,
3932 Interval::make(Some(1_i64), Some(2_i64))?,
3933 Interval::make(Some(-3_i64), Some(0_i64))?,
3934 ),
3935 (
3936 Interval::make(Some(0_i64), Some(3_i64))?,
3937 Interval::make(Some(-2_i64), Some(-1_i64))?,
3938 Interval::make(Some(-3_i64), Some(0_i64))?,
3939 ),
3940 (
3941 Interval::make(Some(0_i64), Some(3_i64))?,
3942 Interval::make(Some(1_i64), Some(2_i64))?,
3943 Interval::make(Some(0_i64), Some(3_i64))?,
3944 ),
3945 (
3946 Interval::make(Some(-3_i64), Some(-1_i64))?,
3947 Interval::make(Some(-3_i64), Some(0_i64))?,
3948 Interval::make(Some(0_i64), None)?,
3949 ),
3950 (
3951 Interval::make(Some(1_i64), Some(3_i64))?,
3952 Interval::make(Some(-3_i64), Some(0_i64))?,
3953 Interval::make(None, Some(0_i64))?,
3954 ),
3955 (
3956 Interval::make(Some(-3_i64), Some(-1_i64))?,
3957 Interval::make(Some(0_i64), Some(3_i64))?,
3958 Interval::make(None, Some(0_i64))?,
3959 ),
3960 (
3961 Interval::make(Some(1_i64), Some(3_i64))?,
3962 Interval::make(Some(0_i64), Some(3_i64))?,
3963 Interval::make(Some(0_i64), None)?,
3964 ),
3965 (
3966 Interval::make(Some(-6_i64), Some(6_i64))?,
3967 Interval::make(Some(-3_i64), Some(0_i64))?,
3968 Interval::make::<i64>(None, None)?,
3969 ),
3970 (
3971 Interval::make(Some(-6_i64), Some(6_i64))?,
3972 Interval::make(Some(0_i64), Some(3_i64))?,
3973 Interval::make::<i64>(None, None)?,
3974 ),
3975 (
3976 Interval::make(Some(0_i64), Some(0_i64))?,
3977 Interval::make(Some(0_i64), Some(0_i64))?,
3978 Interval::make::<i64>(None, None)?,
3979 ),
3980 (
3981 Interval::make(Some(0_u32), Some(0_u32))?,
3982 Interval::make(Some(0_u32), Some(5_u32))?,
3983 Interval::make(Some(0_u32), Some(0_u32))?,
3984 ),
3985 (
3986 Interval::make(Some(0_u32), Some(0_u32))?,
3987 Interval::make(Some(0_u32), Some(0_u32))?,
3988 Interval::make::<u32>(None, None)?,
3989 ),
3990 (
3991 Interval::make(Some(0_u32), Some(5_u32))?,
3992 Interval::make(Some(0_u32), Some(5_u32))?,
3993 Interval::make(Some(0_u32), None)?,
3994 ),
3995 (
3996 Interval::make(Some(1_u32), Some(5_u32))?,
3997 Interval::make(Some(0_u32), Some(5_u32))?,
3998 Interval::make(Some(0_u32), None)?,
3999 ),
4000 (
4001 Interval::make(Some(0_u64), Some(0_u64))?,
4002 Interval::make(Some(0_u64), Some(5_u64))?,
4003 Interval::make(Some(0_u64), Some(0_u64))?,
4004 ),
4005 (
4006 Interval::make(Some(0_u64), Some(0_u64))?,
4007 Interval::make(Some(0_u64), Some(0_u64))?,
4008 Interval::make::<u64>(None, None)?,
4009 ),
4010 (
4011 Interval::make(Some(100_i64), Some(200_i64))?,
4012 Interval::make(Some(1_i64), Some(2_i64))?,
4013 Interval::make(Some(50_i64), Some(200_i64))?,
4014 ),
4015 (
4016 Interval::make(Some(-200_i64), Some(-100_i64))?,
4017 Interval::make(Some(-2_i64), Some(-1_i64))?,
4018 Interval::make(Some(50_i64), Some(200_i64))?,
4019 ),
4020 (
4021 Interval::make(Some(100_i64), Some(200_i64))?,
4022 Interval::make(Some(-2_i64), Some(-1_i64))?,
4023 Interval::make(Some(-200_i64), Some(-50_i64))?,
4024 ),
4025 (
4026 Interval::make(Some(-200_i64), Some(-100_i64))?,
4027 Interval::make(Some(1_i64), Some(2_i64))?,
4028 Interval::make(Some(-200_i64), Some(-50_i64))?,
4029 ),
4030 (
4031 Interval::make(Some(-200_i64), Some(100_i64))?,
4032 Interval::make(Some(1_i64), Some(2_i64))?,
4033 Interval::make(Some(-200_i64), Some(100_i64))?,
4034 ),
4035 (
4036 Interval::make(Some(-100_i64), Some(200_i64))?,
4037 Interval::make(Some(1_i64), Some(2_i64))?,
4038 Interval::make(Some(-100_i64), Some(200_i64))?,
4039 ),
4040 (
4041 Interval::make(Some(10_i64), Some(20_i64))?,
4042 Interval::make::<i64>(None, None)?,
4043 Interval::make::<i64>(None, None)?,
4044 ),
4045 (
4046 Interval::make(Some(-100_i64), Some(200_i64))?,
4047 Interval::make(Some(-1_i64), Some(2_i64))?,
4048 Interval::make::<i64>(None, None)?,
4049 ),
4050 (
4051 Interval::make(Some(-100_i64), Some(200_i64))?,
4052 Interval::make(Some(-2_i64), Some(1_i64))?,
4053 Interval::make::<i64>(None, None)?,
4054 ),
4055 (
4056 Interval::make(Some(100_i64), Some(200_i64))?,
4057 Interval::make(Some(0_i64), Some(1_i64))?,
4058 Interval::make(Some(100_i64), None)?,
4059 ),
4060 (
4061 Interval::make(Some(100_i64), Some(200_i64))?,
4062 Interval::make(None, Some(0_i64))?,
4063 Interval::make(None, Some(0_i64))?,
4064 ),
4065 (
4066 Interval::make(Some(100_i64), Some(200_i64))?,
4067 Interval::make(Some(0_i64), Some(0_i64))?,
4068 Interval::make::<i64>(None, None)?,
4069 ),
4070 (
4071 Interval::make(Some(0_i64), Some(1_i64))?,
4072 Interval::make(Some(100_i64), Some(200_i64))?,
4073 Interval::make(Some(0_i64), Some(0_i64))?,
4074 ),
4075 (
4076 Interval::make(Some(0_i64), Some(1_i64))?,
4077 Interval::make(Some(100_i64), Some(200_i64))?,
4078 Interval::make(Some(0_i64), Some(0_i64))?,
4079 ),
4080 (
4081 Interval::make(Some(1_u32), Some(2_u32))?,
4082 Interval::make(Some(0_u32), Some(0_u32))?,
4083 Interval::make::<u32>(None, None)?,
4084 ),
4085 (
4086 Interval::make(Some(10_u32), Some(20_u32))?,
4087 Interval::make(None, Some(2_u32))?,
4088 Interval::make(Some(5_u32), None)?,
4089 ),
4090 (
4091 Interval::make(Some(10_u32), Some(20_u32))?,
4092 Interval::make(Some(0_u32), Some(2_u32))?,
4093 Interval::make(Some(5_u32), None)?,
4094 ),
4095 (
4096 Interval::make(Some(10_u32), Some(20_u32))?,
4097 Interval::make(Some(0_u32), Some(0_u32))?,
4098 Interval::make::<u32>(None, None)?,
4099 ),
4100 (
4101 Interval::make(Some(12_u64), Some(48_u64))?,
4102 Interval::make(Some(10_u64), Some(20_u64))?,
4103 Interval::make(Some(0_u64), Some(4_u64))?,
4104 ),
4105 (
4106 Interval::make(Some(12_u64), Some(48_u64))?,
4107 Interval::make(None, Some(2_u64))?,
4108 Interval::make(Some(6_u64), None)?,
4109 ),
4110 (
4111 Interval::make(Some(12_u64), Some(48_u64))?,
4112 Interval::make(Some(0_u64), Some(2_u64))?,
4113 Interval::make(Some(6_u64), None)?,
4114 ),
4115 (
4116 Interval::make(None, Some(48_u64))?,
4117 Interval::make(Some(0_u64), Some(2_u64))?,
4118 Interval::make::<u64>(None, None)?,
4119 ),
4120 (
4121 Interval::make(Some(f32::MAX), Some(f32::MAX))?,
4122 Interval::make(Some(-0.1_f32), Some(0.1_f32))?,
4123 Interval::make::<f32>(None, None)?,
4124 ),
4125 (
4126 Interval::make(Some(f32::MIN), None)?,
4127 Interval::make(Some(0.1_f32), Some(0.1_f32))?,
4128 Interval::make::<f32>(None, None)?,
4129 ),
4130 (
4131 Interval::make(Some(-10.0_f32), Some(10.0_f32))?,
4132 Interval::make(Some(-0.1_f32), Some(-0.1_f32))?,
4133 Interval::make(Some(-100.0_f32), Some(100.0_f32))?,
4134 ),
4135 (
4136 Interval::make(Some(-10.0_f32), Some(f32::MAX))?,
4137 Interval::make::<f32>(None, None)?,
4138 Interval::make::<f32>(None, None)?,
4139 ),
4140 (
4141 Interval::make(Some(f32::MIN), Some(10.0_f32))?,
4142 Interval::make(Some(1.0_f32), None)?,
4143 Interval::make(Some(f32::MIN), Some(10.0_f32))?,
4144 ),
4145 (
4146 Interval::make(Some(-0.0_f32), Some(0.0_f32))?,
4147 Interval::make(Some(f32::MAX), None)?,
4148 Interval::make(Some(-0.0_f32), Some(0.0_f32))?,
4149 ),
4150 (
4151 Interval::make(Some(-0.0_f32), Some(0.0_f32))?,
4152 Interval::make(None, Some(-0.0_f32))?,
4153 Interval::make::<f32>(None, None)?,
4154 ),
4155 (
4156 Interval::make(Some(0.0_f32), Some(0.0_f32))?,
4157 Interval::make(Some(f32::MAX), None)?,
4158 Interval::make(Some(0.0_f32), Some(0.0_f32))?,
4159 ),
4160 (
4161 Interval::make(Some(1.0_f32), Some(2.0_f32))?,
4162 Interval::make(Some(0.0_f32), Some(4.0_f32))?,
4163 Interval::make(Some(0.25_f32), None)?,
4164 ),
4165 (
4166 Interval::make(Some(1.0_f32), Some(2.0_f32))?,
4167 Interval::make(Some(-4.0_f32), Some(-0.0_f32))?,
4168 Interval::make(None, Some(-0.25_f32))?,
4169 ),
4170 (
4171 Interval::make(Some(-4.0_f64), Some(2.0_f64))?,
4172 Interval::make(Some(10.0_f64), Some(20.0_f64))?,
4173 Interval::make(Some(-0.4_f64), Some(0.2_f64))?,
4174 ),
4175 (
4176 Interval::make(Some(-0.0_f64), Some(-0.0_f64))?,
4177 Interval::make(None, Some(-0.0_f64))?,
4178 Interval::make(Some(0.0_f64), None)?,
4179 ),
4180 (
4181 Interval::make(Some(1.0_f64), Some(2.0_f64))?,
4182 Interval::make::<f64>(None, None)?,
4183 Interval::make(Some(0.0_f64), None)?,
4184 ),
4185 ];
4186 for case in cases {
4187 let result = case.0.div(&case.1)?;
4188 if case.0.data_type().is_floating() {
4189 assert!(
4190 result.lower().is_null() && case.2.lower().is_null()
4191 || result.lower().le(case.2.lower())
4192 );
4193 assert!(
4194 result.upper().is_null() && case.2.upper().is_null()
4195 || result.upper().ge(case.2.upper())
4196 );
4197 } else {
4198 assert_eq!(result, case.2, "{:?} / {:?}", case.0, case.1);
4199 }
4200 }
4201
4202 Ok(())
4203 }
4204
4205 #[test]
4206 fn test_mul_div_mismatched_operand_types() -> Result<()> {
4207 let lhs = Interval::try_new(
4216 ScalarValue::Decimal128(Some(0), 38, 10),
4217 ScalarValue::Decimal128(Some(100_000_000_000), 38, 10), )?;
4219 let rhs = Interval::try_new(
4220 ScalarValue::Decimal128(Some(1), 20, 0),
4221 ScalarValue::Decimal128(Some(10), 20, 0),
4222 )?;
4223 let div_result = lhs.div(&rhs)?;
4224 assert!(matches!(div_result.data_type(), DataType::Decimal128(_, _)));
4225 let mul_result = lhs.mul(&rhs)?;
4226 assert!(matches!(mul_result.data_type(), DataType::Decimal128(_, _)));
4227
4228 let int_rhs = Interval::make(Some(1_i64), Some(10_i64))?;
4230 let div_int = lhs.div(&int_rhs)?;
4231 assert!(matches!(div_int.data_type(), DataType::Decimal128(_, _)));
4232 let mul_int = lhs.mul(&int_rhs)?;
4233 assert!(matches!(mul_int.data_type(), DataType::Decimal128(_, _)));
4234
4235 Ok(())
4236 }
4237
4238 #[test]
4239 fn test_intersect_mismatched_decimal_types() -> Result<()> {
4240 let lhs = Interval::try_new(
4247 ScalarValue::Decimal128(Some(0), 38, 10),
4248 ScalarValue::Decimal128(Some(100_000_000_000), 38, 10), )?;
4250 let rhs = Interval::try_new(
4251 ScalarValue::Decimal128(Some(5), 20, 0),
4252 ScalarValue::Decimal128(Some(20), 20, 0),
4253 )?;
4254 let intersected = lhs.intersect(&rhs)?.expect("intervals overlap");
4255 let expected = Interval::try_new(
4256 ScalarValue::Decimal128(Some(50_000_000_000), 38, 10), ScalarValue::Decimal128(Some(100_000_000_000), 38, 10), )?;
4259 assert_eq!(intersected, expected);
4260 assert_eq!(intersected.data_type(), DataType::Decimal128(38, 10));
4261
4262 let lhs_disjoint = Interval::try_new(
4264 ScalarValue::Decimal128(Some(0), 38, 10),
4265 ScalarValue::Decimal128(Some(30_000_000_000), 38, 10), )?;
4267 assert_eq!(lhs_disjoint.intersect(&rhs)?, None);
4268
4269 Ok(())
4270 }
4271
4272 #[test]
4273 fn test_union_mismatched_decimal_types() -> Result<()> {
4274 let lhs = Interval::try_new(
4276 ScalarValue::Decimal128(Some(0), 38, 10),
4277 ScalarValue::Decimal128(Some(30_000_000_000), 38, 10), )?;
4279 let rhs = Interval::try_new(
4280 ScalarValue::Decimal128(Some(5), 20, 0),
4281 ScalarValue::Decimal128(Some(20), 20, 0),
4282 )?;
4283 let unioned = lhs.union(&rhs)?;
4284 let expected = Interval::try_new(
4285 ScalarValue::Decimal128(Some(0), 38, 10),
4286 ScalarValue::Decimal128(Some(200_000_000_000), 38, 10), )?;
4288 assert_eq!(unioned, expected);
4289 assert_eq!(unioned.data_type(), DataType::Decimal128(38, 10));
4290
4291 Ok(())
4292 }
4293
4294 #[test]
4295 fn test_contains_mismatched_decimal_types() -> Result<()> {
4296 let rhs = Interval::try_new(
4301 ScalarValue::Decimal128(Some(5), 20, 0),
4302 ScalarValue::Decimal128(Some(10), 20, 0),
4303 )?;
4304
4305 let superset = Interval::try_new(
4307 ScalarValue::Decimal128(Some(0), 38, 10),
4308 ScalarValue::Decimal128(Some(200_000_000_000), 38, 10), )?;
4310 assert_eq!(superset.contains(&rhs)?, Interval::TRUE);
4311
4312 let overlap = Interval::try_new(
4314 ScalarValue::Decimal128(Some(0), 38, 10),
4315 ScalarValue::Decimal128(Some(70_000_000_000), 38, 10), )?;
4317 assert_eq!(overlap.contains(&rhs)?, Interval::TRUE_OR_FALSE);
4318
4319 let disjoint = Interval::try_new(
4321 ScalarValue::Decimal128(Some(0), 38, 10),
4322 ScalarValue::Decimal128(Some(30_000_000_000), 38, 10), )?;
4324 assert_eq!(disjoint.contains(&rhs)?, Interval::FALSE);
4325
4326 let int_rhs = Interval::make(Some(5_i64), Some(10_i64))?;
4328 assert_eq!(superset.contains(&int_rhs)?, Interval::TRUE);
4329
4330 Ok(())
4331 }
4332
4333 #[test]
4334 fn test_overflow_handling() -> Result<()> {
4335 let dt = DataType::Int32;
4337 let op = Operator::Plus;
4338 let lhs = ScalarValue::Int32(Some(i32::MAX));
4339 let rhs = ScalarValue::Int32(Some(1));
4340 let result = handle_overflow::<true>(&dt, op, &lhs, &rhs);
4341 assert_eq!(result, ScalarValue::Int32(None));
4342 let result = handle_overflow::<false>(&dt, op, &lhs, &rhs);
4343 assert_eq!(result, ScalarValue::Int32(Some(i32::MAX)));
4344
4345 let dt = DataType::Float32;
4347 let op = Operator::Multiply;
4348 let lhs = ScalarValue::Float32(Some(f32::MAX));
4349 let rhs = ScalarValue::Float32(Some(2.0));
4350 let result = handle_overflow::<true>(&dt, op, &lhs, &rhs);
4351 assert_eq!(result, ScalarValue::Float32(None));
4352 let result = handle_overflow::<false>(&dt, op, &lhs, &rhs);
4353 assert_eq!(result, ScalarValue::Float32(Some(f32::MAX)));
4354
4355 let lhs = ScalarValue::Float32(Some(f32::MIN));
4357 let rhs = ScalarValue::Float32(Some(2.0));
4358 let result = handle_overflow::<true>(&dt, op, &lhs, &rhs);
4359 assert_eq!(result, ScalarValue::Float32(Some(f32::MIN)));
4360 let result = handle_overflow::<false>(&dt, op, &lhs, &rhs);
4361 assert_eq!(result, ScalarValue::Float32(None));
4362
4363 let dt = DataType::Int64;
4365 let op = Operator::Minus;
4366 let lhs = ScalarValue::Int64(Some(i64::MIN));
4367 let rhs = ScalarValue::Int64(Some(1));
4368 let result = handle_overflow::<true>(&dt, op, &lhs, &rhs);
4369 assert_eq!(result, ScalarValue::Int64(Some(i64::MIN)));
4370 let result = handle_overflow::<false>(&dt, op, &lhs, &rhs);
4371 assert_eq!(result, ScalarValue::Int64(None));
4372
4373 let dt = DataType::UInt32;
4375 let op = Operator::Minus;
4376 let lhs = ScalarValue::UInt32(Some(0));
4377 let rhs = ScalarValue::UInt32(Some(1));
4378 let result = handle_overflow::<true>(&dt, op, &lhs, &rhs);
4379 assert_eq!(result, ScalarValue::UInt32(Some(0)));
4380 let result = handle_overflow::<false>(&dt, op, &lhs, &rhs);
4381 assert_eq!(result, ScalarValue::UInt32(None));
4382
4383 let dt = DataType::Decimal128(38, 35);
4385 let op = Operator::Plus;
4386 let lhs =
4387 ScalarValue::Decimal128(Some(54321543215432154321543215432154321), 35, 35);
4388 let rhs = ScalarValue::Decimal128(Some(10000), 20, 0);
4389 let result = handle_overflow::<true>(&dt, op, &lhs, &rhs);
4390 assert_eq!(result, ScalarValue::Decimal128(None, 38, 35));
4391 let result = handle_overflow::<false>(&dt, op, &lhs, &rhs);
4392 assert_eq!(
4393 result,
4394 ScalarValue::Decimal128(Some(99999999999999999999999999999999999999), 38, 35)
4395 );
4396
4397 Ok(())
4398 }
4399
4400 #[test]
4401 fn test_width_of_intervals() -> Result<()> {
4402 let intervals = [
4403 (
4404 Interval::make(Some(0.25_f64), Some(0.50_f64))?,
4405 ScalarValue::from(0.25_f64),
4406 ),
4407 (
4408 Interval::make(Some(0.5_f64), Some(1.0_f64))?,
4409 ScalarValue::from(0.5_f64),
4410 ),
4411 (
4412 Interval::make(Some(1.0_f64), Some(2.0_f64))?,
4413 ScalarValue::from(1.0_f64),
4414 ),
4415 (
4416 Interval::make(Some(32.0_f64), Some(64.0_f64))?,
4417 ScalarValue::from(32.0_f64),
4418 ),
4419 (
4420 Interval::make(Some(-0.50_f64), Some(-0.25_f64))?,
4421 ScalarValue::from(0.25_f64),
4422 ),
4423 (
4424 Interval::make(Some(-32.0_f64), Some(-16.0_f64))?,
4425 ScalarValue::from(16.0_f64),
4426 ),
4427 (
4428 Interval::make(Some(-0.50_f64), Some(0.25_f64))?,
4429 ScalarValue::from(0.75_f64),
4430 ),
4431 (
4432 Interval::make(Some(-32.0_f64), Some(16.0_f64))?,
4433 ScalarValue::from(48.0_f64),
4434 ),
4435 (
4436 Interval::make(Some(-32_i64), Some(16_i64))?,
4437 ScalarValue::from(48_i64),
4438 ),
4439 ];
4440 for (interval, expected) in intervals {
4441 assert_eq!(interval.width()?, expected);
4442 }
4443
4444 Ok(())
4445 }
4446
4447 #[test]
4448 fn test_cardinality_of_intervals() -> Result<()> {
4449 let distinct_f64 = 4503599627370497;
4454 let distinct_f32 = 8388609;
4455 let intervals = [
4456 Interval::make(Some(0.25_f64), Some(0.50_f64))?,
4457 Interval::make(Some(0.5_f64), Some(1.0_f64))?,
4458 Interval::make(Some(1.0_f64), Some(2.0_f64))?,
4459 Interval::make(Some(32.0_f64), Some(64.0_f64))?,
4460 Interval::make(Some(-0.50_f64), Some(-0.25_f64))?,
4461 Interval::make(Some(-32.0_f64), Some(-16.0_f64))?,
4462 ];
4463 for interval in intervals {
4464 assert_eq!(interval.cardinality().unwrap(), distinct_f64);
4465 }
4466
4467 let intervals = [
4468 Interval::make(Some(0.25_f32), Some(0.50_f32))?,
4469 Interval::make(Some(-1_f32), Some(-0.5_f32))?,
4470 ];
4471 for interval in intervals {
4472 assert_eq!(interval.cardinality().unwrap(), distinct_f32);
4473 }
4474
4475 let interval = Interval::make(Some(-0.0625), Some(0.0625))?;
4482 assert_eq!(interval.cardinality().unwrap(), 9178336040581070850);
4483
4484 let interval = Interval::try_new(
4485 ScalarValue::UInt64(Some(1)),
4486 ScalarValue::UInt64(Some(u64::MAX)),
4487 )?;
4488 assert_eq!(interval.cardinality().unwrap(), u64::MAX);
4489
4490 let interval = Interval::try_new(
4491 ScalarValue::Int64(Some(i64::MIN + 1)),
4492 ScalarValue::Int64(Some(i64::MAX)),
4493 )?;
4494 assert_eq!(interval.cardinality().unwrap(), u64::MAX);
4495
4496 let interval = Interval::try_new(
4497 ScalarValue::Float32(Some(-0.0_f32)),
4498 ScalarValue::Float32(Some(0.0_f32)),
4499 )?;
4500 assert_eq!(interval.cardinality().unwrap(), 2);
4501
4502 let interval = Interval::try_new(
4504 ScalarValue::Date32(Some(0)),
4505 ScalarValue::Date32(Some(10)),
4506 )?;
4507 assert_eq!(interval.cardinality().unwrap(), 11);
4508
4509 let interval = Interval::try_new(
4510 ScalarValue::Date64(Some(1000)),
4511 ScalarValue::Date64(Some(5000)),
4512 )?;
4513 assert_eq!(interval.cardinality().unwrap(), 4001);
4514
4515 let interval = Interval::try_new(
4516 ScalarValue::TimestampSecond(Some(100), None),
4517 ScalarValue::TimestampSecond(Some(200), None),
4518 )?;
4519 assert_eq!(interval.cardinality().unwrap(), 101);
4520
4521 let interval = Interval::try_new(
4522 ScalarValue::TimestampNanosecond(Some(1_000_000_000), None),
4523 ScalarValue::TimestampNanosecond(Some(2_000_000_000), None),
4524 )?;
4525 assert_eq!(interval.cardinality().unwrap(), 1_000_000_001);
4526
4527 let interval = Interval::try_new(
4529 ScalarValue::Decimal128(Some(100), 10, 2),
4530 ScalarValue::Decimal128(Some(110), 10, 2),
4531 )?;
4532 assert_eq!(interval.cardinality().unwrap(), 11);
4533 Ok(())
4534 }
4535
4536 #[test]
4537 fn test_cardinality_full_integer_range_does_not_overflow() -> Result<()> {
4538 let interval = Interval::try_new(
4539 ScalarValue::Int64(Some(i64::MIN)),
4540 ScalarValue::Int64(Some(i64::MAX)),
4541 )?;
4542 assert_eq!(interval.cardinality(), None);
4543
4544 let interval = Interval::try_new(
4545 ScalarValue::UInt64(Some(0)),
4546 ScalarValue::UInt64(Some(u64::MAX)),
4547 )?;
4548 assert_eq!(interval.cardinality(), None);
4549 Ok(())
4550 }
4551
4552 #[test]
4553 fn test_satisfy_comparison() -> Result<()> {
4554 let cases = vec![
4555 (
4556 Interval::make(Some(1000_i64), None)?,
4557 Interval::make(None, Some(1000_i64))?,
4558 true,
4559 Interval::make(Some(1000_i64), None)?,
4560 Interval::make(None, Some(1000_i64))?,
4561 ),
4562 (
4563 Interval::make(None, Some(1000_i64))?,
4564 Interval::make(Some(1000_i64), None)?,
4565 true,
4566 Interval::make(Some(1000_i64), Some(1000_i64))?,
4567 Interval::make(Some(1000_i64), Some(1000_i64))?,
4568 ),
4569 (
4570 Interval::make(Some(1000_i64), None)?,
4571 Interval::make(None, Some(1000_i64))?,
4572 false,
4573 Interval::make(Some(1000_i64), None)?,
4574 Interval::make(None, Some(1000_i64))?,
4575 ),
4576 (
4577 Interval::make(Some(0_i64), Some(1000_i64))?,
4578 Interval::make(Some(500_i64), Some(1500_i64))?,
4579 true,
4580 Interval::make(Some(500_i64), Some(1000_i64))?,
4581 Interval::make(Some(500_i64), Some(1000_i64))?,
4582 ),
4583 (
4584 Interval::make(Some(500_i64), Some(1500_i64))?,
4585 Interval::make(Some(0_i64), Some(1000_i64))?,
4586 true,
4587 Interval::make(Some(500_i64), Some(1500_i64))?,
4588 Interval::make(Some(0_i64), Some(1000_i64))?,
4589 ),
4590 (
4591 Interval::make(Some(0_i64), Some(1000_i64))?,
4592 Interval::make(Some(500_i64), Some(1500_i64))?,
4593 false,
4594 Interval::make(Some(501_i64), Some(1000_i64))?,
4595 Interval::make(Some(500_i64), Some(999_i64))?,
4596 ),
4597 (
4598 Interval::make(Some(500_i64), Some(1500_i64))?,
4599 Interval::make(Some(0_i64), Some(1000_i64))?,
4600 false,
4601 Interval::make(Some(500_i64), Some(1500_i64))?,
4602 Interval::make(Some(0_i64), Some(1000_i64))?,
4603 ),
4604 (
4605 Interval::make::<i64>(None, None)?,
4606 Interval::make(Some(1_i64), Some(1_i64))?,
4607 false,
4608 Interval::make(Some(2_i64), None)?,
4609 Interval::make(Some(1_i64), Some(1_i64))?,
4610 ),
4611 (
4612 Interval::make::<i64>(None, None)?,
4613 Interval::make(Some(1_i64), Some(1_i64))?,
4614 true,
4615 Interval::make(Some(1_i64), None)?,
4616 Interval::make(Some(1_i64), Some(1_i64))?,
4617 ),
4618 (
4619 Interval::make(Some(1_i64), Some(1_i64))?,
4620 Interval::make::<i64>(None, None)?,
4621 false,
4622 Interval::make(Some(1_i64), Some(1_i64))?,
4623 Interval::make(None, Some(0_i64))?,
4624 ),
4625 (
4626 Interval::make(Some(1_i64), Some(1_i64))?,
4627 Interval::make::<i64>(None, None)?,
4628 true,
4629 Interval::make(Some(1_i64), Some(1_i64))?,
4630 Interval::make(None, Some(1_i64))?,
4631 ),
4632 (
4633 Interval::make(Some(1_i64), Some(1_i64))?,
4634 Interval::make::<i64>(None, None)?,
4635 false,
4636 Interval::make(Some(1_i64), Some(1_i64))?,
4637 Interval::make(None, Some(0_i64))?,
4638 ),
4639 (
4640 Interval::make(Some(1_i64), Some(1_i64))?,
4641 Interval::make::<i64>(None, None)?,
4642 true,
4643 Interval::make(Some(1_i64), Some(1_i64))?,
4644 Interval::make(None, Some(1_i64))?,
4645 ),
4646 (
4647 Interval::make::<i64>(None, None)?,
4648 Interval::make(Some(1_i64), Some(1_i64))?,
4649 false,
4650 Interval::make(Some(2_i64), None)?,
4651 Interval::make(Some(1_i64), Some(1_i64))?,
4652 ),
4653 (
4654 Interval::make::<i64>(None, None)?,
4655 Interval::make(Some(1_i64), Some(1_i64))?,
4656 true,
4657 Interval::make(Some(1_i64), None)?,
4658 Interval::make(Some(1_i64), Some(1_i64))?,
4659 ),
4660 (
4661 Interval::make(Some(-1000.0_f32), Some(1000.0_f32))?,
4662 Interval::make(Some(-500.0_f32), Some(500.0_f32))?,
4663 false,
4664 Interval::try_new(
4665 next_value(ScalarValue::Float32(Some(-500.0))),
4666 ScalarValue::Float32(Some(1000.0)),
4667 )?,
4668 Interval::make(Some(-500_f32), Some(500.0_f32))?,
4669 ),
4670 (
4671 Interval::make(Some(-500.0_f32), Some(500.0_f32))?,
4672 Interval::make(Some(-1000.0_f32), Some(1000.0_f32))?,
4673 true,
4674 Interval::make(Some(-500.0_f32), Some(500.0_f32))?,
4675 Interval::make(Some(-1000.0_f32), Some(500.0_f32))?,
4676 ),
4677 (
4678 Interval::make(Some(-500.0_f32), Some(500.0_f32))?,
4679 Interval::make(Some(-1000.0_f32), Some(1000.0_f32))?,
4680 false,
4681 Interval::make(Some(-500.0_f32), Some(500.0_f32))?,
4682 Interval::try_new(
4683 ScalarValue::Float32(Some(-1000.0_f32)),
4684 prev_value(ScalarValue::Float32(Some(500.0_f32))),
4685 )?,
4686 ),
4687 (
4688 Interval::make(Some(-1000.0_f64), Some(1000.0_f64))?,
4689 Interval::make(Some(-500.0_f64), Some(500.0_f64))?,
4690 true,
4691 Interval::make(Some(-500.0_f64), Some(1000.0_f64))?,
4692 Interval::make(Some(-500.0_f64), Some(500.0_f64))?,
4693 ),
4694 (
4695 Interval::make(Some(0_i64), Some(0_i64))?,
4696 Interval::make(Some(-0_i64), Some(0_i64))?,
4697 true,
4698 Interval::make(Some(0_i64), Some(0_i64))?,
4699 Interval::make(Some(-0_i64), Some(0_i64))?,
4700 ),
4701 (
4702 Interval::make(Some(-0_i64), Some(0_i64))?,
4703 Interval::make(Some(-0_i64), Some(-0_i64))?,
4704 true,
4705 Interval::make(Some(-0_i64), Some(0_i64))?,
4706 Interval::make(Some(-0_i64), Some(-0_i64))?,
4707 ),
4708 (
4709 Interval::make(Some(0.0_f64), Some(0.0_f64))?,
4710 Interval::make(Some(-0.0_f64), Some(0.0_f64))?,
4711 true,
4712 Interval::make(Some(0.0_f64), Some(0.0_f64))?,
4713 Interval::make(Some(-0.0_f64), Some(0.0_f64))?,
4714 ),
4715 (
4716 Interval::make(Some(0.0_f64), Some(0.0_f64))?,
4717 Interval::make(Some(-0.0_f64), Some(0.0_f64))?,
4718 false,
4719 Interval::make(Some(0.0_f64), Some(0.0_f64))?,
4720 Interval::make(Some(-0.0_f64), Some(-0.0_f64))?,
4721 ),
4722 (
4723 Interval::make(Some(-0.0_f64), Some(0.0_f64))?,
4724 Interval::make(Some(-0.0_f64), Some(-0.0_f64))?,
4725 true,
4726 Interval::make(Some(-0.0_f64), Some(0.0_f64))?,
4727 Interval::make(Some(-0.0_f64), Some(-0.0_f64))?,
4728 ),
4729 (
4730 Interval::make(Some(-0.0_f64), Some(0.0_f64))?,
4731 Interval::make(Some(-0.0_f64), Some(-0.0_f64))?,
4732 false,
4733 Interval::make(Some(0.0_f64), Some(0.0_f64))?,
4734 Interval::make(Some(-0.0_f64), Some(-0.0_f64))?,
4735 ),
4736 (
4737 Interval::make(Some(0_i64), None)?,
4738 Interval::make(Some(-0_i64), None)?,
4739 true,
4740 Interval::make(Some(0_i64), None)?,
4741 Interval::make(Some(-0_i64), None)?,
4742 ),
4743 (
4744 Interval::make(Some(0_i64), None)?,
4745 Interval::make(Some(-0_i64), None)?,
4746 false,
4747 Interval::make(Some(1_i64), None)?,
4748 Interval::make(Some(-0_i64), None)?,
4749 ),
4750 (
4751 Interval::make(Some(0.0_f64), None)?,
4752 Interval::make(Some(-0.0_f64), None)?,
4753 true,
4754 Interval::make(Some(0.0_f64), None)?,
4755 Interval::make(Some(-0.0_f64), None)?,
4756 ),
4757 (
4758 Interval::make(Some(0.0_f64), None)?,
4759 Interval::make(Some(-0.0_f64), None)?,
4760 false,
4761 Interval::make(Some(0.0_f64), None)?,
4762 Interval::make(Some(-0.0_f64), None)?,
4763 ),
4764 ];
4765 for (first, second, includes_endpoints, left_modified, right_modified) in cases {
4766 assert_eq!(
4767 satisfy_greater(&first, &second, !includes_endpoints)?.unwrap(),
4768 (left_modified, right_modified)
4769 );
4770 }
4771
4772 let infeasible_cases = vec![
4773 (
4774 Interval::make(None, Some(1000_i64))?,
4775 Interval::make(Some(1000_i64), None)?,
4776 false,
4777 ),
4778 (
4779 Interval::make(Some(-1000.0_f32), Some(1000.0_f32))?,
4780 Interval::make(Some(1500.0_f32), Some(2000.0_f32))?,
4781 false,
4782 ),
4783 (
4784 Interval::make(Some(0_i64), Some(0_i64))?,
4785 Interval::make(Some(-0_i64), Some(0_i64))?,
4786 false,
4787 ),
4788 (
4789 Interval::make(Some(-0_i64), Some(0_i64))?,
4790 Interval::make(Some(-0_i64), Some(-0_i64))?,
4791 false,
4792 ),
4793 ];
4794 for (first, second, includes_endpoints) in infeasible_cases {
4795 assert_eq!(satisfy_greater(&first, &second, !includes_endpoints)?, None);
4796 }
4797
4798 Ok(())
4799 }
4800
4801 #[test]
4802 fn test_interval_display() {
4803 let interval = Interval::make(Some(0.25_f32), Some(0.50_f32)).unwrap();
4804 assert_eq!(format!("{interval}"), "[0.25, 0.5]");
4805
4806 let interval = Interval::try_new(
4807 ScalarValue::Float32(Some(f32::NEG_INFINITY)),
4808 ScalarValue::Float32(Some(f32::INFINITY)),
4809 )
4810 .unwrap();
4811 assert_eq!(format!("{interval}"), "[NULL, NULL]");
4812 }
4813
4814 macro_rules! capture_mode_change {
4815 ($TYPE:ty, $TEST_FN_NAME:ident, $CREATE_FN_NAME:ident) => {
4816 capture_mode_change_helper!($TEST_FN_NAME, $CREATE_FN_NAME, $TYPE);
4817 };
4818 }
4819
4820 macro_rules! capture_mode_change_helper {
4821 ($TEST_FN_NAME:ident, $CREATE_FN_NAME:ident, $TYPE:ty) => {
4822 fn $CREATE_FN_NAME(lower: $TYPE, upper: $TYPE) -> Interval {
4823 Interval::try_new(
4824 ScalarValue::try_from(Some(lower as $TYPE)).unwrap(),
4825 ScalarValue::try_from(Some(upper as $TYPE)).unwrap(),
4826 )
4827 .unwrap()
4828 }
4829
4830 fn $TEST_FN_NAME(input: ($TYPE, $TYPE), expect_low: bool, expect_high: bool) {
4831 assert!(expect_low || expect_high);
4832 let interval1 = $CREATE_FN_NAME(input.0, input.0);
4833 let interval2 = $CREATE_FN_NAME(input.1, input.1);
4834 let result = interval1.add(&interval2).unwrap();
4835 let without_fe = $CREATE_FN_NAME(input.0 + input.1, input.0 + input.1);
4836 assert!(
4837 (!expect_low || result.lower < without_fe.lower)
4838 && (!expect_high || result.upper > without_fe.upper)
4839 );
4840 }
4841 };
4842 }
4843
4844 capture_mode_change!(f32, capture_mode_change_f32, create_interval_f32);
4845 capture_mode_change!(f64, capture_mode_change_f64, create_interval_f64);
4846
4847 #[cfg(all(
4848 any(target_arch = "x86_64", target_arch = "aarch64"),
4849 not(target_os = "windows")
4850 ))]
4851 #[test]
4852 fn test_add_intervals_lower_affected_f32() {
4853 let lower = f32::from_bits(1073741887); let upper = f32::from_bits(1098907651); capture_mode_change_f32((lower, upper), true, false);
4857
4858 let lower = f32::from_bits(1072693248); let upper = f32::from_bits(715827883); capture_mode_change_f32((lower, upper), false, true);
4862
4863 let lower = 1.0; let upper = 0.3; capture_mode_change_f64((lower, upper), true, false);
4867
4868 let lower = 1.4999999999999998; let upper = 0.000_000_000_000_000_022_044_604_925_031_31; capture_mode_change_f64((lower, upper), false, true);
4872 }
4873
4874 #[cfg(any(
4875 not(any(target_arch = "x86_64", target_arch = "aarch64")),
4876 target_os = "windows"
4877 ))]
4878 #[test]
4879 fn test_next_impl_add_intervals_f64() {
4880 let lower = 1.5;
4881 let upper = 1.5;
4882 capture_mode_change_f64((lower, upper), true, true);
4883
4884 let lower = 1.5;
4885 let upper = 1.5;
4886 capture_mode_change_f32((lower, upper), true, true);
4887 }
4888
4889 #[test]
4890 fn test_is_superset() -> Result<()> {
4891 let test_cases = vec![
4893 (
4895 Interval::make(Some(10_i32), Some(50_i32))?,
4896 Interval::make(Some(10_i32), Some(50_i32))?,
4897 false,
4898 true,
4899 ),
4900 (
4901 Interval::make(Some(10_i32), Some(50_i32))?,
4902 Interval::make(Some(10_i32), Some(50_i32))?,
4903 true,
4904 false,
4905 ),
4906 (
4908 Interval::make::<i32>(None, None)?,
4909 Interval::make(Some(10_i32), Some(50_i32))?,
4910 false,
4911 true,
4912 ),
4913 (
4914 Interval::make::<i32>(None, None)?,
4915 Interval::make::<i32>(None, None)?,
4916 false,
4917 true,
4918 ),
4919 (
4920 Interval::make::<i32>(None, None)?,
4921 Interval::make::<i32>(None, None)?,
4922 true,
4923 false,
4924 ),
4925 (
4927 Interval::make(Some(0_i32), None)?,
4928 Interval::make(Some(10_i32), Some(50_i32))?,
4929 false,
4930 true,
4931 ),
4932 (
4933 Interval::make(None, Some(100_i32))?,
4934 Interval::make(Some(10_i32), Some(50_i32))?,
4935 false,
4936 true,
4937 ),
4938 (
4940 Interval::make(Some(0_i32), Some(50_i32))?,
4941 Interval::make(Some(25_i32), Some(75_i32))?,
4942 false,
4943 false,
4944 ),
4945 (
4946 Interval::make(Some(0_i32), Some(50_i32))?,
4947 Interval::make(Some(25_i32), Some(75_i32))?,
4948 true,
4949 false,
4950 ),
4951 (
4953 Interval::make(Some(0_i32), Some(50_i32))?,
4954 Interval::make(Some(60_i32), Some(100_i32))?,
4955 false,
4956 false,
4957 ),
4958 (
4960 Interval::make(Some(20_i32), Some(80_i32))?,
4961 Interval::make(Some(0_i32), Some(100_i32))?,
4962 false,
4963 false,
4964 ),
4965 (
4967 Interval::make(Some(0.0_f32), Some(100.0_f32))?,
4968 Interval::make(Some(25.5_f32), Some(75.5_f32))?,
4969 false,
4970 true,
4971 ),
4972 (
4973 Interval::make(Some(0.0_f64), Some(100.0_f64))?,
4974 Interval::make(Some(0.0_f64), Some(100.0_f64))?,
4975 true,
4976 false,
4977 ),
4978 (
4980 Interval::make(Some(0_i32), Some(100_i32))?,
4981 Interval::make(Some(50_i32), Some(50_i32))?,
4982 false,
4983 true,
4984 ),
4985 (
4986 Interval::make(Some(50_i32), Some(50_i32))?,
4987 Interval::make(Some(50_i32), Some(50_i32))?,
4988 false,
4989 true,
4990 ),
4991 (
4992 Interval::make(Some(50_i32), Some(50_i32))?,
4993 Interval::make(Some(50_i32), Some(50_i32))?,
4994 true,
4995 false,
4996 ),
4997 (
4999 Interval::make(Some(0_i32), Some(50_i32))?,
5000 Interval::make(Some(0_i32), Some(25_i32))?,
5001 false,
5002 true,
5003 ),
5004 (
5005 Interval::make(Some(0_i32), Some(50_i32))?,
5006 Interval::make(Some(25_i32), Some(50_i32))?,
5007 false,
5008 true,
5009 ),
5010 ];
5011
5012 for (interval1, interval2, strict, expected) in test_cases {
5013 let result = interval1.is_superset(&interval2, strict)?;
5014 assert_eq!(
5015 result, expected,
5016 "Failed for interval1: {interval1}, interval2: {interval2}, strict: {strict}",
5017 );
5018 }
5019
5020 Ok(())
5021 }
5022
5023 #[test]
5024 fn nullable_and_test() -> Result<()> {
5025 #[rustfmt::skip]
5027 let cases = vec![
5028 (NullableInterval::TRUE, NullableInterval::TRUE, NullableInterval::TRUE),
5029 (NullableInterval::TRUE, NullableInterval::FALSE, NullableInterval::FALSE),
5030 (NullableInterval::TRUE, NullableInterval::UNKNOWN, NullableInterval::UNKNOWN),
5031 (NullableInterval::TRUE, NullableInterval::TRUE_OR_FALSE, NullableInterval::TRUE_OR_FALSE),
5032 (NullableInterval::TRUE, NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN),
5033 (NullableInterval::TRUE, NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN),
5034 (NullableInterval::TRUE, NullableInterval::ANY_TRUTH_VALUE, NullableInterval::ANY_TRUTH_VALUE),
5035 (NullableInterval::FALSE, NullableInterval::TRUE, NullableInterval::FALSE),
5036 (NullableInterval::FALSE, NullableInterval::FALSE, NullableInterval::FALSE),
5037 (NullableInterval::FALSE, NullableInterval::UNKNOWN, NullableInterval::FALSE),
5038 (NullableInterval::FALSE, NullableInterval::TRUE_OR_FALSE, NullableInterval::FALSE),
5039 (NullableInterval::FALSE, NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::FALSE),
5040 (NullableInterval::FALSE, NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::FALSE),
5041 (NullableInterval::FALSE, NullableInterval::ANY_TRUTH_VALUE, NullableInterval::FALSE),
5042 (NullableInterval::UNKNOWN, NullableInterval::TRUE, NullableInterval::UNKNOWN),
5043 (NullableInterval::UNKNOWN, NullableInterval::FALSE, NullableInterval::FALSE),
5044 (NullableInterval::UNKNOWN, NullableInterval::UNKNOWN, NullableInterval::UNKNOWN),
5045 (NullableInterval::UNKNOWN, NullableInterval::TRUE_OR_FALSE, NullableInterval::FALSE_OR_UNKNOWN),
5046 (NullableInterval::UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::UNKNOWN),
5047 (NullableInterval::UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN),
5048 (NullableInterval::UNKNOWN, NullableInterval::ANY_TRUTH_VALUE, NullableInterval::FALSE_OR_UNKNOWN),
5049 (NullableInterval::ANY_TRUTH_VALUE, NullableInterval::TRUE, NullableInterval::ANY_TRUTH_VALUE),
5050 (NullableInterval::ANY_TRUTH_VALUE, NullableInterval::FALSE, NullableInterval::FALSE),
5051 (NullableInterval::ANY_TRUTH_VALUE, NullableInterval::UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN),
5052 (NullableInterval::ANY_TRUTH_VALUE, NullableInterval::TRUE_OR_FALSE, NullableInterval::ANY_TRUTH_VALUE),
5053 (NullableInterval::ANY_TRUTH_VALUE, NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::ANY_TRUTH_VALUE),
5054 (NullableInterval::ANY_TRUTH_VALUE, NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN),
5055 (NullableInterval::ANY_TRUTH_VALUE, NullableInterval::ANY_TRUTH_VALUE, NullableInterval::ANY_TRUTH_VALUE),
5056 (NullableInterval::TRUE_OR_FALSE, NullableInterval::TRUE, NullableInterval::TRUE_OR_FALSE),
5057 (NullableInterval::TRUE_OR_FALSE, NullableInterval::FALSE, NullableInterval::FALSE),
5058 (NullableInterval::TRUE_OR_FALSE, NullableInterval::UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN),
5059 (NullableInterval::TRUE_OR_FALSE, NullableInterval::TRUE_OR_FALSE, NullableInterval::TRUE_OR_FALSE),
5060 (NullableInterval::TRUE_OR_FALSE, NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::ANY_TRUTH_VALUE),
5061 (NullableInterval::TRUE_OR_FALSE, NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN),
5062 (NullableInterval::TRUE_OR_FALSE, NullableInterval::ANY_TRUTH_VALUE, NullableInterval::ANY_TRUTH_VALUE),
5063 (NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::TRUE, NullableInterval::TRUE_OR_UNKNOWN),
5064 (NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::FALSE, NullableInterval::FALSE),
5065 (NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::UNKNOWN, NullableInterval::UNKNOWN),
5066 (NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::TRUE_OR_FALSE, NullableInterval::ANY_TRUTH_VALUE),
5067 (NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN),
5068 (NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN),
5069 (NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::ANY_TRUTH_VALUE, NullableInterval::ANY_TRUTH_VALUE),
5070 (NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::TRUE, NullableInterval::FALSE_OR_UNKNOWN),
5071 (NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::FALSE, NullableInterval::FALSE),
5072 (NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN),
5073 (NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::TRUE_OR_FALSE, NullableInterval::FALSE_OR_UNKNOWN),
5074 (NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN),
5075 (NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN),
5076 (NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::ANY_TRUTH_VALUE, NullableInterval::FALSE_OR_UNKNOWN),
5077 ];
5078
5079 for case in cases {
5080 assert_eq!(
5081 case.0.apply_operator(&Operator::And, &case.1).unwrap(),
5082 case.2,
5083 "Failed for {} AND {}",
5084 case.0,
5085 case.1
5086 );
5087 }
5088 Ok(())
5089 }
5090
5091 #[test]
5092 fn nullable_or_test() -> Result<()> {
5093 #[rustfmt::skip]
5095 let cases = vec![
5096 (NullableInterval::TRUE, NullableInterval::TRUE, NullableInterval::TRUE),
5097 (NullableInterval::TRUE, NullableInterval::FALSE, NullableInterval::TRUE),
5098 (NullableInterval::TRUE, NullableInterval::UNKNOWN, NullableInterval::TRUE),
5099 (NullableInterval::TRUE, NullableInterval::TRUE_OR_FALSE, NullableInterval::TRUE),
5100 (NullableInterval::TRUE, NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::TRUE),
5101 (NullableInterval::TRUE, NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::TRUE),
5102 (NullableInterval::TRUE, NullableInterval::ANY_TRUTH_VALUE, NullableInterval::TRUE),
5103 (NullableInterval::FALSE, NullableInterval::TRUE, NullableInterval::TRUE),
5104 (NullableInterval::FALSE, NullableInterval::FALSE, NullableInterval::FALSE),
5105 (NullableInterval::FALSE, NullableInterval::UNKNOWN, NullableInterval::UNKNOWN),
5106 (NullableInterval::FALSE, NullableInterval::TRUE_OR_FALSE, NullableInterval::TRUE_OR_FALSE),
5107 (NullableInterval::FALSE, NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN),
5108 (NullableInterval::FALSE, NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN),
5109 (NullableInterval::FALSE, NullableInterval::ANY_TRUTH_VALUE, NullableInterval::ANY_TRUTH_VALUE),
5110 (NullableInterval::UNKNOWN, NullableInterval::TRUE, NullableInterval::TRUE),
5111 (NullableInterval::UNKNOWN, NullableInterval::FALSE, NullableInterval::UNKNOWN),
5112 (NullableInterval::UNKNOWN, NullableInterval::UNKNOWN, NullableInterval::UNKNOWN),
5113 (NullableInterval::UNKNOWN, NullableInterval::TRUE_OR_FALSE, NullableInterval::TRUE_OR_UNKNOWN),
5114 (NullableInterval::UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN),
5115 (NullableInterval::UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::UNKNOWN),
5116 (NullableInterval::UNKNOWN, NullableInterval::ANY_TRUTH_VALUE, NullableInterval::TRUE_OR_UNKNOWN),
5117 (NullableInterval::ANY_TRUTH_VALUE, NullableInterval::TRUE, NullableInterval::TRUE),
5118 (NullableInterval::ANY_TRUTH_VALUE, NullableInterval::FALSE, NullableInterval::ANY_TRUTH_VALUE),
5119 (NullableInterval::ANY_TRUTH_VALUE, NullableInterval::UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN),
5120 (NullableInterval::ANY_TRUTH_VALUE, NullableInterval::TRUE_OR_FALSE, NullableInterval::ANY_TRUTH_VALUE),
5121 (NullableInterval::ANY_TRUTH_VALUE, NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN),
5122 (NullableInterval::ANY_TRUTH_VALUE, NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::ANY_TRUTH_VALUE),
5123 (NullableInterval::ANY_TRUTH_VALUE, NullableInterval::ANY_TRUTH_VALUE, NullableInterval::ANY_TRUTH_VALUE),
5124 (NullableInterval::TRUE_OR_FALSE, NullableInterval::TRUE, NullableInterval::TRUE),
5125 (NullableInterval::TRUE_OR_FALSE, NullableInterval::FALSE, NullableInterval::TRUE_OR_FALSE),
5126 (NullableInterval::TRUE_OR_FALSE, NullableInterval::UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN),
5127 (NullableInterval::TRUE_OR_FALSE, NullableInterval::TRUE_OR_FALSE, NullableInterval::TRUE_OR_FALSE),
5128 (NullableInterval::TRUE_OR_FALSE, NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN),
5129 (NullableInterval::TRUE_OR_FALSE, NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::ANY_TRUTH_VALUE),
5130 (NullableInterval::TRUE_OR_FALSE, NullableInterval::ANY_TRUTH_VALUE, NullableInterval::ANY_TRUTH_VALUE),
5131 (NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::TRUE, NullableInterval::TRUE),
5132 (NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::FALSE, NullableInterval::TRUE_OR_UNKNOWN),
5133 (NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN),
5134 (NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::TRUE_OR_FALSE, NullableInterval::TRUE_OR_UNKNOWN),
5135 (NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN),
5136 (NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN),
5137 (NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::ANY_TRUTH_VALUE, NullableInterval::TRUE_OR_UNKNOWN),
5138 (NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::TRUE, NullableInterval::TRUE),
5139 (NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::FALSE, NullableInterval::FALSE_OR_UNKNOWN),
5140 (NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::UNKNOWN, NullableInterval::UNKNOWN),
5141 (NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::TRUE_OR_FALSE, NullableInterval::ANY_TRUTH_VALUE),
5142 (NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::TRUE_OR_UNKNOWN),
5143 (NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::FALSE_OR_UNKNOWN),
5144 (NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::ANY_TRUTH_VALUE, NullableInterval::ANY_TRUTH_VALUE),
5145 ];
5146
5147 for case in cases {
5148 assert_eq!(
5149 case.0.apply_operator(&Operator::Or, &case.1).unwrap(),
5150 case.2,
5151 "Failed for {} OR {}",
5152 case.0,
5153 case.1
5154 );
5155 }
5156 Ok(())
5157 }
5158
5159 #[test]
5160 fn nullable_not_test() -> Result<()> {
5161 #[rustfmt::skip]
5163 let cases = vec![
5164 (NullableInterval::TRUE, NullableInterval::FALSE),
5165 (NullableInterval::FALSE, NullableInterval::TRUE),
5166 (NullableInterval::UNKNOWN, NullableInterval::UNKNOWN),
5167 (NullableInterval::TRUE_OR_FALSE,NullableInterval::TRUE_OR_FALSE),
5168 (NullableInterval::TRUE_OR_UNKNOWN,NullableInterval::FALSE_OR_UNKNOWN),
5169 (NullableInterval::FALSE_OR_UNKNOWN,NullableInterval::TRUE_OR_UNKNOWN),
5170 (NullableInterval::ANY_TRUTH_VALUE, NullableInterval::ANY_TRUTH_VALUE),
5171 ];
5172
5173 for case in cases {
5174 assert_eq!(case.0.not().unwrap(), case.1, "Failed for NOT {}", case.0);
5175 }
5176 Ok(())
5177 }
5178
5179 #[test]
5180 fn nullable_interval_is_certainly_true() {
5181 #[rustfmt::skip]
5183 let test_cases = vec![
5184 (NullableInterval::TRUE, true),
5185 (NullableInterval::FALSE, false),
5186 (NullableInterval::UNKNOWN, false),
5187 (NullableInterval::TRUE_OR_FALSE, false),
5188 (NullableInterval::TRUE_OR_UNKNOWN, false),
5189 (NullableInterval::FALSE_OR_UNKNOWN, false),
5190 (NullableInterval::ANY_TRUTH_VALUE, false),
5191 ];
5192
5193 for (interval, expected) in test_cases {
5194 let result = interval.is_certainly_true();
5195 assert_eq!(result, expected, "Failed for interval: {interval}");
5196 }
5197 }
5198
5199 #[test]
5200 fn nullable_interval_is_true() {
5201 #[rustfmt::skip]
5203 let test_cases = vec![
5204 (NullableInterval::TRUE, NullableInterval::TRUE),
5205 (NullableInterval::FALSE, NullableInterval::FALSE),
5206 (NullableInterval::UNKNOWN, NullableInterval::FALSE),
5207 (NullableInterval::TRUE_OR_FALSE,NullableInterval::TRUE_OR_FALSE),
5208 (NullableInterval::TRUE_OR_UNKNOWN,NullableInterval::TRUE_OR_FALSE),
5209 (NullableInterval::FALSE_OR_UNKNOWN, NullableInterval::FALSE),
5210 (NullableInterval::ANY_TRUTH_VALUE,NullableInterval::TRUE_OR_FALSE),
5211 ];
5212
5213 for (interval, expected) in test_cases {
5214 let result = interval.is_true().unwrap();
5215 assert_eq!(result, expected, "Failed for interval: {interval}");
5216 }
5217 }
5218
5219 #[test]
5220 fn nullable_interval_is_certainly_false() {
5221 #[rustfmt::skip]
5223 let test_cases = vec![
5224 (NullableInterval::TRUE, false),
5225 (NullableInterval::FALSE, true),
5226 (NullableInterval::UNKNOWN, false),
5227 (NullableInterval::TRUE_OR_FALSE, false),
5228 (NullableInterval::TRUE_OR_UNKNOWN, false),
5229 (NullableInterval::FALSE_OR_UNKNOWN, false),
5230 (NullableInterval::ANY_TRUTH_VALUE, false),
5231 ];
5232
5233 for (interval, expected) in test_cases {
5234 let result = interval.is_certainly_false();
5235 assert_eq!(result, expected, "Failed for interval: {interval}");
5236 }
5237 }
5238
5239 #[test]
5240 fn nullable_interval_is_false() {
5241 #[rustfmt::skip]
5243 let test_cases = vec![
5244 (NullableInterval::TRUE, NullableInterval::FALSE),
5245 (NullableInterval::FALSE, NullableInterval::TRUE),
5246 (NullableInterval::UNKNOWN, NullableInterval::FALSE),
5247 (NullableInterval::TRUE_OR_FALSE,NullableInterval::TRUE_OR_FALSE),
5248 (NullableInterval::TRUE_OR_UNKNOWN, NullableInterval::FALSE),
5249 (NullableInterval::FALSE_OR_UNKNOWN,NullableInterval::TRUE_OR_FALSE),
5250 (NullableInterval::ANY_TRUTH_VALUE,NullableInterval::TRUE_OR_FALSE),
5251 ];
5252
5253 for (interval, expected) in test_cases {
5254 let result = interval.is_false().unwrap();
5255 assert_eq!(result, expected, "Failed for interval: {interval}");
5256 }
5257 }
5258
5259 #[test]
5260 fn nullable_interval_is_certainly_unknown() {
5261 #[rustfmt::skip]
5263 let test_cases = vec![
5264 (NullableInterval::TRUE, false),
5265 (NullableInterval::FALSE, false),
5266 (NullableInterval::UNKNOWN, true),
5267 (NullableInterval::TRUE_OR_FALSE, false),
5268 (NullableInterval::TRUE_OR_UNKNOWN, false),
5269 (NullableInterval::FALSE_OR_UNKNOWN, false),
5270 (NullableInterval::ANY_TRUTH_VALUE, false),
5271 ];
5272
5273 for (interval, expected) in test_cases {
5274 let result = interval.is_certainly_unknown();
5275 assert_eq!(result, expected, "Failed for interval: {interval}");
5276 }
5277 }
5278
5279 #[test]
5280 fn nullable_interval_is_unknown() {
5281 #[rustfmt::skip]
5283 let test_cases = vec![
5284 (NullableInterval::TRUE, NullableInterval::FALSE),
5285 (NullableInterval::FALSE, NullableInterval::FALSE),
5286 (NullableInterval::UNKNOWN, NullableInterval::TRUE),
5287 (NullableInterval::TRUE_OR_FALSE, NullableInterval::FALSE),
5288 (NullableInterval::TRUE_OR_UNKNOWN,NullableInterval::TRUE_OR_FALSE),
5289 (NullableInterval::FALSE_OR_UNKNOWN,NullableInterval::TRUE_OR_FALSE),
5290 (NullableInterval::ANY_TRUTH_VALUE,NullableInterval::TRUE_OR_FALSE),
5291 ];
5292
5293 for (interval, expected) in test_cases {
5294 let result = interval.is_unknown().unwrap();
5295 assert_eq!(result, expected, "Failed for interval: {interval}");
5296 }
5297 }
5298
5299 #[test]
5300 fn nullable_interval_contains_value() {
5301 #[rustfmt::skip]
5303 let test_cases = vec![
5304 (NullableInterval::TRUE, ScalarValue::Boolean(Some(true)), true),
5305 (NullableInterval::TRUE, ScalarValue::Boolean(Some(false)), false),
5306 (NullableInterval::TRUE, ScalarValue::Boolean(None), false),
5307 (NullableInterval::TRUE, ScalarValue::Null, false),
5308 (NullableInterval::TRUE, ScalarValue::UInt32(None), false),
5309 (NullableInterval::FALSE, ScalarValue::Boolean(Some(true)), false),
5310 (NullableInterval::FALSE, ScalarValue::Boolean(Some(false)), true),
5311 (NullableInterval::FALSE, ScalarValue::Boolean(None), false),
5312 (NullableInterval::FALSE, ScalarValue::Null, false),
5313 (NullableInterval::FALSE, ScalarValue::UInt32(None), false),
5314 (NullableInterval::UNKNOWN, ScalarValue::Boolean(Some(true)), false),
5315 (NullableInterval::UNKNOWN, ScalarValue::Boolean(Some(false)), false),
5316 (NullableInterval::UNKNOWN, ScalarValue::Boolean(None), true),
5317 (NullableInterval::UNKNOWN, ScalarValue::Null, true),
5318 (NullableInterval::UNKNOWN, ScalarValue::UInt32(None), false),
5319 (NullableInterval::TRUE_OR_FALSE, ScalarValue::Boolean(Some(true)), true),
5320 (NullableInterval::TRUE_OR_FALSE, ScalarValue::Boolean(Some(false)), true),
5321 (NullableInterval::TRUE_OR_FALSE, ScalarValue::Boolean(None), false),
5322 (NullableInterval::TRUE_OR_FALSE, ScalarValue::Null, false),
5323 (NullableInterval::TRUE_OR_FALSE, ScalarValue::UInt32(None), false),
5324 (NullableInterval::TRUE_OR_UNKNOWN, ScalarValue::Boolean(Some(true)), true),
5325 (NullableInterval::TRUE_OR_UNKNOWN, ScalarValue::Boolean(Some(false)), false),
5326 (NullableInterval::TRUE_OR_UNKNOWN, ScalarValue::Boolean(None), true),
5327 (NullableInterval::TRUE_OR_UNKNOWN, ScalarValue::Null, true),
5328 (NullableInterval::TRUE_OR_UNKNOWN, ScalarValue::UInt32(None), false),
5329 (NullableInterval::FALSE_OR_UNKNOWN, ScalarValue::Boolean(Some(true)), false),
5330 (NullableInterval::FALSE_OR_UNKNOWN, ScalarValue::Boolean(Some(false)), true),
5331 (NullableInterval::FALSE_OR_UNKNOWN, ScalarValue::Boolean(None), true),
5332 (NullableInterval::FALSE_OR_UNKNOWN, ScalarValue::Null, true),
5333 (NullableInterval::FALSE_OR_UNKNOWN, ScalarValue::UInt32(None), false),
5334 (NullableInterval::ANY_TRUTH_VALUE, ScalarValue::Boolean(Some(true)), true),
5335 (NullableInterval::ANY_TRUTH_VALUE, ScalarValue::Boolean(Some(false)), true),
5336 (NullableInterval::ANY_TRUTH_VALUE, ScalarValue::Boolean(None), true),
5337 (NullableInterval::ANY_TRUTH_VALUE, ScalarValue::Null, true),
5338 (NullableInterval::ANY_TRUTH_VALUE, ScalarValue::UInt32(None), false),
5339 ];
5340
5341 for (interval, value, expected) in test_cases {
5342 let result = interval.contains_value(value.clone()).unwrap();
5343 assert_eq!(
5344 result, expected,
5345 "Failed for interval: {interval} and value {value:?}",
5346 );
5347 }
5348 }
5349}