From d9b4300213116bb4768d0366f6a6b7029be3128d Mon Sep 17 00:00:00 2001 From: Sumedh Pimplikar Date: Fri, 4 Dec 2026 15:16:57 +0530 Subject: [PATCH] Skip unnecessary work for empty chunked take indices Return a typed empty result when the indices have no chunks, without concatenating the values. Reuse the indices' chunk count for allocation and iteration in the non-empty case. --- .../compute/kernels/vector_selection_take_internal.cc | 9 +++++++-- cpp/src/arrow/compute/kernels/vector_selection_test.cc | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/cpp/src/arrow/compute/kernels/vector_selection_take_internal.cc b/cpp/src/arrow/compute/kernels/vector_selection_take_internal.cc index 1237aae35c62..80b3c1e8aa6a 100644 --- a/cpp/src/arrow/compute/kernels/vector_selection_take_internal.cc +++ b/cpp/src/arrow/compute/kernels/vector_selection_take_internal.cc @@ -581,6 +581,11 @@ class TakeMetaFunction : public MetaFunction { const std::shared_ptr& values, const std::shared_ptr& indices, const TakeOptions& options, ExecContext* ctx) { + const auto num_chunks = indices->num_chunks(); + if (num_chunks == 0) { + return std::make_shared(ArrayVector{}, values->type()); + } + // XXX: for every chunk in indices, values are gathered from all chunks in values to // form a new chunk in the result. Performing this concatenation is not ideal, but // greatly simplifies the implementation before something more efficient is @@ -589,8 +594,8 @@ class TakeMetaFunction : public MetaFunction { ChunkedArrayAsArray(values, ctx->memory_pool())); std::vector args = {std::move(values_array), {}}; std::vector> new_chunks; - new_chunks.resize(indices->num_chunks()); - for (int i = 0; i < indices->num_chunks(); i++) { + new_chunks.resize(num_chunks); + for (int i = 0; i < num_chunks; i++) { args[1] = indices->chunk(i); // XXX: this loop can use TakeCAA once it can handle ChunkedArray // without concatenating first diff --git a/cpp/src/arrow/compute/kernels/vector_selection_test.cc b/cpp/src/arrow/compute/kernels/vector_selection_test.cc index 5e38ac15306f..684752a392c2 100644 --- a/cpp/src/arrow/compute/kernels/vector_selection_test.cc +++ b/cpp/src/arrow/compute/kernels/vector_selection_test.cc @@ -2213,6 +2213,8 @@ TEST(TestTakeKernelWithChunkedIndices, TakeChunkedArray) { ASSERT_RAISES(IndexError, TakeCCC(int_ty, {}, {"[0]"}).Value(&chunked_arr)); ASSERT_RAISES(IndexError, TakeCCC(int_ty, {"[]"}, {"[0]"}).Value(&chunked_arr)); } + + AssertTakeCCC(int64(), {"[1, 2]", "[3, 4]"}, {}, {}); } TEST(TestTakeKernelWithTable, TakeTable) {