1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use flatbuffers::{FlatBufferBuilder, WIPOffset};
use itertools::Itertools;
use vortex_flatbuffers::WriteFlatBuffer;

use crate::stats::{Stat, Statistics};

impl WriteFlatBuffer for &dyn Statistics {
    type Target<'t> = crate::flatbuffers::ArrayStats<'t>;

    fn write_flatbuffer<'fb>(
        &self,
        fbb: &mut FlatBufferBuilder<'fb>,
    ) -> WIPOffset<Self::Target<'fb>> {
        let trailing_zero_freq = self
            .get_as::<Vec<u64>>(Stat::TrailingZeroFreq)
            .map(|v| v.iter().copied().collect_vec())
            .map(|v| fbb.create_vector(v.as_slice()));

        let bit_width_freq = self
            .get_as::<Vec<u64>>(Stat::BitWidthFreq)
            .map(|v| v.iter().copied().collect_vec())
            .map(|v| fbb.create_vector(v.as_slice()));

        let min = self
            .get(Stat::Min)
            .map(|min| min.into_value().write_flatbuffer(fbb));

        let max = self
            .get(Stat::Max)
            .map(|max| max.into_value().write_flatbuffer(fbb));

        let stat_args = &crate::flatbuffers::ArrayStatsArgs {
            min,
            max,
            is_sorted: self.get_as::<bool>(Stat::IsSorted),
            is_strict_sorted: self.get_as::<bool>(Stat::IsStrictSorted),
            is_constant: self.get_as::<bool>(Stat::IsConstant),
            run_count: self.get_as_cast::<u64>(Stat::RunCount),
            true_count: self.get_as_cast::<u64>(Stat::TrueCount),
            null_count: self.get_as_cast::<u64>(Stat::NullCount),
            bit_width_freq,
            trailing_zero_freq,
            uncompressed_size_in_bytes: self.get_as_cast::<u64>(Stat::UncompressedSizeInBytes),
        };

        crate::flatbuffers::ArrayStats::create(fbb, stat_args)
    }
}