Skip to content

Commit e4baf6b

Browse files
authored
GH-38553 : [C++] Replace null_count with MayHaveNulls in ListArrayFromArray and MapArray (#41957)
### Rationale for this change Offsets could have `null_count() == -1` (`kUnknownNullCount`) meaning that offsets might contain nulls that are not accounted for which can produce failures (#38553) when working with `ListArray` or `MapArray`. `null_count()` should be replaced with `MayHaveNulls()`. ### What changes are included in this PR? `null_count` is replaced with `MayHaveNulls` in `ListArrayFromArray`, `MapArray::FromArraysInternal` and `MapArray::ValidateChildData`. Some tests had to be updated. ### Are these changes tested? Yes. ### Are there any user-facing changes? No. * GitHub Issue: #38553 Authored-by: AlenkaF <[email protected]> Signed-off-by: Felipe Oliveira Carvalho <[email protected]>
1 parent ac1eadb commit e4baf6b

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,7 @@ TEST_F(TestMapArray, FromArrays) {
13831383

13841384
// Null bitmap and offset with offset
13851385
ASSERT_RAISES(NotImplemented,
1386-
MapArray::FromArrays(offsets3->Slice(2), keys, items, pool_,
1386+
MapArray::FromArrays(offsets1->Slice(2), keys, items, pool_,
13871387
offsets3->data()->buffers[0]));
13881388
}
13891389

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

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

830-
if (null_bitmap != nullptr && offsets->null_count() > 0) {
830+
if (null_bitmap != nullptr && offsets->data()->MayHaveNulls()) {
831831
return Status::Invalid(
832832
"Ambiguous to specify both validity map and offsets with nulls");
833833
}
@@ -893,13 +893,13 @@ Status MapArray::ValidateChildData(
893893
if (pair_data->type->id() != Type::STRUCT) {
894894
return Status::Invalid("Map array child array should have struct type");
895895
}
896-
if (pair_data->null_count != 0) {
896+
if (pair_data->MayHaveNulls()) {
897897
return Status::Invalid("Map array child array should have no nulls");
898898
}
899899
if (pair_data->child_data.size() != 2) {
900900
return Status::Invalid("Map array child array should have two fields");
901901
}
902-
if (pair_data->child_data[0]->null_count != 0) {
902+
if (pair_data->child_data[0]->MayHaveNulls()) {
903903
return Status::Invalid("Map array keys array should have no nulls");
904904
}
905905
return Status::OK();

‎python/pyarrow/tests/test_array.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,18 @@ def test_list_array_types_from_arrays_fail(list_array_type, list_type_factory):
10131013
arr_slice.offsets, arr_slice.values, mask=arr_slice.is_null())
10141014

10151015

1016+
def test_map_cast():
1017+
# GH-38553
1018+
t = pa.map_(pa.int64(), pa.int64())
1019+
arr = pa.array([{1: 2}], type=t)
1020+
result = arr.cast(pa.map_(pa.int32(), pa.int64()))
1021+
1022+
t_expected = pa.map_(pa.int32(), pa.int64())
1023+
expected = pa.array([{1: 2}], type=t_expected)
1024+
1025+
assert result.equals(expected)
1026+
1027+
10161028
def test_map_labelled():
10171029
# ARROW-13735
10181030
t = pa.map_(pa.field("name", "string", nullable=False), "int64")
@@ -1105,7 +1117,7 @@ def test_map_from_arrays():
11051117

11061118
# error if null bitmap passed to sliced offset
11071119
msg2 = 'Null bitmap with offsets slice not supported.'
1108-
offsets = pa.array(offsets, pa.int32())
1120+
offsets = pa.array([0, 2, 2, 6], pa.int32())
11091121
with pytest.raises(pa.ArrowNotImplementedError, match=msg2):
11101122
pa.MapArray.from_arrays(offsets.slice(2), keys, items, pa.map_(
11111123
keys.type,

0 commit comments

Comments
 (0)