vortex_array/stats/
provider.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::{VortexError, vortex_panic};
5use vortex_scalar::Scalar;
6
7use super::{Precision, Stat, StatType};
8
9pub trait StatsProvider {
10    fn get(&self, stat: Stat) -> Option<Precision<Scalar>>;
11
12    /// Count of stored stats with known values.
13    fn len(&self) -> usize;
14
15    /// Predicate equivalent to a [len][Self::len] of zero.
16    fn is_empty(&self) -> bool {
17        self.len() == 0
18    }
19}
20
21impl<S> StatsProviderExt for S where S: StatsProvider {}
22
23pub trait StatsProviderExt: StatsProvider {
24    fn get_scalar_bound<S: StatType<Scalar>>(&self) -> Option<S::Bound> {
25        self.get(S::STAT).map(|v| v.bound::<S>())
26    }
27
28    fn get_as<T: for<'a> TryFrom<&'a Scalar, Error = VortexError>>(
29        &self,
30        stat: Stat,
31    ) -> Option<Precision<T>> {
32        self.get(stat).map(|v| {
33            v.map(|v| {
34                T::try_from(&v).unwrap_or_else(|err| {
35                    vortex_panic!(
36                        err,
37                        "Failed to get stat {} as {}",
38                        stat,
39                        std::any::type_name::<T>()
40                    )
41                })
42            })
43        })
44    }
45
46    fn get_as_bound<S, U>(&self) -> Option<S::Bound>
47    where
48        S: StatType<U>,
49        U: for<'a> TryFrom<&'a Scalar, Error = VortexError>,
50    {
51        self.get_as::<U>(S::STAT).map(|v| v.bound::<S>())
52    }
53}