From 79d9b3c397ef3b75c28d652b39c047aaade62aad Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 8 Apr 2024 14:50:02 +0200 Subject: [PATCH] GH-41016: [C++] Fix null count check in BooleanArray.true_count() --- cpp/src/arrow/array/array_primitive.cc | 2 +- cpp/src/arrow/array/array_test.cc | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cpp/src/arrow/array/array_primitive.cc b/cpp/src/arrow/array/array_primitive.cc index 7c4a14d93400f..37d762ae42d1f 100644 --- a/cpp/src/arrow/array/array_primitive.cc +++ b/cpp/src/arrow/array/array_primitive.cc @@ -56,7 +56,7 @@ int64_t BooleanArray::false_count() const { } int64_t BooleanArray::true_count() const { - if (data_->null_count.load() != 0) { + if (data_->GetNullCount() != 0) { DCHECK(data_->buffers[0]); return internal::CountAndSetBits(data_->buffers[0]->data(), data_->offset, data_->buffers[1]->data(), data_->offset, diff --git a/cpp/src/arrow/array/array_test.cc b/cpp/src/arrow/array/array_test.cc index 21ac1a09f56e7..d7cf07197baba 100644 --- a/cpp/src/arrow/array/array_test.cc +++ b/cpp/src/arrow/array/array_test.cc @@ -1307,6 +1307,14 @@ TEST(TestBooleanArray, TrueCountFalseCount) { CheckArray(checked_cast(*arr)); CheckArray(checked_cast(*arr->Slice(5))); CheckArray(checked_cast(*arr->Slice(0, 0))); + + // GH-41016 true_count() with array without validity buffer with null_count of -1 + auto data = ArrayFromJSON(boolean(), "[true, false, true]")->data(); + data->null_count = -1; + std::shared_ptr arr2(new BooleanArray(data)); + ASSERT_EQ(arr2->data()->null_count.load(), -1); + ASSERT_EQ(arr2->null_bitmap(), nullptr); + ASSERT_EQ(arr2->true_count(), 2); } TEST(TestPrimitiveAdHoc, TestType) {