friendlymatthew commented on code in PR #7905: URL: https://github.com/apache/arrow-rs/pull/7905#discussion_r2201092686
########## parquet-variant-compute/src/variant_array.rs: ########## @@ -0,0 +1,227 @@ +// 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. + +//! [`VariantArray`] implementation + +use arrow::array::{Array, ArrayData, ArrayRef, AsArray, StructArray}; +use arrow::buffer::NullBuffer; +use arrow_schema::{ArrowError, DataType}; +use parquet_variant::Variant; +use std::any::Any; +use std::sync::Arc; + +/// An array of Parquet [`Variant`] values +/// +/// A [`VariantArray`] wraps an Arrow [`StructArray`] that stores the underlying +/// `metadata` and `value` fields, and adds convenience methods to access +/// the `Variant`s +/// +/// See [`VariantArrayBuilder`] for constructing a `VariantArray`. +/// +/// [`VariantArrayBuilder`]: crate::VariantArrayBuilder +/// +/// # Specification +/// 1. This code follows the conventions for storing variants in Arrow Struct Array +/// defined by [Extension Type for Parquet Variant arrow] and this [document]. +/// At the time of this writing, this is not yet a standardized Arrow extension type. +/// +/// [Extension Type for Parquet Variant arrow]: https://github.com/apache/arrow/issues/46908 +/// [document]: https://docs.google.com/document/d/1pw0AWoMQY3SjD7R4LgbPvMjG_xSCtXp3rZHkVp9jpZ4/edit?usp=sharing +#[derive(Debug)] +pub struct VariantArray { + /// StructArray or up to three fields: + /// 1. A required field named metadata which is binary, large_binary, or binary_view + /// 2. An optional field named value that is binary, large_binary, or binary_view + /// 3. An optional field named typed_value which can be any primitive type or be a list, large_list, list_view or struct + /// + /// If typed_value is a nested type, its elements must be required and must + /// be a struct containing only one of the following: + /// + /// 1. A single required field, of type binary, large_binary, or binary_view named value + /// + /// 2. An optional field named value of type binary, large_binary, or + /// binary_view AND an optional field named typed_value which follows these + /// same rules + /// + /// NOTE: It is also permissible for the metadata field to be + /// Dictionary-Encoded, preferably (but not required) with an index type of + /// int8. + inner: StructArray, +} + +impl VariantArray { + /// Creates a new `VariantArray` from a [`StructArray`]. + /// + /// # Arguments + /// - `inner` - The underlying [`StructArray`] that contains the variant data. + /// + /// # Returns + /// - A new instance of `VariantArray`. + /// + /// # Errors: + /// If the `StructArray` does not contain the required fields + pub fn try_new(inner: ArrayRef) -> Result<Self, ArrowError> { + let Some(inner) = inner.as_struct_opt() else { + return Err(ArrowError::InvalidArgumentError( + "Invalid VariantArray: requires StructArray as input".to_string(), + )); + }; + // Ensure the StructArray has the expected fields + if !inner.fields().iter().any(|f| f.name() == "metadata") { + return Err(ArrowError::InvalidArgumentError( + "Invalid VariantArray: StructArray must contain a 'metadata' field".to_string(), + )); + } + if !inner.fields().iter().any(|f| f.name() == "value") { + return Err(ArrowError::InvalidArgumentError( + "Invalid VariantArray: StructArray must contain a 'value' field".to_string(), + )); + } Review Comment: More of a note to myself for the shredding work, but I don't think we should necessarily error when the `value` column is not found. ########## parquet-variant-compute/src/variant_array.rs: ########## @@ -0,0 +1,227 @@ +// 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. + +//! [`VariantArray`] implementation + +use arrow::array::{Array, ArrayData, ArrayRef, AsArray, StructArray}; +use arrow::buffer::NullBuffer; +use arrow_schema::{ArrowError, DataType}; +use parquet_variant::Variant; +use std::any::Any; +use std::sync::Arc; + +/// An array of Parquet [`Variant`] values +/// +/// A [`VariantArray`] wraps an Arrow [`StructArray`] that stores the underlying +/// `metadata` and `value` fields, and adds convenience methods to access +/// the `Variant`s +/// +/// See [`VariantArrayBuilder`] for constructing a `VariantArray`. +/// +/// [`VariantArrayBuilder`]: crate::VariantArrayBuilder +/// +/// # Specification +/// 1. This code follows the conventions for storing variants in Arrow Struct Array +/// defined by [Extension Type for Parquet Variant arrow] and this [document]. +/// At the time of this writing, this is not yet a standardized Arrow extension type. +/// +/// [Extension Type for Parquet Variant arrow]: https://github.com/apache/arrow/issues/46908 +/// [document]: https://docs.google.com/document/d/1pw0AWoMQY3SjD7R4LgbPvMjG_xSCtXp3rZHkVp9jpZ4/edit?usp=sharing +#[derive(Debug)] +pub struct VariantArray { + /// StructArray or up to three fields: + /// 1. A required field named metadata which is binary, large_binary, or binary_view + /// 2. An optional field named value that is binary, large_binary, or binary_view + /// 3. An optional field named typed_value which can be any primitive type or be a list, large_list, list_view or struct + /// + /// If typed_value is a nested type, its elements must be required and must + /// be a struct containing only one of the following: + /// + /// 1. A single required field, of type binary, large_binary, or binary_view named value + /// + /// 2. An optional field named value of type binary, large_binary, or + /// binary_view AND an optional field named typed_value which follows these + /// same rules + /// + /// NOTE: It is also permissible for the metadata field to be + /// Dictionary-Encoded, preferably (but not required) with an index type of + /// int8. + inner: StructArray, +} + +impl VariantArray { + /// Creates a new `VariantArray` from a [`StructArray`]. + /// + /// # Arguments + /// - `inner` - The underlying [`StructArray`] that contains the variant data. + /// + /// # Returns + /// - A new instance of `VariantArray`. + /// + /// # Errors: + /// If the `StructArray` does not contain the required fields + pub fn try_new(inner: ArrayRef) -> Result<Self, ArrowError> { + let Some(inner) = inner.as_struct_opt() else { + return Err(ArrowError::InvalidArgumentError( + "Invalid VariantArray: requires StructArray as input".to_string(), + )); + }; + // Ensure the StructArray has the expected fields + if !inner.fields().iter().any(|f| f.name() == "metadata") { + return Err(ArrowError::InvalidArgumentError( + "Invalid VariantArray: StructArray must contain a 'metadata' field".to_string(), + )); + } + if !inner.fields().iter().any(|f| f.name() == "value") { + return Err(ArrowError::InvalidArgumentError( + "Invalid VariantArray: StructArray must contain a 'value' field".to_string(), + )); + } Review Comment: More of a note to myself for the shredding work, but I don't think we should necessarily error when the `value` column is not found? -- 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