Skip to content

Commit

Permalink
Use pointer instead of embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Aug 28, 2024
1 parent 8a7d258 commit 4d5b234
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 47 deletions.
2 changes: 1 addition & 1 deletion cpp/src/arrow/array/array_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class ARROW_EXPORT Array {
/// object which backs this Array.
///
/// \return const ArrayStatistics&
const ArrayStatistics& statistics() const { return data_->statistics; }
std::shared_ptr<ArrayStatistics> statistics() const { return data_->statistics; }

protected:
Array() = default;
Expand Down
91 changes: 46 additions & 45 deletions cpp/src/arrow/array/array_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3721,11 +3721,12 @@ class TestArrayDataStatistics : public ::testing::Test {
values_buffer_ = Buffer::FromVector(values_);
data_ = ArrayData::Make(int32(), values_.size(), {null_buffer_, values_buffer_},
null_count_);
data_->statistics.null_count = null_count_;
data_->statistics.min = min_;
data_->statistics.is_min_exact = true;
data_->statistics.max = max_;
data_->statistics.is_max_exact = true;
data_->statistics = std::make_shared<ArrayStatistics>();
data_->statistics->null_count = null_count_;
data_->statistics->min = min_;
data_->statistics->is_min_exact = true;
data_->statistics->max = max_;
data_->statistics->is_max_exact = true;
}

protected:
Expand All @@ -3743,72 +3744,72 @@ TEST_F(TestArrayDataStatistics, MoveConstructor) {
ArrayData copied_data(*data_);
ArrayData moved_data(std::move(copied_data));

ASSERT_TRUE(moved_data.statistics.null_count.has_value());
ASSERT_EQ(null_count_, moved_data.statistics.null_count.value());
ASSERT_TRUE(moved_data.statistics->null_count.has_value());
ASSERT_EQ(null_count_, moved_data.statistics->null_count.value());

ASSERT_TRUE(moved_data.statistics.min.has_value());
ASSERT_TRUE(std::holds_alternative<int64_t>(moved_data.statistics.min.value()));
ASSERT_EQ(min_, std::get<int64_t>(moved_data.statistics.min.value()));
ASSERT_TRUE(moved_data.statistics.is_min_exact);
ASSERT_TRUE(moved_data.statistics->min.has_value());
ASSERT_TRUE(std::holds_alternative<int64_t>(moved_data.statistics->min.value()));
ASSERT_EQ(min_, std::get<int64_t>(moved_data.statistics->min.value()));
ASSERT_TRUE(moved_data.statistics->is_min_exact);

ASSERT_TRUE(moved_data.statistics.max.has_value());
ASSERT_TRUE(std::holds_alternative<int64_t>(moved_data.statistics.max.value()));
ASSERT_EQ(max_, std::get<int64_t>(moved_data.statistics.max.value()));
ASSERT_TRUE(moved_data.statistics.is_max_exact);
ASSERT_TRUE(moved_data.statistics->max.has_value());
ASSERT_TRUE(std::holds_alternative<int64_t>(moved_data.statistics->max.value()));
ASSERT_EQ(max_, std::get<int64_t>(moved_data.statistics->max.value()));
ASSERT_TRUE(moved_data.statistics->is_max_exact);
}

TEST_F(TestArrayDataStatistics, CopyConstructor) {
ArrayData copied_data(*data_);

ASSERT_TRUE(copied_data.statistics.null_count.has_value());
ASSERT_EQ(null_count_, copied_data.statistics.null_count.value());
ASSERT_TRUE(copied_data.statistics->null_count.has_value());
ASSERT_EQ(null_count_, copied_data.statistics->null_count.value());

ASSERT_TRUE(copied_data.statistics.min.has_value());
ASSERT_TRUE(std::holds_alternative<int64_t>(copied_data.statistics.min.value()));
ASSERT_EQ(min_, std::get<int64_t>(copied_data.statistics.min.value()));
ASSERT_TRUE(copied_data.statistics.is_min_exact);
ASSERT_TRUE(copied_data.statistics->min.has_value());
ASSERT_TRUE(std::holds_alternative<int64_t>(copied_data.statistics->min.value()));
ASSERT_EQ(min_, std::get<int64_t>(copied_data.statistics->min.value()));
ASSERT_TRUE(copied_data.statistics->is_min_exact);

ASSERT_TRUE(copied_data.statistics.max.has_value());
ASSERT_TRUE(std::holds_alternative<int64_t>(copied_data.statistics.max.value()));
ASSERT_EQ(max_, std::get<int64_t>(copied_data.statistics.max.value()));
ASSERT_TRUE(copied_data.statistics.is_max_exact);
ASSERT_TRUE(copied_data.statistics->max.has_value());
ASSERT_TRUE(std::holds_alternative<int64_t>(copied_data.statistics->max.value()));
ASSERT_EQ(max_, std::get<int64_t>(copied_data.statistics->max.value()));
ASSERT_TRUE(copied_data.statistics->is_max_exact);
}

TEST_F(TestArrayDataStatistics, MoveAssignment) {
ArrayData copied_data(*data_);
ArrayData moved_data;
moved_data = std::move(copied_data);

ASSERT_TRUE(moved_data.statistics.null_count.has_value());
ASSERT_EQ(null_count_, moved_data.statistics.null_count.value());
ASSERT_TRUE(moved_data.statistics->null_count.has_value());
ASSERT_EQ(null_count_, moved_data.statistics->null_count.value());

ASSERT_TRUE(moved_data.statistics.min.has_value());
ASSERT_TRUE(std::holds_alternative<int64_t>(moved_data.statistics.min.value()));
ASSERT_EQ(min_, std::get<int64_t>(moved_data.statistics.min.value()));
ASSERT_TRUE(moved_data.statistics.is_min_exact);
ASSERT_TRUE(moved_data.statistics->min.has_value());
ASSERT_TRUE(std::holds_alternative<int64_t>(moved_data.statistics->min.value()));
ASSERT_EQ(min_, std::get<int64_t>(moved_data.statistics->min.value()));
ASSERT_TRUE(moved_data.statistics->is_min_exact);

ASSERT_TRUE(moved_data.statistics.max.has_value());
ASSERT_TRUE(std::holds_alternative<int64_t>(moved_data.statistics.max.value()));
ASSERT_EQ(max_, std::get<int64_t>(moved_data.statistics.max.value()));
ASSERT_TRUE(moved_data.statistics.is_max_exact);
ASSERT_TRUE(moved_data.statistics->max.has_value());
ASSERT_TRUE(std::holds_alternative<int64_t>(moved_data.statistics->max.value()));
ASSERT_EQ(max_, std::get<int64_t>(moved_data.statistics->max.value()));
ASSERT_TRUE(moved_data.statistics->is_max_exact);
}

TEST_F(TestArrayDataStatistics, CopyAssignment) {
ArrayData copied_data;
copied_data = *data_;

ASSERT_TRUE(copied_data.statistics.null_count.has_value());
ASSERT_EQ(null_count_, copied_data.statistics.null_count.value());
ASSERT_TRUE(copied_data.statistics->null_count.has_value());
ASSERT_EQ(null_count_, copied_data.statistics->null_count.value());

ASSERT_TRUE(copied_data.statistics.min.has_value());
ASSERT_TRUE(std::holds_alternative<int64_t>(copied_data.statistics.min.value()));
ASSERT_EQ(min_, std::get<int64_t>(copied_data.statistics.min.value()));
ASSERT_TRUE(copied_data.statistics.is_min_exact);
ASSERT_TRUE(copied_data.statistics->min.has_value());
ASSERT_TRUE(std::holds_alternative<int64_t>(copied_data.statistics->min.value()));
ASSERT_EQ(min_, std::get<int64_t>(copied_data.statistics->min.value()));
ASSERT_TRUE(copied_data.statistics->is_min_exact);

ASSERT_TRUE(copied_data.statistics.max.has_value());
ASSERT_TRUE(std::holds_alternative<int64_t>(copied_data.statistics.max.value()));
ASSERT_EQ(max_, std::get<int64_t>(copied_data.statistics.max.value()));
ASSERT_TRUE(copied_data.statistics.is_max_exact);
ASSERT_TRUE(copied_data.statistics->max.has_value());
ASSERT_TRUE(std::holds_alternative<int64_t>(copied_data.statistics->max.value()));
ASSERT_EQ(max_, std::get<int64_t>(copied_data.statistics->max.value()));
ASSERT_TRUE(copied_data.statistics->is_max_exact);
}

template <typename PType>
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/array/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ struct ARROW_EXPORT ArrayData {
std::shared_ptr<ArrayData> dictionary;

// The statistics for this Array.
ArrayStatistics statistics{};
std::shared_ptr<ArrayStatistics> statistics;
};

/// \brief A non-owning Buffer reference
Expand Down

0 comments on commit 4d5b234

Please sign in to comment.