Skip to main content

parquet_variant_compute/
variant_array.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! [`VariantArray`] implementation
19
20use crate::VariantArrayBuilder;
21use crate::type_conversion::{
22    generic_conversion_single_value, generic_conversion_single_value_with_result,
23    primitive_conversion_single_value,
24};
25use arrow::array::{
26    Array, ArrayRef, AsArray, StructArray, downcast_dictionary_array, downcast_run_array,
27    new_null_array,
28};
29use arrow::buffer::NullBuffer;
30use arrow::compute::cast;
31use arrow::datatypes::{
32    Date32Type, Decimal32Type, Decimal64Type, Decimal128Type, Float16Type, Float32Type,
33    Float64Type, Int8Type, Int16Type, Int32Type, Int64Type, Time64MicrosecondType,
34    TimestampMicrosecondType, TimestampNanosecondType,
35};
36use arrow::error::Result;
37use arrow_schema::extension::{ExtensionType, Uuid as UuidExtension};
38use arrow_schema::{ArrowError, DataType, Field, FieldRef, Fields, TimeUnit};
39use chrono::{DateTime, NaiveTime};
40use parquet_variant::{
41    Uuid, Variant, VariantDecimal4, VariantDecimal8, VariantDecimal16, VariantDecimalType as _,
42};
43
44use std::borrow::Cow;
45use std::sync::Arc;
46
47/// Returns the logical bytes at the given index from a binary-like array, resolving dictionary
48/// and run-end encodings. Returns `None` for nulls or if the logical values aren't binary-like.
49pub(crate) fn binary_array_value(array: &dyn Array, index: usize) -> Option<&[u8]> {
50    if array.is_null(index) {
51        return None;
52    }
53    match array.data_type() {
54        DataType::Binary => Some(array.as_binary::<i32>().value(index)),
55        DataType::LargeBinary => Some(array.as_binary::<i64>().value(index)),
56        DataType::BinaryView => Some(array.as_binary_view().value(index)),
57        DataType::Dictionary(..) => downcast_dictionary_array! {
58            array => {
59                let index = array.key(index)?;
60                binary_array_value(array.values().as_ref(), index)
61            },
62            _ => unreachable!(),
63        },
64        DataType::RunEndEncoded(..) => downcast_run_array! {
65            array => {
66                let index = array.get_physical_index(index);
67                binary_array_value(array.values().as_ref(), index)
68            },
69            _ => unreachable!(),
70        },
71        _ => None,
72    }
73}
74
75/// Returns a [`Variant`] from a `metadata` and `value` byte arrays, returns `None`
76/// if one of them is of invalid type.
77pub(crate) fn variant_from_arrays_at<'m, 'v>(
78    metadata: &'m dyn Array,
79    value: &'v dyn Array,
80    index: usize,
81) -> Option<Variant<'m, 'v>> {
82    let metadata = binary_array_value(metadata, index)?;
83    let value = binary_array_value(value, index)?;
84    Some(Variant::new(metadata, value))
85}
86
87/// Returns an all-null binary `value` column of the given length.
88///
89/// The shredding spec requires the `value` column to always be present in the
90/// schema, so producers that have no unshredded values to store must still
91/// emit an all-null column. See <https://github.com/apache/arrow-rs/issues/10306>.
92pub(crate) fn all_null_value_column(len: usize) -> ArrayRef {
93    new_null_array(&DataType::BinaryView, len)
94}
95
96/// Validates that an array has a binary-like data type.
97pub(crate) fn validate_binary_array(array: &dyn Array, field_name: &str) -> Result<()> {
98    match array.data_type() {
99        DataType::Binary | DataType::LargeBinary | DataType::BinaryView => Ok(()),
100        _ => Err(ArrowError::InvalidArgumentError(format!(
101            "VariantArray '{field_name}' field must be Binary, LargeBinary, or BinaryView, got {}",
102            array.data_type()
103        ))),
104    }
105}
106
107/// Validates that a metadata array has binary-like logical values.
108fn validate_metadata_array(array: &dyn Array) -> Result<()> {
109    let is_binary = |data_type: &DataType| {
110        matches!(
111            data_type,
112            DataType::Binary | DataType::LargeBinary | DataType::BinaryView
113        )
114    };
115    match array.data_type() {
116        data_type if is_binary(data_type) => Ok(()),
117        DataType::Dictionary(_, values) if is_binary(values) => Ok(()),
118        DataType::RunEndEncoded(_, values) if is_binary(values.data_type()) => Ok(()),
119        _ => Err(ArrowError::InvalidArgumentError(format!(
120            "VariantArray 'metadata' field must be Binary, LargeBinary, BinaryView, or a Dictionary or RunEndEncoded array of one of those types, got {}",
121            array.data_type()
122        ))),
123    }
124}
125
126/// Arrow Variant [`ExtensionType`].
127///
128/// Represents the canonical Arrow Extension Type for storing variants.
129/// See [`VariantArray`] for more examples of using this extension type.
130pub struct VariantType;
131
132impl ExtensionType for VariantType {
133    const NAME: &'static str = "arrow.parquet.variant";
134
135    // Variants extension metadata is an empty string
136    // <https://github.com/apache/arrow/blob/d803afcc43f5d132506318fd9e162d33b2c3d4cd/docs/source/format/CanonicalExtensions.rst?plain=1#L473>
137    type Metadata = &'static str;
138
139    fn metadata(&self) -> &Self::Metadata {
140        &""
141    }
142
143    fn serialize_metadata(&self) -> Option<String> {
144        Some(String::new())
145    }
146
147    fn deserialize_metadata(_metadata: Option<&str>) -> Result<Self::Metadata> {
148        Ok("")
149    }
150
151    fn supports_data_type(&self, data_type: &DataType) -> Result<()> {
152        if matches!(data_type, DataType::Struct(_)) {
153            Ok(())
154        } else {
155            Err(ArrowError::InvalidArgumentError(format!(
156                "VariantType only supports StructArray, got {data_type}"
157            )))
158        }
159    }
160
161    fn try_new(data_type: &DataType, _metadata: Self::Metadata) -> Result<Self> {
162        Self.supports_data_type(data_type)?;
163        Ok(Self)
164    }
165
166    fn validate(data_type: &DataType, _metadata: Self::Metadata) -> Result<()> {
167        Self.supports_data_type(data_type)
168    }
169}
170
171/// An array of Parquet [`Variant`] values
172///
173/// A [`VariantArray`] wraps an Arrow [`StructArray`] that stores the underlying
174/// `metadata` and `value` fields, and adds convenience methods to access
175/// the [`Variant`]s.
176///
177/// See [`VariantArrayBuilder`] for constructing `VariantArray` row by row.
178///
179/// See the examples below from converting between `VariantArray` and
180/// `StructArray`.
181///
182/// [`VariantArrayBuilder`]: crate::VariantArrayBuilder
183///
184/// # Documentation
185///
186/// Variant is documented as a canonical Arrow extension type in the
187/// [Parquet Variant] section of the [official list of extension types] on
188/// the Apache Arrow website.
189///
190/// [Parquet Variant]: https://arrow.apache.org/docs/format/CanonicalExtensions.html#parquet-variant
191/// [official list of extension types]: https://arrow.apache.org/docs/format/CanonicalExtensions.html
192///
193/// # Example: Check if a [`StructArray`] has the [`VariantType`] extension
194///
195/// Arrow Arrays only provide [`DataType`], but the extension type information
196/// is stored on a [`Field`]. Thus, you must have access to the [`Schema`] or
197/// [`Field`] to check for the extension type.
198///
199/// [`Schema`]: arrow_schema::Schema
200/// ```
201/// # use arrow::array::StructArray;
202/// # use arrow_schema::{Schema, Field, DataType};
203/// # use parquet_variant::Variant;
204/// # use parquet_variant_compute::{VariantArrayBuilder, VariantArray, VariantType};
205/// # fn get_variant_array() -> VariantArray {
206/// #   let mut builder = VariantArrayBuilder::new(10);
207/// #   builder.append_variant(Variant::from("such wow"));
208/// #   builder.build()
209/// # }
210/// # fn get_schema() -> Schema {
211/// #   Schema::new(vec![
212/// #     Field::new("id", DataType::Int32, false),
213/// #     get_variant_array().field("var"),
214/// #   ])
215/// # }
216/// let schema = get_schema();
217/// assert_eq!(schema.fields().len(), 2);
218/// // first field is not a Variant
219/// assert!(!schema.field(0).has_valid_extension_type::<VariantType>());
220/// // second field is a Variant
221/// assert!(schema.field(1).has_valid_extension_type::<VariantType>());
222/// ```
223///
224/// # Example: Constructing the correct [`Field`] for a [`VariantArray`]
225///
226/// You can construct the correct [`Field`] for a [`VariantArray`] using the
227/// [`VariantArray::field`] method.
228///
229/// ```
230/// # use arrow_schema::{Schema, Field, DataType};
231/// # use parquet_variant::Variant;
232/// # use parquet_variant_compute::{VariantArrayBuilder, VariantArray, VariantType};
233/// # fn get_variant_array() -> VariantArray {
234/// #   let mut builder = VariantArrayBuilder::new(10);
235/// #   builder.append_variant(Variant::from("such wow"));
236/// #   builder.build()
237/// # }
238/// let variant_array = get_variant_array();
239/// // First field is an integer id, second field is a variant
240/// let schema = Schema::new(vec![
241///   Field::new("id", DataType::Int32, false),
242///   // call VariantArray::field to get the correct Field
243///   variant_array.field("var"),
244/// ]);
245/// ```
246///
247/// You can also construct the [`Field`] using [`VariantType`] directly
248///
249/// ```
250/// # use arrow_schema::{Schema, Field, DataType};
251/// # use parquet_variant::Variant;
252/// # use parquet_variant_compute::{VariantArrayBuilder, VariantArray, VariantType};
253/// # fn get_variant_array() -> VariantArray {
254/// #   let mut builder = VariantArrayBuilder::new(10);
255/// #   builder.append_variant(Variant::from("such wow"));
256/// #   builder.build()
257/// # }
258/// # let variant_array = get_variant_array();
259/// // The DataType of a VariantArray varies depending on how it is shredded
260/// let data_type = variant_array.data_type().clone();
261/// // First field is an integer id, second field is a variant
262/// let schema = Schema::new(vec![
263///   Field::new("id", DataType::Int32, false),
264///   Field::new("var", data_type, false)
265///     // Add extension metadata to the field using `VariantType`
266///     .with_extension_type(VariantType),
267/// ]);
268/// ```
269///
270/// # Example: Converting a [`VariantArray`] to a [`StructArray`]
271///
272/// ```
273/// # use arrow::array::StructArray;
274/// # use parquet_variant::Variant;
275/// # use parquet_variant_compute::VariantArrayBuilder;
276/// // Create Variant Array
277/// let mut builder = VariantArrayBuilder::new(10);
278/// builder.append_variant(Variant::from("such wow"));
279/// let variant_array = builder.build();
280/// // convert to StructArray
281/// let struct_array: StructArray = variant_array.into();
282/// ```
283///
284/// # Example: Converting a [`StructArray`] to a [`VariantArray`]
285///
286/// ```
287/// # use arrow::array::StructArray;
288/// # use parquet_variant::Variant;
289/// # use parquet_variant_compute::{VariantArrayBuilder, VariantArray};
290/// # fn get_struct_array() -> StructArray {
291/// #   let mut builder = VariantArrayBuilder::new(10);
292/// #   builder.append_variant(Variant::from("such wow"));
293/// #   builder.build().into()
294/// # }
295/// let struct_array: StructArray = get_struct_array();
296/// // try and create a VariantArray from it
297/// let variant_array = VariantArray::try_new(&struct_array).unwrap();
298/// assert_eq!(variant_array.value(0), Variant::from("such wow"));
299/// ```
300///
301#[derive(Debug, Clone)]
302pub struct VariantArray {
303    /// Reference to the underlying StructArray
304    inner: StructArray,
305
306    /// The metadata column of this variant (Binary, LargeBinary, or BinaryView), possibly encoded
307    metadata: ArrayRef,
308
309    /// how is this variant array shredded?
310    shredding_state: ShreddingState,
311}
312
313impl VariantArray {
314    /// Creates a new `VariantArray` from a [`StructArray`].
315    ///
316    /// # Arguments
317    /// - `inner` - The underlying [`StructArray`] that contains the variant data.
318    ///
319    /// # Returns
320    /// - A new instance of `VariantArray`.
321    ///
322    /// # Errors:
323    /// - If the `StructArray` does not contain the required fields
324    ///
325    /// # Requirements of the `StructArray`
326    ///
327    /// 1. A required field named `metadata` which is binary, large_binary, or
328    ///    binary_view, optionally dictionary or run-end-encoded
329    ///
330    /// 2. A required field named `value` that is binary, large_binary, or
331    ///    binary_view
332    ///
333    /// 3. An optional field named `typed_value` which can be any primitive type
334    ///    or be a list, large_list, list_view or struct
335    ///
336    pub fn try_new(inner: &dyn Array) -> Result<Self> {
337        // Canonicalize shredded typed_value fields (e.g. decimal narrowing)
338        let inner = canonicalize_shredded_types(inner)?;
339
340        let Some(inner) = inner.as_struct_opt() else {
341            return Err(ArrowError::InvalidArgumentError(
342                "Invalid VariantArray: requires StructArray as input".to_string(),
343            ));
344        };
345
346        // Note the specification allows for any order so we must search by name
347
348        // Ensure the StructArray has a metadata field with binary-like logical values
349        let Some(metadata_col) = inner.column_by_name("metadata") else {
350            return Err(ArrowError::InvalidArgumentError(
351                "Invalid VariantArray: StructArray must contain a 'metadata' field".to_string(),
352            ));
353        };
354        validate_metadata_array(metadata_col.as_ref())?;
355
356        let shredding_state = ShreddingState::try_from(inner)?;
357
358        // `try_from` synthesizes an all-null `value` when the input omits it. Rebuild the inner
359        // struct so `inner()` and write-back carry the column too.
360        if inner.column_by_name("value").is_none() {
361            return Ok(Self::from_parts(
362                metadata_col.clone(),
363                shredding_state.value_column().clone(),
364                shredding_state.typed_value_column().cloned(),
365                inner.nulls().cloned(),
366            ));
367        }
368
369        // Note these clones are cheap, they just bump the ref count
370        Ok(Self {
371            inner: inner.clone(),
372            metadata: metadata_col.clone(),
373            shredding_state,
374        })
375    }
376
377    /// Note: annotates `value` as nullable, which the spec only permits for shredded
378    /// variants. It is also needed by `variant_get`'s unshredded intermediates, whose
379    /// `value` column can contain unmasked nulls. Unshredded producers should use
380    /// [`Self::from_parts_unshredded`] instead.
381    pub(crate) fn from_parts(
382        metadata: ArrayRef,
383        value: ArrayRef,
384        typed_value: Option<ArrayRef>,
385        nulls: Option<NullBuffer>,
386    ) -> Self {
387        Self::from_parts_with_nullable_value(metadata, value, typed_value, nulls, true)
388    }
389
390    /// Construct an unshredded `VariantArray`, annotating `value` as non-nullable as the
391    /// spec requires when there is no `typed_value` column.
392    ///
393    /// # Panics
394    /// If `value` contains nulls not masked by `nulls`.
395    pub(crate) fn from_parts_unshredded(
396        metadata: ArrayRef,
397        value: ArrayRef,
398        nulls: Option<NullBuffer>,
399    ) -> Self {
400        Self::from_parts_with_nullable_value(metadata, value, None, nulls, false)
401    }
402
403    fn from_parts_with_nullable_value(
404        metadata: ArrayRef,
405        value: ArrayRef,
406        typed_value: Option<ArrayRef>,
407        nulls: Option<NullBuffer>,
408        value_nullable: bool,
409    ) -> Self {
410        let mut builder = StructArrayBuilder::new()
411            .with_field("metadata", metadata.clone(), false)
412            .with_field("value", value.clone(), value_nullable);
413        if let Some(typed_value) = typed_value.clone() {
414            builder = builder.with_field_ref(typed_value_field(&typed_value), typed_value);
415        }
416        if let Some(nulls) = nulls {
417            builder = builder.with_nulls(nulls);
418        }
419
420        Self {
421            inner: builder.build(),
422            metadata,
423            shredding_state: ShreddingState::new(value, typed_value),
424        }
425    }
426
427    /// Returns a reference to the underlying [`StructArray`].
428    pub fn inner(&self) -> &StructArray {
429        &self.inner
430    }
431
432    /// Returns the inner [`StructArray`], consuming self
433    pub fn into_inner(self) -> StructArray {
434        self.inner
435    }
436
437    /// Return the shredding state of this `VariantArray`
438    pub fn shredding_state(&self) -> &ShreddingState {
439        &self.shredding_state
440    }
441
442    /// Return the [`Variant`] instance stored at the given row
443    ///
444    /// This is a convenience wrapper that calls [`VariantArray::try_value`] and unwraps the `Result`.
445    /// Use `try_value` if you need to handle conversion errors gracefully.
446    ///
447    /// # Panics
448    /// Panics if
449    /// * the index is out of bounds,
450    /// * the `metadata`/`value` bytes of the row are invalid, which includes reading a null row, or
451    /// * both `value` and `typed_value` are non-null for a non-struct `typed_value`.
452    pub fn value(&self, index: usize) -> Variant<'_, '_> {
453        self.try_value(index)
454            .unwrap_or_else(|err| panic!("VariantArray::value({index}) failed: {err}"))
455    }
456
457    /// Return the [`Variant`] instance stored at the given row
458    ///
459    /// Note: This method does not check for nulls and the value is arbitrary
460    /// (but still well-defined) if [`is_null`](Self::is_null) returns true for the index.
461    ///
462    /// # Errors
463    ///
464    /// Errors if
465    /// - the index is out of bounds
466    /// - the data in `typed_value` cannot be interpreted as a valid `Variant`
467    /// - both `value` and `typed_value` are non-null for a non-struct `typed_value`
468    ///
469    /// # Panics
470    ///
471    /// Panics if the unshredded `metadata`/`value` bytes fail basic validation, since those are
472    /// read with [`Variant::new`]. This includes reading a row that is null.
473    ///
474    /// If this is a shredded variant but has no value at the shredded location, it
475    /// will return [`Variant::Null`].
476    ///
477    ///
478    /// # Performance Note
479    ///
480    /// This is certainly not the most efficient way to access values in a
481    /// `VariantArray`, but it is useful for testing and debugging.
482    ///
483    /// Note: Does not do deep validation of the [`Variant`], so it is up to the
484    /// caller to ensure that the metadata and value were constructed correctly.
485    pub fn try_value(&self, index: usize) -> Result<Variant<'_, '_>> {
486        if self.len() <= index {
487            return Err(ArrowError::InvalidArgumentError(format!(
488                "Index {index} out of bounds for VariantArray of length {}",
489                self.len()
490            )));
491        }
492
493        let value = self.value_column();
494        match self.typed_value_column() {
495            // Always prefer typed_value, if available
496            Some(typed_value) if typed_value.is_valid(index) => {
497                if !matches!(typed_value.data_type(), DataType::Struct(_)) && value.is_valid(index) {
498                    // Only a partially shredded struct is allowed to have values for both columns
499                    return Err(ArrowError::InvalidArgumentError(
500                        "Invalid variant, conflicting value and typed_value".to_owned(),
501                    ));
502                }
503                typed_value_to_variant(typed_value, index)
504            }
505            // Otherwise fall back to value, if available
506            _ if value.is_valid(index) => variant_from_arrays_at(&self.metadata, value, index)
507                .ok_or_else(|| {
508                    ArrowError::InvalidArgumentError(format!(
509                        "metadata and value fields must be binary-like arrays, instead got {} and {}",
510                        self.metadata.data_type(),
511                        value.data_type()
512                    ))
513                }),
514            // It is technically invalid for both value and typed_value to be null,
515            // but the spec specifically requires readers to return Variant::Null in this case.
516            _ => Ok(Variant::Null),
517        }
518    }
519
520    /// Return a reference to the `metadata` column of the [`StructArray`]
521    pub fn metadata_column(&self) -> &ArrayRef {
522        &self.metadata
523    }
524
525    /// Return a reference to the `value` column of the [`StructArray`]
526    pub fn value_column(&self) -> &ArrayRef {
527        self.shredding_state.value_column()
528    }
529
530    /// Return a reference to the `typed_value` column of the [`StructArray`], if present
531    pub fn typed_value_column(&self) -> Option<&ArrayRef> {
532        self.shredding_state.typed_value_column()
533    }
534
535    /// Return a field to represent this VariantArray in a `Schema` with
536    /// a particular name
537    pub fn field(&self, name: impl Into<String>) -> Field {
538        Field::new(
539            name.into(),
540            self.data_type().clone(),
541            self.inner.is_nullable(),
542        )
543        .with_extension_type(VariantType)
544    }
545
546    /// Returns a new DataType representing this VariantArray's inner type
547    pub fn data_type(&self) -> &DataType {
548        self.inner.data_type()
549    }
550
551    pub fn slice(&self, offset: usize, length: usize) -> Self {
552        let inner = self.inner.slice(offset, length);
553        let metadata = self.metadata.slice(offset, length);
554        let shredding_state = self.shredding_state.slice(offset, length);
555        Self {
556            inner,
557            metadata,
558            shredding_state,
559        }
560    }
561
562    pub fn len(&self) -> usize {
563        self.inner.len()
564    }
565
566    pub fn is_empty(&self) -> bool {
567        self.inner.is_empty()
568    }
569
570    pub fn nulls(&self) -> Option<&NullBuffer> {
571        self.inner.nulls()
572    }
573
574    /// Is the element at index null?
575    pub fn is_null(&self, index: usize) -> bool {
576        self.nulls().is_some_and(|n| n.is_null(index))
577    }
578
579    /// Is the element at index valid (not null)?
580    pub fn is_valid(&self, index: usize) -> bool {
581        !self.is_null(index)
582    }
583
584    /// Returns an iterator over the values in this array
585    pub fn iter(&self) -> VariantArrayIter<'_> {
586        VariantArrayIter::new(self)
587    }
588}
589
590impl<'a> IntoIterator for &'a VariantArray {
591    type Item = Option<Variant<'a, 'a>>;
592    type IntoIter = VariantArrayIter<'a>;
593
594    fn into_iter(self) -> Self::IntoIter {
595        VariantArrayIter::new(self)
596    }
597}
598
599impl PartialEq for VariantArray {
600    fn eq(&self, other: &Self) -> bool {
601        self.inner == other.inner
602    }
603}
604
605impl From<VariantArray> for StructArray {
606    fn from(variant_array: VariantArray) -> Self {
607        variant_array.into_inner()
608    }
609}
610
611impl From<VariantArray> for ArrayRef {
612    fn from(variant_array: VariantArray) -> Self {
613        Arc::new(variant_array.into_inner())
614    }
615}
616
617impl<'m, 'v> FromIterator<Option<Variant<'m, 'v>>> for VariantArray {
618    fn from_iter<T: IntoIterator<Item = Option<Variant<'m, 'v>>>>(iter: T) -> Self {
619        let iter = iter.into_iter();
620
621        let mut b = VariantArrayBuilder::new(iter.size_hint().0);
622        b.extend(iter);
623        b.build()
624    }
625}
626
627impl<'m, 'v> FromIterator<Variant<'m, 'v>> for VariantArray {
628    fn from_iter<T: IntoIterator<Item = Variant<'m, 'v>>>(iter: T) -> Self {
629        Self::from_iter(iter.into_iter().map(Some))
630    }
631}
632
633/// An iterator over [`VariantArray`]
634///
635/// This iterator returns `Option<Option<Variant<'a, 'a>>>` where:
636/// - `None` indicates the end of iteration
637/// - `Some(None)` indicates a null value at this position
638/// - `Some(Some(variant))` indicates a valid variant value
639///
640/// # Example
641///
642/// ```
643/// # use parquet_variant::Variant;
644/// # use parquet_variant_compute::VariantArrayBuilder;
645/// let mut builder = VariantArrayBuilder::new(10);
646/// builder.append_variant(Variant::from(42));
647/// builder.append_null();
648/// builder.append_variant(Variant::from("hello"));
649/// let array = builder.build();
650///
651/// let values = array.iter().collect::<Vec<_>>();
652/// assert_eq!(values.len(), 3);
653/// assert_eq!(values[0], Some(Variant::from(42)));
654/// assert_eq!(values[1], None);
655/// assert_eq!(values[2], Some(Variant::from("hello")));
656/// ```
657#[derive(Debug)]
658pub struct VariantArrayIter<'a> {
659    array: &'a VariantArray,
660    head_i: usize,
661    tail_i: usize,
662}
663
664impl<'a> VariantArrayIter<'a> {
665    /// Creates a new iterator over the given [`VariantArray`]
666    pub fn new(array: &'a VariantArray) -> Self {
667        Self {
668            array,
669            head_i: 0,
670            tail_i: array.len(),
671        }
672    }
673
674    fn value_opt(&self, i: usize) -> Option<Variant<'a, 'a>> {
675        self.array.is_valid(i).then(|| self.array.value(i))
676    }
677}
678
679impl<'a> Iterator for VariantArrayIter<'a> {
680    type Item = Option<Variant<'a, 'a>>;
681
682    #[inline]
683    fn next(&mut self) -> Option<Self::Item> {
684        if self.head_i == self.tail_i {
685            return None;
686        }
687
688        let out = self.value_opt(self.head_i);
689
690        self.head_i += 1;
691
692        Some(out)
693    }
694
695    fn size_hint(&self) -> (usize, Option<usize>) {
696        let remainder = self.tail_i - self.head_i;
697
698        (remainder, Some(remainder))
699    }
700}
701
702impl DoubleEndedIterator for VariantArrayIter<'_> {
703    fn next_back(&mut self) -> Option<Self::Item> {
704        if self.head_i == self.tail_i {
705            return None;
706        }
707
708        self.tail_i -= 1;
709
710        Some(self.value_opt(self.tail_i))
711    }
712}
713
714impl ExactSizeIterator for VariantArrayIter<'_> {}
715
716/// One shredded field of a partially or perfectly shredded variant. For example, suppose the
717/// shredding schema for variant `v` treats it as an object with a single field `a`, where `a` is
718/// itself a struct with the single field `b` of type INT. Then the physical layout of the column
719/// is:
720///
721/// ```text
722/// v: VARIANT {
723///     metadata: BINARY,
724///     value: BINARY,
725///     typed_value: STRUCT {
726///         a: SHREDDED_VARIANT_FIELD {
727///             value: BINARY,
728///             typed_value: STRUCT {
729///                 a: SHREDDED_VARIANT_FIELD {
730///                     value: BINARY,
731///                     typed_value: INT,
732///                 },
733///             },
734///         },
735///     },
736/// }
737/// ```
738///
739/// In the above, each row of `v.value` is either a variant value (shredding failed, `v` was not an
740/// object at all) or a variant object (partial shredding, `v` was an object but included unexpected
741/// fields other than `a`), or is NULL (perfect shredding, `v` was an object containing only the
742/// single expected field `a`).
743///
744/// A similar story unfolds for each `v.typed_value.a.value` -- a variant value if shredding failed
745/// (`v:a` was not an object at all), or a variant object (`v:a` was an object with unexpected
746/// additional fields), or NULL (`v:a` was an object containing only the single expected field `b`).
747///
748/// Finally, `v.typed_value.a.typed_value.b.value` is either NULL (`v:a.b` was an integer) or else a
749/// variant value (which could be `Variant::Null`).
750#[derive(Debug)]
751pub struct ShreddedVariantFieldArray {
752    /// Reference to the underlying StructArray
753    inner: StructArray,
754    shredding_state: ShreddingState,
755}
756
757impl ShreddedVariantFieldArray {
758    /// Creates a new `ShreddedVariantFieldArray` from a [`StructArray`].
759    ///
760    /// # Arguments
761    /// - `inner` - The underlying [`StructArray`] that contains the variant data.
762    ///
763    /// # Returns
764    /// - A new instance of `ShreddedVariantFieldArray`.
765    ///
766    /// # Errors:
767    /// - If the `StructArray` does not contain the required fields
768    ///
769    /// # Requirements of the `StructArray`
770    ///
771    /// 1. A required field named `value` that is binary, large_binary, or
772    ///    binary_view
773    ///
774    /// 2. An optional field named `typed_value` which can be any primitive type
775    ///    or be a list, large_list, list_view or struct
776    ///
777    pub fn try_new(inner: &dyn Array) -> Result<Self> {
778        let Some(inner_struct) = inner.as_struct_opt() else {
779            return Err(ArrowError::InvalidArgumentError(
780                "Invalid ShreddedVariantFieldArray: requires StructArray as input".to_string(),
781            ));
782        };
783
784        let shredding_state = ShreddingState::try_from(inner_struct)?;
785
786        // `try_from` synthesizes an all-null `value` when the input omits it. Rebuild the inner
787        // struct so `inner()` and write-back carry the column too (see `VariantArray::try_new`).
788        if inner_struct.column_by_name("value").is_none() {
789            return Ok(Self::from_parts(
790                shredding_state.value_column().clone(),
791                shredding_state.typed_value_column().cloned(),
792                inner_struct.nulls().cloned(),
793            ));
794        }
795
796        // Note this clone is cheap, it just bumps the ref count
797        Ok(Self {
798            inner: inner_struct.clone(),
799            shredding_state,
800        })
801    }
802
803    /// Return the shredding state of this `VariantArray`
804    pub fn shredding_state(&self) -> &ShreddingState {
805        &self.shredding_state
806    }
807
808    /// Return a reference to the `value` column of the [`StructArray`]
809    pub fn value_column(&self) -> &ArrayRef {
810        self.shredding_state.value_column()
811    }
812
813    /// Return a reference to the `typed_value` column of the [`StructArray`], if present
814    pub fn typed_value_column(&self) -> Option<&ArrayRef> {
815        self.shredding_state.typed_value_column()
816    }
817
818    /// Returns a reference to the underlying [`StructArray`].
819    pub fn inner(&self) -> &StructArray {
820        &self.inner
821    }
822
823    pub(crate) fn from_parts(
824        value: ArrayRef,
825        typed_value: Option<ArrayRef>,
826        nulls: Option<NullBuffer>,
827    ) -> Self {
828        let mut builder = StructArrayBuilder::new().with_field("value", value.clone(), true);
829        if let Some(typed_value) = typed_value.clone() {
830            builder = builder.with_field_ref(typed_value_field(&typed_value), typed_value);
831        }
832        if let Some(nulls) = nulls {
833            builder = builder.with_nulls(nulls);
834        }
835
836        Self {
837            inner: builder.build(),
838            shredding_state: ShreddingState::new(value, typed_value),
839        }
840    }
841
842    /// Returns the inner [`StructArray`], consuming self
843    pub fn into_inner(self) -> StructArray {
844        self.inner
845    }
846
847    pub fn data_type(&self) -> &DataType {
848        self.inner.data_type()
849    }
850
851    pub fn len(&self) -> usize {
852        self.inner.len()
853    }
854
855    pub fn is_empty(&self) -> bool {
856        self.inner.is_empty()
857    }
858
859    pub fn offset(&self) -> usize {
860        self.inner.offset()
861    }
862
863    pub fn nulls(&self) -> Option<&NullBuffer> {
864        // According to the shredding spec, ShreddedVariantFieldArray should be
865        // physically non-nullable - SQL NULL is inferred by both value and
866        // typed_value being physically NULL
867        None
868    }
869    /// Is the element at index null?
870    pub fn is_null(&self, index: usize) -> bool {
871        self.nulls().is_some_and(|n| n.is_null(index))
872    }
873
874    /// Is the element at index valid (not null)?
875    pub fn is_valid(&self, index: usize) -> bool {
876        !self.is_null(index)
877    }
878}
879
880impl From<ShreddedVariantFieldArray> for ArrayRef {
881    fn from(array: ShreddedVariantFieldArray) -> Self {
882        Arc::new(array.into_inner())
883    }
884}
885
886impl From<ShreddedVariantFieldArray> for StructArray {
887    fn from(array: ShreddedVariantFieldArray) -> Self {
888        array.into_inner()
889    }
890}
891
892/// Represents the shredding state of a [`VariantArray`]
893///
894/// [`VariantArray`]s can be shredded according to the [Parquet Variant
895/// Shredding Spec]. Shredding means that the actual value is stored in a typed
896/// `typed_field` instead of the generic `value` field.
897///
898/// The `value` column is always present (the spec requires writers to emit
899/// it); `typed_value` is optional. Values in the two columns must be
900/// interpreted according to the following table (see [Parquet Variant
901/// Shredding Spec] for more details):
902///
903/// | value    | typed_value  | Meaning |
904/// |----------|--------------|---------|
905/// | NULL     | NULL         | The value is missing; only valid for shredded object fields |
906/// | non-NULL | NULL         | The value is present and may be any type, including [`Variant::Null`] |
907/// | NULL     | non-NULL     | The value is present and is the shredded type |
908/// | non-NULL | non-NULL     | The value is present and is a partially shredded object |
909///
910///
911/// Applying the above rules to entire columns, we obtain the following:
912///
913/// | value  | typed_value  | Meaning |
914/// |--------|-------------|---------|
915/// | exists | --          | **Unshredded**: If present, the value may be any type, including [`Variant::Null`]
916/// | exists | exists      | **Shredded**: perfectly if `value` is all-null, otherwise imperfectly |
917///
918/// Note the spec requires the `value` column to always be present in the
919/// schema; structs without one are rejected
920/// (see <https://github.com/apache/arrow-rs/issues/10306>).
921///
922/// NOTE: Partial shredding is a row-wise situation that can arise under imperfect shredding (a
923/// column-wise situation): When both columns exist (imperfect shredding) and the typed_value column
924/// is a struct, then both columns can be non-NULL for the same row if value is a variant object
925/// (partial shredding).
926///
927/// [Parquet Variant Shredding Spec]: https://github.com/apache/parquet-format/blob/master/VariantShredding.md#value-shredding
928#[derive(Debug, Clone)]
929pub struct ShreddingState {
930    value: ArrayRef,
931    typed_value: Option<ArrayRef>,
932}
933
934impl ShreddingState {
935    /// Create a new `ShreddingState` from the given `value` and `typed_value` fields
936    ///
937    /// Note you can create a `ShreddingState` from a &[`StructArray`] using
938    /// `ShreddingState::try_from(&struct_array)`, for example:
939    ///
940    /// ```no_run
941    /// # use arrow::array::StructArray;
942    /// # use parquet_variant_compute::ShreddingState;
943    /// # fn get_struct_array() -> StructArray {
944    /// #   unimplemented!()
945    /// # }
946    /// let struct_array: StructArray = get_struct_array();
947    /// let shredding_state = ShreddingState::try_from(&struct_array).unwrap();
948    /// ```
949    pub fn new(value: ArrayRef, typed_value: Option<ArrayRef>) -> Self {
950        Self { value, typed_value }
951    }
952
953    /// Return a reference to the `value` column
954    pub fn value_column(&self) -> &ArrayRef {
955        &self.value
956    }
957
958    /// Return a reference to the `typed_value` column, if present
959    pub fn typed_value_column(&self) -> Option<&ArrayRef> {
960        self.typed_value.as_ref()
961    }
962
963    /// Slice all the underlying arrays
964    pub fn slice(&self, offset: usize, length: usize) -> Self {
965        Self {
966            value: self.value.slice(offset, length),
967            typed_value: self.typed_value.as_ref().map(|tv| tv.slice(offset, length)),
968        }
969    }
970}
971
972impl TryFrom<&StructArray> for ShreddingState {
973    type Error = ArrowError;
974
975    fn try_from(inner_struct: &StructArray) -> Result<Self> {
976        let typed_value = inner_struct.column_by_name("typed_value").cloned();
977        let value = match inner_struct.column_by_name("value") {
978            Some(value) => {
979                validate_binary_array(value.as_ref(), "value")?;
980                value.clone()
981            }
982            // Lenient read: a shredded group may omit the spec-required `value`. Synthesize an
983            // all-null one so the model always has a `value`.
984            None if typed_value.is_some() => all_null_value_column(inner_struct.len()),
985            None => {
986                return Err(ArrowError::InvalidArgumentError(
987                    "Invalid VariantArray: StructArray must contain a 'value' field".to_string(),
988                ));
989            }
990        };
991        Ok(ShreddingState::new(value, typed_value))
992    }
993}
994
995/// Build the `typed_value` [`FieldRef`] for a shredded column.
996///
997/// The Variant spec maps `FixedSizeBinary(16)` exclusively to UUID, so any
998/// shredded column of that type must carry the canonical [`UuidExtension`]
999/// extension metadata on its field.
1000fn typed_value_field(array: &ArrayRef) -> FieldRef {
1001    let mut field = Field::new("typed_value", array.data_type().clone(), true);
1002    if matches!(array.data_type(), DataType::FixedSizeBinary(16)) {
1003        field = field.with_extension_type(UuidExtension);
1004    }
1005    Arc::new(field)
1006}
1007
1008/// Builds struct arrays from component fields
1009///
1010/// TODO: move to arrow crate
1011#[derive(Debug, Default, Clone)]
1012pub(crate) struct StructArrayBuilder {
1013    fields: Vec<FieldRef>,
1014    arrays: Vec<ArrayRef>,
1015    nulls: Option<NullBuffer>,
1016}
1017
1018impl StructArrayBuilder {
1019    pub fn new() -> Self {
1020        Default::default()
1021    }
1022
1023    /// Add an array to this struct array as a field with the specified name.
1024    pub fn with_field(mut self, field_name: &str, array: ArrayRef, nullable: bool) -> Self {
1025        let field = Field::new(field_name, array.data_type().clone(), nullable);
1026        self.fields.push(Arc::new(field));
1027        self.arrays.push(array);
1028        self
1029    }
1030
1031    /// Add an array to this struct array using a caller-supplied [`FieldRef`].
1032    ///
1033    /// Use this when the field carries metadata (e.g. an extension type) that
1034    /// would be lost if the field were synthesized from the array's data type alone.
1035    pub fn with_field_ref(mut self, field: FieldRef, array: ArrayRef) -> Self {
1036        self.fields.push(field);
1037        self.arrays.push(array);
1038        self
1039    }
1040
1041    /// Set the null buffer for this struct array.
1042    pub fn with_nulls(mut self, nulls: NullBuffer) -> Self {
1043        self.nulls = Some(nulls);
1044        self
1045    }
1046
1047    pub fn build(self) -> StructArray {
1048        let Self {
1049            fields,
1050            arrays,
1051            nulls,
1052        } = self;
1053        StructArray::new(Fields::from(fields), arrays, nulls)
1054    }
1055}
1056
1057/// returns the non-null element at index as a Variant
1058fn typed_value_to_variant(typed_value: &ArrayRef, index: usize) -> Result<Variant<'_, '_>> {
1059    let data_type = typed_value.data_type();
1060    match data_type {
1061        DataType::Null => Ok(Variant::Null),
1062        DataType::Boolean => {
1063            let boolean_array = typed_value.as_boolean();
1064            let value = boolean_array.value(index);
1065            Ok(Variant::from(value))
1066        }
1067        // 16-byte FixedSizeBinary always corresponds to a UUID; all other sizes are illegal.
1068        DataType::FixedSizeBinary(16) => {
1069            let array = typed_value.as_fixed_size_binary();
1070            let value = array.value(index);
1071            Ok(Uuid::from_slice(value).unwrap().into()) // unwrap is safe: slice is always 16 bytes
1072        }
1073        DataType::Binary => {
1074            let array = typed_value.as_binary::<i32>();
1075            let value = array.value(index);
1076            Ok(Variant::from(value))
1077        }
1078        DataType::LargeBinary => {
1079            let array = typed_value.as_binary::<i64>();
1080            let value = array.value(index);
1081            Ok(Variant::from(value))
1082        }
1083        DataType::BinaryView => {
1084            let array = typed_value.as_binary_view();
1085            let value = array.value(index);
1086            Ok(Variant::from(value))
1087        }
1088        DataType::Utf8 => {
1089            let array = typed_value.as_string::<i32>();
1090            let value = array.value(index);
1091            Ok(Variant::from(value))
1092        }
1093        DataType::LargeUtf8 => {
1094            let array = typed_value.as_string::<i64>();
1095            let value = array.value(index);
1096            Ok(Variant::from(value))
1097        }
1098        DataType::Utf8View => {
1099            let array = typed_value.as_string_view();
1100            let value = array.value(index);
1101            Ok(Variant::from(value))
1102        }
1103        DataType::Int8 => {
1104            primitive_conversion_single_value!(Int8Type, typed_value, index)
1105        }
1106        DataType::Int16 => {
1107            primitive_conversion_single_value!(Int16Type, typed_value, index)
1108        }
1109        DataType::Int32 => {
1110            primitive_conversion_single_value!(Int32Type, typed_value, index)
1111        }
1112        DataType::Int64 => {
1113            primitive_conversion_single_value!(Int64Type, typed_value, index)
1114        }
1115        DataType::Float16 => {
1116            primitive_conversion_single_value!(Float16Type, typed_value, index)
1117        }
1118        DataType::Float32 => {
1119            primitive_conversion_single_value!(Float32Type, typed_value, index)
1120        }
1121        DataType::Float64 => {
1122            primitive_conversion_single_value!(Float64Type, typed_value, index)
1123        }
1124        DataType::Decimal32(_, s) => {
1125            generic_conversion_single_value_with_result!(
1126                Decimal32Type,
1127                as_primitive,
1128                |v| VariantDecimal4::try_new(v, *s as u8),
1129                typed_value,
1130                index
1131            )
1132        }
1133        DataType::Decimal64(_, s) => {
1134            generic_conversion_single_value_with_result!(
1135                Decimal64Type,
1136                as_primitive,
1137                |v| VariantDecimal8::try_new(v, *s as u8),
1138                typed_value,
1139                index
1140            )
1141        }
1142        DataType::Decimal128(_, s) => {
1143            generic_conversion_single_value_with_result!(
1144                Decimal128Type,
1145                as_primitive,
1146                |v| VariantDecimal16::try_new(v, *s as u8),
1147                typed_value,
1148                index
1149            )
1150        }
1151        DataType::Date32 => {
1152            generic_conversion_single_value!(
1153                Date32Type,
1154                as_primitive,
1155                |v| Date32Type::to_naive_date_opt(v).unwrap(),
1156                typed_value,
1157                index
1158            )
1159        }
1160        DataType::Time64(TimeUnit::Microsecond) => {
1161            generic_conversion_single_value_with_result!(
1162                Time64MicrosecondType,
1163                as_primitive,
1164                |v| NaiveTime::from_num_seconds_from_midnight_opt(
1165                    (v / 1_000_000) as u32,
1166                    (v % 1_000_000) as u32 * 1000
1167                )
1168                .ok_or_else(|| format!("Invalid microsecond from midnight: {v}")),
1169                typed_value,
1170                index
1171            )
1172        }
1173        DataType::Timestamp(TimeUnit::Microsecond, Some(_)) => {
1174            generic_conversion_single_value!(
1175                TimestampMicrosecondType,
1176                as_primitive,
1177                |v| DateTime::from_timestamp_micros(v).unwrap(),
1178                typed_value,
1179                index
1180            )
1181        }
1182        DataType::Timestamp(TimeUnit::Microsecond, None) => {
1183            generic_conversion_single_value!(
1184                TimestampMicrosecondType,
1185                as_primitive,
1186                |v| DateTime::from_timestamp_micros(v).unwrap().naive_utc(),
1187                typed_value,
1188                index
1189            )
1190        }
1191        DataType::Timestamp(TimeUnit::Nanosecond, Some(_)) => {
1192            generic_conversion_single_value!(
1193                TimestampNanosecondType,
1194                as_primitive,
1195                DateTime::from_timestamp_nanos,
1196                typed_value,
1197                index
1198            )
1199        }
1200        DataType::Timestamp(TimeUnit::Nanosecond, None) => {
1201            generic_conversion_single_value!(
1202                TimestampNanosecondType,
1203                as_primitive,
1204                |v| DateTime::from_timestamp_nanos(v).naive_utc(),
1205                typed_value,
1206                index
1207            )
1208        }
1209        // todo other types here (note this is very similar to cast_to_variant.rs)
1210        // so it would be great to figure out how to share this code
1211        //
1212        // Composite shredded values may require combining `value` and
1213        // `typed_value` and allocating new encoded bytes. `try_value` returns
1214        // borrowed Variant, so callers must unshred the array first.
1215        _ => Err(ArrowError::NotYetImplemented(format!(
1216            "VariantArray::try_value cannot materialize typed_value of type {} \
1217             as a borrowed Variant; call unshred_variant first",
1218            typed_value.data_type()
1219        ))),
1220    }
1221}
1222
1223/// Canonicalize shredded typed_value fields (e.g. decimal narrowing) and
1224/// verify that all data types in the struct are legal for a variant array.
1225fn canonicalize_shredded_types(array: &dyn Array) -> Result<ArrayRef> {
1226    let new_type = canonicalize_and_verify_data_type_impl(array.data_type(), true)?;
1227    if let Cow::Borrowed(_) = new_type
1228        && let Some(array) = array.as_struct_opt()
1229    {
1230        return Ok(Arc::new(array.clone())); // bypass the unnecessary cast
1231    }
1232    cast(array, new_type.as_ref())
1233}
1234
1235/// Recursively visits a data type, ensuring that it only contains data types that can legally
1236/// appear in a (possibly shredded) variant array. It also narrows decimal types to the smallest
1237/// valid precision (e.g. Decimal128 -> Decimal32 when the precision fits).
1238fn canonicalize_and_verify_data_type(data_type: &DataType) -> Result<Cow<'_, DataType>> {
1239    canonicalize_and_verify_data_type_impl(data_type, false)
1240}
1241
1242fn canonicalize_and_verify_data_type_impl(
1243    data_type: &DataType,
1244    skip_top_level_metadata: bool,
1245) -> Result<Cow<'_, DataType>> {
1246    use DataType::*;
1247
1248    // helper macros
1249    macro_rules! fail {
1250        () => {
1251            return Err(ArrowError::InvalidArgumentError(format!(
1252                "Illegal shredded value type: {data_type}"
1253            )))
1254        };
1255    }
1256    macro_rules! borrow {
1257        () => {
1258            Cow::Borrowed(data_type)
1259        };
1260    }
1261
1262    let new_data_type = match data_type {
1263        // Primitive arrow types that have a direct variant counterpart are allowed
1264        Null | Boolean => borrow!(),
1265        Int8 | Int16 | Int32 | Int64 | Float32 | Float64 => borrow!(),
1266
1267        // Unsigned integers and half-float are not allowed
1268        UInt8 | UInt16 | UInt32 | UInt64 | Float16 => fail!(),
1269
1270        // Most decimal types are allowed, with restrictions on precision and scale
1271        //
1272        // NOTE: arrow-parquet reads widens 32- and 64-bit decimals to 128-bit, but the variant spec
1273        // requires using the narrowest decimal type for a given precision. Fix those up first.
1274        Decimal64(p, s) | Decimal128(p, s)
1275            if VariantDecimal4::is_valid_precision_and_scale(p, s) =>
1276        {
1277            Cow::Owned(Decimal32(*p, *s))
1278        }
1279        Decimal128(p, s) if VariantDecimal8::is_valid_precision_and_scale(p, s) => {
1280            Cow::Owned(Decimal64(*p, *s))
1281        }
1282        Decimal32(p, s) if VariantDecimal4::is_valid_precision_and_scale(p, s) => borrow!(),
1283        Decimal64(p, s) if VariantDecimal8::is_valid_precision_and_scale(p, s) => borrow!(),
1284        Decimal128(p, s) if VariantDecimal16::is_valid_precision_and_scale(p, s) => borrow!(),
1285        Decimal32(..) | Decimal64(..) | Decimal128(..) | Decimal256(..) => fail!(),
1286
1287        // Only micro and nano timestamps are allowed
1288        Timestamp(TimeUnit::Microsecond | TimeUnit::Nanosecond, _) => borrow!(),
1289        Timestamp(TimeUnit::Millisecond | TimeUnit::Second, _) => fail!(),
1290
1291        // Only 32-bit dates and 64-bit microsecond time are allowed.
1292        Date32 | Time64(TimeUnit::Microsecond) => borrow!(),
1293        Date64 | Time32(_) | Time64(_) | Duration(_) | Interval(_) => fail!(),
1294
1295        // Binary, string, and their view counterparts are allowed.
1296        Binary | LargeBinary | BinaryView | Utf8 | LargeUtf8 | Utf8View => borrow!(),
1297
1298        // UUID maps to 16-byte fixed-size binary; no other width is allowed
1299        FixedSizeBinary(16) => borrow!(),
1300        FixedSizeBinary(_) | FixedSizeList(..) => fail!(),
1301
1302        // List-like containers and struct are allowed, maps and unions are not
1303        List(field) => match canonicalize_and_verify_field(field)? {
1304            Cow::Borrowed(_) => borrow!(),
1305            Cow::Owned(new_field) => Cow::Owned(DataType::List(new_field)),
1306        },
1307        LargeList(field) => match canonicalize_and_verify_field(field)? {
1308            Cow::Borrowed(_) => borrow!(),
1309            Cow::Owned(new_field) => Cow::Owned(DataType::LargeList(new_field)),
1310        },
1311        ListView(field) => match canonicalize_and_verify_field(field)? {
1312            Cow::Borrowed(_) => borrow!(),
1313            Cow::Owned(new_field) => Cow::Owned(DataType::ListView(new_field)),
1314        },
1315        LargeListView(field) => match canonicalize_and_verify_field(field)? {
1316            Cow::Borrowed(_) => borrow!(),
1317            Cow::Owned(new_field) => Cow::Owned(DataType::LargeListView(new_field)),
1318        },
1319        // Struct is used by the internal layout, and can also represent a shredded variant object.
1320        Struct(fields) => {
1321            // Avoid allocation unless at least one field changes, to avoid unnecessary deep cloning
1322            // of the data type. Even if some fields change, the others are shallow arc clones.
1323            let mut new_fields = std::collections::HashMap::new();
1324            for (i, field) in fields.iter().enumerate() {
1325                if skip_top_level_metadata && field.name() == "metadata" {
1326                    continue;
1327                }
1328                if let Cow::Owned(new_field) = canonicalize_and_verify_field(field)? {
1329                    new_fields.insert(i, new_field);
1330                }
1331            }
1332
1333            if new_fields.is_empty() {
1334                borrow!()
1335            } else {
1336                let new_fields = fields
1337                    .iter()
1338                    .enumerate()
1339                    .map(|(i, field)| new_fields.remove(&i).unwrap_or_else(|| field.clone()));
1340                Cow::Owned(DataType::Struct(new_fields.collect()))
1341            }
1342        }
1343        Map(..) | Union(..) => fail!(),
1344
1345        // We can _possibly_ support (some of) these some day?
1346        Dictionary(..) | RunEndEncoded(..) => fail!(),
1347    };
1348    Ok(new_data_type)
1349}
1350
1351fn canonicalize_and_verify_field(field: &Arc<Field>) -> Result<Cow<'_, Arc<Field>>> {
1352    let new_data_type = canonicalize_and_verify_data_type(field.data_type())?;
1353
1354    // A shredded FixedSizeBinary(16) column is always a UUID. Tag it with the UUID extension type
1355    // on read, as a safety net against writers that emit the column without the extension metadata.
1356    // Canonicalization never rewrites FixedSizeBinary(16), so the type is already correct here.
1357    if matches!(new_data_type.as_ref(), DataType::FixedSizeBinary(16))
1358        && !field.has_valid_extension_type::<UuidExtension>()
1359    {
1360        let new_field = field.as_ref().clone().with_extension_type(UuidExtension);
1361        return Ok(Cow::Owned(Arc::new(new_field)));
1362    }
1363
1364    let Cow::Owned(new_data_type) = new_data_type else {
1365        return Ok(Cow::Borrowed(field));
1366    };
1367    let new_field = field.as_ref().clone().with_data_type(new_data_type);
1368    Ok(Cow::Owned(Arc::new(new_field)))
1369}
1370
1371/// Test-only constructors for perfectly shredded arrays, where every value lives in
1372/// `typed_value` and the required `value` column is all-null.
1373#[cfg(test)]
1374impl VariantArray {
1375    pub(crate) fn perfectly_shredded(
1376        metadata: ArrayRef,
1377        typed_value: ArrayRef,
1378        nulls: Option<NullBuffer>,
1379    ) -> Self {
1380        let value = all_null_value_column(typed_value.len());
1381        Self::from_parts(metadata, value, Some(typed_value), nulls)
1382    }
1383}
1384
1385#[cfg(test)]
1386impl ShreddedVariantFieldArray {
1387    pub(crate) fn perfectly_shredded(typed_value: ArrayRef) -> Self {
1388        let value = all_null_value_column(typed_value.len());
1389        Self::from_parts(value, Some(typed_value), None)
1390    }
1391}
1392
1393#[cfg(test)]
1394mod test {
1395    use crate::{GetOptions, VariantArrayBuilder, json_to_variant, variant_get, variant_to_json};
1396    use std::str::FromStr;
1397
1398    use super::*;
1399    use arrow::array::{
1400        BinaryArray, BinaryDictionaryBuilder, BinaryRunBuilder, BinaryViewArray, Decimal32Array,
1401        Decimal64Array, Decimal128Array, FixedSizeBinaryArray, Int8Array, Int32Array, Int64Array,
1402        LargeBinaryArray, LargeListArray, LargeListViewArray, ListArray, ListViewArray,
1403        StringArray, Time64MicrosecondArray,
1404    };
1405    use arrow::buffer::{OffsetBuffer, ScalarBuffer};
1406    use arrow_schema::{Field, Fields};
1407    use parquet_variant::{EMPTY_VARIANT_METADATA_BYTES, ShortString};
1408
1409    #[test]
1410    fn invalid_not_a_struct_array() {
1411        let array = make_binary_view_array();
1412        // Should fail because the input is not a StructArray
1413        let err = VariantArray::try_new(&array);
1414        assert_eq!(
1415            err.unwrap_err().to_string(),
1416            "Invalid argument error: Invalid VariantArray: requires StructArray as input"
1417        );
1418    }
1419
1420    #[test]
1421    fn invalid_missing_metadata() {
1422        let fields = Fields::from(vec![Field::new("value", DataType::BinaryView, true)]);
1423        let array = StructArray::new(fields, vec![make_binary_view_array()], None);
1424        // Should fail because the StructArray does not contain a 'metadata' field
1425        let err = VariantArray::try_new(&array);
1426        assert_eq!(
1427            err.unwrap_err().to_string(),
1428            "Invalid argument error: Invalid VariantArray: StructArray must contain a 'metadata' field"
1429        );
1430    }
1431
1432    #[test]
1433    fn read_missing_value_column() {
1434        // With `typed_value` present, a missing `value` is read leniently: an all-null `value` is
1435        // synthesized and normalized into the inner struct (so a write-back emits it), and values
1436        // still decode from `typed_value`.
1437        let typed_value = Arc::new(Int64Array::from(vec![Some(1), None, Some(3)])) as ArrayRef;
1438        let metadata =
1439            BinaryViewArray::from_iter_values(std::iter::repeat_n(EMPTY_VARIANT_METADATA_BYTES, 3));
1440        let struct_array = StructArrayBuilder::new()
1441            .with_field("metadata", Arc::new(metadata), false)
1442            .with_field("typed_value", typed_value, true)
1443            .build();
1444        assert!(struct_array.column_by_name("value").is_none());
1445
1446        let variant_array = VariantArray::try_new(&struct_array).unwrap();
1447        assert_eq!(variant_array.value_column().len(), 3);
1448        assert_eq!(variant_array.value_column().null_count(), 3);
1449        assert!(variant_array.inner().column_by_name("value").is_some());
1450        assert!(variant_array.typed_value_column().is_some());
1451        assert_eq!(variant_array.value(0), Variant::from(1i64));
1452        assert_eq!(variant_array.value(1), Variant::Null);
1453        assert_eq!(variant_array.value(2), Variant::from(3i64));
1454
1455        // With no `typed_value` either, there is nothing to read, so `value` stays required.
1456        let fields = Fields::from(vec![Field::new("metadata", DataType::BinaryView, false)]);
1457        let metadata_only = StructArray::new(fields, vec![make_binary_view_array()], None);
1458        let err = VariantArray::try_new(&metadata_only);
1459        assert_eq!(
1460            err.unwrap_err().to_string(),
1461            "Invalid argument error: Invalid VariantArray: StructArray must contain a 'value' field"
1462        );
1463    }
1464
1465    #[test]
1466    fn invalid_metadata_field_type() {
1467        let fields = Fields::from(vec![
1468            Field::new("metadata", DataType::Int32, true), // not supported
1469            Field::new("value", DataType::BinaryView, true),
1470        ]);
1471        let array = StructArray::new(
1472            fields,
1473            vec![make_int32_array(), make_binary_view_array()],
1474            None,
1475        );
1476        let err = VariantArray::try_new(&array);
1477        assert_eq!(
1478            err.unwrap_err().to_string(),
1479            "Invalid argument error: VariantArray 'metadata' field must be Binary, LargeBinary, BinaryView, or a Dictionary or RunEndEncoded array of one of those types, got Int32"
1480        );
1481    }
1482
1483    #[test]
1484    fn encoded_metadata_supports_nulls_slices_and_variant_get() {
1485        let json: ArrayRef = Arc::new(StringArray::from(vec![
1486            Some(r#"{"a":0}"#),
1487            Some(r#"{"a":1}"#),
1488            None,
1489            Some(r#"{"b":3}"#),
1490            Some(r#"{"b":4}"#),
1491        ]));
1492        let baseline = json_to_variant(&json).unwrap();
1493        let metadata = baseline.metadata_column().as_binary_view();
1494        let metadata_a = metadata.value(0);
1495        let metadata_b = metadata.value(3);
1496
1497        let logical_metadata = [
1498            Some(metadata_a),
1499            Some(metadata_a),
1500            None,
1501            Some(metadata_b),
1502            Some(metadata_b),
1503        ];
1504
1505        let mut dictionary = BinaryDictionaryBuilder::<Int8Type>::new();
1506        dictionary.extend(logical_metadata);
1507        let dictionary: ArrayRef = Arc::new(dictionary.finish());
1508
1509        let mut ree = BinaryRunBuilder::<Int16Type>::new();
1510        ree.extend(logical_metadata);
1511        let run_end_encoded: ArrayRef = Arc::new(ree.finish());
1512
1513        for metadata in [dictionary, run_end_encoded] {
1514            assert_eq!(binary_array_value(metadata.as_ref(), 2), None);
1515            let fields = Fields::from(vec![
1516                Field::new("metadata", metadata.data_type().clone(), false),
1517                Field::new("value", baseline.value_column().data_type().clone(), false),
1518            ]);
1519            let input = StructArray::try_new(
1520                fields,
1521                vec![metadata, baseline.value_column().clone()],
1522                baseline.nulls().cloned(),
1523            )
1524            .unwrap()
1525            .slice(1, 3);
1526
1527            let variant = VariantArray::try_new(&input).unwrap();
1528            assert_eq!(variant.value(0), baseline.value(1));
1529            assert!(variant.is_null(1));
1530            assert_eq!(variant.value(2), baseline.value(3));
1531
1532            let input: ArrayRef = Arc::new(input);
1533            let options = GetOptions::new_with_path("b".try_into().unwrap())
1534                .with_as_type(Some(Arc::new(Field::new("b", DataType::Int8, true))));
1535            let result = variant_get(&input, options).unwrap();
1536            assert_eq!(
1537                result.as_primitive::<Int8Type>(),
1538                &Int8Array::from(vec![None, None, Some(3)])
1539            );
1540            assert_eq!(
1541                variant_to_json(&input).unwrap(),
1542                StringArray::from(vec![Some(r#"{"a":1}"#), None, Some(r#"{"b":3}"#)])
1543            );
1544        }
1545    }
1546
1547    #[test]
1548    fn invalid_value_field_type() {
1549        let fields = Fields::from(vec![
1550            Field::new("metadata", DataType::BinaryView, true),
1551            Field::new("value", DataType::Int32, true),
1552        ]);
1553        let array = StructArray::new(
1554            fields,
1555            vec![make_binary_view_array(), make_int32_array()],
1556            None,
1557        );
1558        let err = VariantArray::try_new(&array);
1559        assert_eq!(
1560            err.unwrap_err().to_string(),
1561            "Invalid argument error: VariantArray 'value' field must be Binary, LargeBinary, or BinaryView, got Int32"
1562        );
1563    }
1564
1565    fn make_binary_view_array() -> ArrayRef {
1566        Arc::new(BinaryViewArray::from(vec![b"test" as &[u8]]))
1567    }
1568
1569    fn make_int32_array() -> ArrayRef {
1570        Arc::new(Int32Array::from(vec![1]))
1571    }
1572
1573    fn make_variant_struct_with_typed_value(typed_value: ArrayRef) -> StructArray {
1574        let metadata = BinaryViewArray::from_iter_values(std::iter::repeat_n(
1575            EMPTY_VARIANT_METADATA_BYTES,
1576            typed_value.len(),
1577        ));
1578        let value = new_null_array(&DataType::BinaryView, typed_value.len());
1579        StructArrayBuilder::new()
1580            .with_field("metadata", Arc::new(metadata), false)
1581            .with_field("value", value, true)
1582            .with_field("typed_value", typed_value, true)
1583            .build()
1584    }
1585
1586    #[test]
1587    fn try_new_tags_untagged_uuid_on_read() {
1588        // Simulate a foreign writer that shredded a UUID column as bare FixedSizeBinary(16),
1589        // omitting the UUID extension type.
1590        let typed_value = FixedSizeBinaryArray::try_from_iter(std::iter::repeat_n([0u8; 16], 2));
1591        let input = make_variant_struct_with_typed_value(Arc::new(typed_value.unwrap()));
1592
1593        // try_new canonicalizes on the read path and attaches the extension.
1594        let variant_array = VariantArray::try_new(&input).unwrap();
1595        let typed_value = variant_array.inner().field_by_name("typed_value").unwrap();
1596        assert_eq!(typed_value.data_type(), &DataType::FixedSizeBinary(16));
1597        assert!(typed_value.has_valid_extension_type::<UuidExtension>());
1598    }
1599
1600    #[test]
1601    fn try_new_tags_untagged_nested_uuid_on_read() {
1602        // A shredded object { id: { typed_value: FixedSizeBinary(16) } } whose inner UUID leaf
1603        // carries no extension type; canonicalization must reach it recursively.
1604        let leaf = FixedSizeBinaryArray::try_from_iter(std::iter::repeat_n([0u8; 16], 1)).unwrap();
1605        let inner = StructArrayBuilder::new()
1606            .with_field("typed_value", Arc::new(leaf), true)
1607            .build();
1608        let object = StructArrayBuilder::new()
1609            .with_field("id", Arc::new(inner), false)
1610            .build();
1611        let input = make_variant_struct_with_typed_value(Arc::new(object));
1612
1613        // typed_value (struct) -> id (struct) -> typed_value (the FixedSizeBinary(16) UUID leaf).
1614        let variant_array = VariantArray::try_new(&input).unwrap();
1615        let object = variant_array.typed_value_column().unwrap().as_struct();
1616        let id = object.column_by_name("id").unwrap().as_struct();
1617        let uuid_leaf = id.field_by_name("typed_value").unwrap();
1618        assert!(uuid_leaf.has_valid_extension_type::<UuidExtension>());
1619    }
1620
1621    #[test]
1622    fn all_null_value_column_is_valid_and_unshredded() {
1623        // An all-null `value` column is accepted (only a *missing* column is
1624        // rejected) and the array is unshredded (no typed_value column)
1625        let metadata = BinaryViewArray::from(vec![b"test" as &[u8]; 3]);
1626        let value = new_null_array(&DataType::BinaryView, 3);
1627
1628        let fields = Fields::from(vec![
1629            Field::new("metadata", DataType::BinaryView, false),
1630            Field::new("value", DataType::BinaryView, true),
1631        ]);
1632        let struct_array = StructArray::new(fields, vec![Arc::new(metadata), value], None);
1633
1634        let variant_array = VariantArray::try_new(&struct_array).unwrap();
1635        assert!(variant_array.typed_value_column().is_none());
1636
1637        // The rows are valid but have neither value nor typed_value; the spec
1638        // requires readers to return Variant::Null in this case
1639        for i in 0..variant_array.len() {
1640            assert!(variant_array.is_valid(i));
1641            assert_eq!(variant_array.value(i), Variant::Null);
1642        }
1643    }
1644
1645    #[test]
1646    fn canonicalize_and_verify_list_like_data_types() {
1647        // `parquet/tests/variant_integration.rs` validates Parquet shredded-variant fixtures that
1648        // use Parquet LIST encoding, but those fixtures do not cover Arrow-specific list container
1649        // variants (`LargeList`, `ListView`, `LargeListView`) accepted by `VariantArray::try_new`.
1650        let make_item_binary = || Arc::new(Field::new("item", DataType::Binary, true));
1651        let make_large_binary = || Arc::new(Field::new("item", DataType::LargeBinary, true));
1652        let make_item_binary_view = || Arc::new(Field::new("item", DataType::BinaryView, true));
1653
1654        let cases = vec![
1655            // Binary item
1656            DataType::LargeList(make_item_binary()),
1657            DataType::ListView(make_item_binary()),
1658            DataType::LargeListView(make_item_binary()),
1659            // Large binary item
1660            DataType::LargeList(make_large_binary()),
1661            DataType::ListView(make_large_binary()),
1662            DataType::LargeListView(make_large_binary()),
1663            // Binary view item
1664            DataType::LargeList(make_item_binary_view()),
1665            DataType::ListView(make_item_binary_view()),
1666            DataType::LargeListView(make_item_binary_view()),
1667        ];
1668
1669        for input in cases {
1670            assert_eq!(
1671                canonicalize_and_verify_data_type(&input).unwrap().as_ref(),
1672                &input
1673            );
1674        }
1675    }
1676
1677    #[test]
1678    fn variant_array_try_new_supports_list_like_typed_value() {
1679        let item_field = Arc::new(Field::new("item", DataType::Int64, true));
1680        let values: ArrayRef = Arc::new(Int64Array::from(vec![Some(1), None, Some(3)]));
1681
1682        let typed_values = vec![
1683            Arc::new(ListArray::new(
1684                item_field.clone(),
1685                OffsetBuffer::new(ScalarBuffer::from(vec![0, 2, 3])),
1686                values.clone(),
1687                None,
1688            )) as ArrayRef,
1689            Arc::new(LargeListArray::new(
1690                item_field.clone(),
1691                OffsetBuffer::new(ScalarBuffer::from(vec![0_i64, 2, 3])),
1692                values.clone(),
1693                None,
1694            )) as ArrayRef,
1695            Arc::new(ListViewArray::new(
1696                item_field.clone(),
1697                ScalarBuffer::from(vec![0, 2]),
1698                ScalarBuffer::from(vec![2, 1]),
1699                values.clone(),
1700                None,
1701            )) as ArrayRef,
1702            Arc::new(LargeListViewArray::new(
1703                item_field,
1704                ScalarBuffer::from(vec![0_i64, 2]),
1705                ScalarBuffer::from(vec![2_i64, 1]),
1706                values,
1707                None,
1708            )) as ArrayRef,
1709        ];
1710
1711        for typed_value in typed_values {
1712            let input = make_variant_struct_with_typed_value(typed_value.clone());
1713            let variant_array = VariantArray::try_new(&input).unwrap();
1714            assert_eq!(
1715                variant_array.typed_value_column().unwrap().data_type(),
1716                typed_value.data_type(),
1717            );
1718        }
1719    }
1720
1721    #[test]
1722    fn test_try_value_out_of_bounds() {
1723        let mut b = VariantArrayBuilder::new(2);
1724        b.append_variant(Variant::from(1_i8));
1725        b.append_variant(Variant::Null);
1726        let v = b.build();
1727
1728        assert_eq!(v.try_value(0).unwrap(), Variant::Int8(1));
1729        assert_eq!(v.try_value(1).unwrap(), Variant::Null);
1730
1731        let err = v.try_value(2).unwrap_err();
1732        assert_eq!(
1733            err.to_string(),
1734            "Invalid argument error: Index 2 out of bounds for VariantArray of length 2"
1735        );
1736    }
1737
1738    #[test]
1739    fn test_variant_array_iterable() {
1740        let mut b = VariantArrayBuilder::new(6);
1741
1742        b.append_null();
1743        b.append_variant(Variant::from(1_i8));
1744        b.append_variant(Variant::Null);
1745        b.append_variant(Variant::from(2_i32));
1746        b.append_variant(Variant::from(3_i64));
1747        b.append_null();
1748
1749        let v = b.build();
1750
1751        let variants = v.iter().collect::<Vec<_>>();
1752
1753        assert_eq!(
1754            variants,
1755            vec![
1756                None,
1757                Some(Variant::Int8(1)),
1758                Some(Variant::Null),
1759                Some(Variant::Int32(2)),
1760                Some(Variant::Int64(3)),
1761                None,
1762            ]
1763        );
1764    }
1765
1766    #[test]
1767    fn test_variant_array_iter_double_ended() {
1768        let mut b = VariantArrayBuilder::new(5);
1769
1770        b.append_variant(Variant::from(0_i32));
1771        b.append_null();
1772        b.append_variant(Variant::from(2_i32));
1773        b.append_null();
1774        b.append_variant(Variant::from(4_i32));
1775
1776        let array = b.build();
1777        let mut iter = array.iter();
1778
1779        assert_eq!(iter.next(), Some(Some(Variant::from(0_i32))));
1780        assert_eq!(iter.next(), Some(None));
1781
1782        assert_eq!(iter.next_back(), Some(Some(Variant::from(4_i32))));
1783        assert_eq!(iter.next_back(), Some(None));
1784        assert_eq!(iter.next_back(), Some(Some(Variant::from(2_i32))));
1785
1786        assert_eq!(iter.next_back(), None);
1787        assert_eq!(iter.next(), None);
1788    }
1789
1790    #[test]
1791    fn test_variant_array_iter_reverse() {
1792        let mut b = VariantArrayBuilder::new(5);
1793
1794        b.append_variant(Variant::from("a"));
1795        b.append_null();
1796        b.append_variant(Variant::from("aaa"));
1797        b.append_null();
1798        b.append_variant(Variant::from("aaaaa"));
1799
1800        let array = b.build();
1801
1802        let result: Vec<_> = array.iter().rev().collect();
1803        assert_eq!(
1804            result,
1805            vec![
1806                Some(Variant::from("aaaaa")),
1807                None,
1808                Some(Variant::from("aaa")),
1809                None,
1810                Some(Variant::from("a")),
1811            ]
1812        );
1813    }
1814
1815    #[test]
1816    fn test_variant_array_iter_empty() {
1817        let v = VariantArrayBuilder::new(0).build();
1818        let mut i = v.iter();
1819        assert!(i.next().is_none());
1820        assert!(i.next_back().is_none());
1821    }
1822
1823    #[test]
1824    fn test_from_variant_opts_into_variant_array() {
1825        let v = vec![None, Some(Variant::Null), Some(Variant::BooleanFalse), None];
1826
1827        let variant_array = VariantArray::from_iter(v);
1828
1829        assert_eq!(variant_array.len(), 4);
1830
1831        assert!(variant_array.is_null(0));
1832
1833        assert!(!variant_array.is_null(1));
1834        assert_eq!(variant_array.value(1), Variant::Null);
1835
1836        assert!(!variant_array.is_null(2));
1837        assert_eq!(variant_array.value(2), Variant::BooleanFalse);
1838
1839        assert!(variant_array.is_null(3));
1840    }
1841
1842    #[test]
1843    fn test_from_variants_into_variant_array() {
1844        let v = vec![
1845            Variant::Null,
1846            Variant::BooleanFalse,
1847            Variant::ShortString(ShortString::try_new("norm").unwrap()),
1848        ];
1849
1850        let variant_array = VariantArray::from_iter(v);
1851
1852        assert_eq!(variant_array.len(), 3);
1853
1854        assert!(!variant_array.is_null(0));
1855        assert_eq!(variant_array.value(0), Variant::Null);
1856
1857        assert!(!variant_array.is_null(1));
1858        assert_eq!(variant_array.value(1), Variant::BooleanFalse);
1859
1860        assert!(!variant_array.is_null(2));
1861        assert_eq!(
1862            variant_array.value(2),
1863            Variant::ShortString(ShortString::try_new("norm").unwrap())
1864        );
1865    }
1866
1867    #[test]
1868    fn test_variant_equality() {
1869        let v_iter = [None, Some(Variant::BooleanFalse), Some(Variant::Null), None];
1870        let v = VariantArray::from_iter(v_iter.clone());
1871
1872        {
1873            let v_copy = v.clone();
1874            assert_eq!(v, v_copy);
1875        }
1876
1877        {
1878            let v_iter_reversed = v_iter.iter().cloned().rev();
1879            let v_reversed = VariantArray::from_iter(v_iter_reversed);
1880
1881            assert_ne!(v, v_reversed);
1882        }
1883
1884        {
1885            let v_sliced = v.slice(0, 1);
1886            assert_ne!(v, v_sliced);
1887        }
1888    }
1889
1890    #[test]
1891    fn binary_typed_value_roundtrips() {
1892        // Verify that a shredded variant with Binary typed_value can be read back
1893        let typed_value: ArrayRef = Arc::new(BinaryArray::from(vec![b"hello" as &[u8]]));
1894        let struct_array = make_variant_struct_with_typed_value(typed_value);
1895
1896        let variant_array = VariantArray::try_new(&struct_array).unwrap();
1897        assert_eq!(variant_array.value(0), Variant::from(b"hello" as &[u8]));
1898    }
1899
1900    #[test]
1901    fn large_binary_typed_value_roundtrips() {
1902        // Verify that a shredded variant with LargeBinary typed_value can be read back
1903        let typed_value: ArrayRef = Arc::new(LargeBinaryArray::from(vec![b"world" as &[u8]]));
1904        let struct_array = make_variant_struct_with_typed_value(typed_value);
1905
1906        let variant_array = VariantArray::try_new(&struct_array).unwrap();
1907        assert_eq!(variant_array.value(0), Variant::from(b"world" as &[u8]));
1908    }
1909
1910    macro_rules! invalid_variant_array_test {
1911        ($fn_name: ident, $invalid_typed_value: expr, $error_msg: literal) => {
1912            #[test]
1913            fn $fn_name() {
1914                let invalid_typed_value = $invalid_typed_value;
1915
1916                let struct_array =
1917                    make_variant_struct_with_typed_value(Arc::new(invalid_typed_value));
1918
1919                let array: VariantArray = VariantArray::try_new(&struct_array)
1920                    .expect("should create variant array")
1921                    .into();
1922
1923                let result = array.try_value(0);
1924                assert!(result.is_err());
1925                let error = result.unwrap_err();
1926                assert!(matches!(error, ArrowError::CastError(_)));
1927
1928                let expected: &str = $error_msg;
1929                assert!(
1930                    error.to_string().contains($error_msg),
1931                    "error `{}` did not contain `{}`",
1932                    error,
1933                    expected
1934                )
1935            }
1936        };
1937    }
1938
1939    invalid_variant_array_test!(
1940        test_variant_array_invalid_time,
1941        Time64MicrosecondArray::from(vec![Some(86401000000)]),
1942        "Cast error: Cast failed at index 0 (array type: Time64(µs)): Invalid microsecond from midnight: 86401000000"
1943    );
1944
1945    invalid_variant_array_test!(
1946        test_variant_array_invalid_decimal32,
1947        Decimal32Array::from(vec![Some(1234567890)]),
1948        "Cast error: Cast failed at index 0 (array type: Decimal32(9, 2)): Invalid argument error: 1234567890 is wider than max precision 9"
1949    );
1950
1951    invalid_variant_array_test!(
1952        test_variant_array_invalid_decimal64,
1953        Decimal64Array::from(vec![Some(1234567890123456789)]),
1954        "Cast error: Cast failed at index 0 (array type: Decimal64(18, 6)): Invalid argument error: 1234567890123456789 is wider than max precision 18"
1955    );
1956
1957    invalid_variant_array_test!(
1958        test_variant_array_invalid_decimal128,
1959        Decimal128Array::from(vec![Some(
1960            i128::from_str("123456789012345678901234567890123456789").unwrap()
1961        ),]),
1962        "Cast error: Cast failed at index 0 (array type: Decimal128(38, 10)): Invalid argument error: 123456789012345678901234567890123456789 is wider than max precision 38"
1963    );
1964    #[test]
1965    fn try_value_errors_on_unimplemented_typed_value_type() {
1966        use crate::{json_to_variant, shred_variant};
1967        use arrow::array::StringArray;
1968
1969        let json: ArrayRef = Arc::new(StringArray::from(vec![r#"{"qty": 3}"#]));
1970        let variant = json_to_variant(&json).unwrap();
1971        let shred_type = DataType::Struct(vec![Field::new("qty", DataType::Int64, true)].into());
1972        let shredded = shred_variant(&variant, &shred_type).unwrap();
1973        // Object-shredded typed_value is not yet implemented: reading it must
1974        // error, never silently return Variant::Null
1975        // TODO: https://github.com/apache/arrow-rs/issues/10620
1976        let err = shredded.try_value(0).unwrap_err();
1977        assert!(
1978            err.to_string().starts_with(
1979                "Not yet implemented: VariantArray::try_value cannot materialize typed_value"
1980            ),
1981            "unexpected error: {err}"
1982        );
1983    }
1984}