tustvold commented on code in PR #5576:
URL: https://github.com/apache/arrow-rs/pull/5576#discussion_r1565528767


##########
arrow-array/src/array/list_view_array.rs:
##########
@@ -0,0 +1,1256 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::{get_view_offsets, get_view_sizes, make_array, 
print_long_array};
+use crate::builder::{GenericListViewBuilder, PrimitiveBuilder};
+use crate::iterator::GenericListViewArrayIter;
+use crate::{
+    new_empty_array, Array, ArrayAccessor, ArrayRef, ArrowPrimitiveType, 
FixedSizeListArray,
+    OffsetSizeTrait,
+};
+use arrow_buffer::{NullBuffer, ScalarBuffer};
+use arrow_data::{ArrayData, ArrayDataBuilder};
+use arrow_schema::{ArrowError, DataType, FieldRef};
+use std::any::Any;
+use std::ops::Add;
+use std::sync::Arc;
+
+/// A [`GenericListViewArray`] of variable size lists, storing offsets as 
`i32`.
+///
+// See [`ListViewBuilder`](crate::builder::ListViewBuilder) for how to 
construct a [`ListViewArray`]
+pub type ListViewArray = GenericListViewArray<i32>;
+
+/// A [`GenericListViewArray`] of variable size lists, storing offsets as 
`i64`.
+///
+// See [`LargeListViewArray`](crate::builder::LargeListViewBuilder) for how to 
construct a [`LargeListViewArray`]
+pub type LargeListViewArray = GenericListViewArray<i64>;
+
+///
+/// Different than [`crate::GenericListArray`] as it stores both an offset and 
length
+/// meaning that take / filter operations can be implemented without copying 
the underlying data.
+///
+/// [Variable-size List Layout: ListView Layout]: 
https://arrow.apache.org/docs/format/Columnar.html#listview-layout
+pub struct GenericListViewArray<OffsetSize: OffsetSizeTrait> {
+    data_type: DataType,
+    nulls: Option<NullBuffer>,
+    values: ArrayRef,
+    value_offsets: ScalarBuffer<OffsetSize>,
+    value_sizes: ScalarBuffer<OffsetSize>,
+    len: usize,
+}
+
+impl<OffsetSize: OffsetSizeTrait> Clone for GenericListViewArray<OffsetSize> {
+    fn clone(&self) -> Self {
+        Self {
+            data_type: self.data_type.clone(),
+            nulls: self.nulls.clone(),
+            values: self.values.clone(),
+            value_offsets: self.value_offsets.clone(),
+            value_sizes: self.value_sizes.clone(),
+            len: self.len,
+        }
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait> GenericListViewArray<OffsetSize> {
+    /// The data type constructor of listview array.
+    /// The input is the schema of the child array and
+    /// the output is the [`DataType`], ListView or LargeListView.
+    pub const DATA_TYPE_CONSTRUCTOR: fn(FieldRef) -> DataType = if 
OffsetSize::IS_LARGE {
+        DataType::LargeListView
+    } else {
+        DataType::ListView
+    };
+
+    /// Create a new [`GenericListViewArray`] from the provided parts
+    ///
+    /// # Errors
+    ///
+    /// Errors if
+    ///
+    /// * `offsets.len() != sizes.len()`
+    /// * `offsets.len() != nulls.len()`
+    /// * `offsets.last() > values.len()`
+    /// * `!field.is_nullable() && values.is_nullable()`
+    /// * `field.data_type() != values.data_type()`
+    /// * `0 <= offsets[i] <= length of the child array`
+    /// * `0 <= offsets[i] + size[i] <= length of the child array`
+    pub fn try_new(
+        field: FieldRef,
+        offsets: ScalarBuffer<OffsetSize>,
+        sizes: ScalarBuffer<OffsetSize>,
+        values: ArrayRef,
+        nulls: Option<NullBuffer>,
+    ) -> Result<Self, ArrowError> {
+        for i in 0..offsets.len() {
+            let length = values.len();
+            let offset = offsets[i].as_usize();
+            let size = sizes[i].as_usize();
+            if offset > length {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Invalid offset value for {}ListViewArray, offset: {}, 
length: {}",
+                    OffsetSize::PREFIX,
+                    offset,
+                    length
+                )));
+            }
+            if offset + size > values.len() {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Invalid offset and size values for {}ListViewArray, 
offset: {}, size: {}, values.len(): {}",
+                    OffsetSize::PREFIX, offset, size, values.len()
+                )));
+            }
+        }
+
+        let len = offsets.len();
+        if let Some(n) = nulls.as_ref() {
+            if n.len() != len {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Incorrect length of null buffer for {}ListViewArray, 
expected {len} got {}",
+                    OffsetSize::PREFIX,
+                    n.len(),
+                )));
+            }
+        }
+        if len != sizes.len() {

Review Comment:
   I think this test should be moved ahead of the iteration above



##########
arrow-array/src/array/list_view_array.rs:
##########
@@ -0,0 +1,1256 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::{get_view_offsets, get_view_sizes, make_array, 
print_long_array};
+use crate::builder::{GenericListViewBuilder, PrimitiveBuilder};
+use crate::iterator::GenericListViewArrayIter;
+use crate::{
+    new_empty_array, Array, ArrayAccessor, ArrayRef, ArrowPrimitiveType, 
FixedSizeListArray,
+    OffsetSizeTrait,
+};
+use arrow_buffer::{NullBuffer, ScalarBuffer};
+use arrow_data::{ArrayData, ArrayDataBuilder};
+use arrow_schema::{ArrowError, DataType, FieldRef};
+use std::any::Any;
+use std::ops::Add;
+use std::sync::Arc;
+
+/// A [`GenericListViewArray`] of variable size lists, storing offsets as 
`i32`.
+///
+// See [`ListViewBuilder`](crate::builder::ListViewBuilder) for how to 
construct a [`ListViewArray`]
+pub type ListViewArray = GenericListViewArray<i32>;
+
+/// A [`GenericListViewArray`] of variable size lists, storing offsets as 
`i64`.
+///
+// See [`LargeListViewArray`](crate::builder::LargeListViewBuilder) for how to 
construct a [`LargeListViewArray`]
+pub type LargeListViewArray = GenericListViewArray<i64>;
+
+///
+/// Different than [`crate::GenericListArray`] as it stores both an offset and 
length
+/// meaning that take / filter operations can be implemented without copying 
the underlying data.
+///
+/// [Variable-size List Layout: ListView Layout]: 
https://arrow.apache.org/docs/format/Columnar.html#listview-layout
+pub struct GenericListViewArray<OffsetSize: OffsetSizeTrait> {
+    data_type: DataType,
+    nulls: Option<NullBuffer>,
+    values: ArrayRef,
+    value_offsets: ScalarBuffer<OffsetSize>,
+    value_sizes: ScalarBuffer<OffsetSize>,
+    len: usize,
+}
+
+impl<OffsetSize: OffsetSizeTrait> Clone for GenericListViewArray<OffsetSize> {
+    fn clone(&self) -> Self {
+        Self {
+            data_type: self.data_type.clone(),
+            nulls: self.nulls.clone(),
+            values: self.values.clone(),
+            value_offsets: self.value_offsets.clone(),
+            value_sizes: self.value_sizes.clone(),
+            len: self.len,
+        }
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait> GenericListViewArray<OffsetSize> {
+    /// The data type constructor of listview array.
+    /// The input is the schema of the child array and
+    /// the output is the [`DataType`], ListView or LargeListView.
+    pub const DATA_TYPE_CONSTRUCTOR: fn(FieldRef) -> DataType = if 
OffsetSize::IS_LARGE {
+        DataType::LargeListView
+    } else {
+        DataType::ListView
+    };
+
+    /// Create a new [`GenericListViewArray`] from the provided parts
+    ///
+    /// # Errors
+    ///
+    /// Errors if
+    ///
+    /// * `offsets.len() != sizes.len()`
+    /// * `offsets.len() != nulls.len()`
+    /// * `offsets.last() > values.len()`
+    /// * `!field.is_nullable() && values.is_nullable()`
+    /// * `field.data_type() != values.data_type()`
+    /// * `0 <= offsets[i] <= length of the child array`
+    /// * `0 <= offsets[i] + size[i] <= length of the child array`
+    pub fn try_new(
+        field: FieldRef,
+        offsets: ScalarBuffer<OffsetSize>,
+        sizes: ScalarBuffer<OffsetSize>,
+        values: ArrayRef,
+        nulls: Option<NullBuffer>,
+    ) -> Result<Self, ArrowError> {
+        for i in 0..offsets.len() {
+            let length = values.len();
+            let offset = offsets[i].as_usize();
+            let size = sizes[i].as_usize();
+            if offset > length {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Invalid offset value for {}ListViewArray, offset: {}, 
length: {}",
+                    OffsetSize::PREFIX,
+                    offset,
+                    length
+                )));
+            }
+            if offset + size > values.len() {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Invalid offset and size values for {}ListViewArray, 
offset: {}, size: {}, values.len(): {}",
+                    OffsetSize::PREFIX, offset, size, values.len()
+                )));
+            }
+        }
+
+        let len = offsets.len();
+        if let Some(n) = nulls.as_ref() {
+            if n.len() != len {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Incorrect length of null buffer for {}ListViewArray, 
expected {len} got {}",
+                    OffsetSize::PREFIX,
+                    n.len(),
+                )));
+            }
+        }
+        if len != sizes.len() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "Length of offsets buffer and sizes buffer must be equal for 
{}ListViewArray, got {} and {}",
+                OffsetSize::PREFIX, len, sizes.len()
+            )));
+        }
+
+        if !field.is_nullable() && values.is_nullable() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "Non-nullable field of {}ListViewArray {:?} cannot contain 
nulls",
+                OffsetSize::PREFIX,
+                field.name()
+            )));
+        }
+
+        if field.data_type() != values.data_type() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "{}ListViewArray expected data type {} got {} for {:?}",
+                OffsetSize::PREFIX,
+                field.data_type(),
+                values.data_type(),
+                field.name()
+            )));
+        }
+
+        Ok(Self {
+            data_type: Self::DATA_TYPE_CONSTRUCTOR(field),
+            nulls,
+            values,
+            value_offsets: offsets,
+            value_sizes: sizes,
+            len,
+        })
+    }
+
+    /// Create a new [`GenericListViewArray`] from the provided parts
+    ///
+    /// # Panics
+    ///
+    /// Panics if [`Self::try_new`] returns an error
+    pub fn new(
+        field: FieldRef,
+        offsets: ScalarBuffer<OffsetSize>,
+        sizes: ScalarBuffer<OffsetSize>,
+        values: ArrayRef,
+        nulls: Option<NullBuffer>,
+    ) -> Self {
+        Self::try_new(field, offsets, sizes, values, nulls).unwrap()
+    }
+
+    /// Create a new [`GenericListViewArray`] of length `len` where all values 
are null
+    pub fn new_null(field: FieldRef, len: usize) -> Self {
+        let values = new_empty_array(field.data_type());
+        Self {
+            data_type: Self::DATA_TYPE_CONSTRUCTOR(field),
+            nulls: Some(NullBuffer::new_null(len)),
+            value_offsets: ScalarBuffer::new_zeroed(len),
+            value_sizes: ScalarBuffer::new_zeroed(len),
+            values,
+            len: 0usize,
+        }
+    }
+
+    /// Deconstruct this array into its constituent parts
+    pub fn into_parts(
+        self,
+    ) -> (
+        FieldRef,
+        ScalarBuffer<OffsetSize>,
+        ScalarBuffer<OffsetSize>,
+        ArrayRef,
+        Option<NullBuffer>,
+    ) {
+        let f = match self.data_type {
+            DataType::ListView(f) | DataType::LargeListView(f) => f,
+            _ => unreachable!(),
+        };
+        (
+            f,
+            self.value_offsets,
+            self.value_sizes,
+            self.values,
+            self.nulls,
+        )
+    }
+
+    /// Returns a reference to the offsets of this list
+    ///
+    /// Unlike [`Self::value_offsets`] this returns the [`ScalarBuffer`]
+    /// allowing for zero-copy cloning
+    #[inline]
+    pub fn offsets(&self) -> &ScalarBuffer<OffsetSize> {
+        &self.value_offsets
+    }
+
+    /// Returns a reference to the values of this list
+    #[inline]
+    pub fn values(&self) -> &ArrayRef {
+        &self.values
+    }
+
+    /// Returns a reference to the sizes of this list
+    ///
+    /// Unlike [`Self::value_sizes`] this returns the [`ScalarBuffer`]
+    /// allowing for zero-copy cloning
+    #[inline]
+    pub fn sizes(&self) -> &ScalarBuffer<OffsetSize> {
+        &self.value_sizes
+    }
+
+    /// Returns a clone of the value type of this list.
+    pub fn value_type(&self) -> DataType {
+        self.values.data_type().clone()
+    }
+
+    /// Returns ith value of this list view array.
+    /// # Safety
+    /// Caller must ensure that the index is within the array bounds
+    pub unsafe fn value_unchecked(&self, i: usize) -> ArrayRef {

Review Comment:
   This method does not appear to be correct (and could probably do with a test)



##########
arrow-data/src/data.rs:
##########
@@ -856,6 +860,17 @@ impl ArrayData {
         self.typed_buffer(0, self.len + 1)
     }
 
+    /// Returns a reference to the data in `buffer` as a typed slice
+    /// after validating. The returned slice is guaranteed to have at
+    /// least `len` entries.
+    fn typed_sizes<T: ArrowNativeType + num::Num>(&self) -> Result<&[T], 
ArrowError> {

Review Comment:
   This method isn't necessary



##########
arrow-array/src/array/list_view_array.rs:
##########
@@ -0,0 +1,1256 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::{get_view_offsets, get_view_sizes, make_array, 
print_long_array};
+use crate::builder::{GenericListViewBuilder, PrimitiveBuilder};
+use crate::iterator::GenericListViewArrayIter;
+use crate::{
+    new_empty_array, Array, ArrayAccessor, ArrayRef, ArrowPrimitiveType, 
FixedSizeListArray,
+    OffsetSizeTrait,
+};
+use arrow_buffer::{NullBuffer, ScalarBuffer};
+use arrow_data::{ArrayData, ArrayDataBuilder};
+use arrow_schema::{ArrowError, DataType, FieldRef};
+use std::any::Any;
+use std::ops::Add;
+use std::sync::Arc;
+
+/// A [`GenericListViewArray`] of variable size lists, storing offsets as 
`i32`.
+///
+// See [`ListViewBuilder`](crate::builder::ListViewBuilder) for how to 
construct a [`ListViewArray`]
+pub type ListViewArray = GenericListViewArray<i32>;
+
+/// A [`GenericListViewArray`] of variable size lists, storing offsets as 
`i64`.
+///
+// See [`LargeListViewArray`](crate::builder::LargeListViewBuilder) for how to 
construct a [`LargeListViewArray`]
+pub type LargeListViewArray = GenericListViewArray<i64>;
+
+///
+/// Different than [`crate::GenericListArray`] as it stores both an offset and 
length
+/// meaning that take / filter operations can be implemented without copying 
the underlying data.
+///
+/// [Variable-size List Layout: ListView Layout]: 
https://arrow.apache.org/docs/format/Columnar.html#listview-layout
+pub struct GenericListViewArray<OffsetSize: OffsetSizeTrait> {
+    data_type: DataType,
+    nulls: Option<NullBuffer>,
+    values: ArrayRef,
+    value_offsets: ScalarBuffer<OffsetSize>,
+    value_sizes: ScalarBuffer<OffsetSize>,
+    len: usize,
+}
+
+impl<OffsetSize: OffsetSizeTrait> Clone for GenericListViewArray<OffsetSize> {
+    fn clone(&self) -> Self {
+        Self {
+            data_type: self.data_type.clone(),
+            nulls: self.nulls.clone(),
+            values: self.values.clone(),
+            value_offsets: self.value_offsets.clone(),
+            value_sizes: self.value_sizes.clone(),
+            len: self.len,
+        }
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait> GenericListViewArray<OffsetSize> {
+    /// The data type constructor of listview array.
+    /// The input is the schema of the child array and
+    /// the output is the [`DataType`], ListView or LargeListView.
+    pub const DATA_TYPE_CONSTRUCTOR: fn(FieldRef) -> DataType = if 
OffsetSize::IS_LARGE {
+        DataType::LargeListView
+    } else {
+        DataType::ListView
+    };
+
+    /// Create a new [`GenericListViewArray`] from the provided parts
+    ///
+    /// # Errors
+    ///
+    /// Errors if
+    ///
+    /// * `offsets.len() != sizes.len()`
+    /// * `offsets.len() != nulls.len()`
+    /// * `offsets.last() > values.len()`
+    /// * `!field.is_nullable() && values.is_nullable()`
+    /// * `field.data_type() != values.data_type()`
+    /// * `0 <= offsets[i] <= length of the child array`
+    /// * `0 <= offsets[i] + size[i] <= length of the child array`
+    pub fn try_new(
+        field: FieldRef,
+        offsets: ScalarBuffer<OffsetSize>,
+        sizes: ScalarBuffer<OffsetSize>,
+        values: ArrayRef,
+        nulls: Option<NullBuffer>,
+    ) -> Result<Self, ArrowError> {
+        for i in 0..offsets.len() {
+            let length = values.len();
+            let offset = offsets[i].as_usize();
+            let size = sizes[i].as_usize();
+            if offset > length {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Invalid offset value for {}ListViewArray, offset: {}, 
length: {}",
+                    OffsetSize::PREFIX,
+                    offset,
+                    length
+                )));
+            }
+            if offset + size > values.len() {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Invalid offset and size values for {}ListViewArray, 
offset: {}, size: {}, values.len(): {}",
+                    OffsetSize::PREFIX, offset, size, values.len()
+                )));
+            }
+        }
+
+        let len = offsets.len();
+        if let Some(n) = nulls.as_ref() {
+            if n.len() != len {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Incorrect length of null buffer for {}ListViewArray, 
expected {len} got {}",
+                    OffsetSize::PREFIX,
+                    n.len(),
+                )));
+            }
+        }
+        if len != sizes.len() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "Length of offsets buffer and sizes buffer must be equal for 
{}ListViewArray, got {} and {}",
+                OffsetSize::PREFIX, len, sizes.len()
+            )));
+        }
+
+        if !field.is_nullable() && values.is_nullable() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "Non-nullable field of {}ListViewArray {:?} cannot contain 
nulls",
+                OffsetSize::PREFIX,
+                field.name()
+            )));
+        }
+
+        if field.data_type() != values.data_type() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "{}ListViewArray expected data type {} got {} for {:?}",
+                OffsetSize::PREFIX,
+                field.data_type(),
+                values.data_type(),
+                field.name()
+            )));
+        }
+
+        Ok(Self {
+            data_type: Self::DATA_TYPE_CONSTRUCTOR(field),
+            nulls,
+            values,
+            value_offsets: offsets,
+            value_sizes: sizes,
+            len,
+        })
+    }
+
+    /// Create a new [`GenericListViewArray`] from the provided parts
+    ///
+    /// # Panics
+    ///
+    /// Panics if [`Self::try_new`] returns an error
+    pub fn new(
+        field: FieldRef,
+        offsets: ScalarBuffer<OffsetSize>,
+        sizes: ScalarBuffer<OffsetSize>,
+        values: ArrayRef,
+        nulls: Option<NullBuffer>,
+    ) -> Self {
+        Self::try_new(field, offsets, sizes, values, nulls).unwrap()
+    }
+
+    /// Create a new [`GenericListViewArray`] of length `len` where all values 
are null
+    pub fn new_null(field: FieldRef, len: usize) -> Self {
+        let values = new_empty_array(field.data_type());
+        Self {
+            data_type: Self::DATA_TYPE_CONSTRUCTOR(field),
+            nulls: Some(NullBuffer::new_null(len)),
+            value_offsets: ScalarBuffer::new_zeroed(len),
+            value_sizes: ScalarBuffer::new_zeroed(len),
+            values,
+            len: 0usize,
+        }
+    }
+
+    /// Deconstruct this array into its constituent parts
+    pub fn into_parts(
+        self,
+    ) -> (
+        FieldRef,
+        ScalarBuffer<OffsetSize>,
+        ScalarBuffer<OffsetSize>,
+        ArrayRef,
+        Option<NullBuffer>,
+    ) {
+        let f = match self.data_type {
+            DataType::ListView(f) | DataType::LargeListView(f) => f,
+            _ => unreachable!(),
+        };
+        (
+            f,
+            self.value_offsets,
+            self.value_sizes,
+            self.values,
+            self.nulls,
+        )
+    }
+
+    /// Returns a reference to the offsets of this list
+    ///
+    /// Unlike [`Self::value_offsets`] this returns the [`ScalarBuffer`]
+    /// allowing for zero-copy cloning
+    #[inline]
+    pub fn offsets(&self) -> &ScalarBuffer<OffsetSize> {
+        &self.value_offsets
+    }
+
+    /// Returns a reference to the values of this list
+    #[inline]
+    pub fn values(&self) -> &ArrayRef {
+        &self.values
+    }
+
+    /// Returns a reference to the sizes of this list
+    ///
+    /// Unlike [`Self::value_sizes`] this returns the [`ScalarBuffer`]
+    /// allowing for zero-copy cloning
+    #[inline]
+    pub fn sizes(&self) -> &ScalarBuffer<OffsetSize> {
+        &self.value_sizes
+    }
+
+    /// Returns a clone of the value type of this list.
+    pub fn value_type(&self) -> DataType {
+        self.values.data_type().clone()
+    }
+
+    /// Returns ith value of this list view array.
+    /// # Safety
+    /// Caller must ensure that the index is within the array bounds
+    pub unsafe fn value_unchecked(&self, i: usize) -> ArrayRef {
+        let end = self.value_offsets().get_unchecked(i + 1).as_usize();
+        let start = self.value_offsets().get_unchecked(i).as_usize();
+        self.values.slice(start, end - start)
+    }
+
+    /// Returns ith value of this list view array.
+    pub fn value(&self, i: usize) -> ArrayRef {
+        let offset = self.value_offsets()[i].as_usize();
+        let length = self.value_sizes()[i].as_usize();
+        self.values.slice(offset, length)
+    }
+
+    /// Returns the offset values in the offsets buffer
+    #[inline]
+    pub fn value_offsets(&self) -> &[OffsetSize] {
+        &self.value_offsets
+    }
+
+    /// Returns the sizes values in the offsets buffer
+    #[inline]
+    pub fn value_sizes(&self) -> &[OffsetSize] {
+        &self.value_sizes
+    }
+
+    /// Returns the length for value at index `i`.
+    #[inline]
+    pub fn value_length(&self, i: usize) -> OffsetSize {
+        self.value_sizes[i]
+    }
+
+    /// constructs a new iterator
+    pub fn iter<'a>(&'a self) -> GenericListViewArrayIter<'a, OffsetSize> {
+        GenericListViewArrayIter::<'a, OffsetSize>::new(self)
+    }
+
+    #[inline]
+    fn get_type(data_type: &DataType) -> Option<&DataType> {
+        match (OffsetSize::IS_LARGE, data_type) {
+            (true, DataType::LargeListView(child)) | (false, 
DataType::ListView(child)) => {
+                Some(child.data_type())
+            }
+            _ => None,
+        }
+    }
+
+    /// Returns a zero-copy slice of this array with the indicated offset and 
length.
+    pub fn slice(&self, offset: usize, length: usize) -> Self {
+        Self {
+            data_type: self.data_type.clone(),
+            nulls: self.nulls.as_ref().map(|n| n.slice(offset, length)),
+            values: self.values.clone(),
+            value_offsets: self.value_offsets.slice(offset, length),
+            value_sizes: self.value_sizes.slice(offset, length),
+            len: length,
+        }
+    }
+
+    /// Creates a [`GenericListViewArray`] from an iterator of primitive values
+    ///
+    pub fn from_iter_primitive<T, P, I>(iter: I) -> Self
+    where
+        T: ArrowPrimitiveType,
+        P: IntoIterator<Item = Option<<T as ArrowPrimitiveType>::Native>>,
+        I: IntoIterator<Item = Option<P>>,
+    {
+        let iter = iter.into_iter();
+        let size_hint = iter.size_hint().0;
+        let mut builder =
+            
GenericListViewBuilder::with_capacity(PrimitiveBuilder::<T>::new(), size_hint);
+
+        for i in iter {
+            match i {
+                Some(p) => {
+                    let mut size = 0usize;
+                    for t in p {
+                        builder.values().append_option(t);
+                        size = size.add(1);
+                    }
+                    builder.append(true, size);
+                }
+                None => builder.append(false, 0),
+            }
+        }
+        builder.finish()
+    }
+}
+
+impl<'a, OffsetSize: OffsetSizeTrait> ArrayAccessor for &'a 
GenericListViewArray<OffsetSize> {
+    type Item = ArrayRef;
+
+    fn value(&self, index: usize) -> Self::Item {
+        GenericListViewArray::value(self, index)
+    }
+
+    unsafe fn value_unchecked(&self, index: usize) -> Self::Item {
+        GenericListViewArray::value(self, index)
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait> Array for GenericListViewArray<OffsetSize> {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn to_data(&self) -> ArrayData {
+        self.clone().into()
+    }
+
+    fn into_data(self) -> ArrayData {
+        self.into()
+    }
+
+    fn data_type(&self) -> &DataType {
+        &self.data_type
+    }
+
+    fn slice(&self, offset: usize, length: usize) -> ArrayRef {
+        Arc::new(self.slice(offset, length))
+    }
+
+    fn len(&self) -> usize {
+        self.len
+    }
+
+    fn is_empty(&self) -> bool {
+        self.value_offsets.len() <= 1
+    }
+
+    fn offset(&self) -> usize {
+        0
+    }
+
+    fn nulls(&self) -> Option<&NullBuffer> {
+        self.nulls.as_ref()
+    }
+
+    fn get_buffer_memory_size(&self) -> usize {
+        let mut size = self.values.get_buffer_memory_size();
+        size += self.value_offsets.inner().capacity();
+        if let Some(n) = self.nulls.as_ref() {
+            size += n.buffer().capacity();
+        }
+        size
+    }
+
+    fn get_array_memory_size(&self) -> usize {
+        let mut size = std::mem::size_of::<Self>() + 
self.values.get_array_memory_size();
+        size += self.value_offsets.inner().capacity();
+        if let Some(n) = self.nulls.as_ref() {
+            size += n.buffer().capacity();
+        }
+        size
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait> std::fmt::Debug for 
GenericListViewArray<OffsetSize> {
+    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+        let prefix = OffsetSize::PREFIX;
+        write!(f, "{prefix}ListViewArray\n[\n")?;
+        print_long_array(self, f, |array, index, f| {
+            std::fmt::Debug::fmt(&array.value(index), f)
+        })?;
+        write!(f, "]")
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait> From<GenericListViewArray<OffsetSize>> for 
ArrayData {
+    fn from(array: GenericListViewArray<OffsetSize>) -> Self {
+        let len = array.len();
+        let builder = ArrayDataBuilder::new(array.data_type)
+            .len(len)
+            .nulls(array.nulls)
+            .buffers(vec![
+                array.value_offsets.into_inner(),
+                array.value_sizes.into_inner(),
+            ])
+            .child_data(vec![array.values.to_data()]);
+
+        unsafe { builder.build_unchecked() }
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait> From<ArrayData> for 
GenericListViewArray<OffsetSize> {
+    fn from(data: ArrayData) -> Self {
+        Self::try_new_from_array_data(data)
+            .expect("Expected infallible creation of GenericListViewArray from 
ArrayDataRef failed")
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait> From<FixedSizeListArray> for 
GenericListViewArray<OffsetSize> {
+    fn from(value: FixedSizeListArray) -> Self {
+        let (field, size) = match value.data_type() {
+            DataType::FixedSizeList(f, size) => (f, *size as usize),
+            _ => unreachable!(),
+        };
+        let iter = std::iter::repeat(size).take(value.len());
+        let mut offsets = Vec::with_capacity(iter.size_hint().0);
+        offsets.push(OffsetSize::usize_as(0));

Review Comment:
   This method does not appear to be correct, and could do with test coverage 
(or could be omitted in a first version)



##########
arrow-array/src/array/list_view_array.rs:
##########
@@ -0,0 +1,1256 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::{get_view_offsets, get_view_sizes, make_array, 
print_long_array};
+use crate::builder::{GenericListViewBuilder, PrimitiveBuilder};
+use crate::iterator::GenericListViewArrayIter;
+use crate::{
+    new_empty_array, Array, ArrayAccessor, ArrayRef, ArrowPrimitiveType, 
FixedSizeListArray,
+    OffsetSizeTrait,
+};
+use arrow_buffer::{NullBuffer, ScalarBuffer};
+use arrow_data::{ArrayData, ArrayDataBuilder};
+use arrow_schema::{ArrowError, DataType, FieldRef};
+use std::any::Any;
+use std::ops::Add;
+use std::sync::Arc;
+
+/// A [`GenericListViewArray`] of variable size lists, storing offsets as 
`i32`.
+///
+// See [`ListViewBuilder`](crate::builder::ListViewBuilder) for how to 
construct a [`ListViewArray`]
+pub type ListViewArray = GenericListViewArray<i32>;
+
+/// A [`GenericListViewArray`] of variable size lists, storing offsets as 
`i64`.
+///
+// See [`LargeListViewArray`](crate::builder::LargeListViewBuilder) for how to 
construct a [`LargeListViewArray`]
+pub type LargeListViewArray = GenericListViewArray<i64>;
+
+///
+/// Different than [`crate::GenericListArray`] as it stores both an offset and 
length
+/// meaning that take / filter operations can be implemented without copying 
the underlying data.
+///
+/// [Variable-size List Layout: ListView Layout]: 
https://arrow.apache.org/docs/format/Columnar.html#listview-layout
+pub struct GenericListViewArray<OffsetSize: OffsetSizeTrait> {
+    data_type: DataType,
+    nulls: Option<NullBuffer>,
+    values: ArrayRef,
+    value_offsets: ScalarBuffer<OffsetSize>,
+    value_sizes: ScalarBuffer<OffsetSize>,
+    len: usize,
+}
+
+impl<OffsetSize: OffsetSizeTrait> Clone for GenericListViewArray<OffsetSize> {
+    fn clone(&self) -> Self {
+        Self {
+            data_type: self.data_type.clone(),
+            nulls: self.nulls.clone(),
+            values: self.values.clone(),
+            value_offsets: self.value_offsets.clone(),
+            value_sizes: self.value_sizes.clone(),
+            len: self.len,
+        }
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait> GenericListViewArray<OffsetSize> {
+    /// The data type constructor of listview array.
+    /// The input is the schema of the child array and
+    /// the output is the [`DataType`], ListView or LargeListView.
+    pub const DATA_TYPE_CONSTRUCTOR: fn(FieldRef) -> DataType = if 
OffsetSize::IS_LARGE {
+        DataType::LargeListView
+    } else {
+        DataType::ListView
+    };
+
+    /// Create a new [`GenericListViewArray`] from the provided parts
+    ///
+    /// # Errors
+    ///
+    /// Errors if
+    ///
+    /// * `offsets.len() != sizes.len()`
+    /// * `offsets.len() != nulls.len()`
+    /// * `offsets.last() > values.len()`
+    /// * `!field.is_nullable() && values.is_nullable()`
+    /// * `field.data_type() != values.data_type()`
+    /// * `0 <= offsets[i] <= length of the child array`
+    /// * `0 <= offsets[i] + size[i] <= length of the child array`
+    pub fn try_new(
+        field: FieldRef,
+        offsets: ScalarBuffer<OffsetSize>,
+        sizes: ScalarBuffer<OffsetSize>,
+        values: ArrayRef,
+        nulls: Option<NullBuffer>,
+    ) -> Result<Self, ArrowError> {
+        for i in 0..offsets.len() {
+            let length = values.len();
+            let offset = offsets[i].as_usize();
+            let size = sizes[i].as_usize();
+            if offset > length {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Invalid offset value for {}ListViewArray, offset: {}, 
length: {}",
+                    OffsetSize::PREFIX,
+                    offset,
+                    length
+                )));
+            }
+            if offset + size > values.len() {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Invalid offset and size values for {}ListViewArray, 
offset: {}, size: {}, values.len(): {}",
+                    OffsetSize::PREFIX, offset, size, values.len()
+                )));
+            }
+        }
+
+        let len = offsets.len();
+        if let Some(n) = nulls.as_ref() {
+            if n.len() != len {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Incorrect length of null buffer for {}ListViewArray, 
expected {len} got {}",
+                    OffsetSize::PREFIX,
+                    n.len(),
+                )));
+            }
+        }
+        if len != sizes.len() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "Length of offsets buffer and sizes buffer must be equal for 
{}ListViewArray, got {} and {}",
+                OffsetSize::PREFIX, len, sizes.len()
+            )));
+        }
+
+        if !field.is_nullable() && values.is_nullable() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "Non-nullable field of {}ListViewArray {:?} cannot contain 
nulls",
+                OffsetSize::PREFIX,
+                field.name()
+            )));
+        }
+
+        if field.data_type() != values.data_type() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "{}ListViewArray expected data type {} got {} for {:?}",
+                OffsetSize::PREFIX,
+                field.data_type(),
+                values.data_type(),
+                field.name()
+            )));
+        }
+
+        Ok(Self {
+            data_type: Self::DATA_TYPE_CONSTRUCTOR(field),
+            nulls,
+            values,
+            value_offsets: offsets,
+            value_sizes: sizes,
+            len,
+        })
+    }
+
+    /// Create a new [`GenericListViewArray`] from the provided parts
+    ///
+    /// # Panics
+    ///
+    /// Panics if [`Self::try_new`] returns an error
+    pub fn new(
+        field: FieldRef,
+        offsets: ScalarBuffer<OffsetSize>,
+        sizes: ScalarBuffer<OffsetSize>,
+        values: ArrayRef,
+        nulls: Option<NullBuffer>,
+    ) -> Self {
+        Self::try_new(field, offsets, sizes, values, nulls).unwrap()
+    }
+
+    /// Create a new [`GenericListViewArray`] of length `len` where all values 
are null
+    pub fn new_null(field: FieldRef, len: usize) -> Self {
+        let values = new_empty_array(field.data_type());
+        Self {
+            data_type: Self::DATA_TYPE_CONSTRUCTOR(field),
+            nulls: Some(NullBuffer::new_null(len)),
+            value_offsets: ScalarBuffer::new_zeroed(len),
+            value_sizes: ScalarBuffer::new_zeroed(len),
+            values,
+            len: 0usize,
+        }
+    }
+
+    /// Deconstruct this array into its constituent parts
+    pub fn into_parts(
+        self,
+    ) -> (
+        FieldRef,
+        ScalarBuffer<OffsetSize>,
+        ScalarBuffer<OffsetSize>,
+        ArrayRef,
+        Option<NullBuffer>,
+    ) {
+        let f = match self.data_type {
+            DataType::ListView(f) | DataType::LargeListView(f) => f,
+            _ => unreachable!(),
+        };
+        (
+            f,
+            self.value_offsets,
+            self.value_sizes,
+            self.values,
+            self.nulls,
+        )
+    }
+
+    /// Returns a reference to the offsets of this list
+    ///
+    /// Unlike [`Self::value_offsets`] this returns the [`ScalarBuffer`]
+    /// allowing for zero-copy cloning
+    #[inline]
+    pub fn offsets(&self) -> &ScalarBuffer<OffsetSize> {
+        &self.value_offsets
+    }
+
+    /// Returns a reference to the values of this list
+    #[inline]
+    pub fn values(&self) -> &ArrayRef {
+        &self.values
+    }
+
+    /// Returns a reference to the sizes of this list
+    ///
+    /// Unlike [`Self::value_sizes`] this returns the [`ScalarBuffer`]
+    /// allowing for zero-copy cloning
+    #[inline]
+    pub fn sizes(&self) -> &ScalarBuffer<OffsetSize> {
+        &self.value_sizes
+    }
+
+    /// Returns a clone of the value type of this list.
+    pub fn value_type(&self) -> DataType {
+        self.values.data_type().clone()
+    }
+
+    /// Returns ith value of this list view array.
+    /// # Safety
+    /// Caller must ensure that the index is within the array bounds
+    pub unsafe fn value_unchecked(&self, i: usize) -> ArrayRef {
+        let end = self.value_offsets().get_unchecked(i + 1).as_usize();
+        let start = self.value_offsets().get_unchecked(i).as_usize();
+        self.values.slice(start, end - start)
+    }
+
+    /// Returns ith value of this list view array.
+    pub fn value(&self, i: usize) -> ArrayRef {
+        let offset = self.value_offsets()[i].as_usize();
+        let length = self.value_sizes()[i].as_usize();
+        self.values.slice(offset, length)
+    }
+
+    /// Returns the offset values in the offsets buffer
+    #[inline]
+    pub fn value_offsets(&self) -> &[OffsetSize] {
+        &self.value_offsets
+    }
+
+    /// Returns the sizes values in the offsets buffer
+    #[inline]
+    pub fn value_sizes(&self) -> &[OffsetSize] {
+        &self.value_sizes
+    }
+
+    /// Returns the length for value at index `i`.
+    #[inline]
+    pub fn value_length(&self, i: usize) -> OffsetSize {
+        self.value_sizes[i]
+    }
+
+    /// constructs a new iterator
+    pub fn iter<'a>(&'a self) -> GenericListViewArrayIter<'a, OffsetSize> {
+        GenericListViewArrayIter::<'a, OffsetSize>::new(self)
+    }
+
+    #[inline]
+    fn get_type(data_type: &DataType) -> Option<&DataType> {
+        match (OffsetSize::IS_LARGE, data_type) {
+            (true, DataType::LargeListView(child)) | (false, 
DataType::ListView(child)) => {
+                Some(child.data_type())
+            }
+            _ => None,
+        }
+    }
+
+    /// Returns a zero-copy slice of this array with the indicated offset and 
length.
+    pub fn slice(&self, offset: usize, length: usize) -> Self {
+        Self {
+            data_type: self.data_type.clone(),
+            nulls: self.nulls.as_ref().map(|n| n.slice(offset, length)),
+            values: self.values.clone(),
+            value_offsets: self.value_offsets.slice(offset, length),
+            value_sizes: self.value_sizes.slice(offset, length),
+            len: length,
+        }
+    }
+
+    /// Creates a [`GenericListViewArray`] from an iterator of primitive values
+    ///
+    pub fn from_iter_primitive<T, P, I>(iter: I) -> Self
+    where
+        T: ArrowPrimitiveType,
+        P: IntoIterator<Item = Option<<T as ArrowPrimitiveType>::Native>>,
+        I: IntoIterator<Item = Option<P>>,
+    {
+        let iter = iter.into_iter();
+        let size_hint = iter.size_hint().0;
+        let mut builder =
+            
GenericListViewBuilder::with_capacity(PrimitiveBuilder::<T>::new(), size_hint);
+
+        for i in iter {
+            match i {
+                Some(p) => {
+                    let mut size = 0usize;
+                    for t in p {
+                        builder.values().append_option(t);
+                        size = size.add(1);
+                    }
+                    builder.append(true, size);
+                }
+                None => builder.append(false, 0),
+            }
+        }
+        builder.finish()
+    }
+}
+
+impl<'a, OffsetSize: OffsetSizeTrait> ArrayAccessor for &'a 
GenericListViewArray<OffsetSize> {
+    type Item = ArrayRef;
+
+    fn value(&self, index: usize) -> Self::Item {
+        GenericListViewArray::value(self, index)
+    }
+
+    unsafe fn value_unchecked(&self, index: usize) -> Self::Item {
+        GenericListViewArray::value(self, index)
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait> Array for GenericListViewArray<OffsetSize> {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn to_data(&self) -> ArrayData {
+        self.clone().into()
+    }
+
+    fn into_data(self) -> ArrayData {
+        self.into()
+    }
+
+    fn data_type(&self) -> &DataType {
+        &self.data_type
+    }
+
+    fn slice(&self, offset: usize, length: usize) -> ArrayRef {
+        Arc::new(self.slice(offset, length))
+    }
+
+    fn len(&self) -> usize {
+        self.len
+    }
+
+    fn is_empty(&self) -> bool {
+        self.value_offsets.len() <= 1
+    }
+
+    fn offset(&self) -> usize {
+        0
+    }
+
+    fn nulls(&self) -> Option<&NullBuffer> {
+        self.nulls.as_ref()
+    }
+
+    fn get_buffer_memory_size(&self) -> usize {
+        let mut size = self.values.get_buffer_memory_size();
+        size += self.value_offsets.inner().capacity();
+        if let Some(n) = self.nulls.as_ref() {
+            size += n.buffer().capacity();
+        }
+        size
+    }
+
+    fn get_array_memory_size(&self) -> usize {
+        let mut size = std::mem::size_of::<Self>() + 
self.values.get_array_memory_size();
+        size += self.value_offsets.inner().capacity();

Review Comment:
   Same as above



##########
arrow-array/src/array/list_view_array.rs:
##########
@@ -0,0 +1,1256 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::{get_view_offsets, get_view_sizes, make_array, 
print_long_array};
+use crate::builder::{GenericListViewBuilder, PrimitiveBuilder};
+use crate::iterator::GenericListViewArrayIter;
+use crate::{
+    new_empty_array, Array, ArrayAccessor, ArrayRef, ArrowPrimitiveType, 
FixedSizeListArray,
+    OffsetSizeTrait,
+};
+use arrow_buffer::{NullBuffer, ScalarBuffer};
+use arrow_data::{ArrayData, ArrayDataBuilder};
+use arrow_schema::{ArrowError, DataType, FieldRef};
+use std::any::Any;
+use std::ops::Add;
+use std::sync::Arc;
+
+/// A [`GenericListViewArray`] of variable size lists, storing offsets as 
`i32`.
+///
+// See [`ListViewBuilder`](crate::builder::ListViewBuilder) for how to 
construct a [`ListViewArray`]
+pub type ListViewArray = GenericListViewArray<i32>;
+
+/// A [`GenericListViewArray`] of variable size lists, storing offsets as 
`i64`.
+///
+// See [`LargeListViewArray`](crate::builder::LargeListViewBuilder) for how to 
construct a [`LargeListViewArray`]
+pub type LargeListViewArray = GenericListViewArray<i64>;
+
+///
+/// Different than [`crate::GenericListArray`] as it stores both an offset and 
length
+/// meaning that take / filter operations can be implemented without copying 
the underlying data.
+///
+/// [Variable-size List Layout: ListView Layout]: 
https://arrow.apache.org/docs/format/Columnar.html#listview-layout
+pub struct GenericListViewArray<OffsetSize: OffsetSizeTrait> {
+    data_type: DataType,
+    nulls: Option<NullBuffer>,
+    values: ArrayRef,
+    value_offsets: ScalarBuffer<OffsetSize>,
+    value_sizes: ScalarBuffer<OffsetSize>,
+    len: usize,

Review Comment:
   I don't think we need a separate length field, we can just use the offsets 
or sizes



##########
arrow-array/src/builder/generic_list_view_builder.rs:
##########
@@ -0,0 +1,709 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::builder::ArrayBuilder;
+use crate::{ArrayRef, GenericListViewArray, OffsetSizeTrait};
+use arrow_buffer::{Buffer, BufferBuilder, NullBufferBuilder, ScalarBuffer};
+use arrow_schema::{Field, FieldRef};
+use std::any::Any;
+use std::sync::Arc;
+
+/// Builder for [`GenericListViewArray`]
+///
+#[derive(Debug)]
+pub struct GenericListViewBuilder<OffsetSize: OffsetSizeTrait, T: 
ArrayBuilder> {
+    offsets_builder: BufferBuilder<OffsetSize>,
+    sizes_builder: BufferBuilder<OffsetSize>,
+    null_buffer_builder: NullBufferBuilder,
+    values_builder: T,
+    field: Option<FieldRef>,
+}
+
+impl<O: OffsetSizeTrait, T: ArrayBuilder + Default> Default for 
GenericListViewBuilder<O, T> {
+    fn default() -> Self {
+        Self::new(T::default())
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait, T: ArrayBuilder> ArrayBuilder
+    for GenericListViewBuilder<OffsetSize, T>
+where
+    T: 'static,
+{
+    /// Returns the builder as a non-mutable `Any` reference.
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    /// Returns the builder as a mutable `Any` reference.
+    fn as_any_mut(&mut self) -> &mut dyn Any {
+        self
+    }
+
+    /// Returns the boxed builder as a box of `Any`.
+    fn into_box_any(self: Box<Self>) -> Box<dyn Any> {
+        self
+    }
+
+    /// Returns the number of array slots in the builder
+    fn len(&self) -> usize {
+        self.null_buffer_builder.len()
+    }
+
+    /// Builds the array and reset this builder.
+    fn finish(&mut self) -> ArrayRef {
+        Arc::new(self.finish())
+    }
+
+    /// Builds the array without resetting the builder.
+    fn finish_cloned(&self) -> ArrayRef {
+        Arc::new(self.finish_cloned())
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait, T: ArrayBuilder> 
GenericListViewBuilder<OffsetSize, T> {
+    /// Creates a new [`GenericListViewBuilder`] from a given values array 
builder
+    pub fn new(values_builder: T) -> Self {
+        let capacity = values_builder.len();
+        Self::with_capacity(values_builder, capacity)
+    }
+
+    /// Creates a new [`GenericListViewBuilder`] from a given values array 
builder
+    /// `capacity` is the number of items to pre-allocate space for in this 
builder
+    pub fn with_capacity(values_builder: T, capacity: usize) -> Self {
+        let offsets_builder = BufferBuilder::<OffsetSize>::new(capacity);
+        let sizes_builder = BufferBuilder::<OffsetSize>::new(capacity);
+        Self {
+            offsets_builder,
+            null_buffer_builder: NullBufferBuilder::new(capacity),
+            values_builder,
+            sizes_builder,
+            field: None,
+        }
+    }
+
+    /// Override the field passed to [`GenericListViewArray::new`]
+    ///
+    /// By default a nullable field is created with the name `item`
+    ///
+    /// Note: [`Self::finish`] and [`Self::finish_cloned`] will panic if the
+    /// field's data type does not match that of `T`
+    pub fn with_field(self, field: impl Into<FieldRef>) -> Self {
+        Self {
+            field: Some(field.into()),
+            ..self
+        }
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait, T: ArrayBuilder> 
GenericListViewBuilder<OffsetSize, T>
+where
+    T: 'static,
+{
+    /// Returns the child array builder as a mutable reference.
+    ///
+    /// This mutable reference can be used to append values into the child 
array builder,
+    /// but you must call [`append`](#method.append) to delimit each distinct 
list value.
+    pub fn values(&mut self) -> &mut T {
+        &mut self.values_builder
+    }
+
+    /// Returns the child array builder as an immutable reference
+    pub fn values_ref(&self) -> &T {
+        &self.values_builder
+    }
+
+    /// Finish the current variable-length list array slot
+    ///
+    /// # Panics
+    ///
+    /// Panics if the length of [`Self::values`] exceeds `OffsetSize::MAX`
+    #[inline]
+    pub fn append(&mut self, is_valid: bool, size: usize) {

Review Comment:
   The `size` should be inferred from the length of the values_builder, much 
like it is for `GenericListBuilder`



##########
arrow-data/src/data.rs:
##########
@@ -937,11 +972,23 @@ impl ArrayData {
                 self.validate_offsets::<i32>(values_data.len)?;
                 Ok(())
             }
+            DataType::ListView(field) => {
+                let values_data = 
self.get_single_valid_child_data(field.data_type())?;
+                self.validate_offsets::<i32>(values_data.len)?;

Review Comment:
   This validates the offsets w.r.t to the ListArray semantics, this is 
incorrect



##########
arrow-array/src/array/list_view_array.rs:
##########
@@ -0,0 +1,1256 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::{get_view_offsets, get_view_sizes, make_array, 
print_long_array};
+use crate::builder::{GenericListViewBuilder, PrimitiveBuilder};
+use crate::iterator::GenericListViewArrayIter;
+use crate::{
+    new_empty_array, Array, ArrayAccessor, ArrayRef, ArrowPrimitiveType, 
FixedSizeListArray,
+    OffsetSizeTrait,
+};
+use arrow_buffer::{NullBuffer, ScalarBuffer};
+use arrow_data::{ArrayData, ArrayDataBuilder};
+use arrow_schema::{ArrowError, DataType, FieldRef};
+use std::any::Any;
+use std::ops::Add;
+use std::sync::Arc;
+
+/// A [`GenericListViewArray`] of variable size lists, storing offsets as 
`i32`.
+///
+// See [`ListViewBuilder`](crate::builder::ListViewBuilder) for how to 
construct a [`ListViewArray`]
+pub type ListViewArray = GenericListViewArray<i32>;
+
+/// A [`GenericListViewArray`] of variable size lists, storing offsets as 
`i64`.
+///
+// See [`LargeListViewArray`](crate::builder::LargeListViewBuilder) for how to 
construct a [`LargeListViewArray`]
+pub type LargeListViewArray = GenericListViewArray<i64>;
+
+///
+/// Different than [`crate::GenericListArray`] as it stores both an offset and 
length
+/// meaning that take / filter operations can be implemented without copying 
the underlying data.
+///
+/// [Variable-size List Layout: ListView Layout]: 
https://arrow.apache.org/docs/format/Columnar.html#listview-layout
+pub struct GenericListViewArray<OffsetSize: OffsetSizeTrait> {
+    data_type: DataType,
+    nulls: Option<NullBuffer>,
+    values: ArrayRef,
+    value_offsets: ScalarBuffer<OffsetSize>,
+    value_sizes: ScalarBuffer<OffsetSize>,
+    len: usize,
+}
+
+impl<OffsetSize: OffsetSizeTrait> Clone for GenericListViewArray<OffsetSize> {
+    fn clone(&self) -> Self {
+        Self {
+            data_type: self.data_type.clone(),
+            nulls: self.nulls.clone(),
+            values: self.values.clone(),
+            value_offsets: self.value_offsets.clone(),
+            value_sizes: self.value_sizes.clone(),
+            len: self.len,
+        }
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait> GenericListViewArray<OffsetSize> {
+    /// The data type constructor of listview array.
+    /// The input is the schema of the child array and
+    /// the output is the [`DataType`], ListView or LargeListView.
+    pub const DATA_TYPE_CONSTRUCTOR: fn(FieldRef) -> DataType = if 
OffsetSize::IS_LARGE {
+        DataType::LargeListView
+    } else {
+        DataType::ListView
+    };
+
+    /// Create a new [`GenericListViewArray`] from the provided parts
+    ///
+    /// # Errors
+    ///
+    /// Errors if
+    ///
+    /// * `offsets.len() != sizes.len()`
+    /// * `offsets.len() != nulls.len()`
+    /// * `offsets.last() > values.len()`
+    /// * `!field.is_nullable() && values.is_nullable()`
+    /// * `field.data_type() != values.data_type()`
+    /// * `0 <= offsets[i] <= length of the child array`
+    /// * `0 <= offsets[i] + size[i] <= length of the child array`
+    pub fn try_new(
+        field: FieldRef,
+        offsets: ScalarBuffer<OffsetSize>,
+        sizes: ScalarBuffer<OffsetSize>,
+        values: ArrayRef,
+        nulls: Option<NullBuffer>,
+    ) -> Result<Self, ArrowError> {
+        for i in 0..offsets.len() {
+            let length = values.len();
+            let offset = offsets[i].as_usize();
+            let size = sizes[i].as_usize();
+            if offset > length {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Invalid offset value for {}ListViewArray, offset: {}, 
length: {}",
+                    OffsetSize::PREFIX,
+                    offset,
+                    length
+                )));
+            }
+            if offset + size > values.len() {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Invalid offset and size values for {}ListViewArray, 
offset: {}, size: {}, values.len(): {}",
+                    OffsetSize::PREFIX, offset, size, values.len()
+                )));
+            }
+        }
+
+        let len = offsets.len();
+        if let Some(n) = nulls.as_ref() {
+            if n.len() != len {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Incorrect length of null buffer for {}ListViewArray, 
expected {len} got {}",
+                    OffsetSize::PREFIX,
+                    n.len(),
+                )));
+            }
+        }
+        if len != sizes.len() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "Length of offsets buffer and sizes buffer must be equal for 
{}ListViewArray, got {} and {}",
+                OffsetSize::PREFIX, len, sizes.len()
+            )));
+        }
+
+        if !field.is_nullable() && values.is_nullable() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "Non-nullable field of {}ListViewArray {:?} cannot contain 
nulls",
+                OffsetSize::PREFIX,
+                field.name()
+            )));
+        }
+
+        if field.data_type() != values.data_type() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "{}ListViewArray expected data type {} got {} for {:?}",
+                OffsetSize::PREFIX,
+                field.data_type(),
+                values.data_type(),
+                field.name()
+            )));
+        }
+
+        Ok(Self {
+            data_type: Self::DATA_TYPE_CONSTRUCTOR(field),
+            nulls,
+            values,
+            value_offsets: offsets,
+            value_sizes: sizes,
+            len,
+        })
+    }
+
+    /// Create a new [`GenericListViewArray`] from the provided parts
+    ///
+    /// # Panics
+    ///
+    /// Panics if [`Self::try_new`] returns an error
+    pub fn new(
+        field: FieldRef,
+        offsets: ScalarBuffer<OffsetSize>,
+        sizes: ScalarBuffer<OffsetSize>,
+        values: ArrayRef,
+        nulls: Option<NullBuffer>,
+    ) -> Self {
+        Self::try_new(field, offsets, sizes, values, nulls).unwrap()
+    }
+
+    /// Create a new [`GenericListViewArray`] of length `len` where all values 
are null
+    pub fn new_null(field: FieldRef, len: usize) -> Self {
+        let values = new_empty_array(field.data_type());
+        Self {
+            data_type: Self::DATA_TYPE_CONSTRUCTOR(field),
+            nulls: Some(NullBuffer::new_null(len)),
+            value_offsets: ScalarBuffer::new_zeroed(len),
+            value_sizes: ScalarBuffer::new_zeroed(len),
+            values,
+            len: 0usize,
+        }
+    }
+
+    /// Deconstruct this array into its constituent parts
+    pub fn into_parts(
+        self,
+    ) -> (
+        FieldRef,
+        ScalarBuffer<OffsetSize>,
+        ScalarBuffer<OffsetSize>,
+        ArrayRef,
+        Option<NullBuffer>,
+    ) {
+        let f = match self.data_type {
+            DataType::ListView(f) | DataType::LargeListView(f) => f,
+            _ => unreachable!(),
+        };
+        (
+            f,
+            self.value_offsets,
+            self.value_sizes,
+            self.values,
+            self.nulls,
+        )
+    }
+
+    /// Returns a reference to the offsets of this list
+    ///
+    /// Unlike [`Self::value_offsets`] this returns the [`ScalarBuffer`]
+    /// allowing for zero-copy cloning
+    #[inline]
+    pub fn offsets(&self) -> &ScalarBuffer<OffsetSize> {
+        &self.value_offsets
+    }
+
+    /// Returns a reference to the values of this list
+    #[inline]
+    pub fn values(&self) -> &ArrayRef {
+        &self.values
+    }
+
+    /// Returns a reference to the sizes of this list
+    ///
+    /// Unlike [`Self::value_sizes`] this returns the [`ScalarBuffer`]
+    /// allowing for zero-copy cloning
+    #[inline]
+    pub fn sizes(&self) -> &ScalarBuffer<OffsetSize> {
+        &self.value_sizes
+    }
+
+    /// Returns a clone of the value type of this list.
+    pub fn value_type(&self) -> DataType {
+        self.values.data_type().clone()
+    }
+
+    /// Returns ith value of this list view array.
+    /// # Safety
+    /// Caller must ensure that the index is within the array bounds
+    pub unsafe fn value_unchecked(&self, i: usize) -> ArrayRef {
+        let end = self.value_offsets().get_unchecked(i + 1).as_usize();
+        let start = self.value_offsets().get_unchecked(i).as_usize();
+        self.values.slice(start, end - start)
+    }
+
+    /// Returns ith value of this list view array.
+    pub fn value(&self, i: usize) -> ArrayRef {
+        let offset = self.value_offsets()[i].as_usize();
+        let length = self.value_sizes()[i].as_usize();
+        self.values.slice(offset, length)
+    }
+
+    /// Returns the offset values in the offsets buffer
+    #[inline]
+    pub fn value_offsets(&self) -> &[OffsetSize] {
+        &self.value_offsets
+    }
+
+    /// Returns the sizes values in the offsets buffer
+    #[inline]
+    pub fn value_sizes(&self) -> &[OffsetSize] {
+        &self.value_sizes
+    }
+
+    /// Returns the length for value at index `i`.
+    #[inline]
+    pub fn value_length(&self, i: usize) -> OffsetSize {
+        self.value_sizes[i]
+    }
+
+    /// constructs a new iterator
+    pub fn iter<'a>(&'a self) -> GenericListViewArrayIter<'a, OffsetSize> {
+        GenericListViewArrayIter::<'a, OffsetSize>::new(self)
+    }
+
+    #[inline]
+    fn get_type(data_type: &DataType) -> Option<&DataType> {
+        match (OffsetSize::IS_LARGE, data_type) {
+            (true, DataType::LargeListView(child)) | (false, 
DataType::ListView(child)) => {
+                Some(child.data_type())
+            }
+            _ => None,
+        }
+    }
+
+    /// Returns a zero-copy slice of this array with the indicated offset and 
length.
+    pub fn slice(&self, offset: usize, length: usize) -> Self {
+        Self {
+            data_type: self.data_type.clone(),
+            nulls: self.nulls.as_ref().map(|n| n.slice(offset, length)),
+            values: self.values.clone(),
+            value_offsets: self.value_offsets.slice(offset, length),
+            value_sizes: self.value_sizes.slice(offset, length),
+            len: length,
+        }
+    }
+
+    /// Creates a [`GenericListViewArray`] from an iterator of primitive values
+    ///
+    pub fn from_iter_primitive<T, P, I>(iter: I) -> Self
+    where
+        T: ArrowPrimitiveType,
+        P: IntoIterator<Item = Option<<T as ArrowPrimitiveType>::Native>>,
+        I: IntoIterator<Item = Option<P>>,
+    {
+        let iter = iter.into_iter();
+        let size_hint = iter.size_hint().0;
+        let mut builder =
+            
GenericListViewBuilder::with_capacity(PrimitiveBuilder::<T>::new(), size_hint);
+
+        for i in iter {
+            match i {
+                Some(p) => {
+                    let mut size = 0usize;
+                    for t in p {
+                        builder.values().append_option(t);
+                        size = size.add(1);
+                    }
+                    builder.append(true, size);
+                }
+                None => builder.append(false, 0),
+            }
+        }
+        builder.finish()
+    }
+}
+
+impl<'a, OffsetSize: OffsetSizeTrait> ArrayAccessor for &'a 
GenericListViewArray<OffsetSize> {
+    type Item = ArrayRef;
+
+    fn value(&self, index: usize) -> Self::Item {
+        GenericListViewArray::value(self, index)
+    }
+
+    unsafe fn value_unchecked(&self, index: usize) -> Self::Item {
+        GenericListViewArray::value(self, index)
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait> Array for GenericListViewArray<OffsetSize> {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn to_data(&self) -> ArrayData {
+        self.clone().into()
+    }
+
+    fn into_data(self) -> ArrayData {
+        self.into()
+    }
+
+    fn data_type(&self) -> &DataType {
+        &self.data_type
+    }
+
+    fn slice(&self, offset: usize, length: usize) -> ArrayRef {
+        Arc::new(self.slice(offset, length))
+    }
+
+    fn len(&self) -> usize {
+        self.len
+    }
+
+    fn is_empty(&self) -> bool {
+        self.value_offsets.len() <= 1
+    }
+
+    fn offset(&self) -> usize {
+        0
+    }
+
+    fn nulls(&self) -> Option<&NullBuffer> {
+        self.nulls.as_ref()
+    }
+
+    fn get_buffer_memory_size(&self) -> usize {
+        let mut size = self.values.get_buffer_memory_size();
+        size += self.value_offsets.inner().capacity();

Review Comment:
   This should include the sizes as well



##########
arrow-buffer/src/buffer/scalar.rs:
##########
@@ -94,6 +94,19 @@ impl<T: ArrowNativeType> ScalarBuffer<T> {
     pub fn ptr_eq(&self, other: &Self) -> bool {
         self.buffer.ptr_eq(&other.buffer)
     }
+
+    /// Returns the length of this buffer in units of `T`
+    pub fn new_zeroed(len: usize) -> Self {
+        let len_bytes = 
len.checked_mul(std::mem::size_of::<T>()).expect("overflow");
+        let buffer = MutableBuffer::from_len_zeroed(len_bytes);
+        Self::from(buffer)
+    }
+
+    /// Create a new [`ScalarBuffer`] containing a single 0 value
+    pub fn new_empty() -> Self {
+        let buffer = MutableBuffer::from_len_zeroed(std::mem::size_of::<T>());
+        Self::from(buffer.into_buffer())
+    }

Review Comment:
   This method is not necessary, and is incorrect as it creates a 
`ScalarBuffer` that isn't empty



##########
arrow-array/src/array/mod.rs:
##########
@@ -687,6 +696,30 @@ unsafe fn get_offsets<O: ArrowNativeType>(data: 
&ArrayData) -> OffsetBuffer<O> {
     }
 }
 
+/// Helper function that gets list view offset from an [`ArrayData`]
+///
+/// # Safety
+///
+/// ArrayData must contain a valid [`ScalarBuffer`] as its first buffer
+fn get_view_offsets<O: ArrowNativeType>(data: &ArrayData) -> ScalarBuffer<O> {
+    match data.is_empty() && data.buffers()[0].is_empty() {
+        true => ScalarBuffer::new_empty(),
+        false => ScalarBuffer::new(data.buffers()[0].clone(), data.offset(), 
data.len() + 1),
+    }
+}
+
+/// Helper function that gets list view size from an [`ArrayData`]
+///
+/// # Safety
+///
+/// ArrayData must contain a valid [`ScalarBuffer`] as its second buffer
+fn get_view_sizes<O: ArrowNativeType>(data: &ArrayData) -> ScalarBuffer<O> {
+    match data.is_empty() && data.buffers()[1].is_empty() {
+        true => ScalarBuffer::new_empty(),
+        false => ScalarBuffer::new(data.buffers()[1].clone(), data.offset(), 
data.len()),
+    }
+}

Review Comment:
   These methods aren't necessary, get_offsets exists because `OffsetBuffer` 
must be non-empty



##########
arrow-array/src/array/list_view_array.rs:
##########
@@ -0,0 +1,1256 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::{get_view_offsets, get_view_sizes, make_array, 
print_long_array};
+use crate::builder::{GenericListViewBuilder, PrimitiveBuilder};
+use crate::iterator::GenericListViewArrayIter;
+use crate::{
+    new_empty_array, Array, ArrayAccessor, ArrayRef, ArrowPrimitiveType, 
FixedSizeListArray,
+    OffsetSizeTrait,
+};
+use arrow_buffer::{NullBuffer, ScalarBuffer};
+use arrow_data::{ArrayData, ArrayDataBuilder};
+use arrow_schema::{ArrowError, DataType, FieldRef};
+use std::any::Any;
+use std::ops::Add;
+use std::sync::Arc;
+
+/// A [`GenericListViewArray`] of variable size lists, storing offsets as 
`i32`.
+///
+// See [`ListViewBuilder`](crate::builder::ListViewBuilder) for how to 
construct a [`ListViewArray`]
+pub type ListViewArray = GenericListViewArray<i32>;
+
+/// A [`GenericListViewArray`] of variable size lists, storing offsets as 
`i64`.
+///
+// See [`LargeListViewArray`](crate::builder::LargeListViewBuilder) for how to 
construct a [`LargeListViewArray`]
+pub type LargeListViewArray = GenericListViewArray<i64>;
+
+///
+/// Different than [`crate::GenericListArray`] as it stores both an offset and 
length
+/// meaning that take / filter operations can be implemented without copying 
the underlying data.
+///
+/// [Variable-size List Layout: ListView Layout]: 
https://arrow.apache.org/docs/format/Columnar.html#listview-layout
+pub struct GenericListViewArray<OffsetSize: OffsetSizeTrait> {
+    data_type: DataType,
+    nulls: Option<NullBuffer>,
+    values: ArrayRef,
+    value_offsets: ScalarBuffer<OffsetSize>,
+    value_sizes: ScalarBuffer<OffsetSize>,
+    len: usize,
+}
+
+impl<OffsetSize: OffsetSizeTrait> Clone for GenericListViewArray<OffsetSize> {
+    fn clone(&self) -> Self {
+        Self {
+            data_type: self.data_type.clone(),
+            nulls: self.nulls.clone(),
+            values: self.values.clone(),
+            value_offsets: self.value_offsets.clone(),
+            value_sizes: self.value_sizes.clone(),
+            len: self.len,
+        }
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait> GenericListViewArray<OffsetSize> {
+    /// The data type constructor of listview array.
+    /// The input is the schema of the child array and
+    /// the output is the [`DataType`], ListView or LargeListView.
+    pub const DATA_TYPE_CONSTRUCTOR: fn(FieldRef) -> DataType = if 
OffsetSize::IS_LARGE {
+        DataType::LargeListView
+    } else {
+        DataType::ListView
+    };
+
+    /// Create a new [`GenericListViewArray`] from the provided parts
+    ///
+    /// # Errors
+    ///
+    /// Errors if
+    ///
+    /// * `offsets.len() != sizes.len()`
+    /// * `offsets.len() != nulls.len()`
+    /// * `offsets.last() > values.len()`
+    /// * `!field.is_nullable() && values.is_nullable()`
+    /// * `field.data_type() != values.data_type()`
+    /// * `0 <= offsets[i] <= length of the child array`
+    /// * `0 <= offsets[i] + size[i] <= length of the child array`
+    pub fn try_new(
+        field: FieldRef,
+        offsets: ScalarBuffer<OffsetSize>,
+        sizes: ScalarBuffer<OffsetSize>,
+        values: ArrayRef,
+        nulls: Option<NullBuffer>,
+    ) -> Result<Self, ArrowError> {
+        for i in 0..offsets.len() {
+            let length = values.len();
+            let offset = offsets[i].as_usize();
+            let size = sizes[i].as_usize();
+            if offset > length {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Invalid offset value for {}ListViewArray, offset: {}, 
length: {}",
+                    OffsetSize::PREFIX,
+                    offset,
+                    length
+                )));
+            }
+            if offset + size > values.len() {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Invalid offset and size values for {}ListViewArray, 
offset: {}, size: {}, values.len(): {}",
+                    OffsetSize::PREFIX, offset, size, values.len()
+                )));
+            }
+        }
+
+        let len = offsets.len();
+        if let Some(n) = nulls.as_ref() {
+            if n.len() != len {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Incorrect length of null buffer for {}ListViewArray, 
expected {len} got {}",
+                    OffsetSize::PREFIX,
+                    n.len(),
+                )));
+            }
+        }
+        if len != sizes.len() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "Length of offsets buffer and sizes buffer must be equal for 
{}ListViewArray, got {} and {}",
+                OffsetSize::PREFIX, len, sizes.len()
+            )));
+        }
+
+        if !field.is_nullable() && values.is_nullable() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "Non-nullable field of {}ListViewArray {:?} cannot contain 
nulls",
+                OffsetSize::PREFIX,
+                field.name()
+            )));
+        }
+
+        if field.data_type() != values.data_type() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "{}ListViewArray expected data type {} got {} for {:?}",
+                OffsetSize::PREFIX,
+                field.data_type(),
+                values.data_type(),
+                field.name()
+            )));
+        }
+
+        Ok(Self {
+            data_type: Self::DATA_TYPE_CONSTRUCTOR(field),
+            nulls,
+            values,
+            value_offsets: offsets,
+            value_sizes: sizes,
+            len,
+        })
+    }
+
+    /// Create a new [`GenericListViewArray`] from the provided parts
+    ///
+    /// # Panics
+    ///
+    /// Panics if [`Self::try_new`] returns an error
+    pub fn new(
+        field: FieldRef,
+        offsets: ScalarBuffer<OffsetSize>,
+        sizes: ScalarBuffer<OffsetSize>,
+        values: ArrayRef,
+        nulls: Option<NullBuffer>,
+    ) -> Self {
+        Self::try_new(field, offsets, sizes, values, nulls).unwrap()
+    }
+
+    /// Create a new [`GenericListViewArray`] of length `len` where all values 
are null
+    pub fn new_null(field: FieldRef, len: usize) -> Self {
+        let values = new_empty_array(field.data_type());
+        Self {
+            data_type: Self::DATA_TYPE_CONSTRUCTOR(field),
+            nulls: Some(NullBuffer::new_null(len)),
+            value_offsets: ScalarBuffer::new_zeroed(len),
+            value_sizes: ScalarBuffer::new_zeroed(len),
+            values,
+            len: 0usize,
+        }
+    }
+
+    /// Deconstruct this array into its constituent parts
+    pub fn into_parts(
+        self,
+    ) -> (
+        FieldRef,
+        ScalarBuffer<OffsetSize>,
+        ScalarBuffer<OffsetSize>,
+        ArrayRef,
+        Option<NullBuffer>,
+    ) {
+        let f = match self.data_type {
+            DataType::ListView(f) | DataType::LargeListView(f) => f,
+            _ => unreachable!(),
+        };
+        (
+            f,
+            self.value_offsets,
+            self.value_sizes,
+            self.values,
+            self.nulls,
+        )
+    }
+
+    /// Returns a reference to the offsets of this list
+    ///
+    /// Unlike [`Self::value_offsets`] this returns the [`ScalarBuffer`]
+    /// allowing for zero-copy cloning
+    #[inline]
+    pub fn offsets(&self) -> &ScalarBuffer<OffsetSize> {
+        &self.value_offsets
+    }
+
+    /// Returns a reference to the values of this list
+    #[inline]
+    pub fn values(&self) -> &ArrayRef {
+        &self.values
+    }
+
+    /// Returns a reference to the sizes of this list
+    ///
+    /// Unlike [`Self::value_sizes`] this returns the [`ScalarBuffer`]
+    /// allowing for zero-copy cloning
+    #[inline]
+    pub fn sizes(&self) -> &ScalarBuffer<OffsetSize> {
+        &self.value_sizes
+    }
+
+    /// Returns a clone of the value type of this list.
+    pub fn value_type(&self) -> DataType {
+        self.values.data_type().clone()
+    }
+
+    /// Returns ith value of this list view array.
+    /// # Safety
+    /// Caller must ensure that the index is within the array bounds
+    pub unsafe fn value_unchecked(&self, i: usize) -> ArrayRef {
+        let end = self.value_offsets().get_unchecked(i + 1).as_usize();
+        let start = self.value_offsets().get_unchecked(i).as_usize();
+        self.values.slice(start, end - start)
+    }
+
+    /// Returns ith value of this list view array.
+    pub fn value(&self, i: usize) -> ArrayRef {
+        let offset = self.value_offsets()[i].as_usize();
+        let length = self.value_sizes()[i].as_usize();
+        self.values.slice(offset, length)
+    }
+
+    /// Returns the offset values in the offsets buffer
+    #[inline]
+    pub fn value_offsets(&self) -> &[OffsetSize] {
+        &self.value_offsets
+    }
+
+    /// Returns the sizes values in the offsets buffer
+    #[inline]
+    pub fn value_sizes(&self) -> &[OffsetSize] {
+        &self.value_sizes
+    }
+
+    /// Returns the length for value at index `i`.
+    #[inline]
+    pub fn value_length(&self, i: usize) -> OffsetSize {
+        self.value_sizes[i]
+    }
+
+    /// constructs a new iterator
+    pub fn iter<'a>(&'a self) -> GenericListViewArrayIter<'a, OffsetSize> {
+        GenericListViewArrayIter::<'a, OffsetSize>::new(self)
+    }
+
+    #[inline]
+    fn get_type(data_type: &DataType) -> Option<&DataType> {
+        match (OffsetSize::IS_LARGE, data_type) {
+            (true, DataType::LargeListView(child)) | (false, 
DataType::ListView(child)) => {
+                Some(child.data_type())
+            }
+            _ => None,
+        }
+    }
+
+    /// Returns a zero-copy slice of this array with the indicated offset and 
length.
+    pub fn slice(&self, offset: usize, length: usize) -> Self {
+        Self {
+            data_type: self.data_type.clone(),
+            nulls: self.nulls.as_ref().map(|n| n.slice(offset, length)),
+            values: self.values.clone(),
+            value_offsets: self.value_offsets.slice(offset, length),
+            value_sizes: self.value_sizes.slice(offset, length),
+            len: length,
+        }
+    }
+
+    /// Creates a [`GenericListViewArray`] from an iterator of primitive values
+    ///
+    pub fn from_iter_primitive<T, P, I>(iter: I) -> Self
+    where
+        T: ArrowPrimitiveType,
+        P: IntoIterator<Item = Option<<T as ArrowPrimitiveType>::Native>>,
+        I: IntoIterator<Item = Option<P>>,
+    {
+        let iter = iter.into_iter();
+        let size_hint = iter.size_hint().0;
+        let mut builder =
+            
GenericListViewBuilder::with_capacity(PrimitiveBuilder::<T>::new(), size_hint);
+
+        for i in iter {
+            match i {
+                Some(p) => {
+                    let mut size = 0usize;
+                    for t in p {
+                        builder.values().append_option(t);
+                        size = size.add(1);
+                    }
+                    builder.append(true, size);
+                }
+                None => builder.append(false, 0),
+            }
+        }
+        builder.finish()
+    }
+}
+
+impl<'a, OffsetSize: OffsetSizeTrait> ArrayAccessor for &'a 
GenericListViewArray<OffsetSize> {
+    type Item = ArrayRef;
+
+    fn value(&self, index: usize) -> Self::Item {
+        GenericListViewArray::value(self, index)
+    }
+
+    unsafe fn value_unchecked(&self, index: usize) -> Self::Item {
+        GenericListViewArray::value(self, index)
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait> Array for GenericListViewArray<OffsetSize> {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn to_data(&self) -> ArrayData {
+        self.clone().into()
+    }
+
+    fn into_data(self) -> ArrayData {
+        self.into()
+    }
+
+    fn data_type(&self) -> &DataType {
+        &self.data_type
+    }
+
+    fn slice(&self, offset: usize, length: usize) -> ArrayRef {
+        Arc::new(self.slice(offset, length))
+    }
+
+    fn len(&self) -> usize {
+        self.len
+    }
+
+    fn is_empty(&self) -> bool {
+        self.value_offsets.len() <= 1

Review Comment:
   This does not appear to be correct



##########
arrow-array/src/array/list_view_array.rs:
##########
@@ -0,0 +1,1227 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::array::{get_offsets, get_sizes, make_array, print_long_array};
+use crate::builder::{GenericListViewBuilder, PrimitiveBuilder};
+use crate::iterator::GenericListViewArrayIter;
+use crate::{
+    new_empty_array, Array, ArrayAccessor, ArrayRef, ArrowPrimitiveType, 
FixedSizeListArray,
+    OffsetSizeTrait,
+};
+use arrow_buffer::{NullBuffer, OffsetBuffer, SizeBuffer};
+use arrow_data::{ArrayData, ArrayDataBuilder};
+use arrow_schema::{ArrowError, DataType, FieldRef};
+use std::any::Any;
+use std::ops::Add;
+use std::sync::Arc;
+
+/// A [`GenericListViewArray`] of variable size lists, storing offsets as 
`i32`.
+///
+// See [`ListViewBuilder`](crate::builder::ListViewBuilder) for how to 
construct a [`ListViewArray`]
+pub type ListViewArray = GenericListViewArray<i32>;
+
+/// A [`GenericListViewArray`] of variable size lists, storing offsets as 
`i64`.
+///
+// See [`LargeListViewArray`](crate::builder::LargeListViewBuilder) for how to 
construct a [`LargeListViewArray`]
+pub type LargeListViewArray = GenericListViewArray<i64>;
+
+///
+/// Different than [`crate::GenericListArray`] as it stores both an offset and 
length
+/// meaning that take / filter operations can be implemented without copying 
the underlying data.
+///
+/// [Variable-size List Layout: ListView Layout]: 
https://arrow.apache.org/docs/format/Columnar.html#listview-layout
+pub struct GenericListViewArray<OffsetSize: OffsetSizeTrait> {
+    data_type: DataType,
+    nulls: Option<NullBuffer>,
+    values: ArrayRef,
+    value_offsets: OffsetBuffer<OffsetSize>,
+    value_sizes: SizeBuffer<OffsetSize>,
+    len: usize,
+}
+
+impl<OffsetSize: OffsetSizeTrait> Clone for GenericListViewArray<OffsetSize> {
+    fn clone(&self) -> Self {
+        Self {
+            data_type: self.data_type.clone(),
+            nulls: self.nulls.clone(),
+            values: self.values.clone(),
+            value_offsets: self.value_offsets.clone(),
+            value_sizes: self.value_sizes.clone(),
+            len: self.len,
+        }
+    }
+}
+
+impl<OffsetSize: OffsetSizeTrait> GenericListViewArray<OffsetSize> {
+    /// The data type constructor of listview array.
+    /// The input is the schema of the child array and
+    /// the output is the [`DataType`], ListView or LargeListView.
+    pub const DATA_TYPE_CONSTRUCTOR: fn(FieldRef) -> DataType = if 
OffsetSize::IS_LARGE {
+        DataType::LargeListView
+    } else {
+        DataType::ListView
+    };
+
+    /// Returns the data type of the list view array
+    pub fn try_new(
+        field: FieldRef,
+        offsets: OffsetBuffer<OffsetSize>,
+        sizes: SizeBuffer<OffsetSize>,
+        values: ArrayRef,
+        nulls: Option<NullBuffer>,
+    ) -> Result<Self, ArrowError> {
+        let mut len = 0usize;
+        for i in 0..offsets.len() {
+            let offset = offsets[i].as_usize();
+            let size = sizes[i].as_usize();
+            if offset + size > values.len() {

Review Comment:
   Yes but this needs to be a checked addition as otherwise `offset + size` 
could overflow and therefore incorrectly pass this test



##########
arrow-data/src/data.rs:
##########
@@ -929,6 +944,26 @@ impl ArrayData {
         Ok(())
     }
 
+    fn validate_sizes<T: ArrowNativeType + num::Num + std::fmt::Display>(

Review Comment:
   This appears to be incorrect



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to