Skip to content

Commit 38d78c3

Browse files
authored
Support GenericListViewArray::new_unchecked and refactor ListView json decoder (#9648)
# Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. --> - Closes #9646. # Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> # What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> Support `GenericListViewArray::new_unchecked` and refactor ListView json decoder. Stacked on #9497, benchmark added in #9647 # Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Covered by existing tests # Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. If there are any breaking changes to public APIs, please call them out. --> No
1 parent 471f6c3 commit 38d78c3

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

arrow-array/src/array/list_view_array.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,35 @@ impl<OffsetSize: OffsetSizeTrait> GenericListViewArray<OffsetSize> {
223223
Self::try_new(field, offsets, sizes, values, nulls).unwrap()
224224
}
225225

226+
/// Create a new [`GenericListViewArray`] from the provided parts without validation
227+
///
228+
/// See [`Self::try_new`] for the checked version of this function, and the
229+
/// documentation of that function for the invariants that must be upheld.
230+
///
231+
/// # Safety
232+
///
233+
/// The parts must form a valid [`ListViewArray`] or [`LargeListViewArray`] according
234+
/// to the Arrow spec.
235+
pub unsafe fn new_unchecked(
236+
field: FieldRef,
237+
offsets: ScalarBuffer<OffsetSize>,
238+
sizes: ScalarBuffer<OffsetSize>,
239+
values: ArrayRef,
240+
nulls: Option<NullBuffer>,
241+
) -> Self {
242+
if cfg!(feature = "force_validate") {
243+
return Self::new(field, offsets, sizes, values, nulls);
244+
}
245+
246+
Self {
247+
data_type: Self::DATA_TYPE_CONSTRUCTOR(field),
248+
nulls,
249+
values,
250+
value_offsets: offsets,
251+
value_sizes: sizes,
252+
}
253+
}
254+
226255
/// Create a new [`GenericListViewArray`] of length `len` where all values are null
227256
pub fn new_null(field: FieldRef, len: usize) -> Self {
228257
let values = new_empty_array(field.data_type());

arrow-json/src/reader/list_array.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ use std::marker::PhantomData;
1919
use std::sync::Arc;
2020

2121
use arrow_array::builder::BooleanBufferBuilder;
22-
use arrow_array::{ArrayRef, GenericListArray, OffsetSizeTrait, make_array};
22+
use arrow_array::{ArrayRef, GenericListArray, GenericListViewArray, OffsetSizeTrait};
2323
use arrow_buffer::buffer::NullBuffer;
24-
use arrow_buffer::{Buffer, OffsetBuffer, ScalarBuffer};
25-
use arrow_data::ArrayDataBuilder;
24+
use arrow_buffer::{OffsetBuffer, ScalarBuffer};
2625
use arrow_schema::{ArrowError, DataType, FieldRef};
2726

2827
use crate::reader::tape::{Tape, TapeElement};
@@ -115,22 +114,17 @@ impl<O: OffsetSizeTrait, const IS_VIEW: bool> ArrayDecoder for ListLikeArrayDeco
115114
sizes.push(offsets[i] - offsets[i - 1]);
116115
}
117116
offsets.pop();
118-
let data_type = if O::IS_LARGE {
119-
DataType::LargeListView(self.field.clone())
120-
} else {
121-
DataType::ListView(self.field.clone())
122-
};
123117
// SAFETY: offsets and sizes are constructed correctly from the tape
124-
let array_data = unsafe {
125-
ArrayDataBuilder::new(data_type)
126-
.len(pos.len())
127-
.nulls(nulls)
128-
.child_data(vec![values.to_data()])
129-
.add_buffer(Buffer::from_vec(offsets))
130-
.add_buffer(Buffer::from_vec(sizes))
131-
.build_unchecked()
118+
let array = unsafe {
119+
GenericListViewArray::<O>::new_unchecked(
120+
self.field.clone(),
121+
ScalarBuffer::from(offsets),
122+
ScalarBuffer::from(sizes),
123+
values,
124+
nulls,
125+
)
132126
};
133-
Ok(make_array(array_data))
127+
Ok(Arc::new(array))
134128
} else {
135129
// SAFETY: offsets are built monotonically starting from 0
136130
let offsets = unsafe { OffsetBuffer::<O>::new_unchecked(ScalarBuffer::from(offsets)) };

0 commit comments

Comments
 (0)