parquet/file/
page_encoding_stats.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Per-page encoding information.
19
20use crate::basic::{Encoding, PageType};
21use crate::errors::Result;
22use crate::format::{
23    Encoding as TEncoding, PageEncodingStats as TPageEncodingStats, PageType as TPageType,
24};
25
26/// PageEncodingStats for a column chunk and data page.
27#[derive(Clone, Debug, PartialEq, Eq)]
28pub struct PageEncodingStats {
29    /// the page type (data/dic/...)
30    pub page_type: PageType,
31    /// encoding of the page
32    pub encoding: Encoding,
33    /// number of pages of this type with this encoding
34    pub count: i32,
35}
36
37/// Converts Thrift definition into `PageEncodingStats`.
38pub fn try_from_thrift(thrift_encoding_stats: &TPageEncodingStats) -> Result<PageEncodingStats> {
39    let page_type = PageType::try_from(thrift_encoding_stats.page_type)?;
40    let encoding = Encoding::try_from(thrift_encoding_stats.encoding)?;
41    let count = thrift_encoding_stats.count;
42
43    Ok(PageEncodingStats {
44        page_type,
45        encoding,
46        count,
47    })
48}
49
50/// Converts `PageEncodingStats` into Thrift definition.
51pub fn to_thrift(encoding_stats: &PageEncodingStats) -> TPageEncodingStats {
52    let page_type = TPageType::from(encoding_stats.page_type);
53    let encoding = TEncoding::from(encoding_stats.encoding);
54    let count = encoding_stats.count;
55
56    TPageEncodingStats {
57        page_type,
58        encoding,
59        count,
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_page_encoding_stats_from_thrift() {
69        let stats = PageEncodingStats {
70            page_type: PageType::DATA_PAGE,
71            encoding: Encoding::PLAIN,
72            count: 1,
73        };
74
75        assert_eq!(try_from_thrift(&to_thrift(&stats)).unwrap(), stats);
76    }
77}