Kikkon commented on code in PR #5723: URL: https://github.com/apache/arrow-rs/pull/5723#discussion_r1698524309
########## 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", Review Comment: (: Aha, I directly referenced the error messages from the list array for this part to maintain consistency. -- 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]
