vortex_array/expr/stats/
stat_bound.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::cmp::Ordering;
5
6use crate::expr::stats::Precision;
7use crate::expr::stats::Stat;
8use crate::expr::stats::bound::IntersectionResult;
9use crate::partial_ord::partial_min;
10
11/// `StatType` define the bound of a given statistic. (e.g. `Max` is an upper bound),
12/// this is used to extract the bound from a `Precision` value, (e.g. `p::bound<Max>()`).
13pub trait StatType<T> {
14    type Bound: StatBound<T>;
15
16    const STAT: Stat;
17}
18
19/// `StatBound` defines the operations that can be performed on a bound.
20/// The main bounds are Upper (e.g. max) and Lower (e.g. min).
21pub trait StatBound<T>: Sized {
22    /// Creates a new bound from a Precision statistic.
23    fn lift(value: Precision<T>) -> Self;
24
25    /// Converts `Self` back to `Precision<T>`, inverse of `lift`.
26    fn into_value(self) -> Precision<T>;
27
28    /// Finds the smallest bound that covers both bounds.
29    /// A.k.a. the `meet` of the bound.
30    fn union(&self, other: &Self) -> Option<Self>;
31
32    /// Refines the bounds to the most precise estimate we can make for that bound.
33    /// If the bounds are disjoint, then the result is `None`.
34    /// e.g. `Precision::Inexact(5)` and `Precision::Exact(6)` would result in `Precision::Inexact(5)`.
35    /// A.k.a. the `join` of the bound.
36    fn intersection(&self, other: &Self) -> Option<IntersectionResult<Self>>;
37
38    /// Returns the exact value from the bound if that value is exact, otherwise `None`.
39    fn to_exact(&self) -> Option<&T>;
40}
41
42/// This allows a stat with a `Precision` to be interpreted as a bound.
43impl<T> Precision<T> {
44    /// Applied the stat associated bound to the precision value
45    pub fn bound<S: StatType<T>>(self) -> S::Bound {
46        S::Bound::lift(self)
47    }
48}
49
50impl<T: PartialOrd + Clone> StatBound<T> for Precision<T> {
51    fn lift(value: Precision<T>) -> Self {
52        value
53    }
54
55    fn into_value(self) -> Precision<T> {
56        self
57    }
58
59    fn union(&self, other: &Self) -> Option<Self> {
60        self.clone()
61            .zip(other.clone())
62            .map(|(lhs, rhs)| partial_min(&lhs, &rhs).cloned())
63            .transpose()
64    }
65
66    fn intersection(&self, other: &Self) -> Option<IntersectionResult<Self>> {
67        Some(match (self, other) {
68            (Precision::Exact(lhs), Precision::Exact(rhs)) => {
69                if lhs.partial_cmp(rhs)? == Ordering::Equal {
70                    IntersectionResult::Value(Precision::Exact(lhs.clone()))
71                } else {
72                    IntersectionResult::None
73                }
74            }
75            (Precision::Exact(exact), Precision::Inexact(inexact))
76            | (Precision::Inexact(inexact), Precision::Exact(exact)) => {
77                if exact.partial_cmp(inexact)? == Ordering::Less {
78                    IntersectionResult::Value(Precision::Inexact(exact.clone()))
79                } else {
80                    IntersectionResult::Value(Precision::Exact(exact.clone()))
81                }
82            }
83            (Precision::Inexact(lhs), Precision::Inexact(rhs)) => {
84                IntersectionResult::Value(Precision::Inexact(partial_min(lhs, rhs)?.clone()))
85            }
86        })
87    }
88
89    fn to_exact(&self) -> Option<&T> {
90        match self {
91            Precision::Exact(val) => Some(val),
92            _ => None,
93        }
94    }
95}