scovich commented on code in PR #7905: URL: https://github.com/apache/arrow-rs/pull/7905#discussion_r2202008720
########## 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: The only requirement for shredded variant is that at least one of the two must exist. BTW, does shredded variant spec forbid existence of other (unknown) columns? (I though it did, but I can't find any obvious wording) ########## 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 Review Comment: I don't understand this part? `typed_value` is the arbitrary type of a shredded column. That could be a primitive type (e.g. INT), or it could be struct (with fields that are themselves either strongly typed or variant), or it could even be variant (technically, a writer could choose to shred out a commonly-used field, even if that field doesn't have a strong type that could shred in structured form). Any nested variants must have only `value` and `typed_value` columns tho -- the top-level `metadata` applies to everything. Readers who encounter nested variants inside the `typed_value` column need to be prepared to use the top-level `metadata` when reading them, which may require some plumbing, given that it's not actually materialized locally? -- 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