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
199
200
201
202
// 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, FieldInfo, GroupConvertedType, GroupLogicalType, PhysicalType, PrimitiveConvertedType,
    PrimitiveLogicalType,
};

/// The complete description of a parquet column
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PrimitiveType {
    /// The fields' generic information
    pub field_info: FieldInfo,
    /// The optional logical type
    pub logical_type: Option<PrimitiveLogicalType>,
    /// The optional converted type
    pub converted_type: Option<PrimitiveConvertedType>,
    /// The physical type
    pub physical_type: PhysicalType,
}

impl PrimitiveType {
    /// Helper method to create an optional field with no logical or converted types.
    pub fn from_physical(name: String, physical_type: PhysicalType) -> Self {
        let field_info = FieldInfo {
            name,
            repetition: Repetition::Optional,
            id: None,
        };
        Self {
            field_info,
            converted_type: None,
            logical_type: None,
            physical_type,
        }
    }
}

/// Representation of a Parquet type describing primitive and nested fields,
/// including the top-level schema of the parquet file.
#[derive(Clone, Debug, PartialEq)]
pub enum ParquetType {
    PrimitiveType(PrimitiveType),
    GroupType {
        field_info: FieldInfo,
        logical_type: Option<GroupLogicalType>,
        converted_type: Option<GroupConvertedType>,
        fields: Vec<ParquetType>,
    },
}

/// Accessors
impl ParquetType {
    /// Returns [`FieldInfo`] information about the type.
    pub fn get_field_info(&self) -> &FieldInfo {
        match self {
            Self::PrimitiveType(primitive) => &primitive.field_info,
            Self::GroupType { field_info, .. } => field_info,
        }
    }

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

    /// 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 {
        let basic_match = self.get_field_info() == sub_type.get_field_info();

        match (self, sub_type) {
            (
                Self::PrimitiveType(PrimitiveType { physical_type, .. }),
                Self::PrimitiveType(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(crate) fn new_root(name: String, fields: Vec<ParquetType>) -> Self {
        let field_info = FieldInfo {
            name,
            repetition: Repetition::Optional,
            id: None,
        };
        ParquetType::GroupType {
            field_info,
            fields,
            logical_type: None,
            converted_type: None,
        }
    }

    pub fn from_converted(
        name: String,
        fields: Vec<ParquetType>,
        repetition: Repetition,
        converted_type: Option<GroupConvertedType>,
        id: Option<i32>,
    ) -> Self {
        let field_info = FieldInfo {
            name,
            repetition,
            id,
        };

        ParquetType::GroupType {
            field_info,
            fields,
            converted_type,
            logical_type: None,
        }
    }

    /// # Error
    /// Errors iff the combination of physical, logical and coverted type is not valid.
    pub fn try_from_primitive(
        name: String,
        physical_type: PhysicalType,
        repetition: Repetition,
        converted_type: Option<PrimitiveConvertedType>,
        logical_type: Option<PrimitiveLogicalType>,
        id: Option<i32>,
    ) -> Result<Self> {
        spec::check_converted_invariants(&physical_type, &converted_type)?;
        spec::check_logical_invariants(&physical_type, &logical_type)?;

        let field_info = FieldInfo {
            name,
            repetition,
            id,
        };

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

    /// Helper method to create a [`ParquetType::PrimitiveType`] optional field
    /// with no logical or converted types.
    pub fn from_physical(name: String, physical_type: PhysicalType) -> Self {
        ParquetType::PrimitiveType(PrimitiveType::from_physical(name, physical_type))
    }

    pub fn from_group(
        name: String,
        repetition: Repetition,
        converted_type: Option<GroupConvertedType>,
        logical_type: Option<GroupLogicalType>,
        fields: Vec<ParquetType>,
        id: Option<i32>,
    ) -> Self {
        let field_info = FieldInfo {
            name,
            repetition,
            id,
        };

        ParquetType::GroupType {
            field_info,
            logical_type,
            converted_type,
            fields,
        }
    }
}