-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
basic.rs
1499 lines (1376 loc) · 51.5 KB
/
basic.rs
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! Contains Rust mappings for Thrift definition.
//! Refer to `parquet.thrift` file to see raw definitions.
use std::{convert, fmt, result, str};
use parquet_format as parquet;
use crate::errors::ParquetError;
// ----------------------------------------------------------------------
// Types from the Thrift definition
// ----------------------------------------------------------------------
// Mirrors `parquet::Type`
/// Types supported by Parquet.
/// These physical types are intended to be used in combination with the encodings to
/// control the on disk storage format.
/// For example INT16 is not included as a type since a good encoding of INT32
/// would handle this.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Type {
BOOLEAN,
INT32,
INT64,
INT96,
FLOAT,
DOUBLE,
BYTE_ARRAY,
FIXED_LEN_BYTE_ARRAY,
}
// ----------------------------------------------------------------------
// Mirrors `parquet::ConvertedType`
/// Common types (logical types) used by frameworks when using Parquet.
/// This helps map between types in those frameworks to the base types in Parquet.
/// This is only metadata and not needed to read or write the data.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LogicalType {
NONE,
/// A BYTE_ARRAY actually contains UTF8 encoded chars.
UTF8,
/// A map is converted as an optional field containing a repeated key/value pair.
MAP,
/// A key/value pair is converted into a group of two fields.
MAP_KEY_VALUE,
/// A list is converted into an optional field containing a repeated field for its
/// values.
LIST,
/// An enum is converted into a binary field
ENUM,
/// A decimal value.
/// This may be used to annotate binary or fixed primitive types. The
/// underlying byte array stores the unscaled value encoded as two's
/// complement using big-endian byte order (the most significant byte is the
/// zeroth element).
///
/// This must be accompanied by a (maximum) precision and a scale in the
/// SchemaElement. The precision specifies the number of digits in the decimal
/// and the scale stores the location of the decimal point. For example 1.23
/// would have precision 3 (3 total digits) and scale 2 (the decimal point is
/// 2 digits over).
DECIMAL,
/// A date stored as days since Unix epoch, encoded as the INT32 physical type.
DATE,
/// The total number of milliseconds since midnight. The value is stored as an INT32
/// physical type.
TIME_MILLIS,
/// The total number of microseconds since midnight. The value is stored as an INT64
/// physical type.
TIME_MICROS,
/// Date and time recorded as milliseconds since the Unix epoch.
/// Recorded as a physical type of INT64.
TIMESTAMP_MILLIS,
/// Date and time recorded as microseconds since the Unix epoch.
/// The value is stored as an INT64 physical type.
TIMESTAMP_MICROS,
/// An unsigned 8 bit integer value stored as INT32 physical type.
UINT_8,
/// An unsigned 16 bit integer value stored as INT32 physical type.
UINT_16,
/// An unsigned 32 bit integer value stored as INT32 physical type.
UINT_32,
/// An unsigned 64 bit integer value stored as INT64 physical type.
UINT_64,
/// A signed 8 bit integer value stored as INT32 physical type.
INT_8,
/// A signed 16 bit integer value stored as INT32 physical type.
INT_16,
/// A signed 32 bit integer value stored as INT32 physical type.
INT_32,
/// A signed 64 bit integer value stored as INT64 physical type.
INT_64,
/// A JSON document embedded within a single UTF8 column.
JSON,
/// A BSON document embedded within a single BINARY column.
BSON,
/// An interval of time.
///
/// This type annotates data stored as a FIXED_LEN_BYTE_ARRAY of length 12.
/// This data is composed of three separate little endian unsigned integers.
/// Each stores a component of a duration of time. The first integer identifies
/// the number of months associated with the duration, the second identifies
/// the number of days associated with the duration and the third identifies
/// the number of milliseconds associated with the provided duration.
/// This duration of time is independent of any particular timezone or date.
INTERVAL,
}
// ----------------------------------------------------------------------
// Mirrors `parquet::FieldRepetitionType`
/// Representation of field types in schema.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Repetition {
/// Field is required (can not be null) and each record has exactly 1 value.
REQUIRED,
/// Field is optional (can be null) and each record has 0 or 1 values.
OPTIONAL,
/// Field is repeated and can contain 0 or more values.
REPEATED,
}
// ----------------------------------------------------------------------
// Mirrors `parquet::Encoding`
/// Encodings supported by Parquet.
/// Not all encodings are valid for all types. These enums are also used to specify the
/// encoding of definition and repetition levels.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Encoding {
/// Default byte encoding.
/// - BOOLEAN - 1 bit per value, 0 is false; 1 is true.
/// - INT32 - 4 bytes per value, stored as little-endian.
/// - INT64 - 8 bytes per value, stored as little-endian.
/// - FLOAT - 4 bytes per value, stored as little-endian.
/// - DOUBLE - 8 bytes per value, stored as little-endian.
/// - BYTE_ARRAY - 4 byte length stored as little endian, followed by bytes.
/// - FIXED_LEN_BYTE_ARRAY - just the bytes are stored.
PLAIN,
/// **Deprecated** dictionary encoding.
///
/// The values in the dictionary are encoded using PLAIN encoding.
/// Since it is deprecated, RLE_DICTIONARY encoding is used for a data page, and
/// PLAIN encoding is used for dictionary page.
PLAIN_DICTIONARY,
/// Group packed run length encoding.
///
/// Usable for definition/repetition levels encoding and boolean values.
RLE,
/// Bit packed encoding.
///
/// This can only be used if the data has a known max width.
/// Usable for definition/repetition levels encoding.
BIT_PACKED,
/// Delta encoding for integers, either INT32 or INT64.
///
/// Works best on sorted data.
DELTA_BINARY_PACKED,
/// Encoding for byte arrays to separate the length values and the data.
///
/// The lengths are encoded using DELTA_BINARY_PACKED encoding.
DELTA_LENGTH_BYTE_ARRAY,
/// Incremental encoding for byte arrays.
///
/// Prefix lengths are encoded using DELTA_BINARY_PACKED encoding.
/// Suffixes are stored using DELTA_LENGTH_BYTE_ARRAY encoding.
DELTA_BYTE_ARRAY,
/// Dictionary encoding.
///
/// The ids are encoded using the RLE encoding.
RLE_DICTIONARY,
}
// ----------------------------------------------------------------------
// Mirrors `parquet::CompressionCodec`
/// Supported compression algorithms.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Compression {
UNCOMPRESSED,
SNAPPY,
GZIP,
LZO,
BROTLI,
LZ4,
ZSTD,
}
// ----------------------------------------------------------------------
// Mirrors `parquet::PageType`
/// Available data pages for Parquet file format.
/// Note that some of the page types may not be supported.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PageType {
DATA_PAGE,
INDEX_PAGE,
DICTIONARY_PAGE,
DATA_PAGE_V2,
}
// ----------------------------------------------------------------------
// Mirrors `parquet::ColumnOrder`
/// Sort order for page and column statistics.
///
/// Types are associated with sort orders and column stats are aggregated using a sort
/// order, and a sort order should be considered when comparing values with statistics
/// min/max.
///
/// See reference in
/// https://github.com/apache/parquet-cpp/blob/master/src/parquet/types.h
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SortOrder {
/// Signed (either value or legacy byte-wise) comparison.
SIGNED,
/// Unsigned (depending on physical type either value or byte-wise) comparison.
UNSIGNED,
/// Comparison is undefined.
UNDEFINED,
}
/// Column order that specifies what method was used to aggregate min/max values for
/// statistics.
///
/// If column order is undefined, then it is the legacy behaviour and all values should
/// be compared as signed values/bytes.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ColumnOrder {
/// Column uses the order defined by its logical or physical type
/// (if there is no logical type), parquet-format 2.4.0+.
TYPE_DEFINED_ORDER(SortOrder),
/// Undefined column order, means legacy behaviour before parquet-format 2.4.0.
/// Sort order is always SIGNED.
UNDEFINED,
}
impl ColumnOrder {
/// Returns sort order for a physical/logical type.
pub fn get_sort_order(logical_type: LogicalType, physical_type: Type) -> SortOrder {
match logical_type {
// Unsigned byte-wise comparison.
LogicalType::UTF8
| LogicalType::JSON
| LogicalType::BSON
| LogicalType::ENUM => SortOrder::UNSIGNED,
LogicalType::INT_8
| LogicalType::INT_16
| LogicalType::INT_32
| LogicalType::INT_64 => SortOrder::SIGNED,
LogicalType::UINT_8
| LogicalType::UINT_16
| LogicalType::UINT_32
| LogicalType::UINT_64 => SortOrder::UNSIGNED,
// Signed comparison of the represented value.
LogicalType::DECIMAL => SortOrder::SIGNED,
LogicalType::DATE => SortOrder::SIGNED,
LogicalType::TIME_MILLIS
| LogicalType::TIME_MICROS
| LogicalType::TIMESTAMP_MILLIS
| LogicalType::TIMESTAMP_MICROS => SortOrder::SIGNED,
LogicalType::INTERVAL => SortOrder::UNSIGNED,
LogicalType::LIST | LogicalType::MAP | LogicalType::MAP_KEY_VALUE => {
SortOrder::UNDEFINED
}
// Fall back to physical type.
LogicalType::NONE => Self::get_default_sort_order(physical_type),
}
}
/// Returns default sort order based on physical type.
fn get_default_sort_order(physical_type: Type) -> SortOrder {
match physical_type {
// Order: false, true
Type::BOOLEAN => SortOrder::UNSIGNED,
Type::INT32 | Type::INT64 => SortOrder::SIGNED,
Type::INT96 => SortOrder::UNDEFINED,
// Notes to remember when comparing float/double values:
// If the min is a NaN, it should be ignored.
// If the max is a NaN, it should be ignored.
// If the min is +0, the row group may contain -0 values as well.
// If the max is -0, the row group may contain +0 values as well.
// When looking for NaN values, min and max should be ignored.
Type::FLOAT | Type::DOUBLE => SortOrder::SIGNED,
// unsigned byte-wise comparison
Type::BYTE_ARRAY | Type::FIXED_LEN_BYTE_ARRAY => SortOrder::UNSIGNED,
}
}
/// Returns sort order associated with this column order.
pub fn sort_order(&self) -> SortOrder {
match *self {
ColumnOrder::TYPE_DEFINED_ORDER(order) => order,
ColumnOrder::UNDEFINED => SortOrder::SIGNED,
}
}
}
impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl fmt::Display for LogicalType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl fmt::Display for Repetition {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl fmt::Display for Encoding {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl fmt::Display for Compression {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl fmt::Display for PageType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl fmt::Display for SortOrder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl fmt::Display for ColumnOrder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
// ----------------------------------------------------------------------
// parquet::Type <=> Type conversion
impl convert::From<parquet::Type> for Type {
fn from(value: parquet::Type) -> Self {
match value {
parquet::Type::Boolean => Type::BOOLEAN,
parquet::Type::Int32 => Type::INT32,
parquet::Type::Int64 => Type::INT64,
parquet::Type::Int96 => Type::INT96,
parquet::Type::Float => Type::FLOAT,
parquet::Type::Double => Type::DOUBLE,
parquet::Type::ByteArray => Type::BYTE_ARRAY,
parquet::Type::FixedLenByteArray => Type::FIXED_LEN_BYTE_ARRAY,
}
}
}
impl convert::From<Type> for parquet::Type {
fn from(value: Type) -> Self {
match value {
Type::BOOLEAN => parquet::Type::Boolean,
Type::INT32 => parquet::Type::Int32,
Type::INT64 => parquet::Type::Int64,
Type::INT96 => parquet::Type::Int96,
Type::FLOAT => parquet::Type::Float,
Type::DOUBLE => parquet::Type::Double,
Type::BYTE_ARRAY => parquet::Type::ByteArray,
Type::FIXED_LEN_BYTE_ARRAY => parquet::Type::FixedLenByteArray,
}
}
}
// ----------------------------------------------------------------------
// parquet::ConvertedType <=> LogicalType conversion
impl convert::From<Option<parquet::ConvertedType>> for LogicalType {
fn from(option: Option<parquet::ConvertedType>) -> Self {
match option {
None => LogicalType::NONE,
Some(value) => match value {
parquet::ConvertedType::Utf8 => LogicalType::UTF8,
parquet::ConvertedType::Map => LogicalType::MAP,
parquet::ConvertedType::MapKeyValue => LogicalType::MAP_KEY_VALUE,
parquet::ConvertedType::List => LogicalType::LIST,
parquet::ConvertedType::Enum => LogicalType::ENUM,
parquet::ConvertedType::Decimal => LogicalType::DECIMAL,
parquet::ConvertedType::Date => LogicalType::DATE,
parquet::ConvertedType::TimeMillis => LogicalType::TIME_MILLIS,
parquet::ConvertedType::TimeMicros => LogicalType::TIME_MICROS,
parquet::ConvertedType::TimestampMillis => LogicalType::TIMESTAMP_MILLIS,
parquet::ConvertedType::TimestampMicros => LogicalType::TIMESTAMP_MICROS,
parquet::ConvertedType::Uint8 => LogicalType::UINT_8,
parquet::ConvertedType::Uint16 => LogicalType::UINT_16,
parquet::ConvertedType::Uint32 => LogicalType::UINT_32,
parquet::ConvertedType::Uint64 => LogicalType::UINT_64,
parquet::ConvertedType::Int8 => LogicalType::INT_8,
parquet::ConvertedType::Int16 => LogicalType::INT_16,
parquet::ConvertedType::Int32 => LogicalType::INT_32,
parquet::ConvertedType::Int64 => LogicalType::INT_64,
parquet::ConvertedType::Json => LogicalType::JSON,
parquet::ConvertedType::Bson => LogicalType::BSON,
parquet::ConvertedType::Interval => LogicalType::INTERVAL,
},
}
}
}
impl convert::From<LogicalType> for Option<parquet::ConvertedType> {
fn from(value: LogicalType) -> Self {
match value {
LogicalType::NONE => None,
LogicalType::UTF8 => Some(parquet::ConvertedType::Utf8),
LogicalType::MAP => Some(parquet::ConvertedType::Map),
LogicalType::MAP_KEY_VALUE => Some(parquet::ConvertedType::MapKeyValue),
LogicalType::LIST => Some(parquet::ConvertedType::List),
LogicalType::ENUM => Some(parquet::ConvertedType::Enum),
LogicalType::DECIMAL => Some(parquet::ConvertedType::Decimal),
LogicalType::DATE => Some(parquet::ConvertedType::Date),
LogicalType::TIME_MILLIS => Some(parquet::ConvertedType::TimeMillis),
LogicalType::TIME_MICROS => Some(parquet::ConvertedType::TimeMicros),
LogicalType::TIMESTAMP_MILLIS => {
Some(parquet::ConvertedType::TimestampMillis)
}
LogicalType::TIMESTAMP_MICROS => {
Some(parquet::ConvertedType::TimestampMicros)
}
LogicalType::UINT_8 => Some(parquet::ConvertedType::Uint8),
LogicalType::UINT_16 => Some(parquet::ConvertedType::Uint16),
LogicalType::UINT_32 => Some(parquet::ConvertedType::Uint32),
LogicalType::UINT_64 => Some(parquet::ConvertedType::Uint64),
LogicalType::INT_8 => Some(parquet::ConvertedType::Int8),
LogicalType::INT_16 => Some(parquet::ConvertedType::Int16),
LogicalType::INT_32 => Some(parquet::ConvertedType::Int32),
LogicalType::INT_64 => Some(parquet::ConvertedType::Int64),
LogicalType::JSON => Some(parquet::ConvertedType::Json),
LogicalType::BSON => Some(parquet::ConvertedType::Bson),
LogicalType::INTERVAL => Some(parquet::ConvertedType::Interval),
}
}
}
// ----------------------------------------------------------------------
// parquet::FieldRepetitionType <=> Repetition conversion
impl convert::From<parquet::FieldRepetitionType> for Repetition {
fn from(value: parquet::FieldRepetitionType) -> Self {
match value {
parquet::FieldRepetitionType::Required => Repetition::REQUIRED,
parquet::FieldRepetitionType::Optional => Repetition::OPTIONAL,
parquet::FieldRepetitionType::Repeated => Repetition::REPEATED,
}
}
}
impl convert::From<Repetition> for parquet::FieldRepetitionType {
fn from(value: Repetition) -> Self {
match value {
Repetition::REQUIRED => parquet::FieldRepetitionType::Required,
Repetition::OPTIONAL => parquet::FieldRepetitionType::Optional,
Repetition::REPEATED => parquet::FieldRepetitionType::Repeated,
}
}
}
// ----------------------------------------------------------------------
// parquet::Encoding <=> Encoding conversion
impl convert::From<parquet::Encoding> for Encoding {
fn from(value: parquet::Encoding) -> Self {
match value {
parquet::Encoding::Plain => Encoding::PLAIN,
parquet::Encoding::PlainDictionary => Encoding::PLAIN_DICTIONARY,
parquet::Encoding::Rle => Encoding::RLE,
parquet::Encoding::BitPacked => Encoding::BIT_PACKED,
parquet::Encoding::DeltaBinaryPacked => Encoding::DELTA_BINARY_PACKED,
parquet::Encoding::DeltaLengthByteArray => Encoding::DELTA_LENGTH_BYTE_ARRAY,
parquet::Encoding::DeltaByteArray => Encoding::DELTA_BYTE_ARRAY,
parquet::Encoding::RleDictionary => Encoding::RLE_DICTIONARY,
}
}
}
impl convert::From<Encoding> for parquet::Encoding {
fn from(value: Encoding) -> Self {
match value {
Encoding::PLAIN => parquet::Encoding::Plain,
Encoding::PLAIN_DICTIONARY => parquet::Encoding::PlainDictionary,
Encoding::RLE => parquet::Encoding::Rle,
Encoding::BIT_PACKED => parquet::Encoding::BitPacked,
Encoding::DELTA_BINARY_PACKED => parquet::Encoding::DeltaBinaryPacked,
Encoding::DELTA_LENGTH_BYTE_ARRAY => parquet::Encoding::DeltaLengthByteArray,
Encoding::DELTA_BYTE_ARRAY => parquet::Encoding::DeltaByteArray,
Encoding::RLE_DICTIONARY => parquet::Encoding::RleDictionary,
}
}
}
// ----------------------------------------------------------------------
// parquet::CompressionCodec <=> Compression conversion
impl convert::From<parquet::CompressionCodec> for Compression {
fn from(value: parquet::CompressionCodec) -> Self {
match value {
parquet::CompressionCodec::Uncompressed => Compression::UNCOMPRESSED,
parquet::CompressionCodec::Snappy => Compression::SNAPPY,
parquet::CompressionCodec::Gzip => Compression::GZIP,
parquet::CompressionCodec::Lzo => Compression::LZO,
parquet::CompressionCodec::Brotli => Compression::BROTLI,
parquet::CompressionCodec::Lz4 => Compression::LZ4,
parquet::CompressionCodec::Zstd => Compression::ZSTD,
}
}
}
impl convert::From<Compression> for parquet::CompressionCodec {
fn from(value: Compression) -> Self {
match value {
Compression::UNCOMPRESSED => parquet::CompressionCodec::Uncompressed,
Compression::SNAPPY => parquet::CompressionCodec::Snappy,
Compression::GZIP => parquet::CompressionCodec::Gzip,
Compression::LZO => parquet::CompressionCodec::Lzo,
Compression::BROTLI => parquet::CompressionCodec::Brotli,
Compression::LZ4 => parquet::CompressionCodec::Lz4,
Compression::ZSTD => parquet::CompressionCodec::Zstd,
}
}
}
// ----------------------------------------------------------------------
// parquet::PageType <=> PageType conversion
impl convert::From<parquet::PageType> for PageType {
fn from(value: parquet::PageType) -> Self {
match value {
parquet::PageType::DataPage => PageType::DATA_PAGE,
parquet::PageType::IndexPage => PageType::INDEX_PAGE,
parquet::PageType::DictionaryPage => PageType::DICTIONARY_PAGE,
parquet::PageType::DataPageV2 => PageType::DATA_PAGE_V2,
}
}
}
impl convert::From<PageType> for parquet::PageType {
fn from(value: PageType) -> Self {
match value {
PageType::DATA_PAGE => parquet::PageType::DataPage,
PageType::INDEX_PAGE => parquet::PageType::IndexPage,
PageType::DICTIONARY_PAGE => parquet::PageType::DictionaryPage,
PageType::DATA_PAGE_V2 => parquet::PageType::DataPageV2,
}
}
}
// ----------------------------------------------------------------------
// String conversions for schema parsing.
impl str::FromStr for Repetition {
type Err = ParquetError;
fn from_str(s: &str) -> result::Result<Self, Self::Err> {
match s {
"REQUIRED" => Ok(Repetition::REQUIRED),
"OPTIONAL" => Ok(Repetition::OPTIONAL),
"REPEATED" => Ok(Repetition::REPEATED),
other => Err(general_err!("Invalid repetition {}", other)),
}
}
}
impl str::FromStr for Type {
type Err = ParquetError;
fn from_str(s: &str) -> result::Result<Self, Self::Err> {
match s {
"BOOLEAN" => Ok(Type::BOOLEAN),
"INT32" => Ok(Type::INT32),
"INT64" => Ok(Type::INT64),
"INT96" => Ok(Type::INT96),
"FLOAT" => Ok(Type::FLOAT),
"DOUBLE" => Ok(Type::DOUBLE),
"BYTE_ARRAY" | "BINARY" => Ok(Type::BYTE_ARRAY),
"FIXED_LEN_BYTE_ARRAY" => Ok(Type::FIXED_LEN_BYTE_ARRAY),
other => Err(general_err!("Invalid type {}", other)),
}
}
}
impl str::FromStr for LogicalType {
type Err = ParquetError;
fn from_str(s: &str) -> result::Result<Self, Self::Err> {
match s {
"NONE" => Ok(LogicalType::NONE),
"UTF8" => Ok(LogicalType::UTF8),
"MAP" => Ok(LogicalType::MAP),
"MAP_KEY_VALUE" => Ok(LogicalType::MAP_KEY_VALUE),
"LIST" => Ok(LogicalType::LIST),
"ENUM" => Ok(LogicalType::ENUM),
"DECIMAL" => Ok(LogicalType::DECIMAL),
"DATE" => Ok(LogicalType::DATE),
"TIME_MILLIS" => Ok(LogicalType::TIME_MILLIS),
"TIME_MICROS" => Ok(LogicalType::TIME_MICROS),
"TIMESTAMP_MILLIS" => Ok(LogicalType::TIMESTAMP_MILLIS),
"TIMESTAMP_MICROS" => Ok(LogicalType::TIMESTAMP_MICROS),
"UINT_8" => Ok(LogicalType::UINT_8),
"UINT_16" => Ok(LogicalType::UINT_16),
"UINT_32" => Ok(LogicalType::UINT_32),
"UINT_64" => Ok(LogicalType::UINT_64),
"INT_8" => Ok(LogicalType::INT_8),
"INT_16" => Ok(LogicalType::INT_16),
"INT_32" => Ok(LogicalType::INT_32),
"INT_64" => Ok(LogicalType::INT_64),
"JSON" => Ok(LogicalType::JSON),
"BSON" => Ok(LogicalType::BSON),
"INTERVAL" => Ok(LogicalType::INTERVAL),
other => Err(general_err!("Invalid logical type {}", other)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_display_type() {
assert_eq!(Type::BOOLEAN.to_string(), "BOOLEAN");
assert_eq!(Type::INT32.to_string(), "INT32");
assert_eq!(Type::INT64.to_string(), "INT64");
assert_eq!(Type::INT96.to_string(), "INT96");
assert_eq!(Type::FLOAT.to_string(), "FLOAT");
assert_eq!(Type::DOUBLE.to_string(), "DOUBLE");
assert_eq!(Type::BYTE_ARRAY.to_string(), "BYTE_ARRAY");
assert_eq!(
Type::FIXED_LEN_BYTE_ARRAY.to_string(),
"FIXED_LEN_BYTE_ARRAY"
);
}
#[test]
fn test_from_type() {
assert_eq!(Type::from(parquet::Type::Boolean), Type::BOOLEAN);
assert_eq!(Type::from(parquet::Type::Int32), Type::INT32);
assert_eq!(Type::from(parquet::Type::Int64), Type::INT64);
assert_eq!(Type::from(parquet::Type::Int96), Type::INT96);
assert_eq!(Type::from(parquet::Type::Float), Type::FLOAT);
assert_eq!(Type::from(parquet::Type::Double), Type::DOUBLE);
assert_eq!(Type::from(parquet::Type::ByteArray), Type::BYTE_ARRAY);
assert_eq!(
Type::from(parquet::Type::FixedLenByteArray),
Type::FIXED_LEN_BYTE_ARRAY
);
}
#[test]
fn test_into_type() {
assert_eq!(parquet::Type::Boolean, Type::BOOLEAN.into());
assert_eq!(parquet::Type::Int32, Type::INT32.into());
assert_eq!(parquet::Type::Int64, Type::INT64.into());
assert_eq!(parquet::Type::Int96, Type::INT96.into());
assert_eq!(parquet::Type::Float, Type::FLOAT.into());
assert_eq!(parquet::Type::Double, Type::DOUBLE.into());
assert_eq!(parquet::Type::ByteArray, Type::BYTE_ARRAY.into());
assert_eq!(
parquet::Type::FixedLenByteArray,
Type::FIXED_LEN_BYTE_ARRAY.into()
);
}
#[test]
fn test_from_string_into_type() {
assert_eq!(
Type::BOOLEAN.to_string().parse::<Type>().unwrap(),
Type::BOOLEAN
);
assert_eq!(
Type::INT32.to_string().parse::<Type>().unwrap(),
Type::INT32
);
assert_eq!(
Type::INT64.to_string().parse::<Type>().unwrap(),
Type::INT64
);
assert_eq!(
Type::INT96.to_string().parse::<Type>().unwrap(),
Type::INT96
);
assert_eq!(
Type::FLOAT.to_string().parse::<Type>().unwrap(),
Type::FLOAT
);
assert_eq!(
Type::DOUBLE.to_string().parse::<Type>().unwrap(),
Type::DOUBLE
);
assert_eq!(
Type::BYTE_ARRAY.to_string().parse::<Type>().unwrap(),
Type::BYTE_ARRAY
);
assert_eq!("BINARY".parse::<Type>().unwrap(), Type::BYTE_ARRAY);
assert_eq!(
Type::FIXED_LEN_BYTE_ARRAY
.to_string()
.parse::<Type>()
.unwrap(),
Type::FIXED_LEN_BYTE_ARRAY
);
}
#[test]
fn test_display_logical_type() {
assert_eq!(LogicalType::NONE.to_string(), "NONE");
assert_eq!(LogicalType::UTF8.to_string(), "UTF8");
assert_eq!(LogicalType::MAP.to_string(), "MAP");
assert_eq!(LogicalType::MAP_KEY_VALUE.to_string(), "MAP_KEY_VALUE");
assert_eq!(LogicalType::LIST.to_string(), "LIST");
assert_eq!(LogicalType::ENUM.to_string(), "ENUM");
assert_eq!(LogicalType::DECIMAL.to_string(), "DECIMAL");
assert_eq!(LogicalType::DATE.to_string(), "DATE");
assert_eq!(LogicalType::TIME_MILLIS.to_string(), "TIME_MILLIS");
assert_eq!(LogicalType::DATE.to_string(), "DATE");
assert_eq!(LogicalType::TIME_MICROS.to_string(), "TIME_MICROS");
assert_eq!(
LogicalType::TIMESTAMP_MILLIS.to_string(),
"TIMESTAMP_MILLIS"
);
assert_eq!(
LogicalType::TIMESTAMP_MICROS.to_string(),
"TIMESTAMP_MICROS"
);
assert_eq!(LogicalType::UINT_8.to_string(), "UINT_8");
assert_eq!(LogicalType::UINT_16.to_string(), "UINT_16");
assert_eq!(LogicalType::UINT_32.to_string(), "UINT_32");
assert_eq!(LogicalType::UINT_64.to_string(), "UINT_64");
assert_eq!(LogicalType::INT_8.to_string(), "INT_8");
assert_eq!(LogicalType::INT_16.to_string(), "INT_16");
assert_eq!(LogicalType::INT_32.to_string(), "INT_32");
assert_eq!(LogicalType::INT_64.to_string(), "INT_64");
assert_eq!(LogicalType::JSON.to_string(), "JSON");
assert_eq!(LogicalType::BSON.to_string(), "BSON");
assert_eq!(LogicalType::INTERVAL.to_string(), "INTERVAL");
}
#[test]
fn test_from_logical_type() {
assert_eq!(LogicalType::from(None), LogicalType::NONE);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::Utf8)),
LogicalType::UTF8
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::Map)),
LogicalType::MAP
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::MapKeyValue)),
LogicalType::MAP_KEY_VALUE
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::List)),
LogicalType::LIST
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::Enum)),
LogicalType::ENUM
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::Decimal)),
LogicalType::DECIMAL
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::Date)),
LogicalType::DATE
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::TimeMillis)),
LogicalType::TIME_MILLIS
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::TimeMicros)),
LogicalType::TIME_MICROS
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::TimestampMillis)),
LogicalType::TIMESTAMP_MILLIS
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::TimestampMicros)),
LogicalType::TIMESTAMP_MICROS
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::Uint8)),
LogicalType::UINT_8
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::Uint16)),
LogicalType::UINT_16
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::Uint32)),
LogicalType::UINT_32
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::Uint64)),
LogicalType::UINT_64
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::Int8)),
LogicalType::INT_8
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::Int16)),
LogicalType::INT_16
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::Int32)),
LogicalType::INT_32
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::Int64)),
LogicalType::INT_64
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::Json)),
LogicalType::JSON
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::Bson)),
LogicalType::BSON
);
assert_eq!(
LogicalType::from(Some(parquet::ConvertedType::Interval)),
LogicalType::INTERVAL
);
}
#[test]
fn test_into_logical_type() {
let converted_type: Option<parquet::ConvertedType> = None;
assert_eq!(converted_type, LogicalType::NONE.into());
assert_eq!(Some(parquet::ConvertedType::Utf8), LogicalType::UTF8.into());
assert_eq!(Some(parquet::ConvertedType::Map), LogicalType::MAP.into());
assert_eq!(
Some(parquet::ConvertedType::MapKeyValue),
LogicalType::MAP_KEY_VALUE.into()
);
assert_eq!(Some(parquet::ConvertedType::List), LogicalType::LIST.into());
assert_eq!(Some(parquet::ConvertedType::Enum), LogicalType::ENUM.into());
assert_eq!(
Some(parquet::ConvertedType::Decimal),
LogicalType::DECIMAL.into()
);
assert_eq!(Some(parquet::ConvertedType::Date), LogicalType::DATE.into());
assert_eq!(
Some(parquet::ConvertedType::TimeMillis),
LogicalType::TIME_MILLIS.into()
);
assert_eq!(
Some(parquet::ConvertedType::TimeMicros),
LogicalType::TIME_MICROS.into()
);
assert_eq!(
Some(parquet::ConvertedType::TimestampMillis),
LogicalType::TIMESTAMP_MILLIS.into()
);
assert_eq!(
Some(parquet::ConvertedType::TimestampMicros),
LogicalType::TIMESTAMP_MICROS.into()
);
assert_eq!(
Some(parquet::ConvertedType::Uint8),
LogicalType::UINT_8.into()
);
assert_eq!(
Some(parquet::ConvertedType::Uint16),
LogicalType::UINT_16.into()
);
assert_eq!(
Some(parquet::ConvertedType::Uint32),
LogicalType::UINT_32.into()
);
assert_eq!(
Some(parquet::ConvertedType::Uint64),
LogicalType::UINT_64.into()
);
assert_eq!(
Some(parquet::ConvertedType::Int8),
LogicalType::INT_8.into()
);
assert_eq!(
Some(parquet::ConvertedType::Int16),
LogicalType::INT_16.into()
);
assert_eq!(
Some(parquet::ConvertedType::Int32),
LogicalType::INT_32.into()
);
assert_eq!(
Some(parquet::ConvertedType::Int64),
LogicalType::INT_64.into()
);
assert_eq!(Some(parquet::ConvertedType::Json), LogicalType::JSON.into());
assert_eq!(Some(parquet::ConvertedType::Bson), LogicalType::BSON.into());
assert_eq!(
Some(parquet::ConvertedType::Interval),
LogicalType::INTERVAL.into()
);
}
#[test]
fn test_from_string_into_logical_type() {
assert_eq!(
LogicalType::NONE
.to_string()
.parse::<LogicalType>()
.unwrap(),
LogicalType::NONE
);
assert_eq!(
LogicalType::UTF8
.to_string()
.parse::<LogicalType>()
.unwrap(),
LogicalType::UTF8
);
assert_eq!(
LogicalType::MAP.to_string().parse::<LogicalType>().unwrap(),
LogicalType::MAP
);
assert_eq!(
LogicalType::MAP_KEY_VALUE
.to_string()
.parse::<LogicalType>()
.unwrap(),
LogicalType::MAP_KEY_VALUE
);
assert_eq!(
LogicalType::LIST