Skip to content

Commit 843036f

Browse files
committed
Avoid a clone when creating RunEndArray from ArrayData
1 parent 9e822e0 commit 843036f

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

arrow-array/src/array/run_array.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,27 +213,39 @@ impl<R: RunEndIndexType> RunArray<R> {
213213
impl<R: RunEndIndexType> From<ArrayData> for RunArray<R> {
214214
// The method assumes the caller already validated the data using `ArrayData::validate_data()`
215215
fn from(data: ArrayData) -> Self {
216-
match data.data_type() {
216+
let (data_type, len, _nulls, offset, _buffers, mut child_data) = data.into_parts();
217+
218+
match &data_type {
217219
DataType::RunEndEncoded(_, _) => {}
218220
_ => {
219221
panic!(
220-
"Invalid data type for RunArray. The data type should be DataType::RunEndEncoded"
222+
"Invalid data type {data_type:?} for RunArray. Should be DataType::RunEndEncoded"
221223
);
222224
}
223225
}
224226

225227
// Safety
226228
// ArrayData is valid
227-
let child = &data.child_data()[0];
228-
assert_eq!(child.data_type(), &R::DATA_TYPE, "Incorrect run ends type");
229+
// child[0] is run ends , child[1] is values
230+
let values_child = child_data
231+
.pop()
232+
.expect("RunArray data should have two child arrays");
233+
let run_end_child = child_data
234+
.pop()
235+
.expect("RunArray data should have two child arrays");
236+
assert_eq!(
237+
run_end_child.data_type(),
238+
&R::DATA_TYPE,
239+
"Incorrect run ends type"
240+
);
229241
let run_ends = unsafe {
230-
let scalar = child.buffers()[0].clone().into();
231-
RunEndBuffer::new_unchecked(scalar, data.offset(), data.len())
242+
let scalar = run_end_child.buffers()[0].clone().into();
243+
RunEndBuffer::new_unchecked(scalar, offset, len)
232244
};
233245

234-
let values = make_array(data.child_data()[1].clone());
246+
let values = make_array(values_child);
235247
Self {
236-
data_type: data.data_type().clone(),
248+
data_type,
237249
run_ends,
238250
values,
239251
}

0 commit comments

Comments
 (0)