1mod footer_tail;
90mod memory;
91mod options;
92mod parser;
93mod push_decoder;
94pub(crate) mod reader;
95pub(crate) mod thrift;
96mod writer;
97
98use crate::basic::{EncodingMask, PageType};
99#[cfg(feature = "encryption")]
100use crate::encryption::decrypt::FileDecryptor;
101#[cfg(feature = "encryption")]
102use crate::file::column_crypto_metadata::ColumnCryptoMetaData;
103pub(crate) use crate::file::metadata::memory::HeapSize;
104#[cfg(feature = "encryption")]
105use crate::file::metadata::thrift::encryption::EncryptionAlgorithm;
106use crate::file::page_index::column_index::{ByteArrayColumnIndex, PrimitiveColumnIndex};
107use crate::file::page_index::{column_index::ColumnIndexMetaData, offset_index::PageLocation};
108use crate::file::statistics::Statistics;
109use crate::geospatial::statistics as geo_statistics;
110use crate::schema::types::{
111 ColumnDescPtr, ColumnDescriptor, ColumnPath, SchemaDescPtr, SchemaDescriptor,
112 Type as SchemaType,
113};
114use crate::thrift_struct;
115use crate::{
116 basic::BoundaryOrder,
117 errors::{ParquetError, Result},
118};
119use crate::{
120 basic::{ColumnOrder, Compression, Encoding, Type},
121 parquet_thrift::{
122 ElementType, FieldType, ReadThrift, ThriftCompactInputProtocol,
123 ThriftCompactOutputProtocol, WriteThrift, WriteThriftField,
124 },
125};
126use crate::{
127 data_type::private::ParquetValueType, file::page_index::offset_index::OffsetIndexMetaData,
128};
129
130pub use footer_tail::FooterTail;
131pub use options::{ParquetMetaDataOptions, ParquetStatisticsPolicy};
132pub use push_decoder::ParquetMetaDataPushDecoder;
133pub use reader::{PageIndexPolicy, ParquetMetaDataReader};
134use std::io::Write;
135use std::ops::Range;
136use std::sync::Arc;
137pub use writer::ParquetMetaDataWriter;
138pub(crate) use writer::ThriftMetadataWriter;
139
140pub type ParquetColumnIndex = Vec<Vec<ColumnIndexMetaData>>;
157
158pub type ParquetOffsetIndex = Vec<Vec<OffsetIndexMetaData>>;
170
171#[derive(Debug, Clone, PartialEq)]
189pub struct ParquetMetaData {
190 file_metadata: FileMetaData,
192 row_groups: Vec<RowGroupMetaData>,
194 column_index: Option<ParquetColumnIndex>,
196 offset_index: Option<ParquetOffsetIndex>,
198 #[cfg(feature = "encryption")]
200 file_decryptor: Option<Box<FileDecryptor>>,
201}
202
203impl ParquetMetaData {
204 pub fn new(file_metadata: FileMetaData, row_groups: Vec<RowGroupMetaData>) -> Self {
207 ParquetMetaData {
208 file_metadata,
209 row_groups,
210 column_index: None,
211 offset_index: None,
212 #[cfg(feature = "encryption")]
213 file_decryptor: None,
214 }
215 }
216
217 #[cfg(feature = "encryption")]
220 pub(crate) fn with_file_decryptor(&mut self, file_decryptor: Option<FileDecryptor>) {
221 self.file_decryptor = file_decryptor.map(Box::new);
222 }
223
224 pub fn into_builder(self) -> ParquetMetaDataBuilder {
226 self.into()
227 }
228
229 pub fn file_metadata(&self) -> &FileMetaData {
231 &self.file_metadata
232 }
233
234 #[cfg(feature = "encryption")]
236 pub(crate) fn file_decryptor(&self) -> Option<&FileDecryptor> {
237 self.file_decryptor.as_deref()
238 }
239
240 pub fn num_row_groups(&self) -> usize {
242 self.row_groups.len()
243 }
244
245 pub fn row_group(&self, i: usize) -> &RowGroupMetaData {
248 &self.row_groups[i]
249 }
250
251 pub fn row_groups(&self) -> &[RowGroupMetaData] {
253 &self.row_groups
254 }
255
256 pub fn column_index(&self) -> Option<&ParquetColumnIndex> {
263 self.column_index.as_ref()
264 }
265
266 pub fn offset_index(&self) -> Option<&ParquetOffsetIndex> {
273 self.offset_index.as_ref()
274 }
275
276 pub fn memory_size(&self) -> usize {
291 #[cfg(feature = "encryption")]
292 let encryption_size = self.file_decryptor.heap_size();
293 #[cfg(not(feature = "encryption"))]
294 let encryption_size = 0usize;
295
296 std::mem::size_of::<Self>()
297 + self.file_metadata.heap_size()
298 + self.row_groups.heap_size()
299 + self.column_index.heap_size()
300 + self.offset_index.heap_size()
301 + encryption_size
302 }
303
304 pub(crate) fn set_column_index(&mut self, index: Option<ParquetColumnIndex>) {
306 self.column_index = index;
307 }
308
309 pub(crate) fn set_offset_index(&mut self, index: Option<ParquetOffsetIndex>) {
311 self.offset_index = index;
312 }
313}
314
315pub struct ParquetMetaDataBuilder(ParquetMetaData);
353
354impl ParquetMetaDataBuilder {
355 pub fn new(file_meta_data: FileMetaData) -> Self {
357 Self(ParquetMetaData::new(file_meta_data, vec![]))
358 }
359
360 pub fn new_from_metadata(metadata: ParquetMetaData) -> Self {
362 Self(metadata)
363 }
364
365 pub fn add_row_group(mut self, row_group: RowGroupMetaData) -> Self {
367 self.0.row_groups.push(row_group);
368 self
369 }
370
371 pub fn set_row_groups(mut self, row_groups: Vec<RowGroupMetaData>) -> Self {
373 self.0.row_groups = row_groups;
374 self
375 }
376
377 pub fn take_row_groups(&mut self) -> Vec<RowGroupMetaData> {
383 std::mem::take(&mut self.0.row_groups)
384 }
385
386 pub fn row_groups(&self) -> &[RowGroupMetaData] {
388 &self.0.row_groups
389 }
390
391 pub fn set_column_index(mut self, column_index: Option<ParquetColumnIndex>) -> Self {
393 self.0.column_index = column_index;
394 self
395 }
396
397 pub fn take_column_index(&mut self) -> Option<ParquetColumnIndex> {
399 std::mem::take(&mut self.0.column_index)
400 }
401
402 pub fn column_index(&self) -> Option<&ParquetColumnIndex> {
404 self.0.column_index.as_ref()
405 }
406
407 pub fn set_offset_index(mut self, offset_index: Option<ParquetOffsetIndex>) -> Self {
409 self.0.offset_index = offset_index;
410 self
411 }
412
413 pub fn take_offset_index(&mut self) -> Option<ParquetOffsetIndex> {
415 std::mem::take(&mut self.0.offset_index)
416 }
417
418 pub fn offset_index(&self) -> Option<&ParquetOffsetIndex> {
420 self.0.offset_index.as_ref()
421 }
422
423 #[cfg(feature = "encryption")]
425 pub(crate) fn set_file_decryptor(mut self, file_decryptor: Option<FileDecryptor>) -> Self {
426 self.0.with_file_decryptor(file_decryptor);
427 self
428 }
429
430 pub fn build(self) -> ParquetMetaData {
432 let Self(metadata) = self;
433 metadata
434 }
435}
436
437impl From<ParquetMetaData> for ParquetMetaDataBuilder {
438 fn from(meta_data: ParquetMetaData) -> Self {
439 Self(meta_data)
440 }
441}
442
443thrift_struct!(
444pub struct KeyValue {
446 1: required string key
447 2: optional string value
448}
449);
450
451impl KeyValue {
452 pub fn new<F2>(key: String, value: F2) -> KeyValue
454 where
455 F2: Into<Option<String>>,
456 {
457 KeyValue {
458 key,
459 value: value.into(),
460 }
461 }
462}
463
464thrift_struct!(
465pub struct PageEncodingStats {
467 1: required PageType page_type;
468 2: required Encoding encoding;
469 3: required i32 count;
470}
471);
472
473#[derive(Debug, Clone, PartialEq)]
476enum ParquetPageEncodingStats {
477 Full(Vec<PageEncodingStats>),
479 Mask(EncodingMask),
481}
482
483pub type FileMetaDataPtr = Arc<FileMetaData>;
485
486#[derive(Debug, Clone, PartialEq)]
490pub struct FileMetaData {
491 version: i32,
492 num_rows: i64,
493 created_by: Option<String>,
494 key_value_metadata: Option<Vec<KeyValue>>,
495 schema_descr: SchemaDescPtr,
496 column_orders: Option<Vec<ColumnOrder>>,
497 #[cfg(feature = "encryption")]
498 encryption_algorithm: Option<Box<EncryptionAlgorithm>>,
499 #[cfg(feature = "encryption")]
500 footer_signing_key_metadata: Option<Vec<u8>>,
501}
502
503impl FileMetaData {
504 pub fn new(
506 version: i32,
507 num_rows: i64,
508 created_by: Option<String>,
509 key_value_metadata: Option<Vec<KeyValue>>,
510 schema_descr: SchemaDescPtr,
511 column_orders: Option<Vec<ColumnOrder>>,
512 ) -> Self {
513 FileMetaData {
514 version,
515 num_rows,
516 created_by,
517 key_value_metadata,
518 schema_descr,
519 column_orders,
520 #[cfg(feature = "encryption")]
521 encryption_algorithm: None,
522 #[cfg(feature = "encryption")]
523 footer_signing_key_metadata: None,
524 }
525 }
526
527 #[cfg(feature = "encryption")]
528 pub(crate) fn with_encryption_algorithm(
529 mut self,
530 encryption_algorithm: Option<EncryptionAlgorithm>,
531 ) -> Self {
532 self.encryption_algorithm = encryption_algorithm.map(Box::new);
533 self
534 }
535
536 #[cfg(feature = "encryption")]
537 pub(crate) fn with_footer_signing_key_metadata(
538 mut self,
539 footer_signing_key_metadata: Option<Vec<u8>>,
540 ) -> Self {
541 self.footer_signing_key_metadata = footer_signing_key_metadata;
542 self
543 }
544
545 pub fn version(&self) -> i32 {
547 self.version
548 }
549
550 pub fn num_rows(&self) -> i64 {
552 self.num_rows
553 }
554
555 pub fn created_by(&self) -> Option<&str> {
564 self.created_by.as_deref()
565 }
566
567 pub fn key_value_metadata(&self) -> Option<&Vec<KeyValue>> {
569 self.key_value_metadata.as_ref()
570 }
571
572 pub fn schema(&self) -> &SchemaType {
576 self.schema_descr.root_schema()
577 }
578
579 pub fn schema_descr(&self) -> &SchemaDescriptor {
581 &self.schema_descr
582 }
583
584 pub fn schema_descr_ptr(&self) -> SchemaDescPtr {
586 self.schema_descr.clone()
587 }
588
589 pub fn column_orders(&self) -> Option<&Vec<ColumnOrder>> {
597 self.column_orders.as_ref()
598 }
599
600 pub fn column_order(&self, i: usize) -> ColumnOrder {
603 self.column_orders
604 .as_ref()
605 .map(|data| data[i])
606 .unwrap_or(ColumnOrder::UNDEFINED)
607 }
608}
609
610thrift_struct!(
611pub struct SortingColumn {
613 1: required i32 column_idx
615
616 2: required bool descending
618
619 3: required bool nulls_first
622}
623);
624
625pub type RowGroupMetaDataPtr = Arc<RowGroupMetaData>;
627
628#[derive(Debug, Clone, PartialEq)]
633pub struct RowGroupMetaData {
634 columns: Vec<ColumnChunkMetaData>,
635 num_rows: i64,
636 sorting_columns: Option<Vec<SortingColumn>>,
637 total_byte_size: i64,
638 schema_descr: SchemaDescPtr,
639 file_offset: Option<i64>,
641 ordinal: Option<i16>,
643}
644
645impl RowGroupMetaData {
646 pub fn builder(schema_descr: SchemaDescPtr) -> RowGroupMetaDataBuilder {
648 RowGroupMetaDataBuilder::new(schema_descr)
649 }
650
651 pub fn num_columns(&self) -> usize {
653 self.columns.len()
654 }
655
656 pub fn column(&self, i: usize) -> &ColumnChunkMetaData {
658 &self.columns[i]
659 }
660
661 pub fn columns(&self) -> &[ColumnChunkMetaData] {
663 &self.columns
664 }
665
666 pub fn columns_mut(&mut self) -> &mut [ColumnChunkMetaData] {
668 &mut self.columns
669 }
670
671 pub fn num_rows(&self) -> i64 {
673 self.num_rows
674 }
675
676 pub fn sorting_columns(&self) -> Option<&Vec<SortingColumn>> {
678 self.sorting_columns.as_ref()
679 }
680
681 pub fn total_byte_size(&self) -> i64 {
683 self.total_byte_size
684 }
685
686 pub fn compressed_size(&self) -> i64 {
688 self.columns.iter().map(|c| c.total_compressed_size).sum()
689 }
690
691 pub fn schema_descr(&self) -> &SchemaDescriptor {
693 self.schema_descr.as_ref()
694 }
695
696 pub fn schema_descr_ptr(&self) -> SchemaDescPtr {
698 self.schema_descr.clone()
699 }
700
701 #[inline(always)]
706 pub fn ordinal(&self) -> Option<i16> {
707 self.ordinal
708 }
709
710 #[inline(always)]
712 pub fn file_offset(&self) -> Option<i64> {
713 self.file_offset
714 }
715
716 pub fn into_builder(self) -> RowGroupMetaDataBuilder {
718 RowGroupMetaDataBuilder(self)
719 }
720}
721
722pub struct RowGroupMetaDataBuilder(RowGroupMetaData);
724
725impl RowGroupMetaDataBuilder {
726 fn new(schema_descr: SchemaDescPtr) -> Self {
728 Self(RowGroupMetaData {
729 columns: Vec::with_capacity(schema_descr.num_columns()),
730 schema_descr,
731 file_offset: None,
732 num_rows: 0,
733 sorting_columns: None,
734 total_byte_size: 0,
735 ordinal: None,
736 })
737 }
738
739 pub fn set_num_rows(mut self, value: i64) -> Self {
741 self.0.num_rows = value;
742 self
743 }
744
745 pub fn set_sorting_columns(mut self, value: Option<Vec<SortingColumn>>) -> Self {
747 self.0.sorting_columns = value;
748 self
749 }
750
751 pub fn set_total_byte_size(mut self, value: i64) -> Self {
753 self.0.total_byte_size = value;
754 self
755 }
756
757 pub fn take_columns(&mut self) -> Vec<ColumnChunkMetaData> {
763 std::mem::take(&mut self.0.columns)
764 }
765
766 pub fn set_column_metadata(mut self, value: Vec<ColumnChunkMetaData>) -> Self {
768 self.0.columns = value;
769 self
770 }
771
772 pub fn add_column_metadata(mut self, value: ColumnChunkMetaData) -> Self {
774 self.0.columns.push(value);
775 self
776 }
777
778 pub fn set_ordinal(mut self, value: i16) -> Self {
780 self.0.ordinal = Some(value);
781 self
782 }
783
784 pub fn set_file_offset(mut self, value: i64) -> Self {
786 self.0.file_offset = Some(value);
787 self
788 }
789
790 pub fn build(self) -> Result<RowGroupMetaData> {
792 if self.0.schema_descr.num_columns() != self.0.columns.len() {
793 return Err(general_err!(
794 "Column length mismatch: {} != {}",
795 self.0.schema_descr.num_columns(),
796 self.0.columns.len()
797 ));
798 }
799
800 Ok(self.0)
801 }
802
803 pub(super) fn build_unchecked(self) -> RowGroupMetaData {
805 self.0
806 }
807}
808
809#[derive(Debug, Clone, PartialEq)]
811pub struct ColumnChunkMetaData {
812 column_descr: ColumnDescPtr,
813 encodings: EncodingMask,
814 file_path: Option<String>,
815 file_offset: i64,
816 num_values: i64,
817 compression: Compression,
818 total_compressed_size: i64,
819 total_uncompressed_size: i64,
820 data_page_offset: i64,
821 index_page_offset: Option<i64>,
822 dictionary_page_offset: Option<i64>,
823 statistics: Option<Statistics>,
824 geo_statistics: Option<Box<geo_statistics::GeospatialStatistics>>,
825 encoding_stats: Option<ParquetPageEncodingStats>,
826 bloom_filter_offset: Option<i64>,
827 bloom_filter_length: Option<i32>,
828 offset_index_offset: Option<i64>,
829 offset_index_length: Option<i32>,
830 column_index_offset: Option<i64>,
831 column_index_length: Option<i32>,
832 unencoded_byte_array_data_bytes: Option<i64>,
833 repetition_level_histogram: Option<LevelHistogram>,
834 definition_level_histogram: Option<LevelHistogram>,
835 #[cfg(feature = "encryption")]
836 column_crypto_metadata: Option<Box<ColumnCryptoMetaData>>,
837 #[cfg(feature = "encryption")]
838 encrypted_column_metadata: Option<Vec<u8>>,
839}
840
841#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
850pub struct LevelHistogram {
851 inner: Vec<i64>,
852}
853
854impl LevelHistogram {
855 pub fn try_new(max_level: i16) -> Option<Self> {
861 if max_level > 0 {
862 Some(Self {
863 inner: vec![0; max_level as usize + 1],
864 })
865 } else {
866 None
867 }
868 }
869 pub fn values(&self) -> &[i64] {
871 &self.inner
872 }
873
874 pub fn into_inner(self) -> Vec<i64> {
876 self.inner
877 }
878
879 pub fn get(&self, index: usize) -> Option<i64> {
886 self.inner.get(index).copied()
887 }
888
889 pub fn add(&mut self, other: &Self) {
894 assert_eq!(self.len(), other.len());
895 for (dst, src) in self.inner.iter_mut().zip(other.inner.iter()) {
896 *dst += src;
897 }
898 }
899
900 pub fn len(&self) -> usize {
902 self.inner.len()
903 }
904
905 pub fn is_empty(&self) -> bool {
907 self.inner.is_empty()
908 }
909
910 pub fn reset(&mut self) {
912 for value in self.inner.iter_mut() {
913 *value = 0;
914 }
915 }
916
917 pub fn update_from_levels(&mut self, levels: &[i16]) {
923 for &level in levels {
924 self.inner[level as usize] += 1;
925 }
926 }
927}
928
929impl From<Vec<i64>> for LevelHistogram {
930 fn from(inner: Vec<i64>) -> Self {
931 Self { inner }
932 }
933}
934
935impl From<LevelHistogram> for Vec<i64> {
936 fn from(value: LevelHistogram) -> Self {
937 value.into_inner()
938 }
939}
940
941impl HeapSize for LevelHistogram {
942 fn heap_size(&self) -> usize {
943 self.inner.heap_size()
944 }
945}
946
947impl ColumnChunkMetaData {
949 pub fn builder(column_descr: ColumnDescPtr) -> ColumnChunkMetaDataBuilder {
951 ColumnChunkMetaDataBuilder::new(column_descr)
952 }
953
954 pub fn file_path(&self) -> Option<&str> {
959 self.file_path.as_deref()
960 }
961
962 pub fn file_offset(&self) -> i64 {
969 self.file_offset
970 }
971
972 pub fn column_type(&self) -> Type {
974 self.column_descr.physical_type()
975 }
976
977 pub fn column_path(&self) -> &ColumnPath {
979 self.column_descr.path()
980 }
981
982 pub fn column_descr(&self) -> &ColumnDescriptor {
984 self.column_descr.as_ref()
985 }
986
987 pub fn column_descr_ptr(&self) -> ColumnDescPtr {
989 self.column_descr.clone()
990 }
991
992 pub fn encodings(&self) -> impl Iterator<Item = Encoding> {
994 self.encodings.encodings()
995 }
996
997 pub fn encodings_mask(&self) -> &EncodingMask {
999 &self.encodings
1000 }
1001
1002 pub fn num_values(&self) -> i64 {
1004 self.num_values
1005 }
1006
1007 pub fn compression(&self) -> Compression {
1009 self.compression
1010 }
1011
1012 pub fn compressed_size(&self) -> i64 {
1014 self.total_compressed_size
1015 }
1016
1017 pub fn uncompressed_size(&self) -> i64 {
1019 self.total_uncompressed_size
1020 }
1021
1022 pub fn data_page_offset(&self) -> i64 {
1024 self.data_page_offset
1025 }
1026
1027 pub fn index_page_offset(&self) -> Option<i64> {
1029 self.index_page_offset
1030 }
1031
1032 pub fn dictionary_page_offset(&self) -> Option<i64> {
1034 self.dictionary_page_offset
1035 }
1036
1037 pub fn byte_range(&self) -> (u64, u64) {
1039 let col_start = match self.dictionary_page_offset() {
1040 Some(dictionary_page_offset) => dictionary_page_offset,
1041 None => self.data_page_offset(),
1042 };
1043 let col_len = self.compressed_size();
1044 assert!(
1045 col_start >= 0 && col_len >= 0,
1046 "column start and length should not be negative"
1047 );
1048 (col_start as u64, col_len as u64)
1049 }
1050
1051 pub fn statistics(&self) -> Option<&Statistics> {
1054 self.statistics.as_ref()
1055 }
1056
1057 pub fn geo_statistics(&self) -> Option<&geo_statistics::GeospatialStatistics> {
1060 self.geo_statistics.as_deref()
1061 }
1062
1063 pub fn page_encoding_stats(&self) -> Option<&Vec<PageEncodingStats>> {
1070 match self.encoding_stats.as_ref() {
1071 Some(ParquetPageEncodingStats::Full(stats)) => Some(stats),
1072 _ => None,
1073 }
1074 }
1075
1076 pub fn page_encoding_stats_mask(&self) -> Option<&EncodingMask> {
1106 match self.encoding_stats.as_ref() {
1107 Some(ParquetPageEncodingStats::Mask(stats)) => Some(stats),
1108 _ => None,
1109 }
1110 }
1111
1112 pub fn bloom_filter_offset(&self) -> Option<i64> {
1114 self.bloom_filter_offset
1115 }
1116
1117 pub fn bloom_filter_length(&self) -> Option<i32> {
1119 self.bloom_filter_length
1120 }
1121
1122 pub fn column_index_offset(&self) -> Option<i64> {
1124 self.column_index_offset
1125 }
1126
1127 pub fn column_index_length(&self) -> Option<i32> {
1129 self.column_index_length
1130 }
1131
1132 pub(crate) fn column_index_range(&self) -> Option<Range<u64>> {
1134 let offset = u64::try_from(self.column_index_offset?).ok()?;
1135 let length = u64::try_from(self.column_index_length?).ok()?;
1136 Some(offset..(offset + length))
1137 }
1138
1139 pub fn offset_index_offset(&self) -> Option<i64> {
1141 self.offset_index_offset
1142 }
1143
1144 pub fn offset_index_length(&self) -> Option<i32> {
1146 self.offset_index_length
1147 }
1148
1149 pub(crate) fn offset_index_range(&self) -> Option<Range<u64>> {
1151 let offset = u64::try_from(self.offset_index_offset?).ok()?;
1152 let length = u64::try_from(self.offset_index_length?).ok()?;
1153 Some(offset..(offset + length))
1154 }
1155
1156 pub fn unencoded_byte_array_data_bytes(&self) -> Option<i64> {
1161 self.unencoded_byte_array_data_bytes
1162 }
1163
1164 pub fn repetition_level_histogram(&self) -> Option<&LevelHistogram> {
1170 self.repetition_level_histogram.as_ref()
1171 }
1172
1173 pub fn definition_level_histogram(&self) -> Option<&LevelHistogram> {
1179 self.definition_level_histogram.as_ref()
1180 }
1181
1182 #[cfg(feature = "encryption")]
1184 pub fn crypto_metadata(&self) -> Option<&ColumnCryptoMetaData> {
1185 self.column_crypto_metadata.as_deref()
1186 }
1187
1188 pub fn into_builder(self) -> ColumnChunkMetaDataBuilder {
1190 ColumnChunkMetaDataBuilder::from(self)
1191 }
1192}
1193
1194pub struct ColumnChunkMetaDataBuilder(ColumnChunkMetaData);
1213
1214impl ColumnChunkMetaDataBuilder {
1215 fn new(column_descr: ColumnDescPtr) -> Self {
1219 Self(ColumnChunkMetaData {
1220 column_descr,
1221 encodings: Default::default(),
1222 file_path: None,
1223 file_offset: 0,
1224 num_values: 0,
1225 compression: Compression::UNCOMPRESSED,
1226 total_compressed_size: 0,
1227 total_uncompressed_size: 0,
1228 data_page_offset: 0,
1229 index_page_offset: None,
1230 dictionary_page_offset: None,
1231 statistics: None,
1232 geo_statistics: None,
1233 encoding_stats: None,
1234 bloom_filter_offset: None,
1235 bloom_filter_length: None,
1236 offset_index_offset: None,
1237 offset_index_length: None,
1238 column_index_offset: None,
1239 column_index_length: None,
1240 unencoded_byte_array_data_bytes: None,
1241 repetition_level_histogram: None,
1242 definition_level_histogram: None,
1243 #[cfg(feature = "encryption")]
1244 column_crypto_metadata: None,
1245 #[cfg(feature = "encryption")]
1246 encrypted_column_metadata: None,
1247 })
1248 }
1249
1250 pub fn set_encodings(mut self, encodings: Vec<Encoding>) -> Self {
1252 self.0.encodings = EncodingMask::new_from_encodings(encodings.iter());
1253 self
1254 }
1255
1256 pub fn set_encodings_mask(mut self, encodings: EncodingMask) -> Self {
1258 self.0.encodings = encodings;
1259 self
1260 }
1261
1262 pub fn set_file_path(mut self, value: String) -> Self {
1264 self.0.file_path = Some(value);
1265 self
1266 }
1267
1268 pub fn set_num_values(mut self, value: i64) -> Self {
1270 self.0.num_values = value;
1271 self
1272 }
1273
1274 pub fn set_compression(mut self, value: Compression) -> Self {
1276 self.0.compression = value;
1277 self
1278 }
1279
1280 pub fn set_total_compressed_size(mut self, value: i64) -> Self {
1282 self.0.total_compressed_size = value;
1283 self
1284 }
1285
1286 pub fn set_total_uncompressed_size(mut self, value: i64) -> Self {
1288 self.0.total_uncompressed_size = value;
1289 self
1290 }
1291
1292 pub fn set_data_page_offset(mut self, value: i64) -> Self {
1294 self.0.data_page_offset = value;
1295 self
1296 }
1297
1298 pub fn set_dictionary_page_offset(mut self, value: Option<i64>) -> Self {
1300 self.0.dictionary_page_offset = value;
1301 self
1302 }
1303
1304 pub fn set_index_page_offset(mut self, value: Option<i64>) -> Self {
1306 self.0.index_page_offset = value;
1307 self
1308 }
1309
1310 pub fn set_statistics(mut self, value: Statistics) -> Self {
1312 self.0.statistics = Some(value);
1313 self
1314 }
1315
1316 pub fn set_geo_statistics(mut self, value: Box<geo_statistics::GeospatialStatistics>) -> Self {
1318 self.0.geo_statistics = Some(value);
1319 self
1320 }
1321
1322 pub fn clear_statistics(mut self) -> Self {
1324 self.0.statistics = None;
1325 self
1326 }
1327
1328 pub fn set_page_encoding_stats(mut self, value: Vec<PageEncodingStats>) -> Self {
1332 self.0.encoding_stats = Some(ParquetPageEncodingStats::Full(value));
1333 self
1334 }
1335
1336 pub fn set_page_encoding_stats_mask(mut self, value: EncodingMask) -> Self {
1340 self.0.encoding_stats = Some(ParquetPageEncodingStats::Mask(value));
1341 self
1342 }
1343
1344 pub fn clear_page_encoding_stats(mut self) -> Self {
1346 self.0.encoding_stats = None;
1347 self
1348 }
1349
1350 pub fn set_bloom_filter_offset(mut self, value: Option<i64>) -> Self {
1352 self.0.bloom_filter_offset = value;
1353 self
1354 }
1355
1356 pub fn set_bloom_filter_length(mut self, value: Option<i32>) -> Self {
1358 self.0.bloom_filter_length = value;
1359 self
1360 }
1361
1362 pub fn set_offset_index_offset(mut self, value: Option<i64>) -> Self {
1364 self.0.offset_index_offset = value;
1365 self
1366 }
1367
1368 pub fn set_offset_index_length(mut self, value: Option<i32>) -> Self {
1370 self.0.offset_index_length = value;
1371 self
1372 }
1373
1374 pub fn set_column_index_offset(mut self, value: Option<i64>) -> Self {
1376 self.0.column_index_offset = value;
1377 self
1378 }
1379
1380 pub fn set_column_index_length(mut self, value: Option<i32>) -> Self {
1382 self.0.column_index_length = value;
1383 self
1384 }
1385
1386 pub fn set_unencoded_byte_array_data_bytes(mut self, value: Option<i64>) -> Self {
1388 self.0.unencoded_byte_array_data_bytes = value;
1389 self
1390 }
1391
1392 pub fn set_repetition_level_histogram(mut self, value: Option<LevelHistogram>) -> Self {
1394 self.0.repetition_level_histogram = value;
1395 self
1396 }
1397
1398 pub fn set_definition_level_histogram(mut self, value: Option<LevelHistogram>) -> Self {
1400 self.0.definition_level_histogram = value;
1401 self
1402 }
1403
1404 #[cfg(feature = "encryption")]
1405 pub fn set_column_crypto_metadata(mut self, value: Option<ColumnCryptoMetaData>) -> Self {
1407 self.0.column_crypto_metadata = value.map(Box::new);
1408 self
1409 }
1410
1411 #[cfg(feature = "encryption")]
1412 pub fn set_encrypted_column_metadata(mut self, value: Option<Vec<u8>>) -> Self {
1414 self.0.encrypted_column_metadata = value;
1415 self
1416 }
1417
1418 pub fn build(self) -> Result<ColumnChunkMetaData> {
1420 Ok(self.0)
1421 }
1422}
1423
1424pub struct ColumnIndexBuilder {
1429 column_type: Type,
1430 null_pages: Vec<bool>,
1431 min_values: Vec<Vec<u8>>,
1432 max_values: Vec<Vec<u8>>,
1433 null_counts: Vec<i64>,
1434 boundary_order: BoundaryOrder,
1435 repetition_level_histograms: Option<Vec<i64>>,
1437 definition_level_histograms: Option<Vec<i64>>,
1439 valid: bool,
1447}
1448
1449impl ColumnIndexBuilder {
1450 pub fn new(column_type: Type) -> Self {
1452 ColumnIndexBuilder {
1453 column_type,
1454 null_pages: Vec::new(),
1455 min_values: Vec::new(),
1456 max_values: Vec::new(),
1457 null_counts: Vec::new(),
1458 boundary_order: BoundaryOrder::UNORDERED,
1459 repetition_level_histograms: None,
1460 definition_level_histograms: None,
1461 valid: true,
1462 }
1463 }
1464
1465 pub fn append(
1467 &mut self,
1468 null_page: bool,
1469 min_value: Vec<u8>,
1470 max_value: Vec<u8>,
1471 null_count: i64,
1472 ) {
1473 self.null_pages.push(null_page);
1474 self.min_values.push(min_value);
1475 self.max_values.push(max_value);
1476 self.null_counts.push(null_count);
1477 }
1478
1479 pub fn append_histograms(
1484 &mut self,
1485 repetition_level_histogram: &Option<LevelHistogram>,
1486 definition_level_histogram: &Option<LevelHistogram>,
1487 ) {
1488 if !self.valid {
1489 return;
1490 }
1491 if let Some(rep_lvl_hist) = repetition_level_histogram {
1492 let hist = self.repetition_level_histograms.get_or_insert(Vec::new());
1493 hist.reserve(rep_lvl_hist.len());
1494 hist.extend(rep_lvl_hist.values());
1495 }
1496 if let Some(def_lvl_hist) = definition_level_histogram {
1497 let hist = self.definition_level_histograms.get_or_insert(Vec::new());
1498 hist.reserve(def_lvl_hist.len());
1499 hist.extend(def_lvl_hist.values());
1500 }
1501 }
1502
1503 pub fn set_boundary_order(&mut self, boundary_order: BoundaryOrder) {
1505 self.boundary_order = boundary_order;
1506 }
1507
1508 pub fn to_invalid(&mut self) {
1510 self.valid = false;
1511 }
1512
1513 pub fn valid(&self) -> bool {
1515 self.valid
1516 }
1517
1518 pub fn build(self) -> Result<ColumnIndexMetaData> {
1522 Ok(match self.column_type {
1523 Type::BOOLEAN => {
1524 let index = self.build_page_index()?;
1525 ColumnIndexMetaData::BOOLEAN(index)
1526 }
1527 Type::INT32 => {
1528 let index = self.build_page_index()?;
1529 ColumnIndexMetaData::INT32(index)
1530 }
1531 Type::INT64 => {
1532 let index = self.build_page_index()?;
1533 ColumnIndexMetaData::INT64(index)
1534 }
1535 Type::INT96 => {
1536 let index = self.build_page_index()?;
1537 ColumnIndexMetaData::INT96(index)
1538 }
1539 Type::FLOAT => {
1540 let index = self.build_page_index()?;
1541 ColumnIndexMetaData::FLOAT(index)
1542 }
1543 Type::DOUBLE => {
1544 let index = self.build_page_index()?;
1545 ColumnIndexMetaData::DOUBLE(index)
1546 }
1547 Type::BYTE_ARRAY => {
1548 let index = self.build_byte_array_index()?;
1549 ColumnIndexMetaData::BYTE_ARRAY(index)
1550 }
1551 Type::FIXED_LEN_BYTE_ARRAY => {
1552 let index = self.build_byte_array_index()?;
1553 ColumnIndexMetaData::FIXED_LEN_BYTE_ARRAY(index)
1554 }
1555 })
1556 }
1557
1558 fn build_page_index<T>(self) -> Result<PrimitiveColumnIndex<T>>
1559 where
1560 T: ParquetValueType,
1561 {
1562 let min_values: Vec<&[u8]> = self.min_values.iter().map(|v| v.as_slice()).collect();
1563 let max_values: Vec<&[u8]> = self.max_values.iter().map(|v| v.as_slice()).collect();
1564
1565 PrimitiveColumnIndex::try_new(
1566 self.null_pages,
1567 self.boundary_order,
1568 Some(self.null_counts),
1569 self.repetition_level_histograms,
1570 self.definition_level_histograms,
1571 min_values,
1572 max_values,
1573 )
1574 }
1575
1576 fn build_byte_array_index(self) -> Result<ByteArrayColumnIndex> {
1577 let min_values: Vec<&[u8]> = self.min_values.iter().map(|v| v.as_slice()).collect();
1578 let max_values: Vec<&[u8]> = self.max_values.iter().map(|v| v.as_slice()).collect();
1579
1580 ByteArrayColumnIndex::try_new(
1581 self.null_pages,
1582 self.boundary_order,
1583 Some(self.null_counts),
1584 self.repetition_level_histograms,
1585 self.definition_level_histograms,
1586 min_values,
1587 max_values,
1588 )
1589 }
1590}
1591
1592impl From<ColumnChunkMetaData> for ColumnChunkMetaDataBuilder {
1593 fn from(value: ColumnChunkMetaData) -> Self {
1594 ColumnChunkMetaDataBuilder(value)
1595 }
1596}
1597
1598pub struct OffsetIndexBuilder {
1602 offset_array: Vec<i64>,
1603 compressed_page_size_array: Vec<i32>,
1604 first_row_index_array: Vec<i64>,
1605 unencoded_byte_array_data_bytes_array: Option<Vec<i64>>,
1606 current_first_row_index: i64,
1607}
1608
1609impl Default for OffsetIndexBuilder {
1610 fn default() -> Self {
1611 Self::new()
1612 }
1613}
1614
1615impl OffsetIndexBuilder {
1616 pub fn new() -> Self {
1618 OffsetIndexBuilder {
1619 offset_array: Vec::new(),
1620 compressed_page_size_array: Vec::new(),
1621 first_row_index_array: Vec::new(),
1622 unencoded_byte_array_data_bytes_array: None,
1623 current_first_row_index: 0,
1624 }
1625 }
1626
1627 pub fn append_row_count(&mut self, row_count: i64) {
1629 let current_page_row_index = self.current_first_row_index;
1630 self.first_row_index_array.push(current_page_row_index);
1631 self.current_first_row_index += row_count;
1632 }
1633
1634 pub fn append_offset_and_size(&mut self, offset: i64, compressed_page_size: i32) {
1636 self.offset_array.push(offset);
1637 self.compressed_page_size_array.push(compressed_page_size);
1638 }
1639
1640 pub fn append_unencoded_byte_array_data_bytes(
1642 &mut self,
1643 unencoded_byte_array_data_bytes: Option<i64>,
1644 ) {
1645 if let Some(val) = unencoded_byte_array_data_bytes {
1646 self.unencoded_byte_array_data_bytes_array
1647 .get_or_insert(Vec::new())
1648 .push(val);
1649 }
1650 }
1651
1652 pub fn build(self) -> OffsetIndexMetaData {
1654 let locations = self
1655 .offset_array
1656 .iter()
1657 .zip(self.compressed_page_size_array.iter())
1658 .zip(self.first_row_index_array.iter())
1659 .map(|((offset, size), row_index)| PageLocation {
1660 offset: *offset,
1661 compressed_page_size: *size,
1662 first_row_index: *row_index,
1663 })
1664 .collect::<Vec<_>>();
1665 OffsetIndexMetaData {
1666 page_locations: locations,
1667 unencoded_byte_array_data_bytes: self.unencoded_byte_array_data_bytes_array,
1668 }
1669 }
1670}
1671
1672#[cfg(test)]
1673mod tests {
1674 use super::*;
1675 use crate::basic::{PageType, SortOrder};
1676 use crate::file::metadata::thrift::tests::{
1677 read_column_chunk, read_column_chunk_with_options, read_row_group,
1678 };
1679
1680 #[test]
1681 fn test_row_group_metadata_thrift_conversion() {
1682 let schema_descr = get_test_schema_descr();
1683
1684 let mut columns = vec![];
1685 for ptr in schema_descr.columns() {
1686 let column = ColumnChunkMetaData::builder(ptr.clone()).build().unwrap();
1687 columns.push(column);
1688 }
1689 let row_group_meta = RowGroupMetaData::builder(schema_descr.clone())
1690 .set_num_rows(1000)
1691 .set_total_byte_size(2000)
1692 .set_column_metadata(columns)
1693 .set_ordinal(1)
1694 .build()
1695 .unwrap();
1696
1697 let mut buf = Vec::new();
1698 let mut writer = ThriftCompactOutputProtocol::new(&mut buf);
1699 row_group_meta.write_thrift(&mut writer).unwrap();
1700
1701 let row_group_res = read_row_group(&mut buf, schema_descr).unwrap();
1702
1703 assert_eq!(row_group_res, row_group_meta);
1704 }
1705
1706 #[test]
1707 fn test_row_group_metadata_thrift_conversion_empty() {
1708 let schema_descr = get_test_schema_descr();
1709
1710 let row_group_meta = RowGroupMetaData::builder(schema_descr).build();
1711
1712 assert!(row_group_meta.is_err());
1713 if let Err(e) = row_group_meta {
1714 assert_eq!(
1715 format!("{e}"),
1716 "Parquet error: Column length mismatch: 2 != 0"
1717 );
1718 }
1719 }
1720
1721 #[test]
1723 fn test_row_group_metadata_thrift_corrupted() {
1724 let schema_descr_2cols = Arc::new(SchemaDescriptor::new(Arc::new(
1725 SchemaType::group_type_builder("schema")
1726 .with_fields(vec![
1727 Arc::new(
1728 SchemaType::primitive_type_builder("a", Type::INT32)
1729 .build()
1730 .unwrap(),
1731 ),
1732 Arc::new(
1733 SchemaType::primitive_type_builder("b", Type::INT32)
1734 .build()
1735 .unwrap(),
1736 ),
1737 ])
1738 .build()
1739 .unwrap(),
1740 )));
1741
1742 let schema_descr_3cols = Arc::new(SchemaDescriptor::new(Arc::new(
1743 SchemaType::group_type_builder("schema")
1744 .with_fields(vec![
1745 Arc::new(
1746 SchemaType::primitive_type_builder("a", Type::INT32)
1747 .build()
1748 .unwrap(),
1749 ),
1750 Arc::new(
1751 SchemaType::primitive_type_builder("b", Type::INT32)
1752 .build()
1753 .unwrap(),
1754 ),
1755 Arc::new(
1756 SchemaType::primitive_type_builder("c", Type::INT32)
1757 .build()
1758 .unwrap(),
1759 ),
1760 ])
1761 .build()
1762 .unwrap(),
1763 )));
1764
1765 let row_group_meta_2cols = RowGroupMetaData::builder(schema_descr_2cols.clone())
1766 .set_num_rows(1000)
1767 .set_total_byte_size(2000)
1768 .set_column_metadata(vec![
1769 ColumnChunkMetaData::builder(schema_descr_2cols.column(0))
1770 .build()
1771 .unwrap(),
1772 ColumnChunkMetaData::builder(schema_descr_2cols.column(1))
1773 .build()
1774 .unwrap(),
1775 ])
1776 .set_ordinal(1)
1777 .build()
1778 .unwrap();
1779 let mut buf = Vec::new();
1780 let mut writer = ThriftCompactOutputProtocol::new(&mut buf);
1781 row_group_meta_2cols.write_thrift(&mut writer).unwrap();
1782
1783 let err = read_row_group(&mut buf, schema_descr_3cols)
1784 .unwrap_err()
1785 .to_string();
1786 assert_eq!(
1787 err,
1788 "Parquet error: Column count mismatch. Schema has 3 columns while Row Group has 2"
1789 );
1790 }
1791
1792 #[test]
1793 fn test_column_chunk_metadata_thrift_conversion() {
1794 let column_descr = get_test_schema_descr().column(0);
1795 let col_metadata = ColumnChunkMetaData::builder(column_descr.clone())
1796 .set_encodings_mask(EncodingMask::new_from_encodings(
1797 [Encoding::PLAIN, Encoding::RLE].iter(),
1798 ))
1799 .set_file_path("file_path".to_owned())
1800 .set_num_values(1000)
1801 .set_compression(Compression::SNAPPY)
1802 .set_total_compressed_size(2000)
1803 .set_total_uncompressed_size(3000)
1804 .set_data_page_offset(4000)
1805 .set_dictionary_page_offset(Some(5000))
1806 .set_page_encoding_stats(vec![
1807 PageEncodingStats {
1808 page_type: PageType::DATA_PAGE,
1809 encoding: Encoding::PLAIN,
1810 count: 3,
1811 },
1812 PageEncodingStats {
1813 page_type: PageType::DATA_PAGE,
1814 encoding: Encoding::RLE,
1815 count: 5,
1816 },
1817 ])
1818 .set_bloom_filter_offset(Some(6000))
1819 .set_bloom_filter_length(Some(25))
1820 .set_offset_index_offset(Some(7000))
1821 .set_offset_index_length(Some(25))
1822 .set_column_index_offset(Some(8000))
1823 .set_column_index_length(Some(25))
1824 .set_unencoded_byte_array_data_bytes(Some(2000))
1825 .set_repetition_level_histogram(Some(LevelHistogram::from(vec![100, 100])))
1826 .set_definition_level_histogram(Some(LevelHistogram::from(vec![0, 200])))
1827 .build()
1828 .unwrap();
1829
1830 let mut buf = Vec::new();
1831 let mut writer = ThriftCompactOutputProtocol::new(&mut buf);
1832 col_metadata.write_thrift(&mut writer).unwrap();
1833 let col_chunk_res = read_column_chunk(&mut buf, column_descr.clone()).unwrap();
1834
1835 let expected_metadata = ColumnChunkMetaData::builder(column_descr)
1836 .set_encodings_mask(EncodingMask::new_from_encodings(
1837 [Encoding::PLAIN, Encoding::RLE].iter(),
1838 ))
1839 .set_file_path("file_path".to_owned())
1840 .set_num_values(1000)
1841 .set_compression(Compression::SNAPPY)
1842 .set_total_compressed_size(2000)
1843 .set_total_uncompressed_size(3000)
1844 .set_data_page_offset(4000)
1845 .set_dictionary_page_offset(Some(5000))
1846 .set_page_encoding_stats_mask(EncodingMask::new_from_encodings(
1847 [Encoding::PLAIN, Encoding::RLE].iter(),
1848 ))
1849 .set_bloom_filter_offset(Some(6000))
1850 .set_bloom_filter_length(Some(25))
1851 .set_offset_index_offset(Some(7000))
1852 .set_offset_index_length(Some(25))
1853 .set_column_index_offset(Some(8000))
1854 .set_column_index_length(Some(25))
1855 .set_unencoded_byte_array_data_bytes(Some(2000))
1856 .set_repetition_level_histogram(Some(LevelHistogram::from(vec![100, 100])))
1857 .set_definition_level_histogram(Some(LevelHistogram::from(vec![0, 200])))
1858 .build()
1859 .unwrap();
1860
1861 assert_eq!(col_chunk_res, expected_metadata);
1862 }
1863
1864 #[test]
1865 fn test_column_chunk_metadata_thrift_conversion_full_stats() {
1866 let column_descr = get_test_schema_descr().column(0);
1867 let stats = vec![
1868 PageEncodingStats {
1869 page_type: PageType::DATA_PAGE,
1870 encoding: Encoding::PLAIN,
1871 count: 3,
1872 },
1873 PageEncodingStats {
1874 page_type: PageType::DATA_PAGE,
1875 encoding: Encoding::RLE,
1876 count: 5,
1877 },
1878 ];
1879 let col_metadata = ColumnChunkMetaData::builder(column_descr.clone())
1880 .set_encodings_mask(EncodingMask::new_from_encodings(
1881 [Encoding::PLAIN, Encoding::RLE].iter(),
1882 ))
1883 .set_num_values(1000)
1884 .set_compression(Compression::SNAPPY)
1885 .set_total_compressed_size(2000)
1886 .set_total_uncompressed_size(3000)
1887 .set_data_page_offset(4000)
1888 .set_page_encoding_stats(stats)
1889 .build()
1890 .unwrap();
1891
1892 let mut buf = Vec::new();
1893 let mut writer = ThriftCompactOutputProtocol::new(&mut buf);
1894 col_metadata.write_thrift(&mut writer).unwrap();
1895
1896 let options = ParquetMetaDataOptions::new().with_encoding_stats_as_mask(false);
1897 let col_chunk_res =
1898 read_column_chunk_with_options(&mut buf, column_descr, Some(&options)).unwrap();
1899
1900 assert_eq!(col_chunk_res, col_metadata);
1901 }
1902
1903 #[test]
1904 fn test_column_chunk_metadata_thrift_conversion_empty() {
1905 let column_descr = get_test_schema_descr().column(0);
1906
1907 let col_metadata = ColumnChunkMetaData::builder(column_descr.clone())
1908 .build()
1909 .unwrap();
1910
1911 let mut buf = Vec::new();
1912 let mut writer = ThriftCompactOutputProtocol::new(&mut buf);
1913 col_metadata.write_thrift(&mut writer).unwrap();
1914 let col_chunk_res = read_column_chunk(&mut buf, column_descr).unwrap();
1915
1916 assert_eq!(col_chunk_res, col_metadata);
1917 }
1918
1919 #[test]
1920 fn test_compressed_size() {
1921 let schema_descr = get_test_schema_descr();
1922
1923 let mut columns = vec![];
1924 for column_descr in schema_descr.columns() {
1925 let column = ColumnChunkMetaData::builder(column_descr.clone())
1926 .set_total_compressed_size(500)
1927 .set_total_uncompressed_size(700)
1928 .build()
1929 .unwrap();
1930 columns.push(column);
1931 }
1932 let row_group_meta = RowGroupMetaData::builder(schema_descr)
1933 .set_num_rows(1000)
1934 .set_column_metadata(columns)
1935 .build()
1936 .unwrap();
1937
1938 let compressed_size_res: i64 = row_group_meta.compressed_size();
1939 let compressed_size_exp: i64 = 1000;
1940
1941 assert_eq!(compressed_size_res, compressed_size_exp);
1942 }
1943
1944 #[test]
1945 fn test_memory_size() {
1946 let schema_descr = get_test_schema_descr();
1947
1948 let columns = schema_descr
1949 .columns()
1950 .iter()
1951 .map(|column_descr| {
1952 ColumnChunkMetaData::builder(column_descr.clone())
1953 .set_statistics(Statistics::new::<i32>(None, None, None, None, false))
1954 .build()
1955 })
1956 .collect::<Result<Vec<_>>>()
1957 .unwrap();
1958 let row_group_meta = RowGroupMetaData::builder(schema_descr.clone())
1959 .set_num_rows(1000)
1960 .set_column_metadata(columns)
1961 .build()
1962 .unwrap();
1963 let row_group_meta = vec![row_group_meta];
1964
1965 let version = 2;
1966 let num_rows = 1000;
1967 let created_by = Some(String::from("test harness"));
1968 let key_value_metadata = Some(vec![KeyValue::new(
1969 String::from("Foo"),
1970 Some(String::from("bar")),
1971 )]);
1972 let column_orders = Some(vec![
1973 ColumnOrder::UNDEFINED,
1974 ColumnOrder::TYPE_DEFINED_ORDER(SortOrder::UNSIGNED),
1975 ]);
1976 let file_metadata = FileMetaData::new(
1977 version,
1978 num_rows,
1979 created_by,
1980 key_value_metadata,
1981 schema_descr.clone(),
1982 column_orders,
1983 );
1984
1985 let columns_with_stats = schema_descr
1987 .columns()
1988 .iter()
1989 .map(|column_descr| {
1990 ColumnChunkMetaData::builder(column_descr.clone())
1991 .set_statistics(Statistics::new::<i32>(
1992 Some(0),
1993 Some(100),
1994 None,
1995 None,
1996 false,
1997 ))
1998 .build()
1999 })
2000 .collect::<Result<Vec<_>>>()
2001 .unwrap();
2002
2003 let row_group_meta_with_stats = RowGroupMetaData::builder(schema_descr)
2004 .set_num_rows(1000)
2005 .set_column_metadata(columns_with_stats)
2006 .build()
2007 .unwrap();
2008 let row_group_meta_with_stats = vec![row_group_meta_with_stats];
2009
2010 let parquet_meta = ParquetMetaDataBuilder::new(file_metadata.clone())
2011 .set_row_groups(row_group_meta_with_stats)
2012 .build();
2013
2014 #[cfg(not(feature = "encryption"))]
2015 let base_expected_size = 2766;
2016 #[cfg(feature = "encryption")]
2017 let base_expected_size = 2934;
2018
2019 assert_eq!(parquet_meta.memory_size(), base_expected_size);
2020
2021 let mut column_index = ColumnIndexBuilder::new(Type::BOOLEAN);
2022 column_index.append(false, vec![1u8], vec![2u8, 3u8], 4);
2023 let column_index = column_index.build().unwrap();
2024 let native_index = match column_index {
2025 ColumnIndexMetaData::BOOLEAN(index) => index,
2026 _ => panic!("wrong type of column index"),
2027 };
2028
2029 let mut offset_index = OffsetIndexBuilder::new();
2031 offset_index.append_row_count(1);
2032 offset_index.append_offset_and_size(2, 3);
2033 offset_index.append_unencoded_byte_array_data_bytes(Some(10));
2034 offset_index.append_row_count(1);
2035 offset_index.append_offset_and_size(2, 3);
2036 offset_index.append_unencoded_byte_array_data_bytes(Some(10));
2037 let offset_index = offset_index.build();
2038
2039 let parquet_meta = ParquetMetaDataBuilder::new(file_metadata)
2040 .set_row_groups(row_group_meta)
2041 .set_column_index(Some(vec![vec![ColumnIndexMetaData::BOOLEAN(native_index)]]))
2042 .set_offset_index(Some(vec![vec![offset_index]]))
2043 .build();
2044
2045 #[cfg(not(feature = "encryption"))]
2046 let bigger_expected_size = 3192;
2047 #[cfg(feature = "encryption")]
2048 let bigger_expected_size = 3360;
2049
2050 assert!(bigger_expected_size > base_expected_size);
2052 assert_eq!(parquet_meta.memory_size(), bigger_expected_size);
2053 }
2054
2055 #[test]
2056 #[cfg(feature = "encryption")]
2057 fn test_memory_size_with_decryptor() {
2058 use crate::encryption::decrypt::FileDecryptionProperties;
2059 use crate::file::metadata::thrift::encryption::AesGcmV1;
2060
2061 let schema_descr = get_test_schema_descr();
2062
2063 let columns = schema_descr
2064 .columns()
2065 .iter()
2066 .map(|column_descr| ColumnChunkMetaData::builder(column_descr.clone()).build())
2067 .collect::<Result<Vec<_>>>()
2068 .unwrap();
2069 let row_group_meta = RowGroupMetaData::builder(schema_descr.clone())
2070 .set_num_rows(1000)
2071 .set_column_metadata(columns)
2072 .build()
2073 .unwrap();
2074 let row_group_meta = vec![row_group_meta];
2075
2076 let version = 2;
2077 let num_rows = 1000;
2078 let aad_file_unique = vec![1u8; 8];
2079 let aad_prefix = vec![2u8; 8];
2080 let encryption_algorithm = EncryptionAlgorithm::AES_GCM_V1(AesGcmV1 {
2081 aad_prefix: Some(aad_prefix.clone()),
2082 aad_file_unique: Some(aad_file_unique.clone()),
2083 supply_aad_prefix: Some(true),
2084 });
2085 let footer_key_metadata = Some(vec![3u8; 8]);
2086 let file_metadata =
2087 FileMetaData::new(version, num_rows, None, None, schema_descr.clone(), None)
2088 .with_encryption_algorithm(Some(encryption_algorithm))
2089 .with_footer_signing_key_metadata(footer_key_metadata.clone());
2090
2091 let parquet_meta_data = ParquetMetaDataBuilder::new(file_metadata.clone())
2092 .set_row_groups(row_group_meta.clone())
2093 .build();
2094
2095 let base_expected_size = 2058;
2096 assert_eq!(parquet_meta_data.memory_size(), base_expected_size);
2097
2098 let footer_key = "0123456789012345".as_bytes();
2099 let column_key = "1234567890123450".as_bytes();
2100 let mut decryption_properties_builder =
2101 FileDecryptionProperties::builder(footer_key.to_vec())
2102 .with_aad_prefix(aad_prefix.clone());
2103 for column in schema_descr.columns() {
2104 decryption_properties_builder = decryption_properties_builder
2105 .with_column_key(&column.path().string(), column_key.to_vec());
2106 }
2107 let decryption_properties = decryption_properties_builder.build().unwrap();
2108 let decryptor = FileDecryptor::new(
2109 &decryption_properties,
2110 footer_key_metadata.as_deref(),
2111 aad_file_unique,
2112 aad_prefix,
2113 )
2114 .unwrap();
2115
2116 let parquet_meta_data = ParquetMetaDataBuilder::new(file_metadata.clone())
2117 .set_row_groups(row_group_meta.clone())
2118 .set_file_decryptor(Some(decryptor))
2119 .build();
2120
2121 let expected_size_with_decryptor = 3072;
2122 assert!(expected_size_with_decryptor > base_expected_size);
2123
2124 assert_eq!(
2125 parquet_meta_data.memory_size(),
2126 expected_size_with_decryptor
2127 );
2128 }
2129
2130 fn get_test_schema_descr() -> SchemaDescPtr {
2132 let schema = SchemaType::group_type_builder("schema")
2133 .with_fields(vec![
2134 Arc::new(
2135 SchemaType::primitive_type_builder("a", Type::INT32)
2136 .build()
2137 .unwrap(),
2138 ),
2139 Arc::new(
2140 SchemaType::primitive_type_builder("b", Type::INT32)
2141 .build()
2142 .unwrap(),
2143 ),
2144 ])
2145 .build()
2146 .unwrap();
2147
2148 Arc::new(SchemaDescriptor::new(Arc::new(schema)))
2149 }
2150}