arrow_array/array/
list_view_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 arrow_buffer::{Buffer, NullBuffer, ScalarBuffer};
19use arrow_data::{ArrayData, ArrayDataBuilder};
20use arrow_schema::{ArrowError, DataType, FieldRef};
21use std::any::Any;
22use std::ops::Add;
23use std::sync::Arc;
24
25use crate::array::{make_array, print_long_array};
26use crate::builder::{GenericListViewBuilder, PrimitiveBuilder};
27use crate::iterator::GenericListViewArrayIter;
28use crate::{
29    Array, ArrayAccessor, ArrayRef, ArrowPrimitiveType, FixedSizeListArray, GenericListArray,
30    OffsetSizeTrait, new_empty_array,
31};
32
33/// A [`GenericListViewArray`] of variable size lists, storing offsets as `i32`.
34pub type ListViewArray = GenericListViewArray<i32>;
35
36/// A [`GenericListViewArray`] of variable size lists, storing offsets as `i64`.
37pub type LargeListViewArray = GenericListViewArray<i64>;
38
39/// An array of [variable length lists], specifically in the [list-view layout].
40///
41/// Differs from [`GenericListArray`] (which represents the [list layout]) in that
42/// the sizes of the child arrays are explicitly encoded in a separate buffer, instead
43/// of being derived from the difference between subsequent offsets in the offset buffer.
44///
45/// This allows the offsets (and subsequently child data) to be out of order. It also
46/// allows take / filter operations to be implemented without copying the underlying data.
47///
48/// # Representation
49///
50/// Given the same example array from [`GenericListArray`], it would be represented
51/// as such via a list-view layout array:
52///
53/// ```text
54///                                         ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
55///                                                                         ┌ ─ ─ ─ ─ ─ ─ ┐    │
56///  ┌─────────────┐  ┌───────┐             │     ┌───┐   ┌───┐   ┌───┐       ┌───┐ ┌───┐
57///  │   [A,B,C]   │  │ (0,3) │                   │ 1 │   │ 0 │   │ 3 │     │ │ 1 │ │ A │ │ 0  │
58///  ├─────────────┤  ├───────┤             │     ├───┤   ├───┤   ├───┤       ├───┤ ├───┤
59///  │      []     │  │ (3,0) │                   │ 1 │   │ 3 │   │ 0 │     │ │ 1 │ │ B │ │ 1  │
60///  ├─────────────┤  ├───────┤             │     ├───┤   ├───┤   ├───┤       ├───┤ ├───┤
61///  │    NULL     │  │ (?,?) │                   │ 0 │   │ ? │   │ ? │     │ │ 1 │ │ C │ │ 2  │
62///  ├─────────────┤  ├───────┤             │     ├───┤   ├───┤   ├───┤       ├───┤ ├───┤
63///  │     [D]     │  │ (4,1) │                   │ 1 │   │ 4 │   │ 1 │     │ │ ? │ │ ? │ │ 3  │
64///  ├─────────────┤  ├───────┤             │     ├───┤   ├───┤   ├───┤       ├───┤ ├───┤
65///  │  [NULL, F]  │  │ (5,2) │                   │ 1 │   │ 5 │   │ 2 │     │ │ 1 │ │ D │ │ 4  │
66///  └─────────────┘  └───────┘             │     └───┘   └───┘   └───┘       ├───┤ ├───┤
67///                                                                         │ │ 0 │ │ ? │ │ 5  │
68///     Logical       Logical               │  Validity  Offsets  Sizes       ├───┤ ├───┤
69///      Values       Offset                   (nulls)                      │ │ 1 │ │ F │ │ 6  │
70///                   & Size                │                                 └───┘ └───┘
71///                                                                         │    Values   │    │
72///                 (offsets[i],            │   ListViewArray                   (Array)
73///                  sizes[i])                                              └ ─ ─ ─ ─ ─ ─ ┘    │
74///                                         └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
75/// ```
76///
77/// Another way of representing the same array but taking advantage of the offsets being out of order:
78///
79/// ```text
80///                                         ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
81///                                                                         ┌ ─ ─ ─ ─ ─ ─ ┐    │
82///  ┌─────────────┐  ┌───────┐             │     ┌───┐   ┌───┐   ┌───┐       ┌───┐ ┌───┐
83///  │   [A,B,C]   │  │ (2,3) │                   │ 1 │   │ 2 │   │ 3 │     │ │ 0 │ │ ? │ │ 0  │
84///  ├─────────────┤  ├───────┤             │     ├───┤   ├───┤   ├───┤       ├───┤ ├───┤
85///  │      []     │  │ (0,0) │                   │ 1 │   │ 0 │   │ 0 │     │ │ 1 │ │ F │ │ 1  │
86///  ├─────────────┤  ├───────┤             │     ├───┤   ├───┤   ├───┤       ├───┤ ├───┤
87///  │    NULL     │  │ (?,?) │                   │ 0 │   │ ? │   │ ? │     │ │ 1 │ │ A │ │ 2  │
88///  ├─────────────┤  ├───────┤             │     ├───┤   ├───┤   ├───┤       ├───┤ ├───┤
89///  │     [D]     │  │ (5,1) │                   │ 1 │   │ 5 │   │ 1 │     │ │ 1 │ │ B │ │ 3  │
90///  ├─────────────┤  ├───────┤             │     ├───┤   ├───┤   ├───┤       ├───┤ ├───┤
91///  │  [NULL, F]  │  │ (0,2) │                   │ 1 │   │ 0 │   │ 2 │     │ │ 1 │ │ C │ │ 4  │
92///  └─────────────┘  └───────┘             │     └───┘   └───┘   └───┘       ├───┤ ├───┤
93///                                                                         │ │ 1 │ │ D │ │ 5  │
94///     Logical       Logical               │  Validity  Offsets  Sizes       └───┘ └───┘
95///      Values       Offset                   (nulls)                      │    Values   │    │
96///                   & Size                │                                   (Array)
97///                                                                         └ ─ ─ ─ ─ ─ ─ ┘    │
98///                 (offsets[i],            │   ListViewArray
99///                  sizes[i])                                                                 │
100///                                         └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
101/// ```
102///
103/// [`GenericListArray`]: crate::array::GenericListArray
104/// [variable length lists]: https://arrow.apache.org/docs/format/Columnar.html#variable-size-list-layout
105/// [list layout]: https://arrow.apache.org/docs/format/Columnar.html#list-layout
106/// [list-view layout]: https://arrow.apache.org/docs/format/Columnar.html#listview-layout
107#[derive(Clone)]
108pub struct GenericListViewArray<OffsetSize: OffsetSizeTrait> {
109    data_type: DataType,
110    nulls: Option<NullBuffer>,
111    values: ArrayRef,
112    // Unlike GenericListArray, we do not use OffsetBuffer here as offsets are not
113    // guaranteed to be monotonically increasing.
114    value_offsets: ScalarBuffer<OffsetSize>,
115    value_sizes: ScalarBuffer<OffsetSize>,
116}
117
118impl<OffsetSize: OffsetSizeTrait> GenericListViewArray<OffsetSize> {
119    /// The data type constructor of listview array.
120    /// The input is the schema of the child array and
121    /// the output is the [`DataType`], ListView or LargeListView.
122    pub const DATA_TYPE_CONSTRUCTOR: fn(FieldRef) -> DataType = if OffsetSize::IS_LARGE {
123        DataType::LargeListView
124    } else {
125        DataType::ListView
126    };
127
128    /// Create a new [`GenericListViewArray`] from the provided parts
129    ///
130    /// # Errors
131    ///
132    /// Errors if
133    ///
134    /// * `offsets.len() != sizes.len()`
135    /// * `offsets.len() != nulls.len()`
136    /// * `offsets[i] > values.len()`
137    /// * `!field.is_nullable() && values.is_nullable()`
138    /// * `field.data_type() != values.data_type()`
139    /// * `0 <= offsets[i] <= length of the child array`
140    /// * `0 <= offsets[i] + size[i] <= length of the child array`
141    pub fn try_new(
142        field: FieldRef,
143        offsets: ScalarBuffer<OffsetSize>,
144        sizes: ScalarBuffer<OffsetSize>,
145        values: ArrayRef,
146        nulls: Option<NullBuffer>,
147    ) -> Result<Self, ArrowError> {
148        let len = offsets.len();
149        if let Some(n) = nulls.as_ref() {
150            if n.len() != len {
151                return Err(ArrowError::InvalidArgumentError(format!(
152                    "Incorrect length of null buffer for {}ListViewArray, expected {len} got {}",
153                    OffsetSize::PREFIX,
154                    n.len(),
155                )));
156            }
157        }
158        if len != sizes.len() {
159            return Err(ArrowError::InvalidArgumentError(format!(
160                "Length of offsets buffer and sizes buffer must be equal for {}ListViewArray, got {len} and {}",
161                OffsetSize::PREFIX,
162                sizes.len()
163            )));
164        }
165
166        for (offset, size) in offsets.iter().zip(sizes.iter()) {
167            let offset = offset.as_usize();
168            let size = size.as_usize();
169            if offset.checked_add(size).ok_or_else(|| {
170                ArrowError::InvalidArgumentError(format!(
171                    "Overflow in offset + size for {}ListViewArray",
172                    OffsetSize::PREFIX
173                ))
174            })? > values.len()
175            {
176                return Err(ArrowError::InvalidArgumentError(format!(
177                    "Offset + size for {}ListViewArray must be within the bounds of the child array, got offset: {offset}, size: {size}, child array length: {}",
178                    OffsetSize::PREFIX,
179                    values.len()
180                )));
181            }
182        }
183
184        if !field.is_nullable() && values.is_nullable() {
185            return Err(ArrowError::InvalidArgumentError(format!(
186                "Non-nullable field of {}ListViewArray {:?} cannot contain nulls",
187                OffsetSize::PREFIX,
188                field.name()
189            )));
190        }
191
192        if field.data_type() != values.data_type() {
193            return Err(ArrowError::InvalidArgumentError(format!(
194                "{}ListViewArray expected data type {} got {} for {:?}",
195                OffsetSize::PREFIX,
196                field.data_type(),
197                values.data_type(),
198                field.name()
199            )));
200        }
201
202        Ok(Self {
203            data_type: Self::DATA_TYPE_CONSTRUCTOR(field),
204            nulls,
205            values,
206            value_offsets: offsets,
207            value_sizes: sizes,
208        })
209    }
210
211    /// Create a new [`GenericListViewArray`] from the provided parts
212    ///
213    /// # Panics
214    ///
215    /// Panics if [`Self::try_new`] returns an error
216    pub fn new(
217        field: FieldRef,
218        offsets: ScalarBuffer<OffsetSize>,
219        sizes: ScalarBuffer<OffsetSize>,
220        values: ArrayRef,
221        nulls: Option<NullBuffer>,
222    ) -> Self {
223        Self::try_new(field, offsets, sizes, values, nulls).unwrap()
224    }
225
226    /// Create a new [`GenericListViewArray`] of length `len` where all values are null
227    pub fn new_null(field: FieldRef, len: usize) -> Self {
228        let values = new_empty_array(field.data_type());
229        Self {
230            data_type: Self::DATA_TYPE_CONSTRUCTOR(field),
231            nulls: Some(NullBuffer::new_null(len)),
232            value_offsets: ScalarBuffer::from(vec![OffsetSize::usize_as(0); len]),
233            value_sizes: ScalarBuffer::from(vec![OffsetSize::usize_as(0); len]),
234            values,
235        }
236    }
237
238    /// Deconstruct this array into its constituent parts
239    pub fn into_parts(
240        self,
241    ) -> (
242        FieldRef,
243        ScalarBuffer<OffsetSize>,
244        ScalarBuffer<OffsetSize>,
245        ArrayRef,
246        Option<NullBuffer>,
247    ) {
248        let f = match self.data_type {
249            DataType::ListView(f) | DataType::LargeListView(f) => f,
250            _ => unreachable!(),
251        };
252        (
253            f,
254            self.value_offsets,
255            self.value_sizes,
256            self.values,
257            self.nulls,
258        )
259    }
260
261    /// Returns a reference to the offsets of this list
262    ///
263    /// Unlike [`Self::value_offsets`] this returns the [`ScalarBuffer`]
264    /// allowing for zero-copy cloning
265    #[inline]
266    pub fn offsets(&self) -> &ScalarBuffer<OffsetSize> {
267        &self.value_offsets
268    }
269
270    /// Returns a reference to the values of this list
271    #[inline]
272    pub fn values(&self) -> &ArrayRef {
273        &self.values
274    }
275
276    /// Returns a reference to the sizes of this list
277    ///
278    /// Unlike [`Self::value_sizes`] this returns the [`ScalarBuffer`]
279    /// allowing for zero-copy cloning
280    #[inline]
281    pub fn sizes(&self) -> &ScalarBuffer<OffsetSize> {
282        &self.value_sizes
283    }
284
285    /// Returns a clone of the value type of this list.
286    pub fn value_type(&self) -> DataType {
287        self.values.data_type().clone()
288    }
289
290    /// Returns ith value of this list view array.
291    ///
292    /// Note: This method does not check for nulls and the value is arbitrary
293    /// if [`is_null`](Self::is_null) returns true for the index.
294    ///
295    /// # Safety
296    /// Caller must ensure that the index is within the array bounds
297    pub unsafe fn value_unchecked(&self, i: usize) -> ArrayRef {
298        let offset = unsafe { self.value_offsets().get_unchecked(i).as_usize() };
299        let length = unsafe { self.value_sizes().get_unchecked(i).as_usize() };
300        self.values.slice(offset, length)
301    }
302
303    /// Returns ith value of this list view array.
304    ///
305    /// Note: This method does not check for nulls and the value is arbitrary
306    /// (but still well-defined) if [`is_null`](Self::is_null) returns true for the index.
307    ///
308    /// # Panics
309    /// Panics if the index is out of bounds
310    pub fn value(&self, i: usize) -> ArrayRef {
311        let offset = self.value_offsets()[i].as_usize();
312        let length = self.value_sizes()[i].as_usize();
313        self.values.slice(offset, length)
314    }
315
316    /// Returns the offset values in the offsets buffer
317    #[inline]
318    pub fn value_offsets(&self) -> &[OffsetSize] {
319        &self.value_offsets
320    }
321
322    /// Returns the sizes values in the offsets buffer
323    #[inline]
324    pub fn value_sizes(&self) -> &[OffsetSize] {
325        &self.value_sizes
326    }
327
328    /// Returns the size for value at index `i`.
329    #[inline]
330    pub fn value_size(&self, i: usize) -> OffsetSize {
331        self.value_sizes[i]
332    }
333
334    /// Returns the offset for value at index `i`.
335    pub fn value_offset(&self, i: usize) -> OffsetSize {
336        self.value_offsets[i]
337    }
338
339    /// Constructs a new iterator
340    pub fn iter(&self) -> GenericListViewArrayIter<'_, OffsetSize> {
341        GenericListViewArrayIter::<'_, OffsetSize>::new(self)
342    }
343
344    #[inline]
345    fn get_type(data_type: &DataType) -> Option<&DataType> {
346        match (OffsetSize::IS_LARGE, data_type) {
347            (true, DataType::LargeListView(child)) | (false, DataType::ListView(child)) => {
348                Some(child.data_type())
349            }
350            _ => None,
351        }
352    }
353
354    /// Returns a zero-copy slice of this array with the indicated offset and length.
355    pub fn slice(&self, offset: usize, length: usize) -> Self {
356        Self {
357            data_type: self.data_type.clone(),
358            nulls: self.nulls.as_ref().map(|n| n.slice(offset, length)),
359            values: self.values.clone(),
360            value_offsets: self.value_offsets.slice(offset, length),
361            value_sizes: self.value_sizes.slice(offset, length),
362        }
363    }
364
365    /// Creates a [`GenericListViewArray`] from an iterator of primitive values
366    /// # Example
367    /// ```
368    /// # use arrow_array::ListViewArray;
369    /// # use arrow_array::types::Int32Type;
370    ///
371    /// let data = vec![
372    ///    Some(vec![Some(0), Some(1), Some(2)]),
373    ///    None,
374    ///    Some(vec![Some(3), None, Some(5)]),
375    ///    Some(vec![Some(6), Some(7)]),
376    /// ];
377    /// let list_array = ListViewArray::from_iter_primitive::<Int32Type, _, _>(data);
378    /// println!("{:?}", list_array);
379    /// ```
380    pub fn from_iter_primitive<T, P, I>(iter: I) -> Self
381    where
382        T: ArrowPrimitiveType,
383        P: IntoIterator<Item = Option<<T as ArrowPrimitiveType>::Native>>,
384        I: IntoIterator<Item = Option<P>>,
385    {
386        let iter = iter.into_iter();
387        let size_hint = iter.size_hint().0;
388        let mut builder =
389            GenericListViewBuilder::with_capacity(PrimitiveBuilder::<T>::new(), size_hint);
390
391        for i in iter {
392            match i {
393                Some(p) => {
394                    for t in p {
395                        builder.values().append_option(t);
396                    }
397                    builder.append(true);
398                }
399                None => builder.append(false),
400            }
401        }
402        builder.finish()
403    }
404}
405
406impl<OffsetSize: OffsetSizeTrait> ArrayAccessor for &GenericListViewArray<OffsetSize> {
407    type Item = ArrayRef;
408
409    fn value(&self, index: usize) -> Self::Item {
410        GenericListViewArray::value(self, index)
411    }
412
413    unsafe fn value_unchecked(&self, index: usize) -> Self::Item {
414        unsafe { GenericListViewArray::value_unchecked(self, index) }
415    }
416}
417
418impl<OffsetSize: OffsetSizeTrait> super::private::Sealed for GenericListViewArray<OffsetSize> {}
419
420impl<OffsetSize: OffsetSizeTrait> Array for GenericListViewArray<OffsetSize> {
421    fn as_any(&self) -> &dyn Any {
422        self
423    }
424
425    fn to_data(&self) -> ArrayData {
426        self.clone().into()
427    }
428
429    fn into_data(self) -> ArrayData {
430        self.into()
431    }
432
433    fn data_type(&self) -> &DataType {
434        &self.data_type
435    }
436
437    fn slice(&self, offset: usize, length: usize) -> ArrayRef {
438        Arc::new(self.slice(offset, length))
439    }
440
441    fn len(&self) -> usize {
442        self.sizes().len()
443    }
444
445    fn is_empty(&self) -> bool {
446        self.value_sizes.is_empty()
447    }
448
449    fn shrink_to_fit(&mut self) {
450        if let Some(nulls) = &mut self.nulls {
451            nulls.shrink_to_fit();
452        }
453        self.values.shrink_to_fit();
454        self.value_offsets.shrink_to_fit();
455        self.value_sizes.shrink_to_fit();
456    }
457
458    fn offset(&self) -> usize {
459        0
460    }
461
462    fn nulls(&self) -> Option<&NullBuffer> {
463        self.nulls.as_ref()
464    }
465
466    fn logical_null_count(&self) -> usize {
467        // More efficient that the default implementation
468        self.null_count()
469    }
470
471    fn get_buffer_memory_size(&self) -> usize {
472        let mut size = self.values.get_buffer_memory_size();
473        size += self.value_offsets.inner().capacity();
474        size += self.value_sizes.inner().capacity();
475        if let Some(n) = self.nulls.as_ref() {
476            size += n.buffer().capacity();
477        }
478        size
479    }
480
481    fn get_array_memory_size(&self) -> usize {
482        let mut size = std::mem::size_of::<Self>() + self.values.get_array_memory_size();
483        size += self.value_offsets.inner().capacity();
484        size += self.value_sizes.inner().capacity();
485        if let Some(n) = self.nulls.as_ref() {
486            size += n.buffer().capacity();
487        }
488        size
489    }
490}
491
492impl<OffsetSize: OffsetSizeTrait> std::fmt::Debug for GenericListViewArray<OffsetSize> {
493    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
494        let prefix = OffsetSize::PREFIX;
495        write!(f, "{prefix}ListViewArray\n[\n")?;
496        print_long_array(self, f, |array, index, f| {
497            std::fmt::Debug::fmt(&array.value(index), f)
498        })?;
499        write!(f, "]")
500    }
501}
502
503impl<OffsetSize: OffsetSizeTrait> From<GenericListArray<OffsetSize>>
504    for GenericListViewArray<OffsetSize>
505{
506    fn from(value: GenericListArray<OffsetSize>) -> Self {
507        let (field, offsets, values, nulls) = value.into_parts();
508        let len = offsets.len() - 1;
509        let mut sizes = Vec::with_capacity(len);
510        let mut view_offsets = Vec::with_capacity(len);
511        for (i, offset) in offsets.iter().enumerate().take(len) {
512            view_offsets.push(*offset);
513            sizes.push(offsets[i + 1] - offsets[i]);
514        }
515
516        Self::new(
517            field,
518            ScalarBuffer::from(view_offsets),
519            ScalarBuffer::from(sizes),
520            values,
521            nulls,
522        )
523    }
524}
525
526impl<OffsetSize: OffsetSizeTrait> From<GenericListViewArray<OffsetSize>> for ArrayData {
527    fn from(array: GenericListViewArray<OffsetSize>) -> Self {
528        let len = array.len();
529        let builder = ArrayDataBuilder::new(array.data_type)
530            .len(len)
531            .nulls(array.nulls)
532            .buffers(vec![
533                array.value_offsets.into_inner(),
534                array.value_sizes.into_inner(),
535            ])
536            .child_data(vec![array.values.to_data()]);
537
538        unsafe { builder.build_unchecked() }
539    }
540}
541
542impl<OffsetSize: OffsetSizeTrait> From<ArrayData> for GenericListViewArray<OffsetSize> {
543    fn from(data: ArrayData) -> Self {
544        Self::try_new_from_array_data(data)
545            .expect("Expected infallible creation of GenericListViewArray from ArrayDataRef failed")
546    }
547}
548
549impl<OffsetSize: OffsetSizeTrait> From<FixedSizeListArray> for GenericListViewArray<OffsetSize> {
550    fn from(value: FixedSizeListArray) -> Self {
551        let (field, size) = match value.data_type() {
552            DataType::FixedSizeList(f, size) => (f, *size as usize),
553            _ => unreachable!(),
554        };
555        let mut acc = 0_usize;
556        let iter = std::iter::repeat_n(size, value.len());
557        let mut sizes = Vec::with_capacity(iter.size_hint().0);
558        let mut offsets = Vec::with_capacity(iter.size_hint().0);
559
560        for size in iter {
561            offsets.push(OffsetSize::usize_as(acc));
562            acc = acc.add(size);
563            sizes.push(OffsetSize::usize_as(size));
564        }
565        let sizes = ScalarBuffer::from(sizes);
566        let offsets = ScalarBuffer::from(offsets);
567        Self {
568            data_type: Self::DATA_TYPE_CONSTRUCTOR(field.clone()),
569            nulls: value.nulls().cloned(),
570            values: value.values().clone(),
571            value_offsets: offsets,
572            value_sizes: sizes,
573        }
574    }
575}
576
577impl<OffsetSize: OffsetSizeTrait> GenericListViewArray<OffsetSize> {
578    fn try_new_from_array_data(data: ArrayData) -> Result<Self, ArrowError> {
579        let (data_type, len, nulls, offset, buffers, child_data) = data.into_parts();
580
581        // ArrayData is valid, and verified type above
582        // buffer[0] is offsets, buffer[1] is sizes
583        let num_buffers = buffers.len();
584        let [offsets_buffer, sizes_buffer] : [Buffer; 2] = buffers.try_into().map_err(|_| {
585             ArrowError::InvalidArgumentError(format!(
586                "ListViewArray data should contain two buffers (value offsets & value sizes), had {num_buffers}",
587            ))
588        })?;
589
590        let num_child = child_data.len();
591        let [values]: [ArrayData; 1] = child_data.try_into().map_err(|_| {
592            ArrowError::InvalidArgumentError(format!(
593                "ListViewArray should contain a single child array (values array), had {num_child}",
594            ))
595        })?;
596
597        if let Some(child_data_type) = Self::get_type(&data_type) {
598            if values.data_type() != child_data_type {
599                return Err(ArrowError::InvalidArgumentError(format!(
600                    "{}ListViewArray's child datatype {:?} does not \
601                             correspond to the List's datatype {:?}",
602                    OffsetSize::PREFIX,
603                    values.data_type(),
604                    child_data_type
605                )));
606            }
607        } else {
608            return Err(ArrowError::InvalidArgumentError(format!(
609                "{}ListViewArray's datatype must be {}ListViewArray(). It is {:?}",
610                OffsetSize::PREFIX,
611                OffsetSize::PREFIX,
612                data_type
613            )));
614        }
615
616        let values = make_array(values);
617        let value_offsets = ScalarBuffer::new(offsets_buffer, offset, len);
618        let value_sizes = ScalarBuffer::new(sizes_buffer, offset, len);
619
620        Ok(Self {
621            data_type,
622            nulls,
623            values,
624            value_offsets,
625            value_sizes,
626        })
627    }
628}
629
630#[cfg(test)]
631mod tests {
632    use arrow_buffer::{BooleanBuffer, Buffer, NullBufferBuilder, ScalarBuffer, bit_util};
633    use arrow_schema::Field;
634
635    use crate::builder::{FixedSizeListBuilder, Int32Builder};
636    use crate::cast::AsArray;
637    use crate::types::Int32Type;
638    use crate::{Int32Array, Int64Array};
639
640    use super::*;
641
642    #[test]
643    fn test_empty_list_view_array() {
644        // Construct an empty value array
645        let vec: Vec<i32> = vec![];
646        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
647        let sizes = ScalarBuffer::from(vec![]);
648        let offsets = ScalarBuffer::from(vec![]);
649        let values = Int32Array::from(vec);
650        let list_array = LargeListViewArray::new(field, offsets, sizes, Arc::new(values), None);
651
652        assert_eq!(list_array.len(), 0)
653    }
654
655    #[test]
656    fn test_list_view_array() {
657        // Construct a value array
658        let value_data = ArrayData::builder(DataType::Int32)
659            .len(8)
660            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
661            .build()
662            .unwrap();
663
664        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
665        let sizes = ScalarBuffer::from(vec![3i32, 3, 2]);
666        let offsets = ScalarBuffer::from(vec![0i32, 3, 6]);
667        let values = Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7]);
668        let list_array = ListViewArray::new(field, offsets, sizes, Arc::new(values), None);
669
670        let values = list_array.values();
671        assert_eq!(value_data, values.to_data());
672        assert_eq!(DataType::Int32, list_array.value_type());
673        assert_eq!(3, list_array.len());
674        assert_eq!(0, list_array.null_count());
675        assert_eq!(6, list_array.value_offsets()[2]);
676        assert_eq!(2, list_array.value_sizes()[2]);
677        assert_eq!(2, list_array.value_size(2));
678        assert_eq!(0, list_array.value(0).as_primitive::<Int32Type>().value(0));
679        assert_eq!(
680            0,
681            unsafe { list_array.value_unchecked(0) }
682                .as_primitive::<Int32Type>()
683                .value(0)
684        );
685        for i in 0..3 {
686            assert!(list_array.is_valid(i));
687            assert!(!list_array.is_null(i));
688        }
689    }
690
691    #[test]
692    fn test_large_list_view_array() {
693        // Construct a value array
694        let value_data = ArrayData::builder(DataType::Int32)
695            .len(8)
696            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
697            .build()
698            .unwrap();
699
700        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
701        let sizes = ScalarBuffer::from(vec![3i64, 3, 2]);
702        let offsets = ScalarBuffer::from(vec![0i64, 3, 6]);
703        let values = Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7]);
704        let list_array = LargeListViewArray::new(field, offsets, sizes, Arc::new(values), None);
705
706        let values = list_array.values();
707        assert_eq!(value_data, values.to_data());
708        assert_eq!(DataType::Int32, list_array.value_type());
709        assert_eq!(3, list_array.len());
710        assert_eq!(0, list_array.null_count());
711        assert_eq!(6, list_array.value_offsets()[2]);
712        assert_eq!(2, list_array.value_sizes()[2]);
713        assert_eq!(2, list_array.value_size(2));
714        assert_eq!(0, list_array.value(0).as_primitive::<Int32Type>().value(0));
715        assert_eq!(
716            0,
717            unsafe { list_array.value_unchecked(0) }
718                .as_primitive::<Int32Type>()
719                .value(0)
720        );
721        for i in 0..3 {
722            assert!(list_array.is_valid(i));
723            assert!(!list_array.is_null(i));
724        }
725    }
726
727    #[test]
728    fn test_list_view_array_slice() {
729        // Construct a value array
730        let value_data = ArrayData::builder(DataType::Int32)
731            .len(10)
732            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
733            .build()
734            .unwrap();
735
736        // 01011001 00000001
737        let mut null_bits: [u8; 2] = [0; 2];
738        bit_util::set_bit(&mut null_bits, 0);
739        bit_util::set_bit(&mut null_bits, 3);
740        bit_util::set_bit(&mut null_bits, 4);
741        bit_util::set_bit(&mut null_bits, 6);
742        bit_util::set_bit(&mut null_bits, 8);
743        let buffer = BooleanBuffer::new(Buffer::from(null_bits), 0, 9);
744        let null_buffer = NullBuffer::new(buffer);
745
746        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
747        let sizes = ScalarBuffer::from(vec![2, 0, 0, 2, 2, 0, 3, 0, 1]);
748        let offsets = ScalarBuffer::from(vec![0, 2, 2, 2, 4, 6, 6, 9, 9]);
749        let values = Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
750        let list_array =
751            ListViewArray::new(field, offsets, sizes, Arc::new(values), Some(null_buffer));
752
753        let values = list_array.values();
754        assert_eq!(value_data, values.to_data());
755        assert_eq!(DataType::Int32, list_array.value_type());
756        assert_eq!(9, list_array.len());
757        assert_eq!(4, list_array.null_count());
758        assert_eq!(2, list_array.value_offsets()[3]);
759        assert_eq!(2, list_array.value_sizes()[3]);
760        assert_eq!(2, list_array.value_size(3));
761
762        let sliced_array = list_array.slice(1, 6);
763        assert_eq!(6, sliced_array.len());
764        assert_eq!(3, sliced_array.null_count());
765
766        for i in 0..sliced_array.len() {
767            if bit_util::get_bit(&null_bits, 1 + i) {
768                assert!(sliced_array.is_valid(i));
769            } else {
770                assert!(sliced_array.is_null(i));
771            }
772        }
773
774        // Check offset and length for each non-null value.
775        let sliced_list_array = sliced_array
776            .as_any()
777            .downcast_ref::<ListViewArray>()
778            .unwrap();
779        assert_eq!(2, sliced_list_array.value_offsets()[2]);
780        assert_eq!(2, sliced_list_array.value_sizes()[2]);
781        assert_eq!(2, sliced_list_array.value_size(2));
782
783        assert_eq!(4, sliced_list_array.value_offsets()[3]);
784        assert_eq!(2, sliced_list_array.value_sizes()[3]);
785        assert_eq!(2, sliced_list_array.value_size(3));
786
787        assert_eq!(6, sliced_list_array.value_offsets()[5]);
788        assert_eq!(3, sliced_list_array.value_sizes()[5]);
789        assert_eq!(3, sliced_list_array.value_size(5));
790    }
791
792    #[test]
793    fn test_large_list_view_array_slice() {
794        // Construct a value array
795        let value_data = ArrayData::builder(DataType::Int32)
796            .len(10)
797            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
798            .build()
799            .unwrap();
800
801        // 01011001 00000001
802        let mut null_bits: [u8; 2] = [0; 2];
803        bit_util::set_bit(&mut null_bits, 0);
804        bit_util::set_bit(&mut null_bits, 3);
805        bit_util::set_bit(&mut null_bits, 4);
806        bit_util::set_bit(&mut null_bits, 6);
807        bit_util::set_bit(&mut null_bits, 8);
808        let buffer = BooleanBuffer::new(Buffer::from(null_bits), 0, 9);
809        let null_buffer = NullBuffer::new(buffer);
810
811        // Construct a large list view array from the above two
812        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
813        let sizes = ScalarBuffer::from(vec![2i64, 0, 0, 2, 2, 0, 3, 0, 1]);
814        let offsets = ScalarBuffer::from(vec![0i64, 2, 2, 2, 4, 6, 6, 9, 9]);
815        let values = Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
816        let list_array =
817            LargeListViewArray::new(field, offsets, sizes, Arc::new(values), Some(null_buffer));
818
819        let values = list_array.values();
820        assert_eq!(value_data, values.to_data());
821        assert_eq!(DataType::Int32, list_array.value_type());
822        assert_eq!(9, list_array.len());
823        assert_eq!(4, list_array.null_count());
824        assert_eq!(2, list_array.value_offsets()[3]);
825        assert_eq!(2, list_array.value_sizes()[3]);
826        assert_eq!(2, list_array.value_size(3));
827
828        let sliced_array = list_array.slice(1, 6);
829        assert_eq!(6, sliced_array.len());
830        assert_eq!(3, sliced_array.null_count());
831
832        for i in 0..sliced_array.len() {
833            if bit_util::get_bit(&null_bits, 1 + i) {
834                assert!(sliced_array.is_valid(i));
835            } else {
836                assert!(sliced_array.is_null(i));
837            }
838        }
839
840        // Check offset and length for each non-null value.
841        let sliced_list_array = sliced_array
842            .as_any()
843            .downcast_ref::<LargeListViewArray>()
844            .unwrap();
845        assert_eq!(2, sliced_list_array.value_offsets()[2]);
846        assert_eq!(2, sliced_list_array.value_size(2));
847        assert_eq!(2, sliced_list_array.value_sizes()[2]);
848
849        assert_eq!(4, sliced_list_array.value_offsets()[3]);
850        assert_eq!(2, sliced_list_array.value_size(3));
851        assert_eq!(2, sliced_list_array.value_sizes()[3]);
852
853        assert_eq!(6, sliced_list_array.value_offsets()[5]);
854        assert_eq!(3, sliced_list_array.value_size(5));
855        assert_eq!(2, sliced_list_array.value_sizes()[3]);
856    }
857
858    #[test]
859    #[should_panic(expected = "index out of bounds: the len is 9 but the index is 10")]
860    fn test_list_view_array_index_out_of_bound() {
861        // 01011001 00000001
862        let mut null_bits: [u8; 2] = [0; 2];
863        bit_util::set_bit(&mut null_bits, 0);
864        bit_util::set_bit(&mut null_bits, 3);
865        bit_util::set_bit(&mut null_bits, 4);
866        bit_util::set_bit(&mut null_bits, 6);
867        bit_util::set_bit(&mut null_bits, 8);
868        let buffer = BooleanBuffer::new(Buffer::from(null_bits), 0, 9);
869        let null_buffer = NullBuffer::new(buffer);
870
871        // Construct a buffer for value offsets, for the nested array:
872        //  [[0, 1], null, null, [2, 3], [4, 5], null, [6, 7, 8], null, [9]]
873        // Construct a list array from the above two
874        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
875        let sizes = ScalarBuffer::from(vec![2i32, 0, 0, 2, 2, 0, 3, 0, 1]);
876        let offsets = ScalarBuffer::from(vec![0i32, 2, 2, 2, 4, 6, 6, 9, 9]);
877        let values = Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
878        let list_array =
879            ListViewArray::new(field, offsets, sizes, Arc::new(values), Some(null_buffer));
880
881        assert_eq!(9, list_array.len());
882        list_array.value(10);
883    }
884    #[test]
885    #[should_panic(
886        expected = "ListViewArray data should contain two buffers (value offsets & value sizes), had 0"
887    )]
888    #[cfg(not(feature = "force_validate"))]
889    fn test_list_view_array_invalid_buffer_len() {
890        let value_data = unsafe {
891            ArrayData::builder(DataType::Int32)
892                .len(8)
893                .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
894                .build_unchecked()
895        };
896        let list_data_type =
897            DataType::ListView(Arc::new(Field::new_list_field(DataType::Int32, false)));
898        let list_data = unsafe {
899            ArrayData::builder(list_data_type)
900                .len(3)
901                .add_child_data(value_data)
902                .build_unchecked()
903        };
904        drop(ListViewArray::from(list_data));
905    }
906
907    #[test]
908    #[should_panic(
909        expected = "ListViewArray data should contain two buffers (value offsets & value sizes), had 1"
910    )]
911    #[cfg(not(feature = "force_validate"))]
912    fn test_list_view_array_invalid_child_array_len() {
913        let value_offsets = Buffer::from_slice_ref([0, 2, 5, 7]);
914        let list_data_type =
915            DataType::ListView(Arc::new(Field::new_list_field(DataType::Int32, false)));
916        let list_data = unsafe {
917            ArrayData::builder(list_data_type)
918                .len(3)
919                .add_buffer(value_offsets)
920                .build_unchecked()
921        };
922        drop(ListViewArray::from(list_data));
923    }
924
925    #[test]
926    fn test_list_view_array_offsets_need_not_start_at_zero() {
927        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
928        let sizes = ScalarBuffer::from(vec![0i32, 0, 3]);
929        let offsets = ScalarBuffer::from(vec![2i32, 2, 5]);
930        let values = Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7]);
931        let list_array = ListViewArray::new(field, offsets, sizes, Arc::new(values), None);
932
933        assert_eq!(list_array.value_size(0), 0);
934        assert_eq!(list_array.value_size(1), 0);
935        assert_eq!(list_array.value_size(2), 3);
936    }
937
938    #[test]
939    #[should_panic(expected = "Memory pointer is not aligned with the specified scalar type")]
940    #[cfg(not(feature = "force_validate"))]
941    fn test_list_view_array_alignment() {
942        let offset_buf = Buffer::from_slice_ref([0_u64]);
943        let offset_buf2 = offset_buf.slice(1);
944
945        let size_buf = Buffer::from_slice_ref([0_u64]);
946        let size_buf2 = size_buf.slice(1);
947
948        let values: [i32; 8] = [0; 8];
949        let value_data = unsafe {
950            ArrayData::builder(DataType::Int32)
951                .add_buffer(Buffer::from_slice_ref(values))
952                .build_unchecked()
953        };
954
955        let list_data_type =
956            DataType::ListView(Arc::new(Field::new_list_field(DataType::Int32, false)));
957        let list_data = unsafe {
958            ArrayData::builder(list_data_type)
959                .add_buffer(offset_buf2)
960                .add_buffer(size_buf2)
961                .add_child_data(value_data)
962                .build_unchecked()
963        };
964        drop(ListViewArray::from(list_data));
965    }
966
967    #[test]
968    fn test_empty_offsets() {
969        let f = Arc::new(Field::new("element", DataType::Int32, true));
970        let string = ListViewArray::from(
971            ArrayData::builder(DataType::ListView(f.clone()))
972                .buffers(vec![Buffer::from(&[]), Buffer::from(&[])])
973                .add_child_data(ArrayData::new_empty(&DataType::Int32))
974                .build()
975                .unwrap(),
976        );
977        assert_eq!(string.value_offsets(), &[] as &[i32; 0]);
978        assert_eq!(string.value_sizes(), &[] as &[i32; 0]);
979
980        let string = LargeListViewArray::from(
981            ArrayData::builder(DataType::LargeListView(f))
982                .buffers(vec![Buffer::from(&[]), Buffer::from(&[])])
983                .add_child_data(ArrayData::new_empty(&DataType::Int32))
984                .build()
985                .unwrap(),
986        );
987        assert_eq!(string.len(), 0);
988        assert_eq!(string.value_offsets(), &[] as &[i64; 0]);
989        assert_eq!(string.value_sizes(), &[] as &[i64; 0]);
990    }
991
992    #[test]
993    fn test_try_new() {
994        let offsets = ScalarBuffer::from(vec![0, 1, 4, 5]);
995        let sizes = ScalarBuffer::from(vec![1, 3, 1, 0]);
996        let values = Int32Array::new(vec![1, 2, 3, 4, 5].into(), None);
997        let values = Arc::new(values) as ArrayRef;
998
999        let field = Arc::new(Field::new("element", DataType::Int32, false));
1000        ListViewArray::new(
1001            field.clone(),
1002            offsets.clone(),
1003            sizes.clone(),
1004            values.clone(),
1005            None,
1006        );
1007
1008        let nulls = NullBuffer::new_null(4);
1009        ListViewArray::new(
1010            field.clone(),
1011            offsets,
1012            sizes.clone(),
1013            values.clone(),
1014            Some(nulls),
1015        );
1016
1017        let nulls = NullBuffer::new_null(4);
1018        let offsets = ScalarBuffer::from(vec![0, 1, 2, 3, 4]);
1019        let sizes = ScalarBuffer::from(vec![1, 1, 1, 1, 0]);
1020        let err = LargeListViewArray::try_new(
1021            field,
1022            offsets.clone(),
1023            sizes.clone(),
1024            values.clone(),
1025            Some(nulls),
1026        )
1027        .unwrap_err();
1028
1029        assert_eq!(
1030            err.to_string(),
1031            "Invalid argument error: Incorrect length of null buffer for LargeListViewArray, expected 5 got 4"
1032        );
1033
1034        let field = Arc::new(Field::new("element", DataType::Int64, false));
1035        let err = LargeListViewArray::try_new(
1036            field.clone(),
1037            offsets.clone(),
1038            sizes.clone(),
1039            values.clone(),
1040            None,
1041        )
1042        .unwrap_err();
1043
1044        assert_eq!(
1045            err.to_string(),
1046            "Invalid argument error: LargeListViewArray expected data type Int64 got Int32 for \"element\""
1047        );
1048
1049        let nulls = NullBuffer::new_null(7);
1050        let values = Int64Array::new(vec![0; 7].into(), Some(nulls));
1051        let values = Arc::new(values);
1052
1053        let err = LargeListViewArray::try_new(
1054            field,
1055            offsets.clone(),
1056            sizes.clone(),
1057            values.clone(),
1058            None,
1059        )
1060        .unwrap_err();
1061
1062        assert_eq!(
1063            err.to_string(),
1064            "Invalid argument error: Non-nullable field of LargeListViewArray \"element\" cannot contain nulls"
1065        );
1066    }
1067
1068    #[test]
1069    fn test_from_fixed_size_list() {
1070        let mut builder = FixedSizeListBuilder::new(Int32Builder::new(), 3);
1071        builder.values().append_slice(&[1, 2, 3]);
1072        builder.append(true);
1073        builder.values().append_slice(&[0, 0, 0]);
1074        builder.append(false);
1075        builder.values().append_slice(&[4, 5, 6]);
1076        builder.append(true);
1077        let list: ListViewArray = builder.finish().into();
1078        let values: Vec<_> = list
1079            .iter()
1080            .map(|x| x.map(|x| x.as_primitive::<Int32Type>().values().to_vec()))
1081            .collect();
1082        assert_eq!(values, vec![Some(vec![1, 2, 3]), None, Some(vec![4, 5, 6])]);
1083        let offsets = list.value_offsets();
1084        assert_eq!(offsets, &[0, 3, 6]);
1085        let sizes = list.value_sizes();
1086        assert_eq!(sizes, &[3, 3, 3]);
1087    }
1088
1089    #[test]
1090    fn test_list_view_array_overlap_lists() {
1091        let value_data = unsafe {
1092            ArrayData::builder(DataType::Int32)
1093                .len(8)
1094                .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
1095                .build_unchecked()
1096        };
1097        let list_data_type =
1098            DataType::ListView(Arc::new(Field::new_list_field(DataType::Int32, false)));
1099        let list_data = unsafe {
1100            ArrayData::builder(list_data_type)
1101                .len(2)
1102                .add_buffer(Buffer::from_slice_ref([0, 3])) // offsets
1103                .add_buffer(Buffer::from_slice_ref([5, 5])) // sizes
1104                .add_child_data(value_data)
1105                .build_unchecked()
1106        };
1107        let array = ListViewArray::from(list_data);
1108
1109        assert_eq!(array.len(), 2);
1110        assert_eq!(array.value_size(0), 5);
1111        assert_eq!(array.value_size(1), 5);
1112
1113        let values: Vec<_> = array
1114            .iter()
1115            .map(|x| x.map(|x| x.as_primitive::<Int32Type>().values().to_vec()))
1116            .collect();
1117        assert_eq!(
1118            values,
1119            vec![Some(vec![0, 1, 2, 3, 4]), Some(vec![3, 4, 5, 6, 7])]
1120        );
1121    }
1122
1123    #[test]
1124    fn test_list_view_array_incomplete_offsets() {
1125        let value_data = unsafe {
1126            ArrayData::builder(DataType::Int32)
1127                .len(50)
1128                .add_buffer(Buffer::from_slice_ref((0..50).collect::<Vec<i32>>()))
1129                .build_unchecked()
1130        };
1131        let list_data_type =
1132            DataType::ListView(Arc::new(Field::new_list_field(DataType::Int32, false)));
1133        let list_data = unsafe {
1134            ArrayData::builder(list_data_type)
1135                .len(3)
1136                .add_buffer(Buffer::from_slice_ref([0, 5, 10])) // offsets
1137                .add_buffer(Buffer::from_slice_ref([0, 5, 10])) // sizes
1138                .add_child_data(value_data)
1139                .build_unchecked()
1140        };
1141        let array = ListViewArray::from(list_data);
1142
1143        assert_eq!(array.len(), 3);
1144        assert_eq!(array.value_size(0), 0);
1145        assert_eq!(array.value_size(1), 5);
1146        assert_eq!(array.value_size(2), 10);
1147
1148        let values: Vec<_> = array
1149            .iter()
1150            .map(|x| x.map(|x| x.as_primitive::<Int32Type>().values().to_vec()))
1151            .collect();
1152        assert_eq!(
1153            values,
1154            vec![
1155                Some(vec![]),
1156                Some(vec![5, 6, 7, 8, 9]),
1157                Some(vec![10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
1158            ]
1159        );
1160    }
1161
1162    #[test]
1163    fn test_list_view_array_empty_lists() {
1164        let value_data = unsafe {
1165            ArrayData::builder(DataType::Int32)
1166                .len(0)
1167                .add_buffer(Buffer::from_slice_ref::<i32, &[_; 0]>(&[]))
1168                .build_unchecked()
1169        };
1170        let list_data_type =
1171            DataType::ListView(Arc::new(Field::new_list_field(DataType::Int32, false)));
1172        let list_data = unsafe {
1173            ArrayData::builder(list_data_type)
1174                .len(3)
1175                .add_buffer(Buffer::from_slice_ref([0, 0, 0])) // offsets
1176                .add_buffer(Buffer::from_slice_ref([0, 0, 0])) // sizes
1177                .add_child_data(value_data)
1178                .build_unchecked()
1179        };
1180        let array = ListViewArray::from(list_data);
1181
1182        assert_eq!(array.len(), 3);
1183        assert_eq!(array.value_size(0), 0);
1184        assert_eq!(array.value_size(1), 0);
1185        assert_eq!(array.value_size(2), 0);
1186
1187        let values: Vec<_> = array
1188            .iter()
1189            .map(|x| x.map(|x| x.as_primitive::<Int32Type>().values().to_vec()))
1190            .collect();
1191        assert_eq!(values, vec![Some(vec![]), Some(vec![]), Some(vec![])]);
1192    }
1193
1194    #[test]
1195    fn test_list_view_new_null_len() {
1196        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
1197        let array = ListViewArray::new_null(field, 5);
1198        assert_eq!(array.len(), 5);
1199    }
1200
1201    #[test]
1202    fn test_from_iter_primitive() {
1203        let data = vec![
1204            Some(vec![Some(0), Some(1), Some(2)]),
1205            None,
1206            Some(vec![Some(3), Some(4), Some(5)]),
1207            Some(vec![Some(6), Some(7)]),
1208        ];
1209        let list_array = ListViewArray::from_iter_primitive::<Int32Type, _, _>(data);
1210
1211        //  [[0, 1, 2], NULL, [3, 4, 5], [6, 7]]
1212        let values = Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7]);
1213        let offsets = ScalarBuffer::from(vec![0, 3, 3, 6]);
1214        let sizes = ScalarBuffer::from(vec![3, 0, 3, 2]);
1215        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
1216
1217        let mut nulls = NullBufferBuilder::new(4);
1218        nulls.append(true);
1219        nulls.append(false);
1220        nulls.append_n_non_nulls(2);
1221        let another = ListViewArray::new(field, offsets, sizes, Arc::new(values), nulls.finish());
1222
1223        assert_eq!(list_array, another)
1224    }
1225}