vortex_array/array/
statistics.rs

1use vortex_scalar::Scalar;
2
3use crate::Array;
4use crate::compute::{IsConstantOpts, is_constant_opts, scalar_at};
5use crate::stats::StatsSetRef;
6
7/// Extension functions for arrays that provide statistics.
8pub trait ArrayStatistics {
9    /// Make a best effort attempt to try and figure out if the array is constant, without canonicalizing it.
10    fn is_constant(&self) -> bool;
11
12    /// If [`Self::is_constant`] is true, return the actual constant value as a [`Scalar`].
13    fn as_constant(&self) -> Option<Scalar>;
14}
15
16impl<A: Array + 'static> ArrayStatistics for A {
17    fn is_constant(&self) -> bool {
18        let opts = IsConstantOpts {
19            canonicalize: false,
20        };
21        is_constant_opts(self, &opts)
22            .inspect_err(|e| log::warn!("Failed to compute IsConstant: {e}"))
23            .ok()
24            .unwrap_or_default()
25    }
26
27    fn as_constant(&self) -> Option<Scalar> {
28        self.is_constant()
29            .then(|| scalar_at(self, 0).ok())
30            .flatten()
31    }
32}
33
34pub trait ArrayStatisticsImpl {
35    fn _stats_ref(&self) -> StatsSetRef<'_>;
36}