Skip to content
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
66 changes: 65 additions & 1 deletion cpp/src/arrow/array/array_run_end_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ TEST_P(TestRunEndEncodedArray, LogicalRunEnds) {
ASSERT_OK_AND_ASSIGN(logical_run_ends, ree_slice->LogicalRunEnds(pool));
ASSERT_ARRAYS_EQUAL(*logical_run_ends, *expected_run_ends);
}

TEST_P(TestRunEndEncodedArray, Builder) {
auto value_type = utf8();
auto ree_type = run_end_encoded(run_end_type, value_type);
Expand Down Expand Up @@ -366,6 +365,71 @@ TEST_P(TestRunEndEncodedArray, Builder) {
}
}
}
TEST_P(TestRunEndEncodedArray, BuilderAppendScalarsPrimitiveScalar) {
auto value_type = float32();
auto ree_type = run_end_encoded(run_end_type, value_type);

ASSERT_OK_AND_ASSIGN(std::shared_ptr<ArrayBuilder> builder, MakeBuilder(ree_type));

ASSERT_OK_AND_ASSIGN(auto v1, MakeScalar(float32(), 1.0f));
ASSERT_OK_AND_ASSIGN(auto v2, MakeScalar(float32(), 1.0f));
ASSERT_OK_AND_ASSIGN(auto v3, MakeScalar(float32(), 2.0f));
ASSERT_OK_AND_ASSIGN(auto v4, MakeScalar(float32(), 2.0f));
ASSERT_OK_AND_ASSIGN(auto v5, MakeScalar(float32(), 3.0f));

ScalarVector scalars = {v1, v2, v3, v4, v5};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to add an empty line after each individual statement, can we compact this a bit and only keep empty lines to distinguish between logically different sequences?

ASSERT_OK(builder->AppendScalars(scalars));

ASSERT_EQ(builder->length(), 5);

ASSERT_OK_AND_ASSIGN(auto array, builder->Finish());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also validate the result:

Suggested change
ASSERT_OK_AND_ASSIGN(auto array, builder->Finish());
ASSERT_OK_AND_ASSIGN(auto array, builder->Finish());
ASSERT_OK(array->ValidateFull());


auto ree_array = std::dynamic_pointer_cast<RunEndEncodedArray>(array);

ASSERT_NE(ree_array, NULLPTR);

auto expected_run_ends = ArrayFromJSON(run_end_type, "[2,4,5]");

auto expected_values = ArrayFromJSON(float32(), "[1,2,3]");

ASSERT_ARRAYS_EQUAL(*expected_run_ends, *ree_array->run_ends());

ASSERT_ARRAYS_EQUAL(*expected_values, *ree_array->values());
}

TEST_P(TestRunEndEncodedArray, BuilderAppendScalarsRunEndEncodedScalar) {
auto value_type = float32();
auto ree_type = run_end_encoded(run_end_type, value_type);

ASSERT_OK_AND_ASSIGN(std::shared_ptr<ArrayBuilder> builder, MakeBuilder(ree_type));

ASSERT_OK_AND_ASSIGN(auto s1, MakeScalar(ree_type, *MakeScalar(float32(), 1.0f)));
ASSERT_OK_AND_ASSIGN(auto s2, MakeScalar(ree_type, *MakeScalar(float32(), 1.0f)));
ASSERT_OK_AND_ASSIGN(auto s3, MakeScalar(ree_type, *MakeScalar(float32(), 2.0f)));
ASSERT_OK_AND_ASSIGN(auto s4, MakeScalar(ree_type, *MakeScalar(float32(), 2.0f)));
ASSERT_OK_AND_ASSIGN(auto s5, MakeScalar(ree_type, *MakeScalar(float32(), 3.0f)));

ScalarVector scalars = {s1, s2, s3, s4, s5};

ASSERT_OK(builder->AppendScalars(scalars));

ASSERT_EQ(builder->length(), 5);

ASSERT_OK_AND_ASSIGN(auto array, builder->Finish());

auto ree_array = std::dynamic_pointer_cast<RunEndEncodedArray>(array);

ASSERT_NE(ree_array, NULLPTR);

auto expected_run_ends = ArrayFromJSON(run_end_type, "[2,4,5]");

auto expected_values = ArrayFromJSON(float32(), "[1,2,3]");

ASSERT_ARRAYS_EQUAL(*expected_run_ends, *ree_array->run_ends());

ASSERT_ARRAYS_EQUAL(*expected_values, *ree_array->values());
}

TEST_P(TestRunEndEncodedArray, BuilderReuseAfterFinish) {
// GH-45532: RunEndEncodedBuilder should clear dimensions after a Finish() call
Expand Down
6 changes: 4 additions & 2 deletions cpp/src/arrow/array/builder_run_end.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ Status RunEndEncodedBuilder::AppendEmptyValues(int64_t length) {
UpdateDimensions(committed_logical_length_, 0);
return Status::OK();
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OTOH, let's please keep an empty line between function and method definitions.

Status RunEndEncodedBuilder::AppendScalar(const Scalar& scalar, int64_t n_repeats) {
if (scalar.type->id() == Type::RUN_END_ENCODED) {
return AppendScalar(*internal::checked_cast<const RunEndEncodedScalar&>(scalar).value,
Expand All @@ -213,7 +212,10 @@ Status RunEndEncodedBuilder::AppendScalar(const Scalar& scalar, int64_t n_repeat
}

Status RunEndEncodedBuilder::AppendScalars(const ScalarVector& scalars) {
RETURN_NOT_OK(this->ArrayBuilder::AppendScalars(scalars));
if (scalars.empty()) return Status::OK();
for (const auto& scalar : scalars) {
RETURN_NOT_OK(AppendScalar(*scalar, 1));
}
UpdateDimensions(committed_logical_length_, value_run_builder_->open_run_length());
return Status::OK();
}
Expand Down
Loading