mbrobbel commented on code in PR #8408: URL: https://github.com/apache/arrow-rs/pull/8408#discussion_r2378240955
########## 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 Review Comment: Should we add a method to `Field` for this? ########## parquet/src/arrow/schema/extension.rs: ########## @@ -0,0 +1,62 @@ +// 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 Review Comment: ```suggestion 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, } ``` ########## parquet/src/variant.rs: ########## @@ -67,37 +65,29 @@ //! # } //! ``` //! -//! # Example: Writing JSON with a Parquet file with Variant column +//! # Example: Writing JSON into a Parquet file with Variant column //! ```rust //! # use std::sync::Arc; //! # use arrow_array::{ArrayRef, RecordBatch, StringArray}; -//! # use parquet::variant::compute::json_to_variant; -//! # use parquet::variant::compute::VariantArray; +//! # use arrow_schema::Schema; +//! # use parquet::variant::{json_to_variant, VariantArray}; //! # use parquet::arrow::ArrowWriter; //! # fn main() -> Result<(), parquet::errors::ParquetError> { //! // Create an array of JSON strings, simulating a column of JSON data -//! // TODO use StringViewArray when available -//! let input_array = StringArray::from(vec![ +//! let input_array: ArrayRef = Arc::new(StringArray::from(vec![ //! Some(r#"{"name": "Alice", "age": 30}"#), //! Some(r#"{"name": "Bob", "age": 25, "address": {"city": "New York"}}"#), //! None, //! Some("{}"), -//! ]); -//! let input_array: ArrayRef = Arc::new(input_array); +//! ])); //! //! // Convert the JSON strings to a VariantArray //! let array: VariantArray = json_to_variant(&input_array)?; -//! -//! // TODO support writing VariantArray directly -//! // at the moment it panics when trying to downcast to a struct array -//! // https://github.com/apache/arrow-rs/issues/8296 -//! // let array: ArrayRef = Arc::new(array); -//! let array: ArrayRef = Arc::new(array.into_inner()); -//! //! // create a RecordBatch with the VariantArray -//! let batch = RecordBatch::try_from_iter(vec![("data", array)])?; +//! let schema = Schema::new(vec![array.field("data")]); +//! let batch = RecordBatch::try_new(Arc::new(schema), vec![ArrayRef::from(array)])?; //! -//! // write the RecordBatch to a Parquet file +//! // write the RecordBatch to a Parquet file as normal //! let file = std::fs::File::create("variant-json.parquet")?; //! let mut writer = ArrowWriter::try_new(file, batch.schema(), None)?; //! writer.write(&batch)?; Review Comment: ```suggestion //! // create a RecordBatch with the VariantArray //! let schema = Schema::new(vec![array.field("data")]); //! let batch = RecordBatch::try_new(Arc::new(schema), vec![ArrayRef::from(array)])?; //! //! // write the RecordBatch to a Parquet file as normal //! let file = std::fs::File::create("variant-json.parquet")?; //! let mut writer = ArrowWriter::try_new(file, batch.schema(), None)?; //! writer.write(&batch)?; ``` -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org