Skip to main content

arrow_array/array/
struct_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
18use crate::array::print_long_array;
19use crate::{Array, ArrayRef, RecordBatch, make_array, new_null_array};
20use arrow_buffer::{BooleanBuffer, Buffer, NullBuffer};
21use arrow_data::{ArrayData, ArrayDataBuilder};
22use arrow_schema::{ArrowError, DataType, Field, FieldRef, Fields};
23use std::sync::Arc;
24use std::{any::Any, ops::Index};
25
26/// An array of [structs](https://arrow.apache.org/docs/format/Columnar.html#struct-layout)
27///
28/// Each child (called *field*) is represented by a separate array.
29///
30/// # Comparison with [RecordBatch]
31///
32/// Both [`RecordBatch`] and [`StructArray`] represent a collection of columns / arrays with the
33/// same length.
34///
35/// However, there are a couple of key differences:
36///
37/// * [`StructArray`] can be nested within other [`Array`], including itself
38/// * [`RecordBatch`] can contain top-level metadata on its associated [`Schema`][arrow_schema::Schema]
39/// * [`StructArray`] can contain top-level nulls, i.e. `null`
40/// * [`RecordBatch`] can only represent nulls in its child columns, i.e. `{"field": null}`
41///
42/// [`StructArray`] is therefore a more general data container than [`RecordBatch`], and as such
43/// code that needs to handle both will typically share an implementation in terms of
44/// [`StructArray`] and convert to/from [`RecordBatch`] as necessary.
45///
46/// [`From`] implementations are provided to facilitate this conversion, however, converting
47/// from a [`StructArray`] containing top-level nulls to a [`RecordBatch`] will panic, as there
48/// is no way to preserve them.
49///
50/// # Example: Create an array from a vector of fields
51///
52/// ```
53/// use std::sync::Arc;
54/// use arrow_array::{Array, ArrayRef, BooleanArray, Int32Array, StructArray};
55/// use arrow_schema::{DataType, Field};
56///
57/// let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
58/// let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
59///
60/// let struct_array = StructArray::from(vec![
61///     (
62///         Arc::new(Field::new("b", DataType::Boolean, false)),
63///         boolean.clone() as ArrayRef,
64///     ),
65///     (
66///         Arc::new(Field::new("c", DataType::Int32, false)),
67///         int.clone() as ArrayRef,
68///     ),
69/// ]);
70/// assert_eq!(struct_array.column(0).as_ref(), boolean.as_ref());
71/// assert_eq!(struct_array.column(1).as_ref(), int.as_ref());
72/// assert_eq!(4, struct_array.len());
73/// assert_eq!(0, struct_array.null_count());
74/// assert_eq!(0, struct_array.offset());
75/// ```
76#[derive(Clone)]
77pub struct StructArray {
78    len: usize,
79    data_type: DataType,
80    nulls: Option<NullBuffer>,
81    fields: Vec<ArrayRef>,
82}
83
84impl StructArray {
85    /// Create a new [`StructArray`] from the provided parts, panicking on failure
86    ///
87    /// # Panics
88    ///
89    /// Panics if [`Self::try_new`] returns an error
90    pub fn new(fields: Fields, arrays: Vec<ArrayRef>, nulls: Option<NullBuffer>) -> Self {
91        Self::try_new(fields, arrays, nulls).unwrap()
92    }
93
94    /// Create a new [`StructArray`] from the provided parts, returning an error on failure
95    ///
96    /// The length will be inferred from the length of the child arrays.  Returns an error if
97    /// there are no child arrays.  Consider using [`Self::try_new_with_length`] if the length
98    /// is known to avoid this.
99    ///
100    /// # Errors
101    ///
102    /// Errors if
103    ///
104    /// * `fields.len() == 0`
105    /// * Any reason that [`Self::try_new_with_length`] would error
106    pub fn try_new(
107        fields: Fields,
108        arrays: Vec<ArrayRef>,
109        nulls: Option<NullBuffer>,
110    ) -> Result<Self, ArrowError> {
111        let len = arrays.first().map(|x| x.len()).ok_or_else(||ArrowError::InvalidArgumentError("use StructArray::try_new_with_length or StructArray::new_empty_fields to create a struct array with no fields so that the length can be set correctly".to_string()))?;
112
113        Self::try_new_with_length(fields, arrays, nulls, len)
114    }
115
116    /// Create a new [`StructArray`] from the provided parts, returning an error on failure
117    ///
118    /// # Errors
119    ///
120    /// Errors if
121    ///
122    /// * `fields.len() != arrays.len()`
123    /// * `fields[i].data_type() != arrays[i].data_type()`
124    /// * `arrays[i].len() != arrays[j].len()`
125    /// * `arrays[i].len() != nulls.len()`
126    /// * `!fields[i].is_nullable() && !nulls.contains(arrays[i].nulls())`
127    pub fn try_new_with_length(
128        fields: Fields,
129        arrays: Vec<ArrayRef>,
130        nulls: Option<NullBuffer>,
131        len: usize,
132    ) -> Result<Self, ArrowError> {
133        if fields.len() != arrays.len() {
134            return Err(ArrowError::InvalidArgumentError(format!(
135                "Incorrect number of arrays for StructArray fields, expected {} got {}",
136                fields.len(),
137                arrays.len()
138            )));
139        }
140
141        if let Some(n) = nulls.as_ref() {
142            if n.len() != len {
143                return Err(ArrowError::InvalidArgumentError(format!(
144                    "Incorrect number of nulls for StructArray, expected {len} got {}",
145                    n.len(),
146                )));
147            }
148        }
149
150        for (f, a) in fields.iter().zip(&arrays) {
151            if f.data_type() != a.data_type() {
152                return Err(ArrowError::InvalidArgumentError(format!(
153                    "Incorrect datatype for StructArray field {:?}, expected {} got {}",
154                    f.name(),
155                    f.data_type(),
156                    a.data_type()
157                )));
158            }
159
160            if a.len() != len {
161                return Err(ArrowError::InvalidArgumentError(format!(
162                    "Incorrect array length for StructArray field {:?}, expected {} got {}",
163                    f.name(),
164                    len,
165                    a.len()
166                )));
167            }
168
169            if !f.is_nullable() {
170                if let Some(a) = a.logical_nulls() {
171                    if !nulls.as_ref().map(|n| n.contains(&a)).unwrap_or_default()
172                        && a.null_count() > 0
173                    {
174                        return Err(ArrowError::InvalidArgumentError(format!(
175                            "Found unmasked nulls for non-nullable StructArray field {:?}",
176                            f.name()
177                        )));
178                    }
179                }
180            }
181        }
182
183        Ok(Self {
184            len,
185            data_type: DataType::Struct(fields),
186            nulls: nulls.filter(|n| n.null_count() > 0),
187            fields: arrays,
188        })
189    }
190
191    /// Create a new [`StructArray`] of length `len` where all values are null
192    pub fn new_null(fields: Fields, len: usize) -> Self {
193        let arrays = fields
194            .iter()
195            .map(|f| new_null_array(f.data_type(), len))
196            .collect();
197
198        Self {
199            len,
200            data_type: DataType::Struct(fields),
201            nulls: Some(NullBuffer::new_null(len)),
202            fields: arrays,
203        }
204    }
205
206    /// Create a new [`StructArray`] from the provided parts without validation
207    ///
208    /// The length will be inferred from the length of the child arrays.  Panics if there are no
209    /// child arrays.  Consider using [`Self::new_unchecked_with_length`] if the length is known
210    /// to avoid this.
211    ///
212    /// # Safety
213    ///
214    /// Safe if [`Self::new`] would not panic with the given arguments
215    pub unsafe fn new_unchecked(
216        fields: Fields,
217        arrays: Vec<ArrayRef>,
218        nulls: Option<NullBuffer>,
219    ) -> Self {
220        if cfg!(feature = "force_validate") {
221            return Self::new(fields, arrays, nulls);
222        }
223
224        let len = arrays.first().map(|x| x.len()).expect(
225            "cannot use StructArray::new_unchecked if there are no fields, length is unknown",
226        );
227        Self {
228            len,
229            data_type: DataType::Struct(fields),
230            nulls,
231            fields: arrays,
232        }
233    }
234
235    /// Create a new [`StructArray`] from the provided parts without validation
236    ///
237    /// # Safety
238    ///
239    /// Safe if [`Self::new`] would not panic with the given arguments
240    pub unsafe fn new_unchecked_with_length(
241        fields: Fields,
242        arrays: Vec<ArrayRef>,
243        nulls: Option<NullBuffer>,
244        len: usize,
245    ) -> Self {
246        if cfg!(feature = "force_validate") {
247            return Self::try_new_with_length(fields, arrays, nulls, len).unwrap();
248        }
249
250        Self {
251            len,
252            data_type: DataType::Struct(fields),
253            nulls,
254            fields: arrays,
255        }
256    }
257
258    /// Create a new [`StructArray`] containing no fields
259    ///
260    /// # Panics
261    ///
262    /// If `len != nulls.len()`
263    pub fn new_empty_fields(len: usize, nulls: Option<NullBuffer>) -> Self {
264        if let Some(n) = &nulls {
265            assert_eq!(len, n.len())
266        }
267        Self {
268            len,
269            data_type: DataType::Struct(Fields::empty()),
270            fields: vec![],
271            nulls,
272        }
273    }
274
275    /// Deconstruct this array into its constituent parts
276    pub fn into_parts(self) -> (Fields, Vec<ArrayRef>, Option<NullBuffer>) {
277        let f = match self.data_type {
278            DataType::Struct(f) => f,
279            _ => unreachable!(),
280        };
281        (f, self.fields, self.nulls)
282    }
283
284    /// Returns the field at `pos`.
285    pub fn column(&self, pos: usize) -> &ArrayRef {
286        &self.fields[pos]
287    }
288
289    /// Return the number of fields in this struct array
290    pub fn num_columns(&self) -> usize {
291        self.fields.len()
292    }
293
294    /// Returns the fields of the struct array
295    pub fn columns(&self) -> &[ArrayRef] {
296        &self.fields
297    }
298
299    /// Return field names in this struct array
300    pub fn column_names(&self) -> Vec<&str> {
301        match self.data_type() {
302            DataType::Struct(fields) => fields
303                .iter()
304                .map(|f| f.name().as_str())
305                .collect::<Vec<&str>>(),
306            _ => unreachable!("Struct array's data type is not struct!"),
307        }
308    }
309
310    /// Returns the [`Fields`] of this [`StructArray`]
311    pub fn fields(&self) -> &Fields {
312        match self.data_type() {
313            DataType::Struct(f) => f,
314            _ => unreachable!(),
315        }
316    }
317
318    /// Return child array whose field name equals to column_name
319    ///
320    /// Note: A schema can currently have duplicate field names, in which case
321    /// the first field will always be selected.
322    /// This issue will be addressed in [ARROW-11178](https://issues.apache.org/jira/browse/ARROW-11178)
323    pub fn column_by_name(&self, column_name: &str) -> Option<&ArrayRef> {
324        self.column_names()
325            .iter()
326            .position(|c| c == &column_name)
327            .map(|pos| self.column(pos))
328    }
329
330    /// Returns a zero-copy slice of this array with the indicated offset and length.
331    pub fn slice(&self, offset: usize, len: usize) -> Self {
332        assert!(
333            offset.saturating_add(len) <= self.len,
334            "the length + offset of the sliced StructArray cannot exceed the existing length"
335        );
336
337        let fields = self.fields.iter().map(|a| a.slice(offset, len)).collect();
338
339        Self {
340            len,
341            data_type: self.data_type.clone(),
342            nulls: self.nulls.as_ref().map(|n| n.slice(offset, len)),
343            fields,
344        }
345    }
346}
347
348impl From<ArrayData> for StructArray {
349    fn from(data: ArrayData) -> Self {
350        let parent_offset = data.offset();
351        let parent_len = data.len();
352
353        let fields = data
354            .child_data()
355            .iter()
356            .map(|cd| {
357                if parent_offset != 0 || parent_len != cd.len() {
358                    make_array(cd.slice(parent_offset, parent_len))
359                } else {
360                    make_array(cd.clone())
361                }
362            })
363            .collect();
364
365        Self {
366            len: data.len(),
367            data_type: data.data_type().clone(),
368            nulls: data.nulls().cloned(),
369            fields,
370        }
371    }
372}
373
374impl From<StructArray> for ArrayData {
375    fn from(array: StructArray) -> Self {
376        let builder = ArrayDataBuilder::new(array.data_type)
377            .len(array.len)
378            .nulls(array.nulls)
379            .child_data(array.fields.iter().map(|x| x.to_data()).collect());
380
381        unsafe { builder.build_unchecked() }
382    }
383}
384
385impl TryFrom<Vec<(&str, ArrayRef)>> for StructArray {
386    type Error = ArrowError;
387
388    /// builds a StructArray from a vector of names and arrays.
389    fn try_from(values: Vec<(&str, ArrayRef)>) -> Result<Self, ArrowError> {
390        let (fields, arrays): (Vec<_>, _) = values
391            .into_iter()
392            .map(|(name, array)| {
393                (
394                    Field::new(name, array.data_type().clone(), array.is_nullable()),
395                    array,
396                )
397            })
398            .unzip();
399
400        StructArray::try_new(fields.into(), arrays, None)
401    }
402}
403
404/// SAFETY: Correctly implements the contract of Arrow Arrays
405unsafe impl Array for StructArray {
406    fn as_any(&self) -> &dyn Any {
407        self
408    }
409
410    fn to_data(&self) -> ArrayData {
411        self.clone().into()
412    }
413
414    fn into_data(self) -> ArrayData {
415        self.into()
416    }
417
418    fn data_type(&self) -> &DataType {
419        &self.data_type
420    }
421
422    fn slice(&self, offset: usize, length: usize) -> ArrayRef {
423        Arc::new(self.slice(offset, length))
424    }
425
426    fn len(&self) -> usize {
427        self.len
428    }
429
430    fn is_empty(&self) -> bool {
431        self.len == 0
432    }
433
434    fn shrink_to_fit(&mut self) {
435        if let Some(nulls) = &mut self.nulls {
436            nulls.shrink_to_fit();
437        }
438        self.fields.iter_mut().for_each(|n| n.shrink_to_fit());
439    }
440
441    fn offset(&self) -> usize {
442        0
443    }
444
445    fn nulls(&self) -> Option<&NullBuffer> {
446        self.nulls.as_ref()
447    }
448
449    fn logical_null_count(&self) -> usize {
450        // More efficient that the default implementation
451        self.null_count()
452    }
453
454    fn get_buffer_memory_size(&self) -> usize {
455        let mut size = self.fields.iter().map(|a| a.get_buffer_memory_size()).sum();
456        if let Some(n) = self.nulls.as_ref() {
457            size += n.buffer().capacity();
458        }
459        size
460    }
461
462    fn get_array_memory_size(&self) -> usize {
463        let mut size = self.fields.iter().map(|a| a.get_array_memory_size()).sum();
464        size += std::mem::size_of::<Self>();
465        if let Some(n) = self.nulls.as_ref() {
466            size += n.buffer().capacity();
467        }
468        size
469    }
470}
471
472impl From<Vec<(FieldRef, ArrayRef)>> for StructArray {
473    fn from(v: Vec<(FieldRef, ArrayRef)>) -> Self {
474        let (fields, arrays): (Vec<_>, _) = v.into_iter().unzip();
475        StructArray::new(fields.into(), arrays, None)
476    }
477}
478
479impl std::fmt::Debug for StructArray {
480    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
481        writeln!(f, "StructArray")?;
482        writeln!(f, "-- validity:")?;
483        writeln!(f, "[")?;
484        print_long_array(self, f, |_array, _index, f| write!(f, "valid"))?;
485        writeln!(f, "]\n[")?;
486        for (child_index, name) in self.column_names().iter().enumerate() {
487            let column = self.column(child_index);
488            writeln!(
489                f,
490                "-- child {}: \"{}\" ({:?})",
491                child_index,
492                name,
493                column.data_type()
494            )?;
495            std::fmt::Debug::fmt(column, f)?;
496            writeln!(f)?;
497        }
498        write!(f, "]")
499    }
500}
501
502impl From<(Vec<(FieldRef, ArrayRef)>, Buffer)> for StructArray {
503    fn from(pair: (Vec<(FieldRef, ArrayRef)>, Buffer)) -> Self {
504        let len = pair.0.first().map(|x| x.1.len()).unwrap_or_default();
505        let (fields, arrays): (Vec<_>, Vec<_>) = pair.0.into_iter().unzip();
506        let nulls = NullBuffer::new(BooleanBuffer::new(pair.1, 0, len));
507        Self::new(fields.into(), arrays, Some(nulls))
508    }
509}
510
511impl From<RecordBatch> for StructArray {
512    fn from(value: RecordBatch) -> Self {
513        Self {
514            len: value.num_rows(),
515            data_type: DataType::Struct(value.schema().fields().clone()),
516            nulls: None,
517            fields: value.columns().to_vec(),
518        }
519    }
520}
521
522impl Index<&str> for StructArray {
523    type Output = ArrayRef;
524
525    /// Get a reference to a column's array by name.
526    ///
527    /// Note: A schema can currently have duplicate field names, in which case
528    /// the first field will always be selected.
529    /// This issue will be addressed in [ARROW-11178](https://issues.apache.org/jira/browse/ARROW-11178)
530    ///
531    /// # Panics
532    ///
533    /// Panics if the name is not in the schema.
534    fn index(&self, name: &str) -> &Self::Output {
535        self.column_by_name(name).unwrap()
536    }
537}
538
539#[cfg(test)]
540mod tests {
541    use super::*;
542
543    use crate::{BooleanArray, Float32Array, Float64Array, Int32Array, Int64Array, StringArray};
544    use arrow_buffer::ToByteSlice;
545
546    #[test]
547    fn test_struct_array_builder() {
548        let boolean_array = BooleanArray::from(vec![false, false, true, true]);
549        let int_array = Int64Array::from(vec![42, 28, 19, 31]);
550
551        let fields = vec![
552            Field::new("a", DataType::Boolean, false),
553            Field::new("b", DataType::Int64, false),
554        ];
555        let struct_array_data = ArrayData::builder(DataType::Struct(fields.into()))
556            .len(4)
557            .add_child_data(boolean_array.to_data())
558            .add_child_data(int_array.to_data())
559            .build()
560            .unwrap();
561        let struct_array = StructArray::from(struct_array_data);
562
563        assert_eq!(struct_array.column(0).as_ref(), &boolean_array);
564        assert_eq!(struct_array.column(1).as_ref(), &int_array);
565    }
566
567    #[test]
568    fn test_struct_array_from() {
569        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
570        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
571
572        let struct_array = StructArray::from(vec![
573            (
574                Arc::new(Field::new("b", DataType::Boolean, false)),
575                boolean.clone() as ArrayRef,
576            ),
577            (
578                Arc::new(Field::new("c", DataType::Int32, false)),
579                int.clone() as ArrayRef,
580            ),
581        ]);
582        assert_eq!(struct_array.column(0).as_ref(), boolean.as_ref());
583        assert_eq!(struct_array.column(1).as_ref(), int.as_ref());
584        assert_eq!(4, struct_array.len());
585        assert_eq!(0, struct_array.null_count());
586        assert_eq!(0, struct_array.offset());
587    }
588
589    #[test]
590    fn test_struct_array_from_data_with_offset_and_length() {
591        // Various ways to make the struct array:
592        //
593        // [{x: 2}, {x: 3}, None]
594        //
595        // from slicing larger buffers/arrays with offsets and lengths
596        let int_arr = Int32Array::from(vec![1, 2, 3, 4, 5]);
597        let int_field = Field::new("x", DataType::Int32, false);
598        let struct_nulls = NullBuffer::new(BooleanBuffer::from(vec![true, true, false]));
599        let int_data = int_arr.to_data();
600        // Case 1: Offset + length, nulls are not sliced
601        let case1 = ArrayData::builder(DataType::Struct(Fields::from(vec![int_field.clone()])))
602            .len(3)
603            .offset(1)
604            .nulls(Some(struct_nulls))
605            .add_child_data(int_data.clone())
606            .build()
607            .unwrap();
608
609        // Case 2: Offset + length, nulls are sliced
610        let struct_nulls =
611            NullBuffer::new(BooleanBuffer::from(vec![true, true, true, false, true]).slice(1, 3));
612        let case2 = ArrayData::builder(DataType::Struct(Fields::from(vec![int_field.clone()])))
613            .len(3)
614            .offset(1)
615            .nulls(Some(struct_nulls.clone()))
616            .add_child_data(int_data.clone())
617            .build()
618            .unwrap();
619
620        // Case 3: struct length is smaller than child length but no offset
621        let offset_int_data = int_data.slice(1, 4);
622        let case3 = ArrayData::builder(DataType::Struct(Fields::from(vec![int_field.clone()])))
623            .len(3)
624            .nulls(Some(struct_nulls))
625            .add_child_data(offset_int_data)
626            .build()
627            .unwrap();
628
629        let expected = StructArray::new(
630            Fields::from(vec![int_field.clone()]),
631            vec![Arc::new(int_arr)],
632            Some(NullBuffer::new(BooleanBuffer::from(vec![
633                true, true, true, false, true,
634            ]))),
635        )
636        .slice(1, 3);
637
638        for case in [case1, case2, case3] {
639            let struct_arr_from_data = StructArray::from(case);
640            assert_eq!(struct_arr_from_data, expected);
641            assert_eq!(struct_arr_from_data.column(0), expected.column(0));
642        }
643    }
644
645    #[test]
646    #[should_panic(expected = "assertion failed: (offset + length) <= self.len()")]
647    fn test_struct_array_from_data_with_offset_and_length_error() {
648        let int_arr = Int32Array::from(vec![1, 2, 3, 4, 5]);
649        let int_field = Field::new("x", DataType::Int32, false);
650        let struct_nulls = NullBuffer::new(BooleanBuffer::from(vec![true, true, false]));
651        let int_data = int_arr.to_data();
652        // If parent offset is 3 and len is 3 then child must have 6 items
653        let struct_data =
654            ArrayData::builder(DataType::Struct(Fields::from(vec![int_field.clone()])))
655                .len(3)
656                .offset(3)
657                .nulls(Some(struct_nulls))
658                .add_child_data(int_data)
659                .build()
660                .unwrap();
661        let _ = StructArray::from(struct_data);
662    }
663
664    /// validates that struct can be accessed using `column_name` as index i.e. `struct_array["column_name"]`.
665    #[test]
666    fn test_struct_array_index_access() {
667        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
668        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
669
670        let struct_array = StructArray::from(vec![
671            (
672                Arc::new(Field::new("b", DataType::Boolean, false)),
673                boolean.clone() as ArrayRef,
674            ),
675            (
676                Arc::new(Field::new("c", DataType::Int32, false)),
677                int.clone() as ArrayRef,
678            ),
679        ]);
680        assert_eq!(struct_array["b"].as_ref(), boolean.as_ref());
681        assert_eq!(struct_array["c"].as_ref(), int.as_ref());
682    }
683
684    /// validates that the in-memory representation follows [the spec](https://arrow.apache.org/docs/format/Columnar.html#struct-layout)
685    #[test]
686    fn test_struct_array_from_vec() {
687        let strings: ArrayRef = Arc::new(StringArray::from(vec![
688            Some("joe"),
689            None,
690            None,
691            Some("mark"),
692        ]));
693        let ints: ArrayRef = Arc::new(Int32Array::from(vec![Some(1), Some(2), None, Some(4)]));
694
695        let arr =
696            StructArray::try_from(vec![("f1", strings.clone()), ("f2", ints.clone())]).unwrap();
697
698        let struct_data = arr.into_data();
699        assert_eq!(4, struct_data.len());
700        assert_eq!(0, struct_data.null_count());
701
702        let expected_string_data = ArrayData::builder(DataType::Utf8)
703            .len(4)
704            .null_bit_buffer(Some(Buffer::from(&[9_u8])))
705            .add_buffer(Buffer::from([0, 3, 3, 3, 7].to_byte_slice()))
706            .add_buffer(Buffer::from(b"joemark"))
707            .build()
708            .unwrap();
709
710        let expected_int_data = ArrayData::builder(DataType::Int32)
711            .len(4)
712            .null_bit_buffer(Some(Buffer::from(&[11_u8])))
713            .add_buffer(Buffer::from([1, 2, 0, 4].to_byte_slice()))
714            .build()
715            .unwrap();
716
717        assert_eq!(expected_string_data, struct_data.child_data()[0]);
718        assert_eq!(expected_int_data, struct_data.child_data()[1]);
719    }
720
721    #[test]
722    fn test_struct_array_from_vec_error() {
723        let strings: ArrayRef = Arc::new(StringArray::from(vec![
724            Some("joe"),
725            None,
726            None,
727            // 3 elements, not 4
728        ]));
729        let ints: ArrayRef = Arc::new(Int32Array::from(vec![Some(1), Some(2), None, Some(4)]));
730
731        let err = StructArray::try_from(vec![("f1", strings.clone()), ("f2", ints.clone())])
732            .unwrap_err()
733            .to_string();
734
735        assert_eq!(
736            err,
737            "Invalid argument error: Incorrect array length for StructArray field \"f2\", expected 3 got 4"
738        )
739    }
740
741    #[test]
742    #[should_panic(
743        expected = "Incorrect datatype for StructArray field \\\"b\\\", expected Int16 got Boolean"
744    )]
745    fn test_struct_array_from_mismatched_types_single() {
746        drop(StructArray::from(vec![(
747            Arc::new(Field::new("b", DataType::Int16, false)),
748            Arc::new(BooleanArray::from(vec![false, false, true, true])) as Arc<dyn Array>,
749        )]));
750    }
751
752    #[test]
753    #[should_panic(
754        expected = "Incorrect datatype for StructArray field \\\"b\\\", expected Int16 got Boolean"
755    )]
756    fn test_struct_array_from_mismatched_types_multiple() {
757        drop(StructArray::from(vec![
758            (
759                Arc::new(Field::new("b", DataType::Int16, false)),
760                Arc::new(BooleanArray::from(vec![false, false, true, true])) as Arc<dyn Array>,
761            ),
762            (
763                Arc::new(Field::new("c", DataType::Utf8, false)),
764                Arc::new(Int32Array::from(vec![42, 28, 19, 31])),
765            ),
766        ]));
767    }
768
769    #[test]
770    fn test_struct_array_slice() {
771        let boolean_data = ArrayData::builder(DataType::Boolean)
772            .len(5)
773            .add_buffer(Buffer::from([0b00010000]))
774            .null_bit_buffer(Some(Buffer::from([0b00010001])))
775            .build()
776            .unwrap();
777        let int_data = ArrayData::builder(DataType::Int32)
778            .len(5)
779            .add_buffer(Buffer::from([0, 28, 42, 0, 0].to_byte_slice()))
780            .null_bit_buffer(Some(Buffer::from([0b00000110])))
781            .build()
782            .unwrap();
783
784        let field_types = vec![
785            Field::new("a", DataType::Boolean, true),
786            Field::new("b", DataType::Int32, true),
787        ];
788        let struct_array_data = ArrayData::builder(DataType::Struct(field_types.into()))
789            .len(5)
790            .add_child_data(boolean_data.clone())
791            .add_child_data(int_data.clone())
792            .null_bit_buffer(Some(Buffer::from([0b00010111])))
793            .build()
794            .unwrap();
795        let struct_array = StructArray::from(struct_array_data);
796
797        assert_eq!(5, struct_array.len());
798        assert_eq!(1, struct_array.null_count());
799        assert!(struct_array.is_valid(0));
800        assert!(struct_array.is_valid(1));
801        assert!(struct_array.is_valid(2));
802        assert!(struct_array.is_null(3));
803        assert!(struct_array.is_valid(4));
804        assert_eq!(boolean_data, struct_array.column(0).to_data());
805        assert_eq!(int_data, struct_array.column(1).to_data());
806
807        let c0 = struct_array.column(0);
808        let c0 = c0.as_any().downcast_ref::<BooleanArray>().unwrap();
809        assert_eq!(5, c0.len());
810        assert_eq!(3, c0.null_count());
811        assert!(c0.is_valid(0));
812        assert!(!c0.value(0));
813        assert!(c0.is_null(1));
814        assert!(c0.is_null(2));
815        assert!(c0.is_null(3));
816        assert!(c0.is_valid(4));
817        assert!(c0.value(4));
818
819        let c1 = struct_array.column(1);
820        let c1 = c1.as_any().downcast_ref::<Int32Array>().unwrap();
821        assert_eq!(5, c1.len());
822        assert_eq!(3, c1.null_count());
823        assert!(c1.is_null(0));
824        assert!(c1.is_valid(1));
825        assert_eq!(28, c1.value(1));
826        assert!(c1.is_valid(2));
827        assert_eq!(42, c1.value(2));
828        assert!(c1.is_null(3));
829        assert!(c1.is_null(4));
830
831        let sliced_array = struct_array.slice(2, 3);
832        let sliced_array = sliced_array.as_any().downcast_ref::<StructArray>().unwrap();
833        assert_eq!(3, sliced_array.len());
834        assert_eq!(1, sliced_array.null_count());
835        assert!(sliced_array.is_valid(0));
836        assert!(sliced_array.is_null(1));
837        assert!(sliced_array.is_valid(2));
838
839        let sliced_c0 = sliced_array.column(0);
840        let sliced_c0 = sliced_c0.as_any().downcast_ref::<BooleanArray>().unwrap();
841        assert_eq!(3, sliced_c0.len());
842        assert!(sliced_c0.is_null(0));
843        assert!(sliced_c0.is_null(1));
844        assert!(sliced_c0.is_valid(2));
845        assert!(sliced_c0.value(2));
846
847        let sliced_c1 = sliced_array.column(1);
848        let sliced_c1 = sliced_c1.as_any().downcast_ref::<Int32Array>().unwrap();
849        assert_eq!(3, sliced_c1.len());
850        assert!(sliced_c1.is_valid(0));
851        assert_eq!(42, sliced_c1.value(0));
852        assert!(sliced_c1.is_null(1));
853        assert!(sliced_c1.is_null(2));
854    }
855
856    #[test]
857    #[should_panic(
858        expected = "Incorrect array length for StructArray field \\\"c\\\", expected 1 got 2"
859    )]
860    fn test_invalid_struct_child_array_lengths() {
861        drop(StructArray::from(vec![
862            (
863                Arc::new(Field::new("b", DataType::Float32, false)),
864                Arc::new(Float32Array::from(vec![1.1])) as Arc<dyn Array>,
865            ),
866            (
867                Arc::new(Field::new("c", DataType::Float64, false)),
868                Arc::new(Float64Array::from(vec![2.2, 3.3])),
869            ),
870        ]));
871    }
872
873    #[test]
874    #[should_panic(expected = "use StructArray::try_new_with_length")]
875    fn test_struct_array_from_empty() {
876        // This can't work because we don't know how many rows the array should have.  Previously we inferred 0 but
877        // that often led to bugs.
878        let _ = StructArray::from(vec![]);
879    }
880
881    #[test]
882    fn test_empty_struct_array() {
883        assert!(StructArray::try_new(Fields::empty(), vec![], None).is_err());
884
885        let arr = StructArray::new_empty_fields(10, None);
886        assert_eq!(arr.len(), 10);
887        assert_eq!(arr.null_count(), 0);
888        assert_eq!(arr.num_columns(), 0);
889
890        let arr2 = StructArray::try_new_with_length(Fields::empty(), vec![], None, 10).unwrap();
891        assert_eq!(arr2.len(), 10);
892
893        let arr = StructArray::new_empty_fields(10, Some(NullBuffer::new_null(10)));
894        assert_eq!(arr.len(), 10);
895        assert_eq!(arr.null_count(), 10);
896        assert_eq!(arr.num_columns(), 0);
897
898        let arr2 = StructArray::try_new_with_length(
899            Fields::empty(),
900            vec![],
901            Some(NullBuffer::new_null(10)),
902            10,
903        )
904        .unwrap();
905        assert_eq!(arr2.len(), 10);
906    }
907
908    #[test]
909    #[should_panic(expected = "Found unmasked nulls for non-nullable StructArray field \\\"c\\\"")]
910    fn test_struct_array_from_mismatched_nullability() {
911        drop(StructArray::from(vec![(
912            Arc::new(Field::new("c", DataType::Int32, false)),
913            Arc::new(Int32Array::from(vec![Some(42), None, Some(19)])) as ArrayRef,
914        )]));
915    }
916
917    #[test]
918    fn test_struct_array_fmt_debug() {
919        let arr: StructArray = StructArray::new(
920            vec![Arc::new(Field::new("c", DataType::Int32, true))].into(),
921            vec![Arc::new(Int32Array::from((0..30).collect::<Vec<_>>())) as ArrayRef],
922            Some(NullBuffer::new(BooleanBuffer::from(
923                (0..30).map(|i| i % 2 == 0).collect::<Vec<_>>(),
924            ))),
925        );
926        assert_eq!(
927            format!("{arr:?}"),
928            "StructArray\n-- validity:\n[\n  valid,\n  null,\n  valid,\n  null,\n  valid,\n  null,\n  valid,\n  null,\n  valid,\n  null,\n  ...10 elements...,\n  valid,\n  null,\n  valid,\n  null,\n  valid,\n  null,\n  valid,\n  null,\n  valid,\n  null,\n]\n[\n-- child 0: \"c\" (Int32)\nPrimitiveArray<Int32>\n[\n  0,\n  1,\n  2,\n  3,\n  4,\n  5,\n  6,\n  7,\n  8,\n  9,\n  ...10 elements...,\n  20,\n  21,\n  22,\n  23,\n  24,\n  25,\n  26,\n  27,\n  28,\n  29,\n]\n]"
929        )
930    }
931
932    #[test]
933    fn test_struct_array_logical_nulls() {
934        // Field is non-nullable
935        let field = Field::new("a", DataType::Int32, false);
936        let values = vec![1, 2, 3];
937        // Create a NullBuffer with all bits set to valid (true)
938        let nulls = NullBuffer::from(vec![true, true, true]);
939        let array = Int32Array::new(values.into(), Some(nulls));
940        let child = Arc::new(array) as ArrayRef;
941        assert!(child.logical_nulls().is_some());
942        assert_eq!(child.logical_nulls().unwrap().null_count(), 0);
943
944        let fields = Fields::from(vec![field]);
945        let arrays = vec![child];
946        let nulls = None;
947
948        StructArray::try_new(fields, arrays, nulls).expect("should not error");
949    }
950}