Skip to content

Commit a48b6f9

Browse files
committed
Made suggested changes to UnsafeAppend() and its related functions
1 parent 17c3b07 commit a48b6f9

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

cpp/src/arrow/array/array_struct_test.cc

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,7 @@ TEST(TestFieldRef, GetChildren) {
748748
AssertArraysEqual(*a, *expected_a);
749749
}
750750

751-
TEST(TestStructBuilder, UnsafeAppend) {
752-
751+
TEST(TestStructBuilderUnsafe, UnsafeAppend) {
753752
auto int_type = int32();
754753
auto str_type = utf8();
755754
auto struct_type = struct_({field("a", int_type), field("b", str_type)});
@@ -758,25 +757,27 @@ TEST(TestStructBuilder, UnsafeAppend) {
758757
auto int_builder = std::make_shared<Int32Builder>(pool);
759758
auto str_builder = std::make_shared<StringBuilder>(pool);
760759
StructBuilder builder(struct_type, pool, {int_builder, str_builder});
761-
760+
761+
ASSERT_OK(builder.Reserve(2));
762+
762763
builder.UnsafeAppend();
763764
ASSERT_OK(int_builder->Append(1));
764765
ASSERT_OK(str_builder->Append("hello"));
765766

766767
builder.UnsafeAppend();
767768
ASSERT_OK(int_builder->Append(2));
768769
ASSERT_OK(str_builder->Append("arrow"));
769-
770+
770771
ASSERT_OK(builder.Finish(&final_array));
771772
ASSERT_EQ(2, final_array->length());
772773
ASSERT_EQ(0, final_array->null_count());
773774
auto expected_json = R"([{"a": 1, "b": "hello"}, {"a": 2, "b": "arrow"}])";
775+
774776
auto expected_array = ArrayFromJSON(struct_type, expected_json);
775-
ASSERT_TRUE(final_array->Equals(expected_array));
777+
AssertArraysEqual(*final_array, *expected_array);
776778
}
777779

778-
TEST(TestStructBuilder, UnsafeAppendNull) {
779-
780+
TEST(TestStructBuilderUnsafe, UnsafeAppendNull) {
780781
auto int_type = int32();
781782
auto str_type = utf8();
782783
auto struct_type = struct_({field("a", int_type), field("b", str_type)});
@@ -786,6 +787,8 @@ TEST(TestStructBuilder, UnsafeAppendNull) {
786787
auto str_builder = std::make_shared<StringBuilder>(pool);
787788
StructBuilder builder(struct_type, pool, {int_builder, str_builder});
788789

790+
ASSERT_OK(builder.Reserve(3));
791+
789792
builder.UnsafeAppend();
790793
ASSERT_OK(int_builder->Append(1));
791794
ASSERT_OK(str_builder->Append("hello"));
@@ -801,12 +804,12 @@ TEST(TestStructBuilder, UnsafeAppendNull) {
801804
ASSERT_EQ(1, final_array->null_count());
802805
ASSERT_TRUE(final_array->IsNull(1));
803806
auto expected_json = R"([{"a": 1, "b": "hello"}, null, {"a": 2, "b": "arrow"}])";
807+
804808
auto expected_array = ArrayFromJSON(struct_type, expected_json);
805-
ASSERT_TRUE(final_array->Equals(expected_array));
809+
AssertArraysEqual(*final_array, *expected_array);
806810
}
807811

808-
TEST(TestStructBuilder, UnsafeAppendNulls) {
809-
812+
TEST(TestStructBuilderUnsafe, UnsafeAppendNulls) {
810813
auto int_type = int32();
811814
auto str_type = utf8();
812815
auto struct_type = struct_({field("a", int_type), field("b", str_type)});
@@ -816,6 +819,8 @@ TEST(TestStructBuilder, UnsafeAppendNulls) {
816819
auto str_builder = std::make_shared<StringBuilder>(pool);
817820
StructBuilder builder(struct_type, pool, {int_builder, str_builder});
818821

822+
ASSERT_OK(builder.Reserve(4));
823+
819824
builder.UnsafeAppend();
820825
ASSERT_OK(int_builder->Append(1));
821826
ASSERT_OK(str_builder->Append("hello"));
@@ -827,13 +832,10 @@ TEST(TestStructBuilder, UnsafeAppendNulls) {
827832
ASSERT_OK(str_builder->Append("arrow"));
828833

829834
ASSERT_OK(builder.Finish(&final_array));
830-
ASSERT_EQ(4, final_array->length());
831-
ASSERT_EQ(2, final_array->null_count());
832-
ASSERT_TRUE(final_array->IsNull(1));
833-
ASSERT_TRUE(final_array->IsNull(2));
834-
auto expected_json = R"([{"a": 1, "b": "hello"}, null, null, {"a": 2, "b": "arrow"}])";
835-
auto expected_array = ArrayFromJSON(struct_type, expected_json);
836-
ASSERT_TRUE(final_array->Equals(expected_array));
835+
836+
auto expected_array = ArrayFromJSON(
837+
struct_type, R"([{"a": 1, "b": "hello"}, null, null, {"a": 2, "b": "arrow"}])");
838+
AssertArraysEqual(*final_array, *expected_array);
837839
}
838840

839-
} // namespace arrow
841+
} // namespace arrow

cpp/src/arrow/array/builder_nested.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -767,16 +767,14 @@ class ARROW_EXPORT StructBuilder : public ArrayBuilder {
767767
/// be called independently to maintain data-structure consistency.
768768
Status Append(bool is_valid = true) {
769769
ARROW_RETURN_NOT_OK(Reserve(1));
770-
UnsafeAppendToBitmap(is_valid);
770+
UnsafeAppend(is_valid);
771771
return Status::OK();
772772
}
773773

774-
/// This method is "unsafe" because it does not update the null bitmap
774+
/// This method is "unsafe" because it does not update the null bitmap.
775775
/// It doesn't check the capacity. Caller is responsible for calling Reserve()
776776
/// beforehand.
777-
void UnsafeAppend(){
778-
++length_;
779-
}
777+
void UnsafeAppend(bool is_valid = true) { UnsafeAppendToBitmap(is_valid); }
780778

781779
/// \brief Append a null value. Automatically appends an empty value to each child
782780
/// builder.
@@ -791,12 +789,11 @@ class ARROW_EXPORT StructBuilder : public ArrayBuilder {
791789
/// The caller is responsible for calling Reserve() beforehand to ensure
792790
/// there is enough space. This method will append nulls to all child
793791
/// builders to maintain a consistent state.
794-
Status UnsafeAppendNull(){
795-
null_bitmap_builder_.UnsafeAppend(false);
796-
for(const auto& child: children_){
797-
ARROW_RETURN_NOT_OK(child->AppendNull());
792+
Status UnsafeAppendNull() {
793+
UnsafeAppend(false);
794+
for (const auto& child : children_) {
795+
ARROW_RETURN_NOT_OK(child->AppendEmptyValue());
798796
}
799-
length_++;
800797
return Status::OK();
801798
}
802799

@@ -816,12 +813,14 @@ class ARROW_EXPORT StructBuilder : public ArrayBuilder {
816813
/// there is enough space. This method will append nulls to all child
817814
/// builders to maintain a consistent state.
818815
/// param length The number of null slots to append.
819-
Status UnsafeAppendNulls(int64_t length){
820-
null_bitmap_builder_.UnsafeAppend(length, false);
821-
for(const auto& child:children_){
822-
ARROW_RETURN_NOT_OK(child->AppendNulls(length));
816+
Status UnsafeAppendNulls(int64_t length) {
817+
for (int64_t i = 0; i < length; ++i) {
818+
UnsafeAppend(false);
819+
}
820+
821+
for (const auto& child : children_) {
822+
ARROW_RETURN_NOT_OK(child->AppendEmptyValues(length));
823823
}
824-
++length_;
825824
return Status::OK();
826825
}
827826

0 commit comments

Comments
 (0)