Skip to content

Commit d27b9d4

Browse files
committed
fix: avoid ListBuilder
Avoid a slow ListBuilder pathway when using to_arrow_list that uses our existing `list_from_list_view`, which has the fastpath for when a ListView is zero-copy to List anyway. Signed-off-by: Andrew Duffy <[email protected]>
1 parent 374882d commit d27b9d4

File tree

1 file changed

+4
-25
lines changed

1 file changed

+4
-25
lines changed

vortex-array/src/arrow/compute/to_arrow/canonical.rs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,11 @@ use crate::arrays::NullArray;
7171
use crate::arrays::PrimitiveArray;
7272
use crate::arrays::StructArray;
7373
use crate::arrays::VarBinViewArray;
74+
use crate::arrays::list_from_list_view;
7475
use crate::arrow::IntoArrowArray;
7576
use crate::arrow::array::ArrowArray;
7677
use crate::arrow::compute::ToArrowArgs;
7778
use crate::arrow::compute::to_arrow::null_buffer::to_null_buffer;
78-
use crate::builders::ArrayBuilder;
79-
use crate::builders::ListBuilder;
8079
use crate::compute::InvocationArgs;
8180
use crate::compute::Kernel;
8281
use crate::compute::Output;
@@ -530,28 +529,9 @@ fn to_arrow_list<O: IntegerPType + OffsetSizeTrait>(
530529
array: ListViewArray,
531530
element_field: Option<&FieldRef>,
532531
) -> VortexResult<ArrowArrayRef> {
533-
// Since `ListViewArray` can have lists stored out-of-order, we must rebuild the entire array.
534-
// We also can't use `list_from_list_view` because we need this specific `O` type for offsets.
535-
let mut list_builder = ListBuilder::<O>::with_capacity(
536-
array
537-
.dtype()
538-
.as_list_element_opt()
539-
.vortex_expect("`ListViewArray` somehow was not of type `List`")
540-
.clone(),
541-
array.dtype().nullability(),
542-
array.elements().len(), // This might be wrong, but it's better than nothing.
543-
array.len(),
544-
);
545-
546-
// TODO(connor)[ListView]: We can potentially make a generic version of `list_from_list_view`
547-
// over the offsets so we don't have to rewrite this.
548-
549-
list_builder.extend_from_array(&array.to_array());
550-
let list_array = list_builder.finish_into_list();
532+
// Convert listview -> list, via the fast path when possible.
533+
let list_array = list_from_list_view(array);
551534

552-
// Now that we have a normal `ListArray`, we can convert all the child arrays.
553-
554-
// Convert the child `elements` array to Arrow.
555535
let (elements, element_field) = {
556536
if let Some(element_field) = element_field {
557537
// Convert elements to the specific Arrow type the caller wants.
@@ -573,8 +553,7 @@ fn to_arrow_list<O: IntegerPType + OffsetSizeTrait>(
573553
};
574554

575555
// Convert the child `offsets` and `validity` array to Arrow.
576-
let offsets = list_array
577-
.offsets()
556+
let offsets = cast(list_array.offsets().as_ref(), &DType::from(O::PTYPE))?
578557
.to_primitive()
579558
.buffer::<O>()
580559
.into_arrow_offset_buffer();

0 commit comments

Comments
 (0)