datafusion_expr_common::interval_arithmetic

Struct Interval

Source
pub struct Interval { /* private fields */ }
Expand description

The Interval type represents a closed interval used for computing reliable bounds for mathematical expressions.

Conventions:

  1. Closed bounds: The interval always encompasses its endpoints. We accommodate operations resulting in open intervals by incrementing or decrementing the interval endpoint value to its successor/predecessor.

  2. Unbounded endpoints: If the lower or upper bounds are indeterminate, they are labeled as unbounded. This is represented using a NULL.

  3. Overflow handling: If the lower or upper endpoints exceed their limits after any operation, they either become unbounded or they are fixed to the maximum/minimum value of the datatype, depending on the direction of the overflowing endpoint, opting for the safer choice.

  4. Floating-point special cases:

    • INF values are converted to NULLs while constructing an interval to ensure consistency, with other data types.
    • NaN (Not a Number) results are conservatively result in unbounded endpoints.

Implementations§

Source§

impl Interval

Source

pub const CERTAINLY_FALSE: Self = _

Source

pub const UNCERTAIN: Self = _

Source

pub const CERTAINLY_TRUE: Self = _

Source

pub fn try_new(lower: ScalarValue, upper: ScalarValue) -> Result<Self>

Attempts to create a new Interval from the given lower and upper bounds.

§Notes

This constructor creates intervals in a “canonical” form where:

  • Boolean intervals:
    • Unboundedness (NULL) for boolean endpoints is converted to false for lower and true for upper bounds.
  • Floating-point intervals:
    • Floating-point endpoints with NaN, INF, or NEG_INF are converted to NULLs.
Source

pub fn make<T>(lower: Option<T>, upper: Option<T>) -> Result<Self>
where ScalarValue: From<Option<T>>,

Convenience function to create a new Interval from the given (optional) bounds, for use in tests only. Absence of either endpoint indicates unboundedness on that side. See Interval::try_new for more information.

Source

pub fn make_zero(data_type: &DataType) -> Result<Self>

Creates a singleton zero interval if the datatype supported.

Source

pub fn make_unbounded(data_type: &DataType) -> Result<Self>

Creates an unbounded interval from both sides if the datatype supported.

Source

pub fn make_symmetric_unit_interval(data_type: &DataType) -> Result<Self>

Creates an interval between -1 to 1.

Source

pub fn make_symmetric_pi_interval(data_type: &DataType) -> Result<Self>

Create an interval from -π to π.

Source

pub fn make_symmetric_half_pi_interval(data_type: &DataType) -> Result<Self>

Create an interval from -π/2 to π/2.

Source

pub fn make_non_negative_infinity_interval(data_type: &DataType) -> Result<Self>

Create an interval from 0 to infinity.

Source

pub fn lower(&self) -> &ScalarValue

Returns a reference to the lower bound.

Source

pub fn upper(&self) -> &ScalarValue

Returns a reference to the upper bound.

Source

pub fn into_bounds(self) -> (ScalarValue, ScalarValue)

Converts this Interval into its boundary scalar values. It’s useful when you need to work with the individual bounds directly.

Source

pub fn data_type(&self) -> DataType

This function returns the data type of this interval.

Source

pub fn cast_to( &self, data_type: &DataType, cast_options: &CastOptions<'_>, ) -> Result<Self>

Casts this interval to data_type using cast_options.

Source

pub fn gt<T: Borrow<Self>>(&self, other: T) -> Result<Self>

Decide if this interval is certainly greater than, possibly greater than, or can’t be greater than other by returning [true, true], [false, true] or [false, false] respectively.

NOTE: This function only works with intervals of the same data type. Attempting to compare intervals of different data types will lead to an error.

Source

pub fn gt_eq<T: Borrow<Self>>(&self, other: T) -> Result<Self>

Decide if this interval is certainly greater than or equal to, possibly greater than or equal to, or can’t be greater than or equal to other by returning [true, true], [false, true] or [false, false] respectively.

NOTE: This function only works with intervals of the same data type. Attempting to compare intervals of different data types will lead to an error.

Source

pub fn lt<T: Borrow<Self>>(&self, other: T) -> Result<Self>

Decide if this interval is certainly less than, possibly less than, or can’t be less than other by returning [true, true], [false, true] or [false, false] respectively.

NOTE: This function only works with intervals of the same data type. Attempting to compare intervals of different data types will lead to an error.

Source

pub fn lt_eq<T: Borrow<Self>>(&self, other: T) -> Result<Self>

Decide if this interval is certainly less than or equal to, possibly less than or equal to, or can’t be less than or equal to other by returning [true, true], [false, true] or [false, false] respectively.

NOTE: This function only works with intervals of the same data type. Attempting to compare intervals of different data types will lead to an error.

Source

pub fn equal<T: Borrow<Self>>(&self, other: T) -> Result<Self>

Decide if this interval is certainly equal to, possibly equal to, or can’t be equal to other by returning [true, true], [false, true] or [false, false] respectively.

NOTE: This function only works with intervals of the same data type. Attempting to compare intervals of different data types will lead to an error.

Source

pub fn and<T: Borrow<Self>>(&self, other: T) -> Result<Self>

Compute the logical conjunction of this (boolean) interval with the given boolean interval.

Source

pub fn or<T: Borrow<Self>>(&self, other: T) -> Result<Self>

Compute the logical disjunction of this boolean interval with the given boolean interval.

Source

pub fn not(&self) -> Result<Self>

Compute the logical negation of this (boolean) interval.

Source

pub fn intersect<T: Borrow<Self>>(&self, other: T) -> Result<Option<Self>>

Compute the intersection of this interval with the given interval. If the intersection is empty, return None.

NOTE: This function only works with intervals of the same data type. Attempting to compare intervals of different data types will lead to an error.

Source

pub fn contains_value<T: Borrow<ScalarValue>>(&self, other: T) -> Result<bool>

Decide if this interval certainly contains, possibly contains, or can’t contain a ScalarValue (other) by returning [true, true], [false, true] or [false, false] respectively.

NOTE: This function only works with intervals of the same data type. Attempting to compare intervals of different data types will lead to an error.

Source

pub fn contains<T: Borrow<Self>>(&self, other: T) -> Result<Self>

Decide if this interval is a superset of, overlaps with, or disjoint with other by returning [true, true], [false, true] or [false, false] respectively.

NOTE: This function only works with intervals of the same data type. Attempting to compare intervals of different data types will lead to an error.

Source

pub fn add<T: Borrow<Self>>(&self, other: T) -> Result<Self>

Add the given interval (other) to this interval. Say we have intervals [a1, b1] and [a2, b2], then their sum is [a1 + a2, b1 + b2]. Note that this represents all possible values the sum can take if one can choose single values arbitrarily from each of the operands.

Source

pub fn sub<T: Borrow<Interval>>(&self, other: T) -> Result<Self>

Subtract the given interval (other) from this interval. Say we have intervals [a1, b1] and [a2, b2], then their difference is [a1 - b2, b1 - a2]. Note that this represents all possible values the difference can take if one can choose single values arbitrarily from each of the operands.

Source

pub fn mul<T: Borrow<Self>>(&self, other: T) -> Result<Self>

Multiply the given interval (other) with this interval. Say we have intervals [a1, b1] and [a2, b2], then their product is [min(a1 * a2, a1 * b2, b1 * a2, b1 * b2), max(a1 * a2, a1 * b2, b1 * a2, b1 * b2)]. Note that this represents all possible values the product can take if one can choose single values arbitrarily from each of the operands.

NOTE: This function only works with intervals of the same data type. Attempting to compare intervals of different data types will lead to an error.

Source

pub fn div<T: Borrow<Self>>(&self, other: T) -> Result<Self>

Divide this interval by the given interval (other). Say we have intervals [a1, b1] and [a2, b2], then their division is [a1, b1] * [1 / b2, 1 / a2] if 0 ∉ [a2, b2] and [NEG_INF, INF] otherwise. Note that this represents all possible values the quotient can take if one can choose single values arbitrarily from each of the operands.

NOTE: This function only works with intervals of the same data type. Attempting to compare intervals of different data types will lead to an error.

TODO: Once interval sets are supported, cases where the divisor contains zero should result in an interval set, not the universal set.

Source

pub fn cardinality(&self) -> Option<u64>

Returns the cardinality of this interval, which is the number of all distinct points inside it. This function returns None if:

  • The interval is unbounded from either side, or
  • Cardinality calculations for the datatype in question is not implemented yet, or
  • An overflow occurs during the calculation: This case can only arise when the calculated cardinality does not fit in an u64.
Source

pub fn arithmetic_negate(self) -> Result<Self>

Reflects an Interval around the point zero.

This method computes the arithmetic negation of the interval, reflecting it about the origin of the number line. This operation swaps and negates the lower and upper bounds of the interval.

Trait Implementations§

Source§

impl Clone for Interval

Source§

fn clone(&self) -> Interval

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Interval

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Interval

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Interval

Source§

fn eq(&self, other: &Interval) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Interval

Source§

impl StructuralPartialEq for Interval

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.