vortex_array/stats/
mod.rs1use std::fmt::{Debug, Display, Formatter};
7use std::hash::Hash;
8
9use arrow_buffer::bit_iterator::BitIterator;
10use arrow_buffer::{BooleanBufferBuilder, MutableBuffer};
11use enum_iterator::{Sequence, all, last};
12use log::debug;
13use num_enum::{IntoPrimitive, TryFromPrimitive};
14pub use stats_set::*;
15use vortex_dtype::Nullability::{NonNullable, Nullable};
16use vortex_dtype::{DType, PType};
17
18mod array;
19mod bound;
20pub mod flatbuffers;
21mod precision;
22mod provider;
23mod stat_bound;
24mod stats_set;
25
26pub use array::*;
27pub use bound::{LowerBound, UpperBound};
28pub use precision::Precision;
29pub use provider::*;
30pub use stat_bound::*;
31use vortex_error::VortexExpect;
32
33pub const PRUNING_STATS: &[Stat] = &[
36 Stat::Min,
37 Stat::Max,
38 Stat::Sum,
39 Stat::NullCount,
40 Stat::NaNCount,
41];
42
43#[derive(
44 Debug,
45 Clone,
46 Copy,
47 PartialEq,
48 Eq,
49 PartialOrd,
50 Ord,
51 Hash,
52 Sequence,
53 IntoPrimitive,
54 TryFromPrimitive,
55)]
56#[repr(u8)]
57pub enum Stat {
58 IsConstant = 0,
61 IsSorted = 1,
63 IsStrictSorted = 2,
65 Max = 3,
67 Min = 4,
69 Sum = 5,
71 NullCount = 6,
73 UncompressedSizeInBytes = 7,
75 NaNCount = 8,
77}
78
79pub struct Max;
82pub struct Min;
83pub struct Sum;
84pub struct IsConstant;
85pub struct IsSorted;
86pub struct IsStrictSorted;
87pub struct NullCount;
88pub struct UncompressedSizeInBytes;
89pub struct NaNCount;
90
91impl StatType<bool> for IsConstant {
92 type Bound = Precision<bool>;
93
94 const STAT: Stat = Stat::IsConstant;
95}
96
97impl StatType<bool> for IsSorted {
98 type Bound = Precision<bool>;
99
100 const STAT: Stat = Stat::IsSorted;
101}
102
103impl StatType<bool> for IsStrictSorted {
104 type Bound = Precision<bool>;
105
106 const STAT: Stat = Stat::IsStrictSorted;
107}
108
109impl<T: PartialOrd + Clone> StatType<T> for NullCount {
110 type Bound = UpperBound<T>;
111
112 const STAT: Stat = Stat::NullCount;
113}
114
115impl<T: PartialOrd + Clone> StatType<T> for UncompressedSizeInBytes {
116 type Bound = UpperBound<T>;
117
118 const STAT: Stat = Stat::UncompressedSizeInBytes;
119}
120
121impl<T: PartialOrd + Clone + Debug> StatType<T> for Max {
122 type Bound = UpperBound<T>;
123
124 const STAT: Stat = Stat::Max;
125}
126
127impl<T: PartialOrd + Clone + Debug> StatType<T> for Min {
128 type Bound = LowerBound<T>;
129
130 const STAT: Stat = Stat::Min;
131}
132
133impl<T: PartialOrd + Clone + Debug> StatType<T> for Sum {
134 type Bound = Precision<T>;
135
136 const STAT: Stat = Stat::Sum;
137}
138
139impl<T: PartialOrd + Clone> StatType<T> for NaNCount {
140 type Bound = UpperBound<T>;
141
142 const STAT: Stat = Stat::NaNCount;
143}
144
145impl Stat {
146 pub fn is_commutative(&self) -> bool {
149 match self {
151 Self::IsConstant
152 | Self::Max
153 | Self::Min
154 | Self::NullCount
155 | Self::Sum
156 | Self::NaNCount
157 | Self::UncompressedSizeInBytes => true,
158 Self::IsSorted | Self::IsStrictSorted => false,
159 }
160 }
161
162 pub fn has_same_dtype_as_array(&self) -> bool {
164 matches!(self, Stat::Min | Stat::Max)
165 }
166
167 pub fn dtype(&self, data_type: &DType) -> Option<DType> {
169 Some(match self {
170 Self::IsConstant => DType::Bool(NonNullable),
171 Self::IsSorted => DType::Bool(NonNullable),
172 Self::IsStrictSorted => DType::Bool(NonNullable),
173 Self::Max => data_type.clone(),
174 Self::Min => data_type.clone(),
175 Self::NullCount => DType::Primitive(PType::U64, NonNullable),
176 Self::UncompressedSizeInBytes => DType::Primitive(PType::U64, NonNullable),
177 Self::NaNCount => match data_type {
178 DType::Primitive(ptype, ..) if ptype.is_float() => {
179 DType::Primitive(PType::U64, NonNullable)
180 }
181 _ => return None,
183 },
184 Self::Sum => {
185 match data_type {
189 DType::Bool(_) => DType::Primitive(PType::U64, Nullable),
190 DType::Primitive(ptype, _) => match ptype {
191 PType::U8 | PType::U16 | PType::U32 | PType::U64 => {
192 DType::Primitive(PType::U64, Nullable)
193 }
194 PType::I8 | PType::I16 | PType::I32 | PType::I64 => {
195 DType::Primitive(PType::I64, Nullable)
196 }
197 PType::F16 | PType::F32 | PType::F64 => {
198 DType::Primitive(PType::F64, Nullable)
200 }
201 },
202 DType::Extension(ext_dtype) => self.dtype(ext_dtype.storage_dtype())?,
203 DType::Null
205 | DType::Decimal(..)
207 | DType::Utf8(_)
208 | DType::Binary(_)
209 | DType::Struct(..)
210 | DType::List(..) => return None,
211 }
212 }
213 })
214 }
215
216 pub fn name(&self) -> &str {
217 match self {
218 Self::IsConstant => "is_constant",
219 Self::IsSorted => "is_sorted",
220 Self::IsStrictSorted => "is_strict_sorted",
221 Self::Max => "max",
222 Self::Min => "min",
223 Self::NullCount => "null_count",
224 Self::UncompressedSizeInBytes => "uncompressed_size_in_bytes",
225 Self::Sum => "sum",
226 Self::NaNCount => "nan_count",
227 }
228 }
229
230 pub fn all() -> impl Iterator<Item = Stat> {
231 all::<Self>()
232 }
233}
234
235pub fn as_stat_bitset_bytes(stats: &[Stat]) -> Vec<u8> {
236 let max_stat = u8::from(last::<Stat>().vortex_expect("last stat")) as usize + 1;
237 let mut stat_bitset = BooleanBufferBuilder::new_from_buffer(
239 MutableBuffer::from_len_zeroed(max_stat.div_ceil(8)),
240 max_stat,
241 );
242 for stat in stats {
243 stat_bitset.set_bit(u8::from(*stat) as usize, true);
244 }
245
246 stat_bitset
247 .finish()
248 .into_inner()
249 .into_vec()
250 .unwrap_or_else(|b| b.to_vec())
251}
252
253pub fn stats_from_bitset_bytes(bytes: &[u8]) -> Vec<Stat> {
254 BitIterator::new(bytes, 0, bytes.len() * 8)
255 .enumerate()
256 .filter_map(|(i, b)| b.then_some(i))
257 .filter_map(|i| {
259 let Ok(stat) = u8::try_from(i) else {
260 debug!("invalid stat encountered: {i}");
261 return None;
262 };
263 Stat::try_from(stat).ok()
264 })
265 .collect::<Vec<_>>()
266}
267
268impl Display for Stat {
269 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
270 write!(f, "{}", self.name())
271 }
272}
273
274#[cfg(test)]
275mod test {
276 use enum_iterator::all;
277
278 use crate::arrays::PrimitiveArray;
279 use crate::stats::Stat;
280
281 #[test]
282 fn min_of_nulls_is_not_panic() {
283 let min = PrimitiveArray::from_option_iter::<i32, _>([None, None, None, None])
284 .statistics()
285 .compute_as::<i64>(Stat::Min);
286
287 assert_eq!(min, None);
288 }
289
290 #[test]
291 fn has_same_dtype_as_array() {
292 assert!(Stat::Min.has_same_dtype_as_array());
293 assert!(Stat::Max.has_same_dtype_as_array());
294 for stat in all::<Stat>().filter(|s| !matches!(s, Stat::Min | Stat::Max)) {
295 assert!(!stat.has_same_dtype_as_array());
296 }
297 }
298}