polars_parquet/parquet/statistics/
mod.rs

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
mod binary;
mod boolean;
mod fixed_len_binary;
mod primitive;

pub use binary::BinaryStatistics;
pub use boolean::BooleanStatistics;
pub use fixed_len_binary::FixedLenStatistics;
pub use primitive::PrimitiveStatistics;

use crate::parquet::error::ParquetResult;
use crate::parquet::schema::types::{PhysicalType, PrimitiveType};
pub use crate::parquet::thrift_format::Statistics as ParquetStatistics;

#[derive(Debug, PartialEq)]
pub enum Statistics {
    Binary(BinaryStatistics),
    Boolean(BooleanStatistics),
    FixedLen(FixedLenStatistics),
    Int32(PrimitiveStatistics<i32>),
    Int64(PrimitiveStatistics<i64>),
    Int96(PrimitiveStatistics<[u32; 3]>),
    Float(PrimitiveStatistics<f32>),
    Double(PrimitiveStatistics<f64>),
}

impl Statistics {
    #[inline]
    pub const fn physical_type(&self) -> &PhysicalType {
        use Statistics as S;

        match self {
            S::Binary(_) => &PhysicalType::ByteArray,
            S::Boolean(_) => &PhysicalType::Boolean,
            S::FixedLen(s) => &s.primitive_type.physical_type,
            S::Int32(_) => &PhysicalType::Int32,
            S::Int64(_) => &PhysicalType::Int64,
            S::Int96(_) => &PhysicalType::Int96,
            S::Float(_) => &PhysicalType::Float,
            S::Double(_) => &PhysicalType::Double,
        }
    }

    pub fn clear_min(&mut self) {
        use Statistics as S;
        match self {
            S::Binary(s) => _ = s.min_value.take(),
            S::Boolean(s) => _ = s.min_value.take(),
            S::FixedLen(s) => _ = s.min_value.take(),
            S::Int32(s) => _ = s.min_value.take(),
            S::Int64(s) => _ = s.min_value.take(),
            S::Int96(s) => _ = s.min_value.take(),
            S::Float(s) => _ = s.min_value.take(),
            S::Double(s) => _ = s.min_value.take(),
        };
    }

    pub fn clear_max(&mut self) {
        use Statistics as S;
        match self {
            S::Binary(s) => _ = s.max_value.take(),
            S::Boolean(s) => _ = s.max_value.take(),
            S::FixedLen(s) => _ = s.max_value.take(),
            S::Int32(s) => _ = s.max_value.take(),
            S::Int64(s) => _ = s.max_value.take(),
            S::Int96(s) => _ = s.max_value.take(),
            S::Float(s) => _ = s.max_value.take(),
            S::Double(s) => _ = s.max_value.take(),
        };
    }

    /// Deserializes a raw parquet statistics into [`Statistics`].
    /// # Error
    /// This function errors if it is not possible to read the statistics to the
    /// corresponding `physical_type`.
    #[inline]
    pub fn deserialize(
        statistics: &ParquetStatistics,
        primitive_type: PrimitiveType,
    ) -> ParquetResult<Self> {
        use {PhysicalType as T, PrimitiveStatistics as PrimStat};
        let mut stats: Self = match primitive_type.physical_type {
            T::ByteArray => BinaryStatistics::deserialize(statistics, primitive_type)?.into(),
            T::Boolean => BooleanStatistics::deserialize(statistics)?.into(),
            T::Int32 => PrimStat::<i32>::deserialize(statistics, primitive_type)?.into(),
            T::Int64 => PrimStat::<i64>::deserialize(statistics, primitive_type)?.into(),
            T::Int96 => PrimStat::<[u32; 3]>::deserialize(statistics, primitive_type)?.into(),
            T::Float => PrimStat::<f32>::deserialize(statistics, primitive_type)?.into(),
            T::Double => PrimStat::<f64>::deserialize(statistics, primitive_type)?.into(),
            T::FixedLenByteArray(size) => {
                FixedLenStatistics::deserialize(statistics, size, primitive_type)?.into()
            },
        };

        if statistics.is_min_value_exact.is_some_and(|v| !v) {
            stats.clear_min();
        }
        if statistics.is_max_value_exact.is_some_and(|v| !v) {
            stats.clear_max();
        }

        Ok(stats)
    }
}

macro_rules! statistics_from_as {
    ($($variant:ident($struct:ty) => ($as_ident:ident, $into_ident:ident, $expect_ident:ident, $owned_expect_ident:ident),)+) => {
        $(
            impl From<$struct> for Statistics {
                #[inline]
                fn from(stats: $struct) -> Self {
                    Self::$variant(stats)
                }
            }
        )+

        impl Statistics {
            #[inline]
            pub const fn null_count(&self) -> Option<i64> {
                match self {
                    $(Self::$variant(s) => s.null_count,)+
                }
            }

            /// Serializes [`Statistics`] into a raw parquet statistics.
            #[inline]
            pub fn serialize(&self) -> ParquetStatistics {
                match self {
                    $(Self::$variant(s) => s.serialize(),)+
                }
            }

            const fn variant_str(&self) -> &'static str {
                match self {
                    $(Self::$variant(_) => stringify!($struct),)+
                }
            }

            $(
                #[doc = concat!("Try to take [`Statistics`] as [`", stringify!($struct), "`]")]
                #[inline]
                pub fn $as_ident(&self) -> Option<&$struct> {
                    match self {
                        Self::$variant(s) => Some(s),
                        _ => None,
                    }
                }

                #[doc = concat!("Try to take [`Statistics`] as [`", stringify!($struct), "`]")]
                #[inline]
                pub fn $into_ident(self) -> Option<$struct> {
                    match self {
                        Self::$variant(s) => Some(s),
                        _ => None,
                    }
                }

                #[doc = concat!("Interpret [`Statistics`] to be [`", stringify!($struct), "`]")]
                ///
                /// Panics if it is not the correct variant.
                #[track_caller]
                #[inline]
                pub fn $expect_ident(&self) -> &$struct {
                    let Self::$variant(s) = self else {
                        panic!("Expected Statistics to be {}, found {} instead", stringify!($struct), self.variant_str());
                    };

                    s
                }

                #[doc = concat!("Interpret [`Statistics`] to be [`", stringify!($struct), "`]")]
                ///
                /// Panics if it is not the correct variant.
                #[track_caller]
                #[inline]
                pub fn $owned_expect_ident(self) -> $struct {
                    let Self::$variant(s) = self else {
                        panic!("Expected Statistics to be {}, found {} instead", stringify!($struct), self.variant_str());
                    };

                    s
                }
            )+

        }
    };
}

statistics_from_as! {
    Binary    (BinaryStatistics             ) => (as_binary,   into_binary,   expect_as_binary,   expect_binary  ),
    Boolean   (BooleanStatistics            ) => (as_boolean,  into_boolean,  expect_as_boolean,  expect_boolean ),
    FixedLen  (FixedLenStatistics           ) => (as_fixedlen, into_fixedlen, expect_as_fixedlen, expect_fixedlen),
    Int32     (PrimitiveStatistics<i32>     ) => (as_int32,    into_int32,    expect_as_int32,    expect_int32   ),
    Int64     (PrimitiveStatistics<i64>     ) => (as_int64,    into_int64,    expect_as_int64,    expect_int64   ),
    Int96     (PrimitiveStatistics<[u32; 3]>) => (as_int96,    into_int96,    expect_as_int96,    expect_int96   ),
    Float     (PrimitiveStatistics<f32>     ) => (as_float,    into_float,    expect_as_float,    expect_float   ),
    Double    (PrimitiveStatistics<f64>     ) => (as_double,   into_double,   expect_as_double,   expect_double  ),
}