Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-parquet/src/parquet/schema/io_thrift/to_thrift.rs
8512 views
1
use polars_parquet_format::{ConvertedType, SchemaElement};
2
3
use super::super::types::ParquetType;
4
use crate::parquet::schema::types::PrimitiveType;
5
6
impl ParquetType {
7
/// Method to convert to Thrift.
8
pub(crate) fn to_thrift(&self) -> Vec<SchemaElement> {
9
let mut elements: Vec<SchemaElement> = Vec::new();
10
to_thrift_helper(self, &mut elements, true);
11
elements
12
}
13
}
14
15
/// Constructs list of `SchemaElement` from the schema using depth-first traversal.
16
/// Here we assume that schema is always valid and starts with group type.
17
fn to_thrift_helper(schema: &ParquetType, elements: &mut Vec<SchemaElement>, is_root: bool) {
18
match schema {
19
ParquetType::PrimitiveType(PrimitiveType {
20
field_info,
21
logical_type,
22
converted_type,
23
physical_type,
24
}) => {
25
let (type_, type_length) = (*physical_type).into();
26
let (converted_type, maybe_decimal) = converted_type
27
.map(|x| x.into())
28
.map(|x: (ConvertedType, Option<(i32, i32)>)| (Some(x.0), x.1))
29
.unwrap_or((None, None));
30
31
let element = SchemaElement {
32
type_: Some(type_),
33
type_length,
34
repetition_type: Some(field_info.repetition.into()),
35
name: field_info.name.to_string(),
36
num_children: None,
37
converted_type,
38
precision: maybe_decimal.map(|x| x.0),
39
scale: maybe_decimal.map(|x| x.1),
40
field_id: field_info.id,
41
logical_type: logical_type.map(|x| x.into()),
42
};
43
44
elements.push(element);
45
},
46
ParquetType::GroupType {
47
field_info,
48
fields,
49
logical_type,
50
converted_type,
51
} => {
52
let converted_type = converted_type.map(|x| x.into());
53
54
let repetition_type = if is_root {
55
// https://github.com/apache/parquet-format/blob/7f06e838cbd1b7dbd722ff2580b9c2525e37fc46/src/main/thrift/parquet.thrift#L363
56
None
57
} else {
58
Some(field_info.repetition)
59
};
60
61
let element = SchemaElement {
62
type_: None,
63
type_length: None,
64
repetition_type: repetition_type.map(|x| x.into()),
65
name: field_info.name.to_string(),
66
num_children: Some(fields.len() as i32),
67
converted_type,
68
scale: None,
69
precision: None,
70
field_id: field_info.id,
71
logical_type: logical_type.map(|x| x.into()),
72
};
73
74
elements.push(element);
75
76
// Add child elements for a group
77
for field in fields {
78
to_thrift_helper(field, elements, false);
79
}
80
},
81
}
82
}
83
84