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
use parquet_format_async_temp::SchemaElement;
use crate::{
error::ParquetError,
schema::{io_message::from_message, types::ParquetType, Repetition},
};
use crate::{error::Result, schema::types::BasicTypeInfo};
use super::column_descriptor::ColumnDescriptor;
#[derive(Debug, Clone)]
pub struct SchemaDescriptor {
name: String,
fields: Vec<ParquetType>,
leaves: Vec<ColumnDescriptor>,
}
impl SchemaDescriptor {
pub fn new(name: String, fields: Vec<ParquetType>) -> Self {
let mut leaves = vec![];
for f in &fields {
let mut path = vec![];
build_tree(f, f, 0, 0, &mut leaves, &mut path);
}
Self {
name,
fields,
leaves,
}
}
pub fn column(&self, i: usize) -> &ColumnDescriptor {
&self.leaves[i]
}
pub fn columns(&self) -> &[ColumnDescriptor] {
&self.leaves
}
pub fn num_columns(&self) -> usize {
self.leaves.len()
}
pub fn name(&self) -> &str {
&self.name
}
pub fn fields(&self) -> &[ParquetType] {
&self.fields
}
pub(crate) fn into_thrift(self) -> Result<Vec<SchemaElement>> {
ParquetType::GroupType {
basic_info: BasicTypeInfo::new(self.name, Repetition::Optional, None, true),
logical_type: None,
converted_type: None,
fields: self.fields,
}
.to_thrift()
}
fn try_from_type(type_: ParquetType) -> Result<Self> {
match type_ {
ParquetType::GroupType {
basic_info, fields, ..
} => Ok(Self::new(basic_info.name().to_string(), fields)),
_ => Err(ParquetError::OutOfSpec(
"The parquet schema MUST be a group type".to_string(),
)),
}
}
pub(crate) fn try_from_thrift(elements: &[&SchemaElement]) -> Result<Self> {
let schema = ParquetType::try_from_thrift(elements)?;
Self::try_from_type(schema)
}
pub fn try_from_message(message: &str) -> Result<Self> {
let schema = from_message(message)?;
Self::try_from_type(schema)
}
}
fn build_tree<'a>(
tp: &'a ParquetType,
base_tp: &ParquetType,
mut max_rep_level: i16,
mut max_def_level: i16,
leaves: &mut Vec<ColumnDescriptor>,
path_so_far: &mut Vec<&'a str>,
) {
path_so_far.push(tp.name());
match *tp.get_basic_info().repetition() {
Repetition::Optional => {
max_def_level += 1;
}
Repetition::Repeated => {
max_def_level += 1;
max_rep_level += 1;
}
_ => {}
}
match tp {
ParquetType::PrimitiveType { .. } => {
let path_in_schema = path_so_far.iter().copied().map(String::from).collect();
leaves.push(ColumnDescriptor::new(
tp.clone(),
max_def_level,
max_rep_level,
path_in_schema,
base_tp.clone(),
));
}
ParquetType::GroupType { ref fields, .. } => {
for f in fields {
build_tree(
f,
base_tp,
max_rep_level,
max_def_level,
leaves,
path_so_far,
);
path_so_far.pop();
}
}
}
}