alamb commented on code in PR #7404:
URL: https://github.com/apache/arrow-rs/pull/7404#discussion_r2046627397


##########
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:
   > Make Variant an ExtensionType on top of Struct type in Arrow
   > 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?
   
   Yes I think this sounds right. The closer we can keep the arrow 
implementation to the spec and the other implementations the easier it will be 
to use in my opinion. If there is good reason to deviate we can discuss that 
too but in general I think following the existing implementations will make 
things easier
   
   In terms of GroupType and encodings in Parquet, you may need to re-generate 
the thrift definitions to pick up the most recent version of the spec: 
https://github.com/apache/parquet-format/tree/master
   
   We would do that by updating the revision here and then rerunning regen.sh:
   
   
https://github.com/apache/arrow-rs/blob/390de282d8c570ec9dd647efebd7a9b425a04d1f/parquet/regen.sh#L20
   
   



-- 
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

Reply via email to