polars_parquet/parquet/statistics/
fixed_len_binary.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
use polars_parquet_format::Statistics as ParquetStatistics;

use crate::parquet::error::{ParquetError, ParquetResult};
use crate::parquet::schema::types::PrimitiveType;

#[derive(Debug, Clone, PartialEq)]
pub struct FixedLenStatistics {
    pub primitive_type: PrimitiveType,
    pub null_count: Option<i64>,
    pub distinct_count: Option<i64>,
    pub max_value: Option<Vec<u8>>,
    pub min_value: Option<Vec<u8>>,
}

impl FixedLenStatistics {
    pub fn deserialize(
        v: &ParquetStatistics,
        size: usize,
        primitive_type: PrimitiveType,
    ) -> ParquetResult<Self> {
        if let Some(ref v) = v.max_value {
            if v.len() != size {
                return Err(ParquetError::oos(
                    "The max_value of statistics MUST be plain encoded",
                ));
            }
        };
        if let Some(ref v) = v.min_value {
            if v.len() != size {
                return Err(ParquetError::oos(
                    "The min_value of statistics MUST be plain encoded",
                ));
            }
        };

        Ok(Self {
            primitive_type,
            null_count: v.null_count,
            distinct_count: v.distinct_count,
            max_value: v.max_value.clone().map(|mut x| {
                x.truncate(size);
                x
            }),
            min_value: v.min_value.clone().map(|mut x| {
                x.truncate(size);
                x
            }),
        })
    }

    pub fn serialize(&self) -> ParquetStatistics {
        ParquetStatistics {
            null_count: self.null_count,
            distinct_count: self.distinct_count,
            max_value: self.max_value.clone(),
            min_value: self.min_value.clone(),
            max: None,
            min: None,
            is_max_value_exact: None,
            is_min_value_exact: None,
        }
    }
}