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::{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> Array for GenericListViewArray<OffsetSize> {
419    fn as_any(&self) -> &dyn Any {
420        self
421    }
422
423    fn to_data(&self) -> ArrayData {
424        self.clone().into()
425    }
426
427    fn into_data(self) -> ArrayData {
428        self.into()
429    }
430
431    fn data_type(&self) -> &DataType {
432        &self.data_type
433    }
434
435    fn slice(&self, offset: usize, length: usize) -> ArrayRef {
436        Arc::new(self.slice(offset, length))
437    }
438
439    fn len(&self) -> usize {
440        self.sizes().len()
441    }
442
443    fn is_empty(&self) -> bool {
444        self.value_sizes.is_empty()
445    }
446
447    fn shrink_to_fit(&mut self) {
448        if let Some(nulls) = &mut self.nulls {
449            nulls.shrink_to_fit();
450        }
451        self.values.shrink_to_fit();
452        self.value_offsets.shrink_to_fit();
453        self.value_sizes.shrink_to_fit();
454    }
455
456    fn offset(&self) -> usize {
457        0
458    }
459
460    fn nulls(&self) -> Option<&NullBuffer> {
461        self.nulls.as_ref()
462    }
463
464    fn logical_null_count(&self) -> usize {
465        // More efficient that the default implementation
466        self.null_count()
467    }
468
469    fn get_buffer_memory_size(&self) -> usize {
470        let mut size = self.values.get_buffer_memory_size();
471        size += self.value_offsets.inner().capacity();
472        size += self.value_sizes.inner().capacity();
473        if let Some(n) = self.nulls.as_ref() {
474            size += n.buffer().capacity();
475        }
476        size
477    }
478
479    fn get_array_memory_size(&self) -> usize {
480        let mut size = std::mem::size_of::<Self>() + self.values.get_array_memory_size();
481        size += self.value_offsets.inner().capacity();
482        size += self.value_sizes.inner().capacity();
483        if let Some(n) = self.nulls.as_ref() {
484            size += n.buffer().capacity();
485        }
486        size
487    }
488}
489
490impl<OffsetSize: OffsetSizeTrait> std::fmt::Debug for GenericListViewArray<OffsetSize> {
491    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
492        let prefix = OffsetSize::PREFIX;
493        write!(f, "{prefix}ListViewArray\n[\n")?;
494        print_long_array(self, f, |array, index, f| {
495            std::fmt::Debug::fmt(&array.value(index), f)
496        })?;
497        write!(f, "]")
498    }
499}
500
501impl<OffsetSize: OffsetSizeTrait> From<GenericListArray<OffsetSize>>
502    for GenericListViewArray<OffsetSize>
503{
504    fn from(value: GenericListArray<OffsetSize>) -> Self {
505        let (field, offsets, values, nulls) = value.into_parts();
506        let len = offsets.len() - 1;
507        let mut sizes = Vec::with_capacity(len);
508        let mut view_offsets = Vec::with_capacity(len);
509        for (i, offset) in offsets.iter().enumerate().take(len) {
510            view_offsets.push(*offset);
511            sizes.push(offsets[i + 1] - offsets[i]);
512        }
513
514        Self::new(
515            field,
516            ScalarBuffer::from(view_offsets),
517            ScalarBuffer::from(sizes),
518            values,
519            nulls,
520        )
521    }
522}
523
524impl<OffsetSize: OffsetSizeTrait> From<GenericListViewArray<OffsetSize>> for ArrayData {
525    fn from(array: GenericListViewArray<OffsetSize>) -> Self {
526        let len = array.len();
527        let builder = ArrayDataBuilder::new(array.data_type)
528            .len(len)
529            .nulls(array.nulls)
530            .buffers(vec![
531                array.value_offsets.into_inner(),
532                array.value_sizes.into_inner(),
533            ])
534            .child_data(vec![array.values.to_data()]);
535
536        unsafe { builder.build_unchecked() }
537    }
538}
539
540impl<OffsetSize: OffsetSizeTrait> From<ArrayData> for GenericListViewArray<OffsetSize> {
541    fn from(data: ArrayData) -> Self {
542        Self::try_new_from_array_data(data)
543            .expect("Expected infallible creation of GenericListViewArray from ArrayDataRef failed")
544    }
545}
546
547impl<OffsetSize: OffsetSizeTrait> From<FixedSizeListArray> for GenericListViewArray<OffsetSize> {
548    fn from(value: FixedSizeListArray) -> Self {
549        let (field, size) = match value.data_type() {
550            DataType::FixedSizeList(f, size) => (f, *size as usize),
551            _ => unreachable!(),
552        };
553        let mut acc = 0_usize;
554        let iter = std::iter::repeat_n(size, value.len());
555        let mut sizes = Vec::with_capacity(iter.size_hint().0);
556        let mut offsets = Vec::with_capacity(iter.size_hint().0);
557
558        for size in iter {
559            offsets.push(OffsetSize::usize_as(acc));
560            acc = acc.add(size);
561            sizes.push(OffsetSize::usize_as(size));
562        }
563        let sizes = ScalarBuffer::from(sizes);
564        let offsets = ScalarBuffer::from(offsets);
565        Self {
566            data_type: Self::DATA_TYPE_CONSTRUCTOR(field.clone()),
567            nulls: value.nulls().cloned(),
568            values: value.values().clone(),
569            value_offsets: offsets,
570            value_sizes: sizes,
571        }
572    }
573}
574
575impl<OffsetSize: OffsetSizeTrait> GenericListViewArray<OffsetSize> {
576    fn try_new_from_array_data(data: ArrayData) -> Result<Self, ArrowError> {
577        if data.buffers().len() != 2 {
578            return Err(ArrowError::InvalidArgumentError(format!(
579                "ListViewArray data should contain two buffers (value offsets & value sizes), had {}",
580                data.buffers().len()
581            )));
582        }
583
584        if data.child_data().len() != 1 {
585            return Err(ArrowError::InvalidArgumentError(format!(
586                "ListViewArray should contain a single child array (values array), had {}",
587                data.child_data().len()
588            )));
589        }
590
591        let values = data.child_data()[0].clone();
592
593        if let Some(child_data_type) = Self::get_type(data.data_type()) {
594            if values.data_type() != child_data_type {
595                return Err(ArrowError::InvalidArgumentError(format!(
596                    "{}ListViewArray's child datatype {:?} does not \
597                             correspond to the List's datatype {:?}",
598                    OffsetSize::PREFIX,
599                    values.data_type(),
600                    child_data_type
601                )));
602            }
603        } else {
604            return Err(ArrowError::InvalidArgumentError(format!(
605                "{}ListViewArray's datatype must be {}ListViewArray(). It is {:?}",
606                OffsetSize::PREFIX,
607                OffsetSize::PREFIX,
608                data.data_type()
609            )));
610        }
611
612        let values = make_array(values);
613        // ArrayData is valid, and verified type above
614        let value_offsets = ScalarBuffer::new(data.buffers()[0].clone(), data.offset(), data.len());
615        let value_sizes = ScalarBuffer::new(data.buffers()[1].clone(), data.offset(), data.len());
616
617        Ok(Self {
618            data_type: data.data_type().clone(),
619            nulls: data.nulls().cloned(),
620            values,
621            value_offsets,
622            value_sizes,
623        })
624    }
625}
626
627#[cfg(test)]
628mod tests {
629    use arrow_buffer::{BooleanBuffer, Buffer, NullBufferBuilder, ScalarBuffer, bit_util};
630    use arrow_schema::Field;
631
632    use crate::builder::{FixedSizeListBuilder, Int32Builder};
633    use crate::cast::AsArray;
634    use crate::types::Int32Type;
635    use crate::{Int32Array, Int64Array};
636
637    use super::*;
638
639    #[test]
640    fn test_empty_list_view_array() {
641        // Construct an empty value array
642        let vec: Vec<i32> = vec![];
643        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
644        let sizes = ScalarBuffer::from(vec![]);
645        let offsets = ScalarBuffer::from(vec![]);
646        let values = Int32Array::from(vec);
647        let list_array = LargeListViewArray::new(field, offsets, sizes, Arc::new(values), None);
648
649        assert_eq!(list_array.len(), 0)
650    }
651
652    #[test]
653    fn test_list_view_array() {
654        // Construct a value array
655        let value_data = ArrayData::builder(DataType::Int32)
656            .len(8)
657            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
658            .build()
659            .unwrap();
660
661        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
662        let sizes = ScalarBuffer::from(vec![3i32, 3, 2]);
663        let offsets = ScalarBuffer::from(vec![0i32, 3, 6]);
664        let values = Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7]);
665        let list_array = ListViewArray::new(field, offsets, sizes, Arc::new(values), None);
666
667        let values = list_array.values();
668        assert_eq!(value_data, values.to_data());
669        assert_eq!(DataType::Int32, list_array.value_type());
670        assert_eq!(3, list_array.len());
671        assert_eq!(0, list_array.null_count());
672        assert_eq!(6, list_array.value_offsets()[2]);
673        assert_eq!(2, list_array.value_sizes()[2]);
674        assert_eq!(2, list_array.value_size(2));
675        assert_eq!(0, list_array.value(0).as_primitive::<Int32Type>().value(0));
676        assert_eq!(
677            0,
678            unsafe { list_array.value_unchecked(0) }
679                .as_primitive::<Int32Type>()
680                .value(0)
681        );
682        for i in 0..3 {
683            assert!(list_array.is_valid(i));
684            assert!(!list_array.is_null(i));
685        }
686    }
687
688    #[test]
689    fn test_large_list_view_array() {
690        // Construct a value array
691        let value_data = ArrayData::builder(DataType::Int32)
692            .len(8)
693            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
694            .build()
695            .unwrap();
696
697        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
698        let sizes = ScalarBuffer::from(vec![3i64, 3, 2]);
699        let offsets = ScalarBuffer::from(vec![0i64, 3, 6]);
700        let values = Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7]);
701        let list_array = LargeListViewArray::new(field, offsets, sizes, Arc::new(values), None);
702
703        let values = list_array.values();
704        assert_eq!(value_data, values.to_data());
705        assert_eq!(DataType::Int32, list_array.value_type());
706        assert_eq!(3, list_array.len());
707        assert_eq!(0, list_array.null_count());
708        assert_eq!(6, list_array.value_offsets()[2]);
709        assert_eq!(2, list_array.value_sizes()[2]);
710        assert_eq!(2, list_array.value_size(2));
711        assert_eq!(0, list_array.value(0).as_primitive::<Int32Type>().value(0));
712        assert_eq!(
713            0,
714            unsafe { list_array.value_unchecked(0) }
715                .as_primitive::<Int32Type>()
716                .value(0)
717        );
718        for i in 0..3 {
719            assert!(list_array.is_valid(i));
720            assert!(!list_array.is_null(i));
721        }
722    }
723
724    #[test]
725    fn test_list_view_array_slice() {
726        // Construct a value array
727        let value_data = ArrayData::builder(DataType::Int32)
728            .len(10)
729            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
730            .build()
731            .unwrap();
732
733        // 01011001 00000001
734        let mut null_bits: [u8; 2] = [0; 2];
735        bit_util::set_bit(&mut null_bits, 0);
736        bit_util::set_bit(&mut null_bits, 3);
737        bit_util::set_bit(&mut null_bits, 4);
738        bit_util::set_bit(&mut null_bits, 6);
739        bit_util::set_bit(&mut null_bits, 8);
740        let buffer = BooleanBuffer::new(Buffer::from(null_bits), 0, 9);
741        let null_buffer = NullBuffer::new(buffer);
742
743        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
744        let sizes = ScalarBuffer::from(vec![2, 0, 0, 2, 2, 0, 3, 0, 1]);
745        let offsets = ScalarBuffer::from(vec![0, 2, 2, 2, 4, 6, 6, 9, 9]);
746        let values = Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
747        let list_array =
748            ListViewArray::new(field, offsets, sizes, Arc::new(values), Some(null_buffer));
749
750        let values = list_array.values();
751        assert_eq!(value_data, values.to_data());
752        assert_eq!(DataType::Int32, list_array.value_type());
753        assert_eq!(9, list_array.len());
754        assert_eq!(4, list_array.null_count());
755        assert_eq!(2, list_array.value_offsets()[3]);
756        assert_eq!(2, list_array.value_sizes()[3]);
757        assert_eq!(2, list_array.value_size(3));
758
759        let sliced_array = list_array.slice(1, 6);
760        assert_eq!(6, sliced_array.len());
761        assert_eq!(3, sliced_array.null_count());
762
763        for i in 0..sliced_array.len() {
764            if bit_util::get_bit(&null_bits, 1 + i) {
765                assert!(sliced_array.is_valid(i));
766            } else {
767                assert!(sliced_array.is_null(i));
768            }
769        }
770
771        // Check offset and length for each non-null value.
772        let sliced_list_array = sliced_array
773            .as_any()
774            .downcast_ref::<ListViewArray>()
775            .unwrap();
776        assert_eq!(2, sliced_list_array.value_offsets()[2]);
777        assert_eq!(2, sliced_list_array.value_sizes()[2]);
778        assert_eq!(2, sliced_list_array.value_size(2));
779
780        assert_eq!(4, sliced_list_array.value_offsets()[3]);
781        assert_eq!(2, sliced_list_array.value_sizes()[3]);
782        assert_eq!(2, sliced_list_array.value_size(3));
783
784        assert_eq!(6, sliced_list_array.value_offsets()[5]);
785        assert_eq!(3, sliced_list_array.value_sizes()[5]);
786        assert_eq!(3, sliced_list_array.value_size(5));
787    }
788
789    #[test]
790    fn test_large_list_view_array_slice() {
791        // Construct a value array
792        let value_data = ArrayData::builder(DataType::Int32)
793            .len(10)
794            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
795            .build()
796            .unwrap();
797
798        // 01011001 00000001
799        let mut null_bits: [u8; 2] = [0; 2];
800        bit_util::set_bit(&mut null_bits, 0);
801        bit_util::set_bit(&mut null_bits, 3);
802        bit_util::set_bit(&mut null_bits, 4);
803        bit_util::set_bit(&mut null_bits, 6);
804        bit_util::set_bit(&mut null_bits, 8);
805        let buffer = BooleanBuffer::new(Buffer::from(null_bits), 0, 9);
806        let null_buffer = NullBuffer::new(buffer);
807
808        // Construct a large list view array from the above two
809        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
810        let sizes = ScalarBuffer::from(vec![2i64, 0, 0, 2, 2, 0, 3, 0, 1]);
811        let offsets = ScalarBuffer::from(vec![0i64, 2, 2, 2, 4, 6, 6, 9, 9]);
812        let values = Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
813        let list_array =
814            LargeListViewArray::new(field, offsets, sizes, Arc::new(values), Some(null_buffer));
815
816        let values = list_array.values();
817        assert_eq!(value_data, values.to_data());
818        assert_eq!(DataType::Int32, list_array.value_type());
819        assert_eq!(9, list_array.len());
820        assert_eq!(4, list_array.null_count());
821        assert_eq!(2, list_array.value_offsets()[3]);
822        assert_eq!(2, list_array.value_sizes()[3]);
823        assert_eq!(2, list_array.value_size(3));
824
825        let sliced_array = list_array.slice(1, 6);
826        assert_eq!(6, sliced_array.len());
827        assert_eq!(3, sliced_array.null_count());
828
829        for i in 0..sliced_array.len() {
830            if bit_util::get_bit(&null_bits, 1 + i) {
831                assert!(sliced_array.is_valid(i));
832            } else {
833                assert!(sliced_array.is_null(i));
834            }
835        }
836
837        // Check offset and length for each non-null value.
838        let sliced_list_array = sliced_array
839            .as_any()
840            .downcast_ref::<LargeListViewArray>()
841            .unwrap();
842        assert_eq!(2, sliced_list_array.value_offsets()[2]);
843        assert_eq!(2, sliced_list_array.value_size(2));
844        assert_eq!(2, sliced_list_array.value_sizes()[2]);
845
846        assert_eq!(4, sliced_list_array.value_offsets()[3]);
847        assert_eq!(2, sliced_list_array.value_size(3));
848        assert_eq!(2, sliced_list_array.value_sizes()[3]);
849
850        assert_eq!(6, sliced_list_array.value_offsets()[5]);
851        assert_eq!(3, sliced_list_array.value_size(5));
852        assert_eq!(2, sliced_list_array.value_sizes()[3]);
853    }
854
855    #[test]
856    #[should_panic(expected = "index out of bounds: the len is 9 but the index is 10")]
857    fn test_list_view_array_index_out_of_bound() {
858        // 01011001 00000001
859        let mut null_bits: [u8; 2] = [0; 2];
860        bit_util::set_bit(&mut null_bits, 0);
861        bit_util::set_bit(&mut null_bits, 3);
862        bit_util::set_bit(&mut null_bits, 4);
863        bit_util::set_bit(&mut null_bits, 6);
864        bit_util::set_bit(&mut null_bits, 8);
865        let buffer = BooleanBuffer::new(Buffer::from(null_bits), 0, 9);
866        let null_buffer = NullBuffer::new(buffer);
867
868        // Construct a buffer for value offsets, for the nested array:
869        //  [[0, 1], null, null, [2, 3], [4, 5], null, [6, 7, 8], null, [9]]
870        // Construct a list array from the above two
871        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
872        let sizes = ScalarBuffer::from(vec![2i32, 0, 0, 2, 2, 0, 3, 0, 1]);
873        let offsets = ScalarBuffer::from(vec![0i32, 2, 2, 2, 4, 6, 6, 9, 9]);
874        let values = Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
875        let list_array =
876            ListViewArray::new(field, offsets, sizes, Arc::new(values), Some(null_buffer));
877
878        assert_eq!(9, list_array.len());
879        list_array.value(10);
880    }
881    #[test]
882    #[should_panic(
883        expected = "ListViewArray data should contain two buffers (value offsets & value sizes), had 0"
884    )]
885    #[cfg(not(feature = "force_validate"))]
886    fn test_list_view_array_invalid_buffer_len() {
887        let value_data = unsafe {
888            ArrayData::builder(DataType::Int32)
889                .len(8)
890                .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
891                .build_unchecked()
892        };
893        let list_data_type =
894            DataType::ListView(Arc::new(Field::new_list_field(DataType::Int32, false)));
895        let list_data = unsafe {
896            ArrayData::builder(list_data_type)
897                .len(3)
898                .add_child_data(value_data)
899                .build_unchecked()
900        };
901        drop(ListViewArray::from(list_data));
902    }
903
904    #[test]
905    #[should_panic(
906        expected = "ListViewArray data should contain two buffers (value offsets & value sizes), had 1"
907    )]
908    #[cfg(not(feature = "force_validate"))]
909    fn test_list_view_array_invalid_child_array_len() {
910        let value_offsets = Buffer::from_slice_ref([0, 2, 5, 7]);
911        let list_data_type =
912            DataType::ListView(Arc::new(Field::new_list_field(DataType::Int32, false)));
913        let list_data = unsafe {
914            ArrayData::builder(list_data_type)
915                .len(3)
916                .add_buffer(value_offsets)
917                .build_unchecked()
918        };
919        drop(ListViewArray::from(list_data));
920    }
921
922    #[test]
923    fn test_list_view_array_offsets_need_not_start_at_zero() {
924        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
925        let sizes = ScalarBuffer::from(vec![0i32, 0, 3]);
926        let offsets = ScalarBuffer::from(vec![2i32, 2, 5]);
927        let values = Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7]);
928        let list_array = ListViewArray::new(field, offsets, sizes, Arc::new(values), None);
929
930        assert_eq!(list_array.value_size(0), 0);
931        assert_eq!(list_array.value_size(1), 0);
932        assert_eq!(list_array.value_size(2), 3);
933    }
934
935    #[test]
936    #[should_panic(expected = "Memory pointer is not aligned with the specified scalar type")]
937    #[cfg(not(feature = "force_validate"))]
938    fn test_list_view_array_alignment() {
939        let offset_buf = Buffer::from_slice_ref([0_u64]);
940        let offset_buf2 = offset_buf.slice(1);
941
942        let size_buf = Buffer::from_slice_ref([0_u64]);
943        let size_buf2 = size_buf.slice(1);
944
945        let values: [i32; 8] = [0; 8];
946        let value_data = unsafe {
947            ArrayData::builder(DataType::Int32)
948                .add_buffer(Buffer::from_slice_ref(values))
949                .build_unchecked()
950        };
951
952        let list_data_type =
953            DataType::ListView(Arc::new(Field::new_list_field(DataType::Int32, false)));
954        let list_data = unsafe {
955            ArrayData::builder(list_data_type)
956                .add_buffer(offset_buf2)
957                .add_buffer(size_buf2)
958                .add_child_data(value_data)
959                .build_unchecked()
960        };
961        drop(ListViewArray::from(list_data));
962    }
963
964    #[test]
965    fn test_empty_offsets() {
966        let f = Arc::new(Field::new("element", DataType::Int32, true));
967        let string = ListViewArray::from(
968            ArrayData::builder(DataType::ListView(f.clone()))
969                .buffers(vec![Buffer::from(&[]), Buffer::from(&[])])
970                .add_child_data(ArrayData::new_empty(&DataType::Int32))
971                .build()
972                .unwrap(),
973        );
974        assert_eq!(string.value_offsets(), &[] as &[i32; 0]);
975        assert_eq!(string.value_sizes(), &[] as &[i32; 0]);
976
977        let string = LargeListViewArray::from(
978            ArrayData::builder(DataType::LargeListView(f))
979                .buffers(vec![Buffer::from(&[]), Buffer::from(&[])])
980                .add_child_data(ArrayData::new_empty(&DataType::Int32))
981                .build()
982                .unwrap(),
983        );
984        assert_eq!(string.len(), 0);
985        assert_eq!(string.value_offsets(), &[] as &[i64; 0]);
986        assert_eq!(string.value_sizes(), &[] as &[i64; 0]);
987    }
988
989    #[test]
990    fn test_try_new() {
991        let offsets = ScalarBuffer::from(vec![0, 1, 4, 5]);
992        let sizes = ScalarBuffer::from(vec![1, 3, 1, 0]);
993        let values = Int32Array::new(vec![1, 2, 3, 4, 5].into(), None);
994        let values = Arc::new(values) as ArrayRef;
995
996        let field = Arc::new(Field::new("element", DataType::Int32, false));
997        ListViewArray::new(
998            field.clone(),
999            offsets.clone(),
1000            sizes.clone(),
1001            values.clone(),
1002            None,
1003        );
1004
1005        let nulls = NullBuffer::new_null(4);
1006        ListViewArray::new(
1007            field.clone(),
1008            offsets,
1009            sizes.clone(),
1010            values.clone(),
1011            Some(nulls),
1012        );
1013
1014        let nulls = NullBuffer::new_null(4);
1015        let offsets = ScalarBuffer::from(vec![0, 1, 2, 3, 4]);
1016        let sizes = ScalarBuffer::from(vec![1, 1, 1, 1, 0]);
1017        let err = LargeListViewArray::try_new(
1018            field,
1019            offsets.clone(),
1020            sizes.clone(),
1021            values.clone(),
1022            Some(nulls),
1023        )
1024        .unwrap_err();
1025
1026        assert_eq!(
1027            err.to_string(),
1028            "Invalid argument error: Incorrect length of null buffer for LargeListViewArray, expected 5 got 4"
1029        );
1030
1031        let field = Arc::new(Field::new("element", DataType::Int64, false));
1032        let err = LargeListViewArray::try_new(
1033            field.clone(),
1034            offsets.clone(),
1035            sizes.clone(),
1036            values.clone(),
1037            None,
1038        )
1039        .unwrap_err();
1040
1041        assert_eq!(
1042            err.to_string(),
1043            "Invalid argument error: LargeListViewArray expected data type Int64 got Int32 for \"element\""
1044        );
1045
1046        let nulls = NullBuffer::new_null(7);
1047        let values = Int64Array::new(vec![0; 7].into(), Some(nulls));
1048        let values = Arc::new(values);
1049
1050        let err = LargeListViewArray::try_new(
1051            field,
1052            offsets.clone(),
1053            sizes.clone(),
1054            values.clone(),
1055            None,
1056        )
1057        .unwrap_err();
1058
1059        assert_eq!(
1060            err.to_string(),
1061            "Invalid argument error: Non-nullable field of LargeListViewArray \"element\" cannot contain nulls"
1062        );
1063    }
1064
1065    #[test]
1066    fn test_from_fixed_size_list() {
1067        let mut builder = FixedSizeListBuilder::new(Int32Builder::new(), 3);
1068        builder.values().append_slice(&[1, 2, 3]);
1069        builder.append(true);
1070        builder.values().append_slice(&[0, 0, 0]);
1071        builder.append(false);
1072        builder.values().append_slice(&[4, 5, 6]);
1073        builder.append(true);
1074        let list: ListViewArray = builder.finish().into();
1075        let values: Vec<_> = list
1076            .iter()
1077            .map(|x| x.map(|x| x.as_primitive::<Int32Type>().values().to_vec()))
1078            .collect();
1079        assert_eq!(values, vec![Some(vec![1, 2, 3]), None, Some(vec![4, 5, 6])]);
1080        let offsets = list.value_offsets();
1081        assert_eq!(offsets, &[0, 3, 6]);
1082        let sizes = list.value_sizes();
1083        assert_eq!(sizes, &[3, 3, 3]);
1084    }
1085
1086    #[test]
1087    fn test_list_view_array_overlap_lists() {
1088        let value_data = unsafe {
1089            ArrayData::builder(DataType::Int32)
1090                .len(8)
1091                .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
1092                .build_unchecked()
1093        };
1094        let list_data_type =
1095            DataType::ListView(Arc::new(Field::new_list_field(DataType::Int32, false)));
1096        let list_data = unsafe {
1097            ArrayData::builder(list_data_type)
1098                .len(2)
1099                .add_buffer(Buffer::from_slice_ref([0, 3])) // offsets
1100                .add_buffer(Buffer::from_slice_ref([5, 5])) // sizes
1101                .add_child_data(value_data)
1102                .build_unchecked()
1103        };
1104        let array = ListViewArray::from(list_data);
1105
1106        assert_eq!(array.len(), 2);
1107        assert_eq!(array.value_size(0), 5);
1108        assert_eq!(array.value_size(1), 5);
1109
1110        let values: Vec<_> = array
1111            .iter()
1112            .map(|x| x.map(|x| x.as_primitive::<Int32Type>().values().to_vec()))
1113            .collect();
1114        assert_eq!(
1115            values,
1116            vec![Some(vec![0, 1, 2, 3, 4]), Some(vec![3, 4, 5, 6, 7])]
1117        );
1118    }
1119
1120    #[test]
1121    fn test_list_view_array_incomplete_offsets() {
1122        let value_data = unsafe {
1123            ArrayData::builder(DataType::Int32)
1124                .len(50)
1125                .add_buffer(Buffer::from_slice_ref((0..50).collect::<Vec<i32>>()))
1126                .build_unchecked()
1127        };
1128        let list_data_type =
1129            DataType::ListView(Arc::new(Field::new_list_field(DataType::Int32, false)));
1130        let list_data = unsafe {
1131            ArrayData::builder(list_data_type)
1132                .len(3)
1133                .add_buffer(Buffer::from_slice_ref([0, 5, 10])) // offsets
1134                .add_buffer(Buffer::from_slice_ref([0, 5, 10])) // sizes
1135                .add_child_data(value_data)
1136                .build_unchecked()
1137        };
1138        let array = ListViewArray::from(list_data);
1139
1140        assert_eq!(array.len(), 3);
1141        assert_eq!(array.value_size(0), 0);
1142        assert_eq!(array.value_size(1), 5);
1143        assert_eq!(array.value_size(2), 10);
1144
1145        let values: Vec<_> = array
1146            .iter()
1147            .map(|x| x.map(|x| x.as_primitive::<Int32Type>().values().to_vec()))
1148            .collect();
1149        assert_eq!(
1150            values,
1151            vec![
1152                Some(vec![]),
1153                Some(vec![5, 6, 7, 8, 9]),
1154                Some(vec![10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
1155            ]
1156        );
1157    }
1158
1159    #[test]
1160    fn test_list_view_array_empty_lists() {
1161        let value_data = unsafe {
1162            ArrayData::builder(DataType::Int32)
1163                .len(0)
1164                .add_buffer(Buffer::from_slice_ref::<i32, &[_; 0]>(&[]))
1165                .build_unchecked()
1166        };
1167        let list_data_type =
1168            DataType::ListView(Arc::new(Field::new_list_field(DataType::Int32, false)));
1169        let list_data = unsafe {
1170            ArrayData::builder(list_data_type)
1171                .len(3)
1172                .add_buffer(Buffer::from_slice_ref([0, 0, 0])) // offsets
1173                .add_buffer(Buffer::from_slice_ref([0, 0, 0])) // sizes
1174                .add_child_data(value_data)
1175                .build_unchecked()
1176        };
1177        let array = ListViewArray::from(list_data);
1178
1179        assert_eq!(array.len(), 3);
1180        assert_eq!(array.value_size(0), 0);
1181        assert_eq!(array.value_size(1), 0);
1182        assert_eq!(array.value_size(2), 0);
1183
1184        let values: Vec<_> = array
1185            .iter()
1186            .map(|x| x.map(|x| x.as_primitive::<Int32Type>().values().to_vec()))
1187            .collect();
1188        assert_eq!(values, vec![Some(vec![]), Some(vec![]), Some(vec![])]);
1189    }
1190
1191    #[test]
1192    fn test_list_view_new_null_len() {
1193        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
1194        let array = ListViewArray::new_null(field, 5);
1195        assert_eq!(array.len(), 5);
1196    }
1197
1198    #[test]
1199    fn test_from_iter_primitive() {
1200        let data = vec![
1201            Some(vec![Some(0), Some(1), Some(2)]),
1202            None,
1203            Some(vec![Some(3), Some(4), Some(5)]),
1204            Some(vec![Some(6), Some(7)]),
1205        ];
1206        let list_array = ListViewArray::from_iter_primitive::<Int32Type, _, _>(data);
1207
1208        //  [[0, 1, 2], NULL, [3, 4, 5], [6, 7]]
1209        let values = Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7]);
1210        let offsets = ScalarBuffer::from(vec![0, 3, 3, 6]);
1211        let sizes = ScalarBuffer::from(vec![3, 0, 3, 2]);
1212        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
1213
1214        let mut nulls = NullBufferBuilder::new(4);
1215        nulls.append(true);
1216        nulls.append(false);
1217        nulls.append_n_non_nulls(2);
1218        let another = ListViewArray::new(field, offsets, sizes, Arc::new(values), nulls.finish());
1219
1220        assert_eq!(list_array, another)
1221    }
1222}