Skip to content

Commit a4e3909

Browse files
AnuragRaut08pitrou
andauthored
GH-51029: [C++] Fix MapArray validation with unknown null count (#51093)
### Rationale for this change `MapArray` validation uses `MayHaveNulls()` to check that the map child and keys contain no nulls. When an all-valid validity bitmap is present with an unknown null count, `MayHaveNulls()` can report that the array may contain nulls even though all values are valid. During a cast of a `MapArray`, this can cause validation to return an invalid status which is passed to `ARROW_CHECK_OK`, resulting in a process abort instead of a recoverable error. ### What changes are included in this PR? - Use `GetNullCount()` instead of `MayHaveNulls()` when validating the `MapArray` child and keys. - Add a regression test covering an all-valid validity bitmap with `kUnknownNullCount`. ### Are these changes tested? Yes, with existing and additional unit tests. ### Are there any user-facing changes? Only a bugfix. ### This PR contains a "Critical Fix". This fixes a bug that can cause a process crash when casting a valid `MapArray` whose keys carry an all-valid validity bitmap with an unknown null count. This is easily reproduced in Python: ```python import pyarrow as pa m = pa.array([{"a": "1"}, {"b": "2"}], type=pa.map_(pa.string(), pa.string())) m.filter(pa.array([False, True])).cast(pa.map_(pa.large_string(), pa.large_string())) ``` * GitHub Issue: #51029 Lead-authored-by: Anurag Raut <[email protected]> Co-authored-by: Antoine Pitrou <[email protected]> Signed-off-by: Antoine Pitrou <[email protected]>
1 parent a62870c commit a4e3909

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

‎cpp/src/arrow/array/array_list_test.cc‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,43 @@ TEST_F(TestMapArray, ValueBuilder) {
15321532
ASSERT_ARRAYS_EQUAL(*actual_list, map_as_list);
15331533
}
15341534

1535+
// GH-51029: Validate MapArray keys with an all-valid bitmap and unknown null count.
1536+
TEST_F(TestMapArray, ValidateKeysWithAllValidBitmap) {
1537+
auto keys = ArrayFromJSON(utf8(), R"(["a", "b"])");
1538+
auto items = ArrayFromJSON(int32(), "[1, 2]");
1539+
auto offsets = ArrayFromJSON(int32(), "[0, 1, 2]");
1540+
1541+
// Inject an all-valid validity bitmap with kUnknownNullCount.
1542+
auto keys_data = keys->data()->Copy();
1543+
keys_data->buffers[0] = ArrayFromJSON(boolean(), "[true, true]")->data()->buffers[1];
1544+
keys_data->null_count = kUnknownNullCount;
1545+
1546+
ASSERT_OK_AND_ASSIGN(auto result,
1547+
MapArray::FromArrays(offsets, MakeArray(keys_data), items));
1548+
ASSERT_OK(result->ValidateFull());
1549+
ASSERT_EQ(result->length(), 2);
1550+
}
1551+
1552+
TEST_F(TestMapArray, FromArraysWithAllValidOffsetsBitmap) {
1553+
auto offsets = ArrayFromJSON(int32(), "[0, 1, 2]");
1554+
auto keys = ArrayFromJSON(utf8(), R"(["a", "b"])");
1555+
auto items = ArrayFromJSON(int32(), "[1, 2]");
1556+
1557+
// Inject an all-valid validity bitmap with kUnknownNullCount.
1558+
auto offsets_data = offsets->data()->Copy();
1559+
offsets_data->buffers[0] =
1560+
ArrayFromJSON(boolean(), "[true, true, true]")->data()->buffers[1];
1561+
offsets_data->null_count = kUnknownNullCount;
1562+
offsets = MakeArray(offsets_data);
1563+
1564+
auto null_bitmap = ArrayFromJSON(boolean(), "[true, true]")->data()->buffers[1];
1565+
1566+
ASSERT_OK_AND_ASSIGN(auto result,
1567+
MapArray::FromArrays(offsets, keys, items, pool_, null_bitmap));
1568+
ASSERT_OK(result->ValidateFull());
1569+
ASSERT_EQ(result->length(), 2);
1570+
}
1571+
15351572
// ----------------------------------------------------------------------
15361573
// FixedSizeList tests
15371574

‎cpp/src/arrow/array/array_nested.cc‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Result<std::shared_ptr<typename TypeTraits<TYPE>::ArrayType>> ListArrayFromArray
115115
return Status::TypeError("List offsets must be ", OffsetArrowType::type_name());
116116
}
117117

118-
if (null_bitmap != nullptr && offsets.data()->MayHaveNulls()) {
118+
if (null_bitmap != nullptr && offsets.data()->GetNullCount() != 0) {
119119
return Status::Invalid(
120120
"Ambiguous to specify both validity map and offsets with nulls");
121121
}
@@ -826,7 +826,7 @@ Result<std::shared_ptr<Array>> MapArray::FromArraysInternal(
826826
return Status::Invalid("Map key and item arrays must be equal length");
827827
}
828828

829-
if (null_bitmap != nullptr && offsets->data()->MayHaveNulls()) {
829+
if (null_bitmap != nullptr && offsets->data()->GetNullCount() != 0) {
830830
return Status::Invalid(
831831
"Ambiguous to specify both validity map and offsets with nulls");
832832
}
@@ -835,7 +835,7 @@ Result<std::shared_ptr<Array>> MapArray::FromArraysInternal(
835835
return Status::NotImplemented("Null bitmap with offsets slice not supported.");
836836
}
837837

838-
if (offsets->data()->MayHaveNulls()) {
838+
if (offsets->data()->GetNullCount() != 0) {
839839
ARROW_ASSIGN_OR_RAISE(auto buffers,
840840
CleanListOffsets<MapType>(NULLPTR, *offsets, pool));
841841
return std::make_shared<MapArray>(type, offsets->length() - 1, std::move(buffers),
@@ -896,13 +896,13 @@ Status MapArray::ValidateChildData(
896896
if (pair_data->type->id() != Type::STRUCT) {
897897
return Status::Invalid("Map array child array should have struct type");
898898
}
899-
if (pair_data->MayHaveNulls()) {
899+
if (pair_data->GetNullCount() != 0) {
900900
return Status::Invalid("Map array child array should have no nulls");
901901
}
902902
if (pair_data->child_data.size() != 2) {
903903
return Status::Invalid("Map array child array should have two fields");
904904
}
905-
if (pair_data->child_data[0]->MayHaveNulls()) {
905+
if (pair_data->child_data[0]->GetNullCount() != 0) {
906906
return Status::Invalid("Map array keys array should have no nulls");
907907
}
908908
return Status::OK();

0 commit comments

Comments
 (0)