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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
use std::rc::Rc;
use super::statistics::{self, Statistics};
use basic::{ColumnOrder, Compression, Encoding, Type};
use errors::{ParquetError, Result};
use schema::types::{ColumnDescriptor, ColumnDescPtr, ColumnPath};
use schema::types::{SchemaDescriptor, SchemaDescPtr, Type as SchemaType, TypePtr};
use parquet_format::{ColumnChunk, ColumnMetaData, RowGroup};
pub type ParquetMetaDataPtr = Rc<ParquetMetaData>;
pub struct ParquetMetaData {
file_metadata: FileMetaDataPtr,
row_groups: Vec<RowGroupMetaDataPtr>
}
impl ParquetMetaData {
pub fn new(file_metadata: FileMetaData, row_groups: Vec<RowGroupMetaData>) -> Self {
ParquetMetaData {
file_metadata: Rc::new(file_metadata),
row_groups: row_groups.into_iter().map(|r| Rc::new(r)).collect()
}
}
pub fn file_metadata(&self) -> FileMetaDataPtr {
self.file_metadata.clone()
}
pub fn num_row_groups(&self) -> usize {
self.row_groups.len()
}
pub fn row_group(&self, i: usize) -> RowGroupMetaDataPtr {
self.row_groups[i].clone()
}
pub fn row_groups(&self) -> &[RowGroupMetaDataPtr] {
&self.row_groups.as_slice()
}
}
pub type FileMetaDataPtr = Rc<FileMetaData>;
pub struct FileMetaData {
version: i32,
num_rows: i64,
created_by: Option<String>,
schema: TypePtr,
schema_descr: SchemaDescPtr,
column_orders: Option<Vec<ColumnOrder>>
}
impl FileMetaData {
pub fn new(
version: i32,
num_rows: i64,
created_by: Option<String>,
schema: TypePtr,
schema_descr: SchemaDescPtr,
column_orders: Option<Vec<ColumnOrder>>
) -> Self {
FileMetaData {
version,
num_rows,
created_by,
schema,
schema_descr,
column_orders
}
}
pub fn version(&self) -> i32 {
self.version
}
pub fn num_rows(&self) -> i64 {
self.num_rows
}
pub fn created_by(&self) -> &Option<String> {
&self.created_by
}
pub fn schema(&self) -> &SchemaType {
self.schema.as_ref()
}
pub fn schema_descr(&self) -> &SchemaDescriptor {
&self.schema_descr
}
pub fn schema_descr_ptr(&self) -> SchemaDescPtr {
self.schema_descr.clone()
}
pub fn column_orders(&self) -> Option<&Vec<ColumnOrder>> {
self.column_orders.as_ref()
}
pub fn column_order(&self, i: usize) -> ColumnOrder {
self.column_orders.as_ref().map(|data| data[i]).unwrap_or(ColumnOrder::UNDEFINED)
}
}
pub type RowGroupMetaDataPtr = Rc<RowGroupMetaData>;
pub struct RowGroupMetaData {
columns: Vec<ColumnChunkMetaDataPtr>,
num_rows: i64,
total_byte_size: i64,
schema_descr: SchemaDescPtr
}
impl RowGroupMetaData {
pub fn num_columns(&self) -> usize {
self.columns.len()
}
pub fn column(&self, i: usize) -> &ColumnChunkMetaData {
&self.columns[i]
}
pub fn columns(&self) -> &[ColumnChunkMetaDataPtr] {
&self.columns
}
pub fn num_rows(&self) -> i64 {
self.num_rows
}
pub fn total_byte_size(&self) -> i64 {
self.total_byte_size
}
pub fn schema_descr(&self) -> &SchemaDescriptor {
self.schema_descr.as_ref()
}
pub fn schema_descr_ptr(&self) -> SchemaDescPtr {
self.schema_descr.clone()
}
pub fn from_thrift(
schema_descr: SchemaDescPtr,
mut rg: RowGroup
) -> Result<RowGroupMetaData> {
assert_eq!(schema_descr.num_columns(), rg.columns.len());
let total_byte_size = rg.total_byte_size;
let num_rows = rg.num_rows;
let mut columns = vec![];
for (c, d) in rg.columns.drain(0..).zip(schema_descr.columns()) {
let cc = ColumnChunkMetaData::from_thrift(d.clone(), c)?;
columns.push(Rc::new(cc));
}
Ok(RowGroupMetaData {
columns,
num_rows,
total_byte_size,
schema_descr
})
}
}
pub type ColumnChunkMetaDataPtr = Rc<ColumnChunkMetaData>;
pub struct ColumnChunkMetaData {
column_type: Type,
column_path: ColumnPath,
column_descr: ColumnDescPtr,
encodings: Vec<Encoding>,
file_path: Option<String>,
file_offset: i64,
num_values: i64,
compression: Compression,
total_compressed_size: i64,
total_uncompressed_size: i64,
data_page_offset: i64,
index_page_offset: Option<i64>,
dictionary_page_offset: Option<i64>,
statistics: Option<Statistics>
}
impl ColumnChunkMetaData {
pub fn file_path(&self) -> Option<&String> {
self.file_path.as_ref()
}
pub fn file_offset(&self) -> i64 {
self.file_offset
}
pub fn column_type(&self) -> Type {
self.column_type
}
pub fn column_path(&self) -> &ColumnPath {
&self.column_path
}
pub fn column_descr(&self) -> &ColumnDescriptor {
self.column_descr.as_ref()
}
pub fn column_descr_ptr(&self) -> ColumnDescPtr {
self.column_descr.clone()
}
pub fn encodings(&self) -> &Vec<Encoding> {
&self.encodings
}
pub fn num_values(&self) -> i64 {
self.num_values
}
pub fn compression(&self) -> Compression {
self.compression
}
pub fn compressed_size(&self) -> i64 {
self.total_compressed_size
}
pub fn uncompressed_size(&self) -> i64 {
self.total_uncompressed_size
}
pub fn data_page_offset(&self) -> i64 {
self.data_page_offset
}
pub fn has_index_page(&self) -> bool {
self.index_page_offset.is_some()
}
pub fn index_page_offset(&self) -> Option<i64> {
self.index_page_offset
}
pub fn has_dictionary_page(&self) -> bool {
self.dictionary_page_offset.is_some()
}
pub fn dictionary_page_offset(&self) -> Option<i64> {
self.dictionary_page_offset
}
pub fn statistics(&self) -> Option<&Statistics> {
self.statistics.as_ref()
}
fn from_thrift(column_descr: ColumnDescPtr, cc: ColumnChunk) -> Result<Self> {
if cc.meta_data.is_none() {
return Err(general_err!("Expected to have column metadata"));
}
let mut col_metadata: ColumnMetaData = cc.meta_data.unwrap();
let column_type = Type::from(col_metadata.type_);
let column_path = ColumnPath::new(col_metadata.path_in_schema);
let encodings = col_metadata.encodings.drain(0..).map(Encoding::from).collect();
let compression = Compression::from(col_metadata.codec);
let file_path = cc.file_path;
let file_offset = cc.file_offset;
let num_values = col_metadata.num_values;
let total_compressed_size = col_metadata.total_compressed_size;
let total_uncompressed_size = col_metadata.total_uncompressed_size;
let data_page_offset = col_metadata.data_page_offset;
let index_page_offset = col_metadata.index_page_offset;
let dictionary_page_offset = col_metadata.dictionary_page_offset;
let statistics = statistics::from_thrift(column_type, col_metadata.statistics);
let result = ColumnChunkMetaData {
column_type,
column_path,
column_descr,
encodings,
file_path,
file_offset,
num_values,
compression,
total_compressed_size,
total_uncompressed_size,
data_page_offset,
index_page_offset,
dictionary_page_offset,
statistics
};
Ok(result)
}
}