alamb commented on code in PR #8409: URL: https://github.com/apache/arrow-rs/pull/8409#discussion_r2383088147
########## parquet/src/arrow/schema/extension.rs: ########## @@ -0,0 +1,127 @@ +// 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::errors::ParquetError; +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 try_add_extension_type( + mut arrow_field: Field, + parquet_type: &Type, +) -> Result<Field, ParquetError> { + let Some(parquet_logical_type) = parquet_type.get_basic_info().logical_type() else { + return Ok(arrow_field); + }; + match parquet_logical_type { + #[cfg(feature = "variant_experimental")] + LogicalType::Variant => { Review Comment: I filed a ticket to track this idea: - https://github.com/apache/arrow-rs/issues/8479 In my mind while the build flag approach in this PR is not ideal, it is no worse than what is on `main` today, though other people may disagree ########## parquet/src/arrow/schema/extension.rs: ########## @@ -34,23 +35,51 @@ use arrow_schema::Field; /// 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(mut arrow_field: Field, parquet_type: &Type) -> Field { - match parquet_type.get_basic_info().logical_type() { +pub(crate) fn try_add_extension_type( + mut arrow_field: Field, + parquet_type: &Type, +) -> Result<Field, ParquetError> { + let Some(parquet_logical_type) = parquet_type.get_basic_info().logical_type() else { + return Ok(arrow_field); + }; + match parquet_logical_type { #[cfg(feature = "variant_experimental")] - Some(LogicalType::Variant) => { - // try to add the Variant extension type, but if that fails (e.g. because the - // storage type is not supported), just return the field as is - arrow_field - .try_with_extension_type(parquet_variant_compute::VariantType) - .ok(); - arrow_field + LogicalType::Variant => { + arrow_field.try_with_extension_type(parquet_variant_compute::VariantType)?; } - // TODO add other LogicalTypes here - _ => arrow_field, + #[cfg(feature = "arrow_canonical_extension_types")] Review Comment: The core change of this PR is moving the code that handles extension types from the `schema` module to the new `extension` module and put them behind some named functions. ########## parquet/src/arrow/schema/mod.rs: ########## @@ -844,12 +809,26 @@ mod tests { Field::new("float16", DataType::Float16, true), Field::new("string", DataType::Utf8, true), Field::new("string_2", DataType::Utf8, true), - Field::new("json", DataType::Utf8, true), + json_field(), Review Comment: this now actually fails when the canonical extension types are enabled, because a JSON parquet field is now (correctly) annotated with the extension type field ########## parquet/src/arrow/schema/mod.rs: ########## @@ -618,16 +615,7 @@ fn arrow_to_parquet_type(field: &Field, coerce_types: bool) -> Result<Type> { .with_repetition(repetition) .with_id(id) .with_length(*length) - .with_logical_type( - #[cfg(feature = "arrow_canonical_extension_types")] - // If set, map arrow uuid extension type to parquet uuid logical type. - field - .try_extension_type::<Uuid>() - .ok() - .map(|_| LogicalType::Uuid), - #[cfg(not(feature = "arrow_canonical_extension_types"))] - None, - ) + .with_logical_type(logical_type_for_fixed_size_binary(field)) Review Comment: this code is just moved into the extension module ########## parquet/src/arrow/schema/mod.rs: ########## @@ -2268,13 +2248,11 @@ mod tests { Some(LogicalType::Json) ); - // TODO: roundtrip - // https://github.com/apache/arrow-rs/issues/7063 - // let arrow_schema = parquet_to_arrow_schema(&parquet_schema, None)?; - // assert_eq!( - // arrow_schema.field(0).try_extension_type::<Json>()?, - // Json::default() - // ); + let arrow_schema = parquet_to_arrow_schema(&parquet_schema, None)?; Review Comment: it works! -- 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]
