PinkCrow007 commented on code in PR #7404: URL: https://github.com/apache/arrow-rs/pull/7404#discussion_r2043575052
########## arrow-array/src/array/variant_array.rs: ########## @@ -0,0 +1,628 @@ +// 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. + +use crate::array::print_long_array; +use crate::builder::{ArrayBuilder, BinaryBuilder}; +use crate::{Array, ArrayRef}; +use arrow_buffer::{Buffer, NullBuffer, OffsetBuffer, ScalarBuffer}; +use arrow_data::{ArrayData, ArrayDataBuilder}; +use arrow_schema::{ArrowError, DataType, Field}; + +#[cfg(feature = "canonical_extension_types")] +use arrow_schema::extension::Variant; +use std::sync::Arc; +use std::any::Any; + +/// An array of Variant values. +/// +/// The Variant extension type stores data as two binary values: metadata and value. +/// This array stores each Variant as a concatenated binary value (metadata + value). +/// +/// # Example +/// +/// ``` +/// use arrow_array::VariantArray; +/// use arrow_schema::extension::Variant; +/// use arrow_array::Array; // Import the Array trait +/// +/// // Create metadata and value for each variant +/// let metadata = vec![ +/// 0x01, // header: version=1, sorted=0, offset_size=1 +/// 0x01, // dictionary_size = 1 +/// 0x00, // offset 0 +/// 0x03, // offset 3 +/// b'k', b'e', b'y' // dictionary bytes +/// ]; +/// let variant_type = Variant::new(metadata.clone(), vec![]); +/// +/// // Create variants with different values +/// let variants = vec![ +/// Variant::new(metadata.clone(), b"null".to_vec()), +/// Variant::new(metadata.clone(), b"true".to_vec()), +/// Variant::new(metadata.clone(), b"{\"a\": 1}".to_vec()), +/// ]; +/// +/// // Create a VariantArray +/// let variant_array = VariantArray::from_variants(variant_type, variants.clone()).expect("Failed to create VariantArray"); +/// +/// // Access variants from the array +/// assert_eq!(variant_array.len(), 3); +/// let retrieved = variant_array.value(0).expect("Failed to get value"); +/// assert_eq!(retrieved.metadata(), &metadata); +/// assert_eq!(retrieved.value(), b"null"); +/// ``` +#[cfg(feature = "canonical_extension_types")] +pub mod variant_array_module { + use super::*; + + /// An array of Variant values. + /// + /// The Variant extension type stores data as two binary values: metadata and value. + /// This array stores each Variant as a concatenated binary value (metadata + value). + /// + /// # Example + /// + /// ``` + /// use arrow_array::VariantArray; + /// use arrow_schema::extension::Variant; + /// use arrow_array::Array; // Import the Array trait + /// + /// // Create metadata and value for each variant + /// let metadata = vec![ + /// 0x01, // header: version=1, sorted=0, offset_size=1 + /// 0x01, // dictionary_size = 1 + /// 0x00, // offset 0 + /// 0x03, // offset 3 + /// b'k', b'e', b'y' // dictionary bytes + /// ]; + /// let variant_type = Variant::new(metadata.clone(), vec![]); + /// + /// // Create variants with different values + /// let variants = vec![ + /// Variant::new(metadata.clone(), b"null".to_vec()), + /// Variant::new(metadata.clone(), b"true".to_vec()), + /// Variant::new(metadata.clone(), b"{\"a\": 1}".to_vec()), + /// ]; + /// + /// // Create a VariantArray + /// let variant_array = VariantArray::from_variants(variant_type, variants.clone()).expect("Failed to create VariantArray"); + /// + /// // Access variants from the array + /// assert_eq!(variant_array.len(), 3); + /// let retrieved = variant_array.value(0).expect("Failed to get value"); + /// assert_eq!(retrieved.metadata(), &metadata); + /// assert_eq!(retrieved.value(), b"null"); + /// ``` + #[derive(Clone, Debug)] + pub struct VariantArray { Review Comment: Thanks so much @alamb! Your brilliant comment and the C++ implementation really got me thinking — maybe I should tackle this from the ground up. Currently, in my implementation: 1. In Arrow: Variant is an ExtensionType over **Binary** 2. In Parquet: Variant is implemented as a **PrimitiveType** (not a GroupType) - It’s a PrimitiveType where metadata and value are concatenated into one binary blob - VariantArray handles the parsing/splitting of this binary data I initially tried using GroupType but faced issues during arrow_to_parquet conversion. Since the Arrow side is a single Binary ExtensionType, mapping it to a GroupType (which contains two separate binary fields) caused a mismatch in column expectations that was tricky to resolve cleanly. However, your comment and the C++ design raise an important question about alignment and extensibility. Would it be better to: 1. Make Variant an ExtensionType on top of **Struct** type in Arrow 2. Use **GroupType** containing two binary fields in Parquet This aligns with the C++ version and avoids the conversion mismatch. What do you think — does that sound like a better design? -- 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