diff --git a/datafusion/common/src/stats.rs b/datafusion/common/src/stats.rs index d2ce965c5c49..77f7c7f66d0e 100644 --- a/datafusion/common/src/stats.rs +++ b/datafusion/common/src/stats.rs @@ -31,6 +31,14 @@ pub enum Precision { Exact(T), /// The value is not known exactly, but is likely close to this value Inexact(T), + /// The value is know to be at most (inclusive) of this value. + /// + /// The actual value may be smaller, but it is never greater. + AtMost(T), + /// The value is known to be at least (inclusive) of this value. + /// + /// The actual value may be greater but it is never smaller. + AtLeast(T), /// Nothing is known about the value #[default] Absent, @@ -41,7 +49,40 @@ impl Precision { /// Otherwise, it returns `None`. pub fn get_value(&self) -> Option<&T> { match self { - Precision::Exact(value) | Precision::Inexact(value) => Some(value), + Precision::Exact(value) + | Precision::Inexact(value) + | Precision::AtLeast(value) + | Precision::AtMost(T) => Some(value), + Precision::Absent => None, + } + } + + /// Returns the minimum possible value. + /// + /// # Return Value + /// - `Some(value)`: actual value is at least `value` and may be greater. + /// It will never be less than the returned value. + /// + /// - `None`: means the minimum value is unknown. + pub fn min_value(&self) -> Option<&T> { + match self { + Precision::Exact(value) | Precision::AtLeast(value) => Some(value), + Precision::Inexact(_) | Precision::AtMost(_) => None, + Precision::Absent => None, + } + } + + /// Returns the maximum possible value. + /// + /// # Return Value + /// - `Some(value)`: actual value is at most `value` and may be less. + /// It will never be greater than the returned value. + /// + /// - `None`: means the maximum value is unknown. + pub fn max_value(&self) -> Option<&T> { + match self { + Precision::Exact(value) | Precision::AtMost(value) => Some(value), + Precision::Inexact(_) | Precision::AtLeast(_) => None, Precision::Absent => None, } } @@ -462,6 +503,16 @@ impl ColumnStatistics { } } + /// return the minimum value this column can have, if known + pub fn min_value(&self) -> Option<&ScalarValue> { + self.min_value.get_value() + } + + /// return the maximum value this column can have, if known + pub fn max_value(&self) -> Option<&ScalarValue> { + self.max_value.get_value() + } + /// If the exactness of a [`ColumnStatistics`] instance is lost, this /// function relaxes the exactness of all information by converting them /// [`Precision::Inexact`].