Skip to content

GH-46578: [C++][Statistics] Fix the ownership handling of arrow::ArrayData::statistics in arrow::ArrayData::CopyTo and arrow::ArrayDataViewOrCopy #46625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions cpp/src/arrow/array/array_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3958,6 +3958,7 @@ TEST_F(TestArrayDataStatistics, CopyAssignment) {
TEST_F(TestArrayDataStatistics, CopyTo) {
ASSERT_OK_AND_ASSIGN(auto copied_data,
data_->CopyTo(arrow::default_cpu_memory_manager()));
ASSERT_NE(copied_data->statistics, data_->statistics);

ASSERT_TRUE(copied_data->statistics->null_count.has_value());
ASSERT_EQ(null_count_, copied_data->statistics->null_count.value());
Expand All @@ -3972,6 +3973,17 @@ TEST_F(TestArrayDataStatistics, CopyTo) {
ASSERT_EQ(max_, std::get<int64_t>(copied_data->statistics->max.value()));
ASSERT_TRUE(copied_data->statistics->is_max_exact);
}
TEST_F(TestArrayDataStatistics, CopyToNullArray) {
NullArray array(10);
auto& array_data = array.data();
auto statistics = std::make_shared<ArrayStatistics>();
statistics->null_count = 10;
array.data()->statistics = statistics;
ASSERT_OK_AND_ASSIGN(auto copied_array_data,
array_data->CopyTo(arrow::default_cpu_memory_manager()));
ASSERT_NE(copied_array_data->statistics, array.statistics());
ASSERT_EQ(*copied_array_data->statistics, *array.statistics());
}

TEST_F(TestArrayDataStatistics, Slice) {
auto sliced_data = data_->Slice(0, 1);
Expand Down
15 changes: 13 additions & 2 deletions cpp/src/arrow/array/data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,13 @@ Result<std::shared_ptr<ArrayData>> CopyToImpl(const ArrayData& data,
Fn&& copy_fn) {
auto output = ArrayData::Make(data.type, data.length, data.null_count, data.offset);
output->buffers.resize(data.buffers.size());
bool is_viewed = true;
bool has_buffer = false;
for (auto&& [buf, out_buf] : internal::Zip(data.buffers, output->buffers)) {
if (buf) {
has_buffer = true;
ARROW_ASSIGN_OR_RAISE(out_buf, copy_fn(buf, to));
is_viewed &= (out_buf->address() == buf->address());
}
}

Expand All @@ -164,8 +168,15 @@ Result<std::shared_ptr<ArrayData>> CopyToImpl(const ArrayData& data,
if (data.dictionary) {
ARROW_ASSIGN_OR_RAISE(output->dictionary, CopyToImpl(*data.dictionary, to, copy_fn));
}

output->statistics = data.statistics;
if (is_viewed && has_buffer) {
output->statistics = data.statistics;
} else {
// Note that some types, like Null, use the same path for both copy and view
// operations. In such cases, it is assumed that arrow::ArrayData is also copied.
if (data.statistics != nullptr) {
output->statistics = std::make_shared<ArrayStatistics>(*data.statistics);
}
}

return output;
}
Expand Down
Loading
Loading