vortex_bytebool/
stats.rs

1use vortex_array::stats::{Precision, Stat, StatsSet};
2use vortex_array::vtable::StatisticsVTable;
3use vortex_array::{Array, ToCanonical};
4use vortex_error::VortexResult;
5
6use super::{ByteBoolArray, ByteBoolEncoding};
7
8impl StatisticsVTable<&ByteBoolArray> for ByteBoolEncoding {
9    fn compute_statistics(&self, array: &ByteBoolArray, stat: Stat) -> VortexResult<StatsSet> {
10        if array.is_empty() {
11            return Ok(StatsSet::default());
12        }
13
14        // TODO(adamgs): This is slightly wasteful and could be optimized in the future
15        let bools = array.to_bool()?;
16        Ok(bools
17            .statistics()
18            .compute_stat(stat)?
19            .map(|value| StatsSet::of(stat, Precision::exact(value)))
20            .unwrap_or_default())
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use vortex_array::ArrayVariants;
27
28    use super::*;
29
30    #[test]
31    fn bool_stats() {
32        let bool_arr =
33            ByteBoolArray::from(vec![false, false, true, true, false, true, true, false]);
34        assert!(!bool_arr.statistics().compute_is_strict_sorted().unwrap());
35        assert!(!bool_arr.statistics().compute_is_sorted().unwrap());
36        assert!(!bool_arr.statistics().compute_is_constant().unwrap());
37        assert!(!bool_arr.statistics().compute_min::<bool>().unwrap());
38        assert!(bool_arr.statistics().compute_max::<bool>().unwrap());
39        assert_eq!(bool_arr.as_bool_typed().unwrap().true_count().unwrap(), 4);
40    }
41
42    #[test]
43    fn strict_sorted() {
44        let bool_arr_1 = ByteBoolArray::from(vec![false, true]);
45        assert!(bool_arr_1.statistics().compute_is_strict_sorted().unwrap());
46        assert!(bool_arr_1.statistics().compute_is_sorted().unwrap());
47
48        let bool_arr_2 = ByteBoolArray::from(vec![true]);
49        assert!(bool_arr_2.statistics().compute_is_strict_sorted().unwrap());
50        assert!(bool_arr_2.statistics().compute_is_sorted().unwrap());
51
52        let bool_arr_3 = ByteBoolArray::from(vec![false]);
53        assert!(bool_arr_3.statistics().compute_is_strict_sorted().unwrap());
54        assert!(bool_arr_3.statistics().compute_is_sorted().unwrap());
55
56        let bool_arr_4 = ByteBoolArray::from(vec![true, false]);
57        assert!(!bool_arr_4.statistics().compute_is_strict_sorted().unwrap());
58        assert!(!bool_arr_4.statistics().compute_is_sorted().unwrap());
59
60        let bool_arr_5 = ByteBoolArray::from(vec![false, true, true]);
61        assert!(!bool_arr_5.statistics().compute_is_strict_sorted().unwrap());
62        assert!(bool_arr_5.statistics().compute_is_sorted().unwrap());
63    }
64
65    #[test]
66    fn nullable_stats() {
67        let bool_arr = ByteBoolArray::from(vec![
68            Some(false),
69            Some(true),
70            None,
71            Some(true),
72            Some(false),
73            None,
74            None,
75        ]);
76        assert!(!bool_arr.statistics().compute_is_strict_sorted().unwrap());
77        assert!(!bool_arr.statistics().compute_is_sorted().unwrap());
78        assert!(!bool_arr.statistics().compute_is_constant().unwrap());
79        assert!(!bool_arr.statistics().compute_min::<bool>().unwrap());
80        assert!(bool_arr.statistics().compute_max::<bool>().unwrap());
81        assert_eq!(bool_arr.as_bool_typed().unwrap().true_count().unwrap(), 2);
82    }
83
84    #[test]
85    fn all_nullable_stats() {
86        let bool_arr = ByteBoolArray::from(vec![None, None, None, None, None]);
87        assert!(!bool_arr.statistics().compute_is_strict_sorted().unwrap());
88        assert!(bool_arr.statistics().compute_is_sorted().unwrap());
89        assert!(bool_arr.statistics().compute_is_constant().unwrap());
90        assert!(
91            bool_arr
92                .statistics()
93                .compute_stat(Stat::Min)
94                .unwrap()
95                .is_none()
96        );
97        assert!(
98            bool_arr
99                .statistics()
100                .compute_stat(Stat::Max)
101                .unwrap()
102                .is_none()
103        );
104        assert_eq!(bool_arr.as_bool_typed().unwrap().true_count().unwrap(), 0);
105    }
106}