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
// see https://github.com/apache/parquet-format/blob/master/LogicalTypes.md
use crate::error::Result;
use std::collections::HashMap;

use super::super::Repetition;
use super::{
    spec, BasicTypeInfo, GroupConvertedType, LogicalType, PhysicalType, PrimitiveConvertedType,
};

/// Representation of a Parquet type.
/// Used to describe primitive leaf fields and structs, including top-level schema.
/// Note that the top-level schema type is represented using `GroupType` whose
/// repetition is `None`.
#[derive(Clone, Debug, PartialEq)]
pub enum ParquetType {
    PrimitiveType {
        basic_info: BasicTypeInfo,
        logical_type: Option<LogicalType>,
        converted_type: Option<PrimitiveConvertedType>,
        physical_type: PhysicalType,
    },
    GroupType {
        basic_info: BasicTypeInfo,
        logical_type: Option<LogicalType>,
        converted_type: Option<GroupConvertedType>,
        fields: Vec<ParquetType>,
    },
}

/// Accessors
impl ParquetType {
    /// Returns [`BasicTypeInfo`] information about the type.
    pub fn get_basic_info(&self) -> &BasicTypeInfo {
        match *self {
            Self::PrimitiveType { ref basic_info, .. } => basic_info,
            Self::GroupType { ref basic_info, .. } => basic_info,
        }
    }

    /// Returns this type's field name.
    pub fn name(&self) -> &str {
        self.get_basic_info().name()
    }

    pub fn is_root(&self) -> bool {
        self.get_basic_info().is_root()
    }

    /// Checks if `sub_type` schema is part of current schema.
    /// This method can be used to check if projected columns are part of the root schema.
    pub fn check_contains(&self, sub_type: &ParquetType) -> bool {
        // Names match, and repetitions match or not set for both
        let basic_match = self.get_basic_info().name() == sub_type.get_basic_info().name()
            && (self.is_root() && sub_type.is_root()
                || !self.is_root()
                    && !sub_type.is_root()
                    && self.get_basic_info().repetition()
                        == sub_type.get_basic_info().repetition());

        match (self, sub_type) {
            (
                Self::PrimitiveType { physical_type, .. },
                Self::PrimitiveType {
                    physical_type: other_physical_type,
                    ..
                },
            ) => basic_match && physical_type == other_physical_type,
            (
                Self::GroupType { fields, .. },
                Self::GroupType {
                    fields: other_fields,
                    ..
                },
            ) => {
                // build hashmap of name -> Type
                let mut field_map = HashMap::new();
                for field in fields {
                    field_map.insert(field.name(), field);
                }

                for field in other_fields {
                    if !field_map
                        .get(field.name())
                        .map(|tpe| tpe.check_contains(field))
                        .unwrap_or(false)
                    {
                        return false;
                    }
                }
                true
            }
            _ => false,
        }
    }
}

/// Constructors
impl ParquetType {
    pub fn new_root(name: String, fields: Vec<ParquetType>) -> Self {
        let basic_info = BasicTypeInfo::new(name, Repetition::Optional, None, true);
        ParquetType::GroupType {
            basic_info,
            fields,
            logical_type: None,
            converted_type: None,
        }
    }

    pub fn from_converted(
        name: String,
        fields: Vec<ParquetType>,
        repetition: Option<Repetition>,
        converted_type: Option<GroupConvertedType>,
        id: Option<i32>,
    ) -> Self {
        let basic_info =
            BasicTypeInfo::new(name, repetition.unwrap_or(Repetition::Optional), id, false);
        ParquetType::GroupType {
            basic_info,
            fields,
            converted_type,
            logical_type: None,
        }
    }

    pub fn try_from_primitive(
        name: String,
        physical_type: PhysicalType,
        repetition: Repetition,
        converted_type: Option<PrimitiveConvertedType>,
        logical_type: Option<LogicalType>,
        id: Option<i32>,
    ) -> Result<Self> {
        spec::check_converted_invariants(&physical_type, &converted_type)?;
        spec::check_logical_invariants(&physical_type, &logical_type)?;

        let basic_info = BasicTypeInfo::new(name, repetition, id, false);

        Ok(ParquetType::PrimitiveType {
            basic_info,
            converted_type,
            logical_type,
            physical_type,
        })
    }

    pub fn from_physical(name: String, physical_type: PhysicalType) -> Self {
        let basic_info = BasicTypeInfo::new(name, Repetition::Optional, None, false);
        ParquetType::PrimitiveType {
            basic_info,
            converted_type: None,
            logical_type: None,
            physical_type,
        }
    }

    pub fn try_from_group(
        name: String,
        repetition: Repetition,
        converted_type: Option<GroupConvertedType>,
        logical_type: Option<LogicalType>,
        fields: Vec<ParquetType>,
        id: Option<i32>,
    ) -> Result<Self> {
        let basic_info = BasicTypeInfo::new(name, repetition, id, false);

        Ok(ParquetType::GroupType {
            basic_info,
            logical_type,
            converted_type,
            fields,
        })
    }
}