Skip to content

Commit

Permalink
GH-37072: [C++] MakeArrayOfNulls should respect Field::nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
bkietz committed Oct 13, 2023
1 parent a2561e3 commit 807b476
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 227 deletions.
18 changes: 8 additions & 10 deletions cpp/src/arrow/array/array_run_end_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,7 @@ TEST_P(TestRunEndEncodedArray, Validate) {
BitmapFromVector<bool>({true, false}, &null_bitmap);
has_null_buffer->data()->buffers[0] = null_bitmap;
EXPECT_RAISES_WITH_MESSAGE_THAT(
Invalid,
::testing::HasSubstr(
std::string("Invalid: Run end encoded array should not have a null bitmap.")),
Invalid, ::testing::HasSubstr(std::string("should not have a null bitmap")),
has_null_buffer->Validate());

auto too_many_children = MakeArray(good_array->data()->Copy());
Expand All @@ -503,7 +501,7 @@ TEST_P(TestRunEndEncodedArray, Validate) {
values_nullptr->Validate());

auto run_ends_string = MakeArray(good_array->data()->Copy());
run_ends_string->data()->child_data[0] = values->data();
run_ends_string->data()->child_data[0] = MakeEmptyArray(utf8()).ValueOrDie()->data();
EXPECT_RAISES_WITH_MESSAGE_THAT(
Invalid,
::testing::HasSubstr(
Expand All @@ -529,10 +527,9 @@ TEST_P(TestRunEndEncodedArray, Validate) {
malformed_array->data()->buffers.emplace_back(NULLPTR);
EXPECT_RAISES_WITH_MESSAGE_THAT(
Invalid,
::testing::HasSubstr(
std::string(
"Invalid: Run ends array invalid: Expected 2 buffers in array of type ") +
run_end_type->ToString() + ", got 3"),
::testing::HasSubstr(std::string("Invalid: Run ends array invalid: Invalid: "
"Expected 2 buffers in array of type ") +
run_end_type->ToString() + ", got 3"),
run_ends_malformed->Validate());
}

Expand All @@ -543,8 +540,9 @@ TEST_P(TestRunEndEncodedArray, Validate) {
malformed_array->data()->buffers.emplace_back(NULLPTR);
EXPECT_RAISES_WITH_MESSAGE_THAT(
Invalid,
::testing::HasSubstr("Invalid: Values array invalid: Expected 2 buffers in array "
"of type int32, got 3"),
::testing::HasSubstr(
"Invalid: Values array invalid: Invalid: Expected 2 buffers in array "
"of type int32, got 3"),
values_malformed->Validate());
}

Expand Down
13 changes: 13 additions & 0 deletions cpp/src/arrow/array/array_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ static std::vector<std::shared_ptr<DataType>> TestArrayUtilitiesAgainstTheseType

TEST_F(TestArray, TestMakeArrayOfNull) {
for (int64_t length : {0, 1, 16, 133}) {
ARROW_SCOPED_TRACE("length = ", length);
for (auto type : TestArrayUtilitiesAgainstTheseTypes()) {
ARROW_SCOPED_TRACE("type = ", type->ToString());
ASSERT_OK_AND_ASSIGN(auto array, MakeArrayOfNull(type, length));
Expand Down Expand Up @@ -438,6 +439,18 @@ TEST_F(TestArray, TestMakeArrayOfNull) {
}
}
}

auto req = [](auto type) { return field("", std::move(type), /*nullable=*/false); };

// union with no nullable fields cannot represent a null
ASSERT_RAISES(Invalid, MakeArrayOfNull(dense_union({req(int8())}), 1));

// struct with no nullable fields has a top level bitmap and can mask them
ASSERT_OK_AND_ASSIGN(auto s, MakeArrayOfNull(struct_({req(int8())}), 1));
ASSERT_OK(s->ValidateFull());

ASSERT_OK_AND_ASSIGN(s, MakeArrayOfNull(struct_({req(dictionary(int8(), int8()))}), 1));
ASSERT_OK(s->ValidateFull());
}

TEST_F(TestArray, TestMakeArrayOfNullUnion) {
Expand Down
Loading

0 comments on commit 807b476

Please sign in to comment.