Kikkon commented on code in PR #5723:
URL: https://github.com/apache/arrow-rs/pull/5723#discussion_r1698542635


##########
arrow-array/src/array/list_view_array.rs:
##########
@@ -0,0 +1,938 @@
+// 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 std::any::Any;
+use std::sync::Arc;
+
+use arrow_buffer::{NullBuffer, ScalarBuffer};
+use arrow_data::{ArrayData, ArrayDataBuilder};
+use arrow_schema::{ArrowError, DataType, FieldRef};
+
+use crate::array::{make_array, print_long_array};
+use crate::iterator::GenericListViewArrayIter;
+use crate::{new_empty_array, Array, ArrayAccessor, ArrayRef, 
FixedSizeListArray, OffsetSizeTrait};
+
+/// 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 from [`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>,
+}
+
+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(),
+        }
+    }
+}
+
+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> {
+        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()
+            )));
+        }
+
+        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.saturating_add(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()
+                )));
+            }
+        }
+
+        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,
+        })
+    }
+
+    /// 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::from(vec![]),
+            value_sizes: ScalarBuffer::from(vec![]),
+            values,
+        }
+    }
+
+    /// 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 offset = self.value_offsets()[i].as_usize();
+        let length = self.value_sizes()[i].as_usize();
+        self.values.slice(offset, length)
+    }
+
+    /// 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 {

Review Comment:
   You are right! I changed the naming and completed the corresponding methods.



-- 
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