PinkCrow007 commented on code in PR #7404: URL: https://github.com/apache/arrow-rs/pull/7404#discussion_r2049303185
########## 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: > 5b564 Thanks @alamb ! Currently, the main branch of arrow-rs is using the same revision as I did, which doesn’t include the VARIANT logical type yet. I looked into updating to the latest revision from parquet-format, but it introduces new types like GEOMETRY, GEOGRAPHY, and other changes that are not yet supported in the arrow-rs main branch. So if I stick with the current revision, I’ll need to manually add VARIANT to format.rs. If I upgrade to the latest parquet-format, I’ll also need to patch more places. Let me know if you think there’s a cleaner path forward! -- 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