Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions arrow-array/src/array/list_view_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use arrow_buffer::{NullBuffer, ScalarBuffer};
use arrow_buffer::{Buffer, NullBuffer, ScalarBuffer};
use arrow_data::{ArrayData, ArrayDataBuilder};
use arrow_schema::{ArrowError, DataType, FieldRef};
use std::any::Any;
Expand Down Expand Up @@ -576,23 +576,25 @@ impl<OffsetSize: OffsetSizeTrait> From<FixedSizeListArray> for GenericListViewAr

impl<OffsetSize: OffsetSizeTrait> GenericListViewArray<OffsetSize> {
fn try_new_from_array_data(data: ArrayData) -> Result<Self, ArrowError> {
if data.buffers().len() != 2 {
return Err(ArrowError::InvalidArgumentError(format!(
"ListViewArray data should contain two buffers (value offsets & value sizes), had {}",
data.buffers().len()
)));
}
let (data_type, len, nulls, offset, buffers, child_data) = data.into_parts();

if data.child_data().len() != 1 {
return Err(ArrowError::InvalidArgumentError(format!(
"ListViewArray should contain a single child array (values array), had {}",
data.child_data().len()
)));
}
// ArrayData is valid, and verified type above
// buffer[0] is offsets, buffer[1] is sizes
let num_buffers = buffers.len();
let [offsets_buffer, sizes_buffer] : [Buffer; 2] = buffers.try_into().map_err(|_| {
ArrowError::InvalidArgumentError(format!(
"ListViewArray data should contain two buffers (value offsets & value sizes), had {num_buffers}",
))
})?;

let values = data.child_data()[0].clone();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This saves a real clone of ArrayData (and thus an allocation)

let num_child = child_data.len();
let [values]: [ArrayData; 1] = child_data.try_into().map_err(|_| {
ArrowError::InvalidArgumentError(format!(
"ListViewArray should contain a single child array (values array), had {num_child}",
))
})?;

if let Some(child_data_type) = Self::get_type(data.data_type()) {
if let Some(child_data_type) = Self::get_type(&data_type) {
if values.data_type() != child_data_type {
return Err(ArrowError::InvalidArgumentError(format!(
"{}ListViewArray's child datatype {:?} does not \
Expand All @@ -607,18 +609,17 @@ impl<OffsetSize: OffsetSizeTrait> GenericListViewArray<OffsetSize> {
"{}ListViewArray's datatype must be {}ListViewArray(). It is {:?}",
OffsetSize::PREFIX,
OffsetSize::PREFIX,
data.data_type()
data_type
)));
}

let values = make_array(values);
// ArrayData is valid, and verified type above
let value_offsets = ScalarBuffer::new(data.buffers()[0].clone(), data.offset(), data.len());
let value_sizes = ScalarBuffer::new(data.buffers()[1].clone(), data.offset(), data.len());
let value_offsets = ScalarBuffer::new(offsets_buffer, offset, len);
let value_sizes = ScalarBuffer::new(sizes_buffer, offset, len);

Ok(Self {
data_type: data.data_type().clone(),
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This saves a DataType::drop and a clone of NullBuffer -- probably not a huge deal but unecessary

nulls: data.nulls().cloned(),
data_type,
nulls,
values,
value_offsets,
value_sizes,
Expand Down
Loading