Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions cpp/src/arrow/chunked_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,17 @@ std::shared_ptr<ChunkedArray> 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++;
}
Expand Down
31 changes: 31 additions & 0 deletions cpp/src/arrow/chunked_array_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <vector>

#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"
Expand Down Expand Up @@ -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<ArrayStatistics>();
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<ChunkedArray>(empty, int8());
Expand Down