From d859c14d75f7b7c0e3d073bf524b194b36288eac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aurantium=F0=9F=8D=8A?= <280805072+Aurantium-47@users.noreply.github.com> Date: Sun, 22 Nov 2026 02:39:51 -0800 Subject: [PATCH] GH-35126: [C++] Reuse full plain chunks in ChunkedArray::Slice Reuse existing Array wrappers for complete aligned plain chunks. Retain the slicing path for boundaries, statistics, nested data, dictionaries and extension arrays. --- cpp/src/arrow/chunked_array.cc | 13 ++++++++++-- cpp/src/arrow/chunked_array_test.cc | 31 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/cpp/src/arrow/chunked_array.cc b/cpp/src/arrow/chunked_array.cc index d5bb1a8c1e9e..5ac9fb22d378 100644 --- a/cpp/src/arrow/chunked_array.cc +++ b/cpp/src/arrow/chunked_array.cc @@ -201,8 +201,17 @@ std::shared_ptr ChunkedArray::Slice(int64_t offset, int64_t length new_chunks.push_back(chunk(std::min(curr_chunk, num_chunks() - 1))->Slice(0, 0)); } else { while (curr_chunk < num_chunks() && length > 0) { - new_chunks.push_back(chunk(curr_chunk)->Slice(offset, length)); - length -= chunk(curr_chunk)->length() - offset; + const auto& current = chunk(curr_chunk); + const auto& data = *current->data(); + if (offset == 0 && length >= current->length() && !data.statistics && + data.child_data.empty() && !data.dictionary && + current->type_id() != Type::EXTENSION) { + // No new bounds or statistics handling is needed for a full plain chunk. + new_chunks.push_back(current); + } else { + new_chunks.push_back(current->Slice(offset, length)); + } + length -= current->length() - offset; offset = 0; curr_chunk++; } diff --git a/cpp/src/arrow/chunked_array_test.cc b/cpp/src/arrow/chunked_array_test.cc index 1862e308b38e..4fce11e0406b 100644 --- a/cpp/src/arrow/chunked_array_test.cc +++ b/cpp/src/arrow/chunked_array_test.cc @@ -24,6 +24,7 @@ #include #include "arrow/array/builder_run_end.h" +#include "arrow/array/statistics.h" #include "arrow/chunk_resolver.h" #include "arrow/scalar.h" #include "arrow/status.h" @@ -276,6 +277,36 @@ TEST_F(TestChunkedArray, SliceEquals) { ASSERT_TRUE(slice5->type()->Equals(one_->type())); } +TEST_F(TestChunkedArray, SliceSharesPlainFullChunks) { + auto head = ArrayFromJSON(int32(), "[null, 2]"); + auto middle = ArrayFromJSON(int32(), "[9, 3, null, 7]")->Slice(1, 2); + auto empty = middle->Slice(1, 0); + auto tail = ArrayFromJSON(int32(), "[5, 6]"); + ChunkedArray input({head, empty, middle, tail}); + auto result = input.Slice(1, 4); + ASSERT_EQ(result->num_chunks(), 4); + EXPECT_EQ(result->chunk(1), empty); + EXPECT_EQ(result->chunk(2), middle); + EXPECT_EQ(result->chunk(2)->offset(), 1); + AssertArraysEqual(*ArrayFromJSON(int32(), "[2]"), *result->chunk(0)); + AssertArraysEqual(*ArrayFromJSON(int32(), "[3, null]"), *result->chunk(2)); + AssertArraysEqual(*ArrayFromJSON(int32(), "[5]"), *result->chunk(3)); +} + +TEST_F(TestChunkedArray, SliceDropsStatisticsOnAlignedChunks) { + auto chunk_with_statistics = ArrayFromJSON(int32(), "[1, null]"); + auto statistics = std::make_shared(); + statistics->null_count = int64_t{1}; + chunk_with_statistics->data()->statistics = statistics; + ChunkedArray input({chunk_with_statistics}); + + auto result = input.Slice(0, input.length()); + ASSERT_EQ(result->num_chunks(), 1); + AssertArraysEqual(*chunk_with_statistics, *result->chunk(0)); + EXPECT_EQ(chunk_with_statistics->data()->statistics, statistics); + EXPECT_EQ(result->chunk(0)->data()->statistics, nullptr); +} + TEST_F(TestChunkedArray, ZeroChunksIssues) { ArrayVector empty = {}; auto no_chunks = std::make_shared(empty, int8());