Skip to main content

vortex_array/stats/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Traits and utilities to compute and access array statistics.
5
6use arrow_buffer::BooleanBufferBuilder;
7use arrow_buffer::MutableBuffer;
8use arrow_buffer::bit_iterator::BitIterator;
9use enum_iterator::last;
10pub use expr::min_max;
11pub use expr::nan_count;
12pub use expr::null_count;
13pub use expr::stat;
14pub use expr::sum;
15pub use stats_set::*;
16
17mod array;
18pub mod expr;
19pub mod flatbuffers;
20pub(crate) mod rewrite;
21pub mod session;
22mod stats_set;
23
24pub use array::*;
25pub use session::*;
26use vortex_error::VortexExpect;
27
28use crate::expr::stats::Stat;
29
30/// Statistics that are used for pruning files (i.e., we want to ensure they are computed when compressing/writing).
31/// Sum is included for boolean arrays.
32pub const PRUNING_STATS: &[Stat] = &[
33    Stat::Min,
34    Stat::Max,
35    Stat::Sum,
36    Stat::NullCount,
37    Stat::NaNCount,
38];
39
40pub fn as_stat_bitset_bytes(stats: &[Stat]) -> Vec<u8> {
41    let max_stat = u8::from(last::<Stat>().vortex_expect("last stat")) as usize + 1;
42    // TODO(ngates): use vortex-buffer::BitBuffer
43    let mut stat_bitset = BooleanBufferBuilder::new_from_buffer(
44        MutableBuffer::from_len_zeroed(max_stat.div_ceil(8)),
45        max_stat,
46    );
47    for stat in stats {
48        stat_bitset.set_bit(u8::from(*stat) as usize, true);
49    }
50
51    stat_bitset
52        .finish()
53        .into_inner()
54        .into_vec()
55        .unwrap_or_else(|b| b.to_vec())
56}
57
58pub fn stats_from_bitset_bytes(bytes: &[u8]) -> Vec<Stat> {
59    BitIterator::new(bytes, 0, bytes.len() * 8)
60        .enumerate()
61        .filter_map(|(i, b)| b.then_some(i))
62        // Filter out indices failing conversion, these are stats written by newer version of library
63        .filter_map(|i| {
64            let Ok(stat) = u8::try_from(i) else {
65                tracing::debug!("invalid stat encountered: {i}");
66                return None;
67            };
68            Stat::try_from(stat).ok()
69        })
70        .collect::<Vec<_>>()
71}