arrow_array/array/
mod.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//! The concrete array definitions
19
20mod binary_array;
21
22use crate::types::*;
23use arrow_buffer::{ArrowNativeType, NullBuffer, OffsetBuffer, ScalarBuffer};
24use arrow_data::ArrayData;
25use arrow_schema::{DataType, IntervalUnit, TimeUnit};
26use std::any::Any;
27use std::sync::Arc;
28
29pub use binary_array::*;
30
31mod boolean_array;
32pub use boolean_array::*;
33
34mod byte_array;
35pub use byte_array::*;
36
37mod dictionary_array;
38pub use dictionary_array::*;
39
40mod fixed_size_binary_array;
41pub use fixed_size_binary_array::*;
42
43mod fixed_size_list_array;
44pub use fixed_size_list_array::*;
45
46mod list_array;
47pub use list_array::*;
48
49mod map_array;
50pub use map_array::*;
51
52mod null_array;
53pub use null_array::*;
54
55mod primitive_array;
56pub use primitive_array::*;
57
58mod string_array;
59pub use string_array::*;
60
61mod struct_array;
62pub use struct_array::*;
63
64mod union_array;
65pub use union_array::*;
66
67mod run_array;
68
69pub use run_array::*;
70
71mod byte_view_array;
72
73pub use byte_view_array::*;
74
75mod list_view_array;
76
77pub use list_view_array::*;
78
79use crate::iterator::ArrayIter;
80
81mod private {
82    /// Private marker trait to ensure [`super::Array`] can not be implemented outside this crate
83    pub trait Sealed {}
84
85    impl<T: Sealed> Sealed for &T {}
86}
87
88/// An array in the [arrow columnar format](https://arrow.apache.org/docs/format/Columnar.html)
89///
90/// This trait is sealed as it is not intended for custom array types, rather only
91/// those defined in this crate.
92pub trait Array: std::fmt::Debug + Send + Sync + private::Sealed {
93    /// Returns the array as [`Any`] so that it can be
94    /// downcasted to a specific implementation.
95    ///
96    /// # Example:
97    ///
98    /// ```
99    /// # use std::sync::Arc;
100    /// # use arrow_array::{Int32Array, RecordBatch};
101    /// # use arrow_schema::{Schema, Field, DataType, ArrowError};
102    ///
103    /// let id = Int32Array::from(vec![1, 2, 3, 4, 5]);
104    /// let batch = RecordBatch::try_new(
105    ///     Arc::new(Schema::new(vec![Field::new("id", DataType::Int32, false)])),
106    ///     vec![Arc::new(id)]
107    /// ).unwrap();
108    ///
109    /// let int32array = batch
110    ///     .column(0)
111    ///     .as_any()
112    ///     .downcast_ref::<Int32Array>()
113    ///     .expect("Failed to downcast");
114    /// ```
115    fn as_any(&self) -> &dyn Any;
116
117    /// Returns the underlying data of this array
118    fn to_data(&self) -> ArrayData;
119
120    /// Returns the underlying data of this array
121    ///
122    /// Unlike [`Array::to_data`] this consumes self, allowing it avoid unnecessary clones
123    fn into_data(self) -> ArrayData;
124
125    /// Returns a reference to the [`DataType`] of this array.
126    ///
127    /// # Example:
128    ///
129    /// ```
130    /// use arrow_schema::DataType;
131    /// use arrow_array::{Array, Int32Array};
132    ///
133    /// let array = Int32Array::from(vec![1, 2, 3, 4, 5]);
134    ///
135    /// assert_eq!(*array.data_type(), DataType::Int32);
136    /// ```
137    fn data_type(&self) -> &DataType;
138
139    /// Returns a zero-copy slice of this array with the indicated offset and length.
140    ///
141    /// # Example:
142    ///
143    /// ```
144    /// use arrow_array::{Array, Int32Array};
145    ///
146    /// let array = Int32Array::from(vec![1, 2, 3, 4, 5]);
147    /// // Make slice over the values [2, 3, 4]
148    /// let array_slice = array.slice(1, 3);
149    ///
150    /// assert_eq!(&array_slice, &Int32Array::from(vec![2, 3, 4]));
151    /// ```
152    fn slice(&self, offset: usize, length: usize) -> ArrayRef;
153
154    /// Returns the length (i.e., number of elements) of this array.
155    ///
156    /// # Example:
157    ///
158    /// ```
159    /// use arrow_array::{Array, Int32Array};
160    ///
161    /// let array = Int32Array::from(vec![1, 2, 3, 4, 5]);
162    ///
163    /// assert_eq!(array.len(), 5);
164    /// ```
165    fn len(&self) -> usize;
166
167    /// Returns whether this array is empty.
168    ///
169    /// # Example:
170    ///
171    /// ```
172    /// use arrow_array::{Array, Int32Array};
173    ///
174    /// let array = Int32Array::from(vec![1, 2, 3, 4, 5]);
175    ///
176    /// assert_eq!(array.is_empty(), false);
177    /// ```
178    fn is_empty(&self) -> bool;
179
180    /// Shrinks the capacity of any exclusively owned buffer as much as possible
181    ///
182    /// Shared or externally allocated buffers will be ignored, and
183    /// any buffer offsets will be preserved.
184    fn shrink_to_fit(&mut self) {}
185
186    /// Returns the offset into the underlying data used by this array(-slice).
187    /// Note that the underlying data can be shared by many arrays.
188    /// This defaults to `0`.
189    ///
190    /// # Example:
191    ///
192    /// ```
193    /// use arrow_array::{Array, BooleanArray};
194    ///
195    /// let array = BooleanArray::from(vec![false, false, true, true]);
196    /// let array_slice = array.slice(1, 3);
197    ///
198    /// assert_eq!(array.offset(), 0);
199    /// assert_eq!(array_slice.offset(), 1);
200    /// ```
201    fn offset(&self) -> usize;
202
203    /// Returns the null buffer of this array if any.
204    ///
205    /// The null buffer contains the "physical" nulls of an array, that is how
206    /// the nulls are represented in the underlying arrow format.
207    ///
208    /// The physical representation is efficient, but is sometimes non intuitive
209    /// for certain array types such as those with nullable child arrays like
210    /// [`DictionaryArray::values`], [`RunArray::values`] or [`UnionArray`], or without a
211    /// null buffer, such as [`NullArray`].
212    ///
213    /// To determine if each element of such an array is "logically" null,
214    /// use the slower [`Array::logical_nulls`] to obtain a computed mask.
215    fn nulls(&self) -> Option<&NullBuffer>;
216
217    /// Returns a potentially computed [`NullBuffer`] that represents the logical
218    /// null values of this array, if any.
219    ///
220    /// Logical nulls represent the values that are null in the array,
221    /// regardless of the underlying physical arrow representation.
222    ///
223    /// For most array types, this is equivalent to the "physical" nulls
224    /// returned by [`Array::nulls`]. It is different for the following cases, because which
225    /// elements are null is not encoded in a single null buffer:
226    ///
227    /// * [`DictionaryArray`] where [`DictionaryArray::values`] contains nulls
228    /// * [`RunArray`] where [`RunArray::values`] contains nulls
229    /// * [`NullArray`] where all indices are nulls
230    /// * [`UnionArray`] where the selected values contains nulls
231    ///
232    /// In these cases a logical [`NullBuffer`] will be computed, encoding the
233    /// logical nullability of these arrays, beyond what is encoded in
234    /// [`Array::nulls`]
235    fn logical_nulls(&self) -> Option<NullBuffer> {
236        self.nulls().cloned()
237    }
238
239    /// Returns whether the element at `index` is null according to [`Array::nulls`]
240    ///
241    /// Note: For performance reasons, this method returns nullability solely as determined by the
242    /// null buffer. This difference can lead to surprising results, for example, [`NullArray::is_null`] always
243    /// returns `false` as the array lacks a null buffer. Similarly [`DictionaryArray`], [`RunArray`] and [`UnionArray`] may
244    /// encode nullability in their children. See [`Self::logical_nulls`] for more information.
245    ///
246    /// # Example:
247    ///
248    /// ```
249    /// use arrow_array::{Array, Int32Array, NullArray};
250    ///
251    /// let array = Int32Array::from(vec![Some(1), None]);
252    /// assert_eq!(array.is_null(0), false);
253    /// assert_eq!(array.is_null(1), true);
254    ///
255    /// // NullArrays do not have a null buffer, and therefore always
256    /// // return false for is_null.
257    /// let array = NullArray::new(1);
258    /// assert_eq!(array.is_null(0), false);
259    /// ```
260    fn is_null(&self, index: usize) -> bool {
261        self.nulls().map(|n| n.is_null(index)).unwrap_or_default()
262    }
263
264    /// Returns whether the element at `index` is *not* null, the
265    /// opposite of [`Self::is_null`].
266    ///
267    /// # Example:
268    ///
269    /// ```
270    /// use arrow_array::{Array, Int32Array};
271    ///
272    /// let array = Int32Array::from(vec![Some(1), None]);
273    ///
274    /// assert_eq!(array.is_valid(0), true);
275    /// assert_eq!(array.is_valid(1), false);
276    /// ```
277    fn is_valid(&self, index: usize) -> bool {
278        !self.is_null(index)
279    }
280
281    /// Returns the total number of physical null values in this array.
282    ///
283    /// Note: this method returns the physical null count, i.e. that encoded in [`Array::nulls`],
284    /// see [`Array::logical_nulls`] for logical nullability
285    ///
286    /// # Example:
287    ///
288    /// ```
289    /// use arrow_array::{Array, Int32Array};
290    ///
291    /// // Construct an array with values [1, NULL, NULL]
292    /// let array = Int32Array::from(vec![Some(1), None, None]);
293    ///
294    /// assert_eq!(array.null_count(), 2);
295    /// ```
296    fn null_count(&self) -> usize {
297        self.nulls().map(|n| n.null_count()).unwrap_or_default()
298    }
299
300    /// Returns the total number of logical null values in this array.
301    ///
302    /// Note: this method returns the logical null count, i.e. that encoded in
303    /// [`Array::logical_nulls`]. In general this is equivalent to [`Array::null_count`] but may differ in the
304    /// presence of logical nullability, see [`Array::nulls`] and [`Array::logical_nulls`].
305    ///
306    /// # Example:
307    ///
308    /// ```
309    /// use arrow_array::{Array, Int32Array};
310    ///
311    /// // Construct an array with values [1, NULL, NULL]
312    /// let array = Int32Array::from(vec![Some(1), None, None]);
313    ///
314    /// assert_eq!(array.logical_null_count(), 2);
315    /// ```
316    fn logical_null_count(&self) -> usize {
317        self.logical_nulls()
318            .map(|n| n.null_count())
319            .unwrap_or_default()
320    }
321
322    /// Returns `false` if the array is guaranteed to not contain any logical nulls
323    ///
324    /// This is generally equivalent to `Array::logical_null_count() != 0` unless determining
325    /// the logical nulls is expensive, in which case this method can return true even for an
326    /// array without nulls.
327    ///
328    /// This is also generally equivalent to `Array::null_count() != 0` but may differ in the
329    /// presence of logical nullability, see [`Array::logical_null_count`] and [`Array::null_count`].
330    ///
331    /// Implementations will return `true` unless they can cheaply prove no logical nulls
332    /// are present. For example a [`DictionaryArray`] with nullable values will still return true,
333    /// even if the nulls present in [`DictionaryArray::values`] are not referenced by any key,
334    /// and therefore would not appear in [`Array::logical_nulls`].
335    fn is_nullable(&self) -> bool {
336        self.logical_null_count() != 0
337    }
338
339    /// Returns the total number of bytes of memory pointed to by this array.
340    /// The buffers store bytes in the Arrow memory format, and include the data as well as the validity map.
341    /// Note that this does not always correspond to the exact memory usage of an array,
342    /// since multiple arrays can share the same buffers or slices thereof.
343    fn get_buffer_memory_size(&self) -> usize;
344
345    /// Returns the total number of bytes of memory occupied physically by this array.
346    /// This value will always be greater than returned by `get_buffer_memory_size()` and
347    /// includes the overhead of the data structures that contain the pointers to the various buffers.
348    fn get_array_memory_size(&self) -> usize;
349}
350
351/// A reference-counted reference to a generic `Array`
352pub type ArrayRef = Arc<dyn Array>;
353
354impl private::Sealed for ArrayRef {}
355
356/// Ergonomics: Allow use of an ArrayRef as an `&dyn Array`
357impl Array for ArrayRef {
358    fn as_any(&self) -> &dyn Any {
359        self.as_ref().as_any()
360    }
361
362    fn to_data(&self) -> ArrayData {
363        self.as_ref().to_data()
364    }
365
366    fn into_data(self) -> ArrayData {
367        self.to_data()
368    }
369
370    fn data_type(&self) -> &DataType {
371        self.as_ref().data_type()
372    }
373
374    fn slice(&self, offset: usize, length: usize) -> ArrayRef {
375        self.as_ref().slice(offset, length)
376    }
377
378    fn len(&self) -> usize {
379        self.as_ref().len()
380    }
381
382    fn is_empty(&self) -> bool {
383        self.as_ref().is_empty()
384    }
385
386    /// For shared buffers, this is a no-op.
387    fn shrink_to_fit(&mut self) {
388        if let Some(slf) = Arc::get_mut(self) {
389            slf.shrink_to_fit();
390        } else {
391            // We ignore shared buffers.
392        }
393    }
394
395    fn offset(&self) -> usize {
396        self.as_ref().offset()
397    }
398
399    fn nulls(&self) -> Option<&NullBuffer> {
400        self.as_ref().nulls()
401    }
402
403    fn logical_nulls(&self) -> Option<NullBuffer> {
404        self.as_ref().logical_nulls()
405    }
406
407    fn is_null(&self, index: usize) -> bool {
408        self.as_ref().is_null(index)
409    }
410
411    fn is_valid(&self, index: usize) -> bool {
412        self.as_ref().is_valid(index)
413    }
414
415    fn null_count(&self) -> usize {
416        self.as_ref().null_count()
417    }
418
419    fn logical_null_count(&self) -> usize {
420        self.as_ref().logical_null_count()
421    }
422
423    fn is_nullable(&self) -> bool {
424        self.as_ref().is_nullable()
425    }
426
427    fn get_buffer_memory_size(&self) -> usize {
428        self.as_ref().get_buffer_memory_size()
429    }
430
431    fn get_array_memory_size(&self) -> usize {
432        self.as_ref().get_array_memory_size()
433    }
434}
435
436impl<T: Array> Array for &T {
437    fn as_any(&self) -> &dyn Any {
438        T::as_any(self)
439    }
440
441    fn to_data(&self) -> ArrayData {
442        T::to_data(self)
443    }
444
445    fn into_data(self) -> ArrayData {
446        self.to_data()
447    }
448
449    fn data_type(&self) -> &DataType {
450        T::data_type(self)
451    }
452
453    fn slice(&self, offset: usize, length: usize) -> ArrayRef {
454        T::slice(self, offset, length)
455    }
456
457    fn len(&self) -> usize {
458        T::len(self)
459    }
460
461    fn is_empty(&self) -> bool {
462        T::is_empty(self)
463    }
464
465    fn offset(&self) -> usize {
466        T::offset(self)
467    }
468
469    fn nulls(&self) -> Option<&NullBuffer> {
470        T::nulls(self)
471    }
472
473    fn logical_nulls(&self) -> Option<NullBuffer> {
474        T::logical_nulls(self)
475    }
476
477    fn is_null(&self, index: usize) -> bool {
478        T::is_null(self, index)
479    }
480
481    fn is_valid(&self, index: usize) -> bool {
482        T::is_valid(self, index)
483    }
484
485    fn null_count(&self) -> usize {
486        T::null_count(self)
487    }
488
489    fn logical_null_count(&self) -> usize {
490        T::logical_null_count(self)
491    }
492
493    fn is_nullable(&self) -> bool {
494        T::is_nullable(self)
495    }
496
497    fn get_buffer_memory_size(&self) -> usize {
498        T::get_buffer_memory_size(self)
499    }
500
501    fn get_array_memory_size(&self) -> usize {
502        T::get_array_memory_size(self)
503    }
504}
505
506/// A generic trait for accessing the values of an [`Array`]
507///
508/// This trait helps write specialized implementations of algorithms for
509/// different array types. Specialized implementations allow the compiler
510/// to optimize the code for the specific array type, which can lead to
511/// significant performance improvements.
512///
513/// # Example
514/// For example, to write three different implementations of a string length function
515/// for [`StringArray`], [`LargeStringArray`], and [`StringViewArray`], you can write
516///
517/// ```
518/// # use std::sync::Arc;
519/// # use arrow_array::{ArrayAccessor, ArrayRef, ArrowPrimitiveType, OffsetSizeTrait, PrimitiveArray};
520/// # use arrow_buffer::ArrowNativeType;
521/// # use arrow_array::cast::AsArray;
522/// # use arrow_array::iterator::ArrayIter;
523/// # use arrow_array::types::{Int32Type, Int64Type};
524/// # use arrow_schema::{ArrowError, DataType};
525/// /// This function takes a dynamically typed `ArrayRef` and calls
526/// /// calls one of three specialized implementations
527/// fn character_length(arg: ArrayRef) -> Result<ArrayRef, ArrowError> {
528///     match arg.data_type() {
529///         DataType::Utf8 => {
530///             // downcast the ArrayRef to a StringArray and call the specialized implementation
531///             let string_array = arg.as_string::<i32>();
532///             character_length_general::<Int32Type, _>(string_array)
533///         }
534///         DataType::LargeUtf8 => {
535///             character_length_general::<Int64Type, _>(arg.as_string::<i64>())
536///         }
537///         DataType::Utf8View => {
538///             character_length_general::<Int32Type, _>(arg.as_string_view())
539///         }
540///         _ => Err(ArrowError::InvalidArgumentError("Unsupported data type".to_string())),
541///     }
542/// }
543///
544/// /// A generic implementation of the character_length function
545/// /// This function uses the `ArrayAccessor` trait to access the values of the array
546/// /// so the compiler can generated specialized implementations for different array types
547/// ///
548/// /// Returns a new array with the length of each string in the input array
549/// /// * Int32Array for Utf8 and Utf8View arrays (lengths are 32-bit integers)
550/// /// * Int64Array for LargeUtf8 arrays (lengths are 64-bit integers)
551/// ///
552/// /// This is generic on the type of the primitive array (different string arrays have
553/// /// different lengths) and the type of the array accessor (different string arrays
554/// /// have different ways to access the values)
555/// fn character_length_general<'a, T: ArrowPrimitiveType, V: ArrayAccessor<Item = &'a str>>(
556///     array: V,
557/// ) -> Result<ArrayRef, ArrowError>
558/// where
559///     T::Native: OffsetSizeTrait,
560/// {
561///     let iter = ArrayIter::new(array);
562///     // Create a Int32Array / Int64Array with the length of each string
563///     let result = iter
564///         .map(|string| {
565///             string.map(|string: &str| {
566///                 T::Native::from_usize(string.chars().count())
567///                     .expect("should not fail as string.chars will always return integer")
568///             })
569///         })
570///         .collect::<PrimitiveArray<T>>();
571///
572///     /// Return the result as a new ArrayRef (dynamically typed)
573///     Ok(Arc::new(result) as ArrayRef)
574/// }
575/// ```
576///
577/// # Validity
578///
579/// An [`ArrayAccessor`] must always return a well-defined value for an index
580/// that is within the bounds `0..Array::len`, including for null indexes where
581/// [`Array::is_null`] is true.
582///
583/// The value at null indexes is unspecified, and implementations must not rely
584/// on a specific value such as [`Default::default`] being returned, however, it
585/// must not be undefined
586pub trait ArrayAccessor: Array {
587    /// The Arrow type of the element being accessed.
588    type Item: Send + Sync;
589
590    /// Returns the element at index `i`
591    /// # Panics
592    /// Panics if the value is outside the bounds of the array
593    fn value(&self, index: usize) -> Self::Item;
594
595    /// Returns the element at index `i`
596    /// # Safety
597    /// Caller is responsible for ensuring that the index is within the bounds of the array
598    unsafe fn value_unchecked(&self, index: usize) -> Self::Item;
599}
600
601/// A trait for Arrow String Arrays, currently three types are supported:
602/// - `StringArray`
603/// - `LargeStringArray`
604/// - `StringViewArray`
605///
606/// This trait helps to abstract over the different types of string arrays
607/// so that we don't need to duplicate the implementation for each type.
608pub trait StringArrayType<'a>: ArrayAccessor<Item = &'a str> + Sized {
609    /// Returns true if all data within this string array is ASCII
610    fn is_ascii(&self) -> bool;
611
612    /// Constructs a new iterator
613    fn iter(&self) -> ArrayIter<Self>;
614}
615
616impl<'a, O: OffsetSizeTrait> StringArrayType<'a> for &'a GenericStringArray<O> {
617    fn is_ascii(&self) -> bool {
618        GenericStringArray::<O>::is_ascii(self)
619    }
620
621    fn iter(&self) -> ArrayIter<Self> {
622        GenericStringArray::<O>::iter(self)
623    }
624}
625impl<'a> StringArrayType<'a> for &'a StringViewArray {
626    fn is_ascii(&self) -> bool {
627        StringViewArray::is_ascii(self)
628    }
629
630    fn iter(&self) -> ArrayIter<Self> {
631        StringViewArray::iter(self)
632    }
633}
634
635/// A trait for Arrow Binary Arrays, currently four types are supported:
636/// - `BinaryArray`
637/// - `LargeBinaryArray`
638/// - `BinaryViewArray`
639/// - `FixedSizeBinaryArray`
640///
641/// This trait helps to abstract over the different types of binary arrays
642/// so that we don't need to duplicate the implementation for each type.
643pub trait BinaryArrayType<'a>: ArrayAccessor<Item = &'a [u8]> + Sized {
644    /// Constructs a new iterator
645    fn iter(&self) -> ArrayIter<Self>;
646}
647
648impl<'a, O: OffsetSizeTrait> BinaryArrayType<'a> for &'a GenericBinaryArray<O> {
649    fn iter(&self) -> ArrayIter<Self> {
650        GenericBinaryArray::<O>::iter(self)
651    }
652}
653impl<'a> BinaryArrayType<'a> for &'a BinaryViewArray {
654    fn iter(&self) -> ArrayIter<Self> {
655        BinaryViewArray::iter(self)
656    }
657}
658impl<'a> BinaryArrayType<'a> for &'a FixedSizeBinaryArray {
659    fn iter(&self) -> ArrayIter<Self> {
660        FixedSizeBinaryArray::iter(self)
661    }
662}
663
664impl PartialEq for dyn Array + '_ {
665    fn eq(&self, other: &Self) -> bool {
666        self.to_data().eq(&other.to_data())
667    }
668}
669
670impl<T: Array> PartialEq<T> for dyn Array + '_ {
671    fn eq(&self, other: &T) -> bool {
672        self.to_data().eq(&other.to_data())
673    }
674}
675
676impl PartialEq for NullArray {
677    fn eq(&self, other: &NullArray) -> bool {
678        self.to_data().eq(&other.to_data())
679    }
680}
681
682impl<T: ArrowPrimitiveType> PartialEq for PrimitiveArray<T> {
683    fn eq(&self, other: &PrimitiveArray<T>) -> bool {
684        self.to_data().eq(&other.to_data())
685    }
686}
687
688impl<K: ArrowDictionaryKeyType> PartialEq for DictionaryArray<K> {
689    fn eq(&self, other: &Self) -> bool {
690        self.to_data().eq(&other.to_data())
691    }
692}
693
694impl PartialEq for BooleanArray {
695    fn eq(&self, other: &BooleanArray) -> bool {
696        self.to_data().eq(&other.to_data())
697    }
698}
699
700impl<OffsetSize: OffsetSizeTrait> PartialEq for GenericStringArray<OffsetSize> {
701    fn eq(&self, other: &Self) -> bool {
702        self.to_data().eq(&other.to_data())
703    }
704}
705
706impl<OffsetSize: OffsetSizeTrait> PartialEq for GenericBinaryArray<OffsetSize> {
707    fn eq(&self, other: &Self) -> bool {
708        self.to_data().eq(&other.to_data())
709    }
710}
711
712impl PartialEq for FixedSizeBinaryArray {
713    fn eq(&self, other: &Self) -> bool {
714        self.to_data().eq(&other.to_data())
715    }
716}
717
718impl<OffsetSize: OffsetSizeTrait> PartialEq for GenericListArray<OffsetSize> {
719    fn eq(&self, other: &Self) -> bool {
720        self.to_data().eq(&other.to_data())
721    }
722}
723
724impl<OffsetSize: OffsetSizeTrait> PartialEq for GenericListViewArray<OffsetSize> {
725    fn eq(&self, other: &Self) -> bool {
726        self.to_data().eq(&other.to_data())
727    }
728}
729
730impl PartialEq for MapArray {
731    fn eq(&self, other: &Self) -> bool {
732        self.to_data().eq(&other.to_data())
733    }
734}
735
736impl PartialEq for FixedSizeListArray {
737    fn eq(&self, other: &Self) -> bool {
738        self.to_data().eq(&other.to_data())
739    }
740}
741
742impl PartialEq for StructArray {
743    fn eq(&self, other: &Self) -> bool {
744        self.to_data().eq(&other.to_data())
745    }
746}
747
748impl<T: ByteViewType + ?Sized> PartialEq for GenericByteViewArray<T> {
749    fn eq(&self, other: &Self) -> bool {
750        self.to_data().eq(&other.to_data())
751    }
752}
753
754impl<R: RunEndIndexType> PartialEq for RunArray<R> {
755    fn eq(&self, other: &Self) -> bool {
756        self.to_data().eq(&other.to_data())
757    }
758}
759
760/// Constructs an array using the input `data`.
761/// Returns a reference-counted `Array` instance.
762pub fn make_array(data: ArrayData) -> ArrayRef {
763    match data.data_type() {
764        DataType::Boolean => Arc::new(BooleanArray::from(data)) as ArrayRef,
765        DataType::Int8 => Arc::new(Int8Array::from(data)) as ArrayRef,
766        DataType::Int16 => Arc::new(Int16Array::from(data)) as ArrayRef,
767        DataType::Int32 => Arc::new(Int32Array::from(data)) as ArrayRef,
768        DataType::Int64 => Arc::new(Int64Array::from(data)) as ArrayRef,
769        DataType::UInt8 => Arc::new(UInt8Array::from(data)) as ArrayRef,
770        DataType::UInt16 => Arc::new(UInt16Array::from(data)) as ArrayRef,
771        DataType::UInt32 => Arc::new(UInt32Array::from(data)) as ArrayRef,
772        DataType::UInt64 => Arc::new(UInt64Array::from(data)) as ArrayRef,
773        DataType::Float16 => Arc::new(Float16Array::from(data)) as ArrayRef,
774        DataType::Float32 => Arc::new(Float32Array::from(data)) as ArrayRef,
775        DataType::Float64 => Arc::new(Float64Array::from(data)) as ArrayRef,
776        DataType::Date32 => Arc::new(Date32Array::from(data)) as ArrayRef,
777        DataType::Date64 => Arc::new(Date64Array::from(data)) as ArrayRef,
778        DataType::Time32(TimeUnit::Second) => Arc::new(Time32SecondArray::from(data)) as ArrayRef,
779        DataType::Time32(TimeUnit::Millisecond) => {
780            Arc::new(Time32MillisecondArray::from(data)) as ArrayRef
781        }
782        DataType::Time64(TimeUnit::Microsecond) => {
783            Arc::new(Time64MicrosecondArray::from(data)) as ArrayRef
784        }
785        DataType::Time64(TimeUnit::Nanosecond) => {
786            Arc::new(Time64NanosecondArray::from(data)) as ArrayRef
787        }
788        DataType::Timestamp(TimeUnit::Second, _) => {
789            Arc::new(TimestampSecondArray::from(data)) as ArrayRef
790        }
791        DataType::Timestamp(TimeUnit::Millisecond, _) => {
792            Arc::new(TimestampMillisecondArray::from(data)) as ArrayRef
793        }
794        DataType::Timestamp(TimeUnit::Microsecond, _) => {
795            Arc::new(TimestampMicrosecondArray::from(data)) as ArrayRef
796        }
797        DataType::Timestamp(TimeUnit::Nanosecond, _) => {
798            Arc::new(TimestampNanosecondArray::from(data)) as ArrayRef
799        }
800        DataType::Interval(IntervalUnit::YearMonth) => {
801            Arc::new(IntervalYearMonthArray::from(data)) as ArrayRef
802        }
803        DataType::Interval(IntervalUnit::DayTime) => {
804            Arc::new(IntervalDayTimeArray::from(data)) as ArrayRef
805        }
806        DataType::Interval(IntervalUnit::MonthDayNano) => {
807            Arc::new(IntervalMonthDayNanoArray::from(data)) as ArrayRef
808        }
809        DataType::Duration(TimeUnit::Second) => {
810            Arc::new(DurationSecondArray::from(data)) as ArrayRef
811        }
812        DataType::Duration(TimeUnit::Millisecond) => {
813            Arc::new(DurationMillisecondArray::from(data)) as ArrayRef
814        }
815        DataType::Duration(TimeUnit::Microsecond) => {
816            Arc::new(DurationMicrosecondArray::from(data)) as ArrayRef
817        }
818        DataType::Duration(TimeUnit::Nanosecond) => {
819            Arc::new(DurationNanosecondArray::from(data)) as ArrayRef
820        }
821        DataType::Binary => Arc::new(BinaryArray::from(data)) as ArrayRef,
822        DataType::LargeBinary => Arc::new(LargeBinaryArray::from(data)) as ArrayRef,
823        DataType::FixedSizeBinary(_) => Arc::new(FixedSizeBinaryArray::from(data)) as ArrayRef,
824        DataType::BinaryView => Arc::new(BinaryViewArray::from(data)) as ArrayRef,
825        DataType::Utf8 => Arc::new(StringArray::from(data)) as ArrayRef,
826        DataType::LargeUtf8 => Arc::new(LargeStringArray::from(data)) as ArrayRef,
827        DataType::Utf8View => Arc::new(StringViewArray::from(data)) as ArrayRef,
828        DataType::List(_) => Arc::new(ListArray::from(data)) as ArrayRef,
829        DataType::LargeList(_) => Arc::new(LargeListArray::from(data)) as ArrayRef,
830        DataType::ListView(_) => Arc::new(ListViewArray::from(data)) as ArrayRef,
831        DataType::LargeListView(_) => Arc::new(LargeListViewArray::from(data)) as ArrayRef,
832        DataType::Struct(_) => Arc::new(StructArray::from(data)) as ArrayRef,
833        DataType::Map(_, _) => Arc::new(MapArray::from(data)) as ArrayRef,
834        DataType::Union(_, _) => Arc::new(UnionArray::from(data)) as ArrayRef,
835        DataType::FixedSizeList(_, _) => Arc::new(FixedSizeListArray::from(data)) as ArrayRef,
836        DataType::Dictionary(key_type, _) => match key_type.as_ref() {
837            DataType::Int8 => Arc::new(DictionaryArray::<Int8Type>::from(data)) as ArrayRef,
838            DataType::Int16 => Arc::new(DictionaryArray::<Int16Type>::from(data)) as ArrayRef,
839            DataType::Int32 => Arc::new(DictionaryArray::<Int32Type>::from(data)) as ArrayRef,
840            DataType::Int64 => Arc::new(DictionaryArray::<Int64Type>::from(data)) as ArrayRef,
841            DataType::UInt8 => Arc::new(DictionaryArray::<UInt8Type>::from(data)) as ArrayRef,
842            DataType::UInt16 => Arc::new(DictionaryArray::<UInt16Type>::from(data)) as ArrayRef,
843            DataType::UInt32 => Arc::new(DictionaryArray::<UInt32Type>::from(data)) as ArrayRef,
844            DataType::UInt64 => Arc::new(DictionaryArray::<UInt64Type>::from(data)) as ArrayRef,
845            dt => unimplemented!("Unexpected dictionary key type {dt}"),
846        },
847        DataType::RunEndEncoded(run_ends_type, _) => match run_ends_type.data_type() {
848            DataType::Int16 => Arc::new(RunArray::<Int16Type>::from(data)) as ArrayRef,
849            DataType::Int32 => Arc::new(RunArray::<Int32Type>::from(data)) as ArrayRef,
850            DataType::Int64 => Arc::new(RunArray::<Int64Type>::from(data)) as ArrayRef,
851            dt => unimplemented!("Unexpected data type for run_ends array {dt}"),
852        },
853        DataType::Null => Arc::new(NullArray::from(data)) as ArrayRef,
854        DataType::Decimal32(_, _) => Arc::new(Decimal32Array::from(data)) as ArrayRef,
855        DataType::Decimal64(_, _) => Arc::new(Decimal64Array::from(data)) as ArrayRef,
856        DataType::Decimal128(_, _) => Arc::new(Decimal128Array::from(data)) as ArrayRef,
857        DataType::Decimal256(_, _) => Arc::new(Decimal256Array::from(data)) as ArrayRef,
858        dt => unimplemented!("Unexpected data type {dt}"),
859    }
860}
861
862/// Creates a new empty array
863///
864/// ```
865/// use std::sync::Arc;
866/// use arrow_schema::DataType;
867/// use arrow_array::{ArrayRef, Int32Array, new_empty_array};
868///
869/// let empty_array = new_empty_array(&DataType::Int32);
870/// let array: ArrayRef = Arc::new(Int32Array::from(vec![] as Vec<i32>));
871///
872/// assert_eq!(&array, &empty_array);
873/// ```
874pub fn new_empty_array(data_type: &DataType) -> ArrayRef {
875    let data = ArrayData::new_empty(data_type);
876    make_array(data)
877}
878
879/// Creates a new array of `data_type` of length `length` filled
880/// entirely of `NULL` values
881///
882/// ```
883/// use std::sync::Arc;
884/// use arrow_schema::DataType;
885/// use arrow_array::{ArrayRef, Int32Array, new_null_array};
886///
887/// let null_array = new_null_array(&DataType::Int32, 3);
888/// let array: ArrayRef = Arc::new(Int32Array::from(vec![None, None, None]));
889///
890/// assert_eq!(&array, &null_array);
891/// ```
892pub fn new_null_array(data_type: &DataType, length: usize) -> ArrayRef {
893    make_array(ArrayData::new_null(data_type, length))
894}
895
896/// Helper function that gets offset from an [`ArrayData`]
897///
898/// # Safety
899///
900/// - ArrayData must contain a valid [`OffsetBuffer`] as its first buffer
901unsafe fn get_offsets<O: ArrowNativeType>(data: &ArrayData) -> OffsetBuffer<O> {
902    match data.is_empty() && data.buffers()[0].is_empty() {
903        true => OffsetBuffer::new_empty(),
904        false => {
905            let buffer =
906                ScalarBuffer::new(data.buffers()[0].clone(), data.offset(), data.len() + 1);
907            // Safety:
908            // ArrayData is valid
909            unsafe { OffsetBuffer::new_unchecked(buffer) }
910        }
911    }
912}
913
914/// Helper function for printing potentially long arrays.
915fn print_long_array<A, F>(array: &A, f: &mut std::fmt::Formatter, print_item: F) -> std::fmt::Result
916where
917    A: Array,
918    F: Fn(&A, usize, &mut std::fmt::Formatter) -> std::fmt::Result,
919{
920    let head = std::cmp::min(10, array.len());
921
922    for i in 0..head {
923        if array.is_null(i) {
924            writeln!(f, "  null,")?;
925        } else {
926            write!(f, "  ")?;
927            print_item(array, i, f)?;
928            writeln!(f, ",")?;
929        }
930    }
931    if array.len() > 10 {
932        if array.len() > 20 {
933            writeln!(f, "  ...{} elements...,", array.len() - 20)?;
934        }
935
936        let tail = std::cmp::max(head, array.len() - 10);
937
938        for i in tail..array.len() {
939            if array.is_null(i) {
940                writeln!(f, "  null,")?;
941            } else {
942                write!(f, "  ")?;
943                print_item(array, i, f)?;
944                writeln!(f, ",")?;
945            }
946        }
947    }
948    Ok(())
949}
950
951#[cfg(test)]
952mod tests {
953    use super::*;
954    use crate::cast::{as_union_array, downcast_array};
955    use crate::downcast_run_array;
956    use arrow_buffer::MutableBuffer;
957    use arrow_schema::{Field, Fields, UnionFields, UnionMode};
958
959    #[test]
960    fn test_empty_primitive() {
961        let array = new_empty_array(&DataType::Int32);
962        let a = array.as_any().downcast_ref::<Int32Array>().unwrap();
963        assert_eq!(a.len(), 0);
964        let expected: &[i32] = &[];
965        assert_eq!(a.values(), expected);
966    }
967
968    #[test]
969    fn test_empty_variable_sized() {
970        let array = new_empty_array(&DataType::Utf8);
971        let a = array.as_any().downcast_ref::<StringArray>().unwrap();
972        assert_eq!(a.len(), 0);
973        assert_eq!(a.value_offsets()[0], 0i32);
974    }
975
976    #[test]
977    fn test_empty_list_primitive() {
978        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, false)));
979        let array = new_empty_array(&data_type);
980        let a = array.as_any().downcast_ref::<ListArray>().unwrap();
981        assert_eq!(a.len(), 0);
982        assert_eq!(a.value_offsets()[0], 0i32);
983    }
984
985    #[test]
986    fn test_null_boolean() {
987        let array = new_null_array(&DataType::Boolean, 9);
988        let a = array.as_any().downcast_ref::<BooleanArray>().unwrap();
989        assert_eq!(a.len(), 9);
990        for i in 0..9 {
991            assert!(a.is_null(i));
992        }
993    }
994
995    #[test]
996    fn test_null_primitive() {
997        let array = new_null_array(&DataType::Int32, 9);
998        let a = array.as_any().downcast_ref::<Int32Array>().unwrap();
999        assert_eq!(a.len(), 9);
1000        for i in 0..9 {
1001            assert!(a.is_null(i));
1002        }
1003    }
1004
1005    #[test]
1006    fn test_null_struct() {
1007        // It is possible to create a null struct containing a non-nullable child
1008        // see https://github.com/apache/arrow-rs/pull/3244 for details
1009        let struct_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
1010        let array = new_null_array(&struct_type, 9);
1011
1012        let a = array.as_any().downcast_ref::<StructArray>().unwrap();
1013        assert_eq!(a.len(), 9);
1014        assert_eq!(a.column(0).len(), 9);
1015        for i in 0..9 {
1016            assert!(a.is_null(i));
1017        }
1018
1019        // Make sure we can slice the resulting array.
1020        a.slice(0, 5);
1021    }
1022
1023    #[test]
1024    fn test_null_variable_sized() {
1025        let array = new_null_array(&DataType::Utf8, 9);
1026        let a = array.as_any().downcast_ref::<StringArray>().unwrap();
1027        assert_eq!(a.len(), 9);
1028        assert_eq!(a.value_offsets()[9], 0i32);
1029        for i in 0..9 {
1030            assert!(a.is_null(i));
1031        }
1032    }
1033
1034    #[test]
1035    fn test_null_list_primitive() {
1036        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
1037        let array = new_null_array(&data_type, 9);
1038        let a = array.as_any().downcast_ref::<ListArray>().unwrap();
1039        assert_eq!(a.len(), 9);
1040        assert_eq!(a.value_offsets()[9], 0i32);
1041        for i in 0..9 {
1042            assert!(a.is_null(i));
1043        }
1044    }
1045
1046    #[test]
1047    fn test_null_map() {
1048        let data_type = DataType::Map(
1049            Arc::new(Field::new(
1050                "entry",
1051                DataType::Struct(Fields::from(vec![
1052                    Field::new("key", DataType::Utf8, false),
1053                    Field::new("value", DataType::Int32, true),
1054                ])),
1055                false,
1056            )),
1057            false,
1058        );
1059        let array = new_null_array(&data_type, 9);
1060        let a = array.as_any().downcast_ref::<MapArray>().unwrap();
1061        assert_eq!(a.len(), 9);
1062        assert_eq!(a.value_offsets()[9], 0i32);
1063        for i in 0..9 {
1064            assert!(a.is_null(i));
1065        }
1066    }
1067
1068    #[test]
1069    fn test_null_dictionary() {
1070        let values =
1071            vec![None, None, None, None, None, None, None, None, None] as Vec<Option<&str>>;
1072
1073        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
1074        let array = Arc::new(array) as ArrayRef;
1075
1076        let null_array = new_null_array(array.data_type(), 9);
1077        assert_eq!(&array, &null_array);
1078        assert_eq!(
1079            array.to_data().buffers()[0].len(),
1080            null_array.to_data().buffers()[0].len()
1081        );
1082    }
1083
1084    #[test]
1085    fn test_null_union() {
1086        for mode in [UnionMode::Sparse, UnionMode::Dense] {
1087            let data_type = DataType::Union(
1088                UnionFields::try_new(
1089                    vec![2, 1],
1090                    vec![
1091                        Field::new("foo", DataType::Int32, true),
1092                        Field::new("bar", DataType::Int64, true),
1093                    ],
1094                )
1095                .unwrap(),
1096                mode,
1097            );
1098            let array = new_null_array(&data_type, 4);
1099
1100            let array = as_union_array(array.as_ref());
1101            assert_eq!(array.len(), 4);
1102            assert_eq!(array.null_count(), 0);
1103            assert_eq!(array.logical_null_count(), 4);
1104
1105            for i in 0..4 {
1106                let a = array.value(i);
1107                assert_eq!(a.len(), 1);
1108                assert_eq!(a.null_count(), 1);
1109                assert_eq!(a.logical_null_count(), 1);
1110                assert!(a.is_null(0))
1111            }
1112
1113            array.to_data().validate_full().unwrap();
1114        }
1115    }
1116
1117    #[test]
1118    #[allow(unused_parens)]
1119    fn test_null_runs() {
1120        for r in [DataType::Int16, DataType::Int32, DataType::Int64] {
1121            let data_type = DataType::RunEndEncoded(
1122                Arc::new(Field::new("run_ends", r, false)),
1123                Arc::new(Field::new("values", DataType::Utf8, true)),
1124            );
1125
1126            let array = new_null_array(&data_type, 4);
1127            let array = array.as_ref();
1128
1129            downcast_run_array! {
1130                array => {
1131                    assert_eq!(array.len(), 4);
1132                    assert_eq!(array.null_count(), 0);
1133                    assert_eq!(array.logical_null_count(), 4);
1134                    assert_eq!(array.values().len(), 1);
1135                    assert_eq!(array.values().null_count(), 1);
1136                    assert_eq!(array.run_ends().len(), 4);
1137                    assert_eq!(array.run_ends().values(), &[4]);
1138
1139                    let idx = array.get_physical_indices(&[0, 1, 2, 3]).unwrap();
1140                    assert_eq!(idx, &[0,0,0,0]);
1141                }
1142                d => unreachable!("{d}")
1143            }
1144        }
1145    }
1146
1147    #[test]
1148    fn test_null_fixed_size_binary() {
1149        for size in [1, 2, 7] {
1150            let array = new_null_array(&DataType::FixedSizeBinary(size), 6);
1151            let array = array
1152                .as_ref()
1153                .as_any()
1154                .downcast_ref::<FixedSizeBinaryArray>()
1155                .unwrap();
1156
1157            assert_eq!(array.len(), 6);
1158            assert_eq!(array.null_count(), 6);
1159            assert_eq!(array.logical_null_count(), 6);
1160            array.iter().for_each(|x| assert!(x.is_none()));
1161        }
1162    }
1163
1164    #[test]
1165    fn test_memory_size_null() {
1166        let null_arr = NullArray::new(32);
1167
1168        assert_eq!(0, null_arr.get_buffer_memory_size());
1169        assert_eq!(
1170            std::mem::size_of::<usize>(),
1171            null_arr.get_array_memory_size()
1172        );
1173    }
1174
1175    #[test]
1176    fn test_memory_size_primitive() {
1177        let arr = PrimitiveArray::<Int64Type>::from_iter_values(0..128);
1178        let empty = PrimitiveArray::<Int64Type>::from(ArrayData::new_empty(arr.data_type()));
1179
1180        // subtract empty array to avoid magic numbers for the size of additional fields
1181        assert_eq!(
1182            arr.get_array_memory_size() - empty.get_array_memory_size(),
1183            128 * std::mem::size_of::<i64>()
1184        );
1185    }
1186
1187    #[test]
1188    fn test_memory_size_primitive_sliced() {
1189        let arr = PrimitiveArray::<Int64Type>::from_iter_values(0..128);
1190        let slice1 = arr.slice(0, 64);
1191        let slice2 = arr.slice(64, 64);
1192
1193        // both slices report the full buffer memory usage, even though the buffers are shared
1194        assert_eq!(slice1.get_array_memory_size(), arr.get_array_memory_size());
1195        assert_eq!(slice2.get_array_memory_size(), arr.get_array_memory_size());
1196    }
1197
1198    #[test]
1199    fn test_memory_size_primitive_nullable() {
1200        let arr: PrimitiveArray<Int64Type> = (0..128)
1201            .map(|i| if i % 20 == 0 { Some(i) } else { None })
1202            .collect();
1203        let empty_with_bitmap = PrimitiveArray::<Int64Type>::from(
1204            ArrayData::builder(arr.data_type().clone())
1205                .add_buffer(MutableBuffer::new(0).into())
1206                .null_bit_buffer(Some(MutableBuffer::new_null(0).into()))
1207                .build()
1208                .unwrap(),
1209        );
1210
1211        // expected size is the size of the PrimitiveArray struct,
1212        // which includes the optional validity buffer
1213        // plus one buffer on the heap
1214        assert_eq!(
1215            std::mem::size_of::<PrimitiveArray<Int64Type>>(),
1216            empty_with_bitmap.get_array_memory_size()
1217        );
1218
1219        // subtract empty array to avoid magic numbers for the size of additional fields
1220        // the size of the validity bitmap is rounded up to 64 bytes
1221        assert_eq!(
1222            arr.get_array_memory_size() - empty_with_bitmap.get_array_memory_size(),
1223            128 * std::mem::size_of::<i64>() + 64
1224        );
1225    }
1226
1227    #[test]
1228    fn test_memory_size_dictionary() {
1229        let values = PrimitiveArray::<Int64Type>::from_iter_values(0..16);
1230        let keys = PrimitiveArray::<Int16Type>::from_iter_values(
1231            (0..256).map(|i| (i % values.len()) as i16),
1232        );
1233
1234        let dict_data_type = DataType::Dictionary(
1235            Box::new(keys.data_type().clone()),
1236            Box::new(values.data_type().clone()),
1237        );
1238        let dict_data = keys
1239            .into_data()
1240            .into_builder()
1241            .data_type(dict_data_type)
1242            .child_data(vec![values.into_data()])
1243            .build()
1244            .unwrap();
1245
1246        let empty_data = ArrayData::new_empty(&DataType::Dictionary(
1247            Box::new(DataType::Int16),
1248            Box::new(DataType::Int64),
1249        ));
1250
1251        let arr = DictionaryArray::<Int16Type>::from(dict_data);
1252        let empty = DictionaryArray::<Int16Type>::from(empty_data);
1253
1254        let expected_keys_size = 256 * std::mem::size_of::<i16>();
1255        assert_eq!(
1256            arr.keys().get_array_memory_size() - empty.keys().get_array_memory_size(),
1257            expected_keys_size
1258        );
1259
1260        let expected_values_size = 16 * std::mem::size_of::<i64>();
1261        assert_eq!(
1262            arr.values().get_array_memory_size() - empty.values().get_array_memory_size(),
1263            expected_values_size
1264        );
1265
1266        let expected_size = expected_keys_size + expected_values_size;
1267        assert_eq!(
1268            arr.get_array_memory_size() - empty.get_array_memory_size(),
1269            expected_size
1270        );
1271    }
1272
1273    /// Test function that takes an &dyn Array
1274    fn compute_my_thing(arr: &dyn Array) -> bool {
1275        !arr.is_empty()
1276    }
1277
1278    #[test]
1279    fn test_array_ref_as_array() {
1280        let arr: Int32Array = vec![1, 2, 3].into_iter().map(Some).collect();
1281
1282        // works well!
1283        assert!(compute_my_thing(&arr));
1284
1285        // Should also work when wrapped as an ArrayRef
1286        let arr: ArrayRef = Arc::new(arr);
1287        assert!(compute_my_thing(&arr));
1288        assert!(compute_my_thing(arr.as_ref()));
1289    }
1290
1291    #[test]
1292    fn test_downcast_array() {
1293        let array: Int32Array = vec![1, 2, 3].into_iter().map(Some).collect();
1294
1295        let boxed: ArrayRef = Arc::new(array);
1296        let array: Int32Array = downcast_array(&boxed);
1297
1298        let expected: Int32Array = vec![1, 2, 3].into_iter().map(Some).collect();
1299        assert_eq!(array, expected);
1300    }
1301}