etseidl commented on code in PR #8340:
URL: https://github.com/apache/arrow-rs/pull/8340#discussion_r2373568434


##########
parquet/src/file/metadata/parser.rs:
##########
@@ -0,0 +1,467 @@
+// 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.
+
+//! Internal metadata parsing routines
+//!
+//! In general these functions parse thrift-encoded metadata from a byte slice
+//! into the corresponding Rust structures
+
+use crate::basic::ColumnOrder;
+use crate::errors::ParquetError;
+use crate::file::metadata::{
+    ColumnChunkMetaData, FileMetaData, PageIndexPolicy, ParquetMetaData, 
RowGroupMetaData,
+};
+use crate::file::page_index::index::Index;
+use crate::file::page_index::index_reader::{decode_column_index, 
decode_offset_index};
+use crate::file::page_index::offset_index::OffsetIndexMetaData;
+use crate::schema::types;
+use crate::schema::types::SchemaDescriptor;
+use crate::thrift::TCompactSliceInputProtocol;
+use crate::thrift::TSerializable;
+use bytes::Bytes;
+use std::sync::Arc;
+
+#[cfg(feature = "encryption")]
+use crate::encryption::{
+    decrypt::{FileDecryptionProperties, FileDecryptor},
+    modules::create_footer_aad,
+};
+#[cfg(feature = "encryption")]
+use crate::format::EncryptionAlgorithm;
+
+/// Decodes [`ParquetMetaData`] from the provided bytes.
+///
+/// Typically this is used to decode the metadata from the end of a parquet
+/// file. The format of `buf` is the Thrift compact binary protocol, as 
specified
+/// by the [Parquet Spec].
+///
+/// [Parquet Spec]: https://github.com/apache/parquet-format#metadata
+pub(crate) fn decode_metadata(buf: &[u8]) -> 
crate::errors::Result<ParquetMetaData> {
+    let mut prot = TCompactSliceInputProtocol::new(buf);
+
+    let t_file_metadata: crate::format::FileMetaData =
+        crate::format::FileMetaData::read_from_in_protocol(&mut prot)
+            .map_err(|e| general_err!("Could not parse metadata: {}", e))?;
+    let schema = types::from_thrift(&t_file_metadata.schema)?;
+    let schema_descr = Arc::new(SchemaDescriptor::new(schema));
+
+    let mut row_groups = Vec::new();
+    for rg in t_file_metadata.row_groups {
+        row_groups.push(RowGroupMetaData::from_thrift(schema_descr.clone(), 
rg)?);
+    }
+    let column_orders = parse_column_orders(t_file_metadata.column_orders, 
&schema_descr)?;
+
+    let file_metadata = FileMetaData::new(
+        t_file_metadata.version,
+        t_file_metadata.num_rows,
+        t_file_metadata.created_by,
+        t_file_metadata.key_value_metadata,
+        schema_descr,
+        column_orders,
+    );
+
+    Ok(ParquetMetaData::new(file_metadata, row_groups))
+}
+
+/// Parses column orders from Thrift definition.
+/// If no column orders are defined, returns `None`.
+pub(crate) fn parse_column_orders(
+    t_column_orders: Option<Vec<crate::format::ColumnOrder>>,
+    schema_descr: &SchemaDescriptor,
+) -> crate::errors::Result<Option<Vec<ColumnOrder>>> {
+    match t_column_orders {
+        Some(orders) => {
+            // Should always be the case
+            if orders.len() != schema_descr.num_columns() {
+                return Err(general_err!("Column order length mismatch"));
+            };
+            let mut res = Vec::new();
+            for (i, column) in schema_descr.columns().iter().enumerate() {
+                match orders[i] {
+                    crate::format::ColumnOrder::TYPEORDER(_) => {
+                        let sort_order = ColumnOrder::get_sort_order(
+                            column.logical_type(),
+                            column.converted_type(),
+                            column.physical_type(),
+                        );
+                        res.push(ColumnOrder::TYPE_DEFINED_ORDER(sort_order));
+                    }
+                }
+            }
+            Ok(Some(res))
+        }
+        None => Ok(None),
+    }
+}
+
+pub(crate) fn parse_column_index(

Review Comment:
   Somewhat parallel, but they for the most part converge eventually (except 
for the main `ParquetMetaData` parser).
   
   Do you think encryption support will always be feature gated? Or will there 
eventually be just the one path? I wouldn't want to get too fancy if the latter.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to