paleolimbot commented on code in PR #8408:
URL: https://github.com/apache/arrow-rs/pull/8408#discussion_r2376960226


##########
parquet/src/arrow/schema/extension.rs:
##########
@@ -0,0 +1,68 @@
+// 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.
+
+//! Arrow Extension Type Support for Parquet
+//!
+//! This module contains mapping code to map Parquet [`LogicalType`]s to/from
+//! Arrow [`ExtensionType`]s.
+//!
+//! Extension types are represented using the metadata from Arrow [`Field`]s
+//! with the key "ARROW:extension:name".
+
+use crate::basic::LogicalType;
+use crate::schema::types::Type;
+use arrow_schema::extension::ExtensionType;
+use arrow_schema::Field;
+
+/// Adds extension type metadata, if necessary, based on the Parquet field's
+/// [`LogicalType`]
+///
+/// Some Parquet logical types, such as Variant, do not map directly to an
+/// Arrow DataType, and instead are represented by an Arrow ExtensionType.
+/// Extension types are attached to Arrow Fields via metadata.
+pub(crate) fn add_extension_type(arrow_field: Field, parquet_type: &Type) -> 
Field {
+    let result = match parquet_type.get_basic_info().logical_type() {
+        #[cfg(feature = "variant_experimental")]
+        Some(LogicalType::Variant) => {
+            
arrow_field.with_extension_type(parquet_variant_compute::VariantType)
+        }
+        // TODO add other LogicalTypes here
+        _ => arrow_field,
+    };
+    result
+}
+
+/// Return the Parquet logical type to use for the specified Arrow field, if 
any.
+#[cfg(feature = "variant_experimental")]
+pub(crate) fn logical_type_for_struct(field: &Field) -> Option<LogicalType> {
+    use parquet_variant_compute::VariantType;
+    // Check the name (= quick and cheap) and only try_extension_type if the 
name matches
+    // to avoid unnecessary String allocations in ArrowError
+    if field.extension_type_name()? != VariantType::NAME {
+        return None;
+    }
+    match field.try_extension_type::<VariantType>() {
+        Ok(VariantType) => Some(LogicalType::Variant),
+        // Given check above, this should not error, but if it does ignore
+        Err(_e) => None,
+    }

Review Comment:
   I know you're working to make this more generic, but as a pattern this is 
also how Arrow C++ does the conversion (except in Arrow C++ we can just look at 
the `DataType` and here you have to poke the field metadata and parse it. Arrow 
C++ just hard-codes a few names/storage types too (but no compile time flag on 
the Parquet library...just a static extension registry that would control 
whether an extension type shows up as a `DataType` or a field with metadata).



##########
parquet/src/arrow/schema/extension.rs:
##########
@@ -0,0 +1,68 @@
+// 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.
+
+//! Arrow Extension Type Support for Parquet
+//!
+//! This module contains mapping code to map Parquet [`LogicalType`]s to/from
+//! Arrow [`ExtensionType`]s.
+//!
+//! Extension types are represented using the metadata from Arrow [`Field`]s
+//! with the key "ARROW:extension:name".
+
+use crate::basic::LogicalType;
+use crate::schema::types::Type;
+use arrow_schema::extension::ExtensionType;
+use arrow_schema::Field;
+
+/// Adds extension type metadata, if necessary, based on the Parquet field's
+/// [`LogicalType`]
+///
+/// Some Parquet logical types, such as Variant, do not map directly to an
+/// Arrow DataType, and instead are represented by an Arrow ExtensionType.
+/// Extension types are attached to Arrow Fields via metadata.
+pub(crate) fn add_extension_type(arrow_field: Field, parquet_type: &Type) -> 
Field {
+    let result = match parquet_type.get_basic_info().logical_type() {
+        #[cfg(feature = "variant_experimental")]
+        Some(LogicalType::Variant) => {
+            
arrow_field.with_extension_type(parquet_variant_compute::VariantType)

Review Comment:
   Should this be `try_with_extension_type()` to avoid a panic in the event of 
an incorrect storage type?
   
   Arrow C++ I believe will just return you the storage type in the event of an 
in appropriate storage type, plus you can also pass an option to disable 
converting Parquet logical types to extension types (it's off by default in C++ 
but on by default in pyarrow).
   
   I'm not sure if it applies here, but in Arrow C++ the `Field` could have an 
extension type loaded into it already in the event that a user wrote the 
Parquet file with `store_schema=True` (and read the file with the appropriate 
option to look for/load the embedded Arrow schema). If that's true you'll have 
to pick which one takes precedence (I think in Arrow C++ it's the version from 
the embedded schema but I'll have to look it up).



-- 
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