From 1d05bfbd109c51bbd5d2ce2e71a82aa1cc2fd259 Mon Sep 17 00:00:00 2001 From: arash andishgar Date: Tue, 7 Jul 2026 07:37:15 +0330 Subject: [PATCH 1/2] Append IsNull and IsVaili with relevant tests --- cpp/src/arrow/array/array_test.cc | 27 +++++++++++++++++++++++++++ cpp/src/arrow/array/builder_base.h | 25 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/cpp/src/arrow/array/array_test.cc b/cpp/src/arrow/array/array_test.cc index dcfe1c76c301..d3ce98f3b7ef 100644 --- a/cpp/src/arrow/array/array_test.cc +++ b/cpp/src/arrow/array/array_test.cc @@ -1252,6 +1252,33 @@ TEST_F(TestBuilder, TestResizeDownsize) { ASSERT_GE(500, builder.capacity()); ASSERT_EQ(500, builder.length()); } +namespace { + +void AssertBuilderValidityAtIndices(const ArrayBuilder& builder, + const std::vector& indices, + const std::vector& expected_validity) { + for (size_t i = 0; i < indices.size(); ++i) { + ASSERT_EQ(expected_validity[i], builder.IsValid(indices[i])); + } +} + +} // namespace + +TEST_F(TestBuilder, TestIsNull) { + NullBuilder null_builder(pool_); + ASSERT_OK(null_builder.AppendNull()); + ASSERT_OK(null_builder.Append(nullptr)); + AssertBuilderValidityAtIndices(null_builder, {0, 1}, {false, false}); + + Int32Builder int32_builder(pool_); + ASSERT_OK(int32_builder.Append(0)); + ASSERT_OK(int32_builder.AppendNull()); + ASSERT_OK(int32_builder.Append(2)); + ASSERT_OK(int32_builder.AppendNulls(3)); + ASSERT_OK(int32_builder.Append(6)); + ASSERT_OK(int32_builder.AppendEmptyValues(3)); + AssertBuilderValidityAtIndices(int32_builder, {0, 1, 4, 7}, {true, false, false, true}); +} template class TestPrimitiveBuilder : public TestBuilder { diff --git a/cpp/src/arrow/array/builder_base.h b/cpp/src/arrow/array/builder_base.h index ecd2136f5d20..717859fc40e8 100644 --- a/cpp/src/arrow/array/builder_base.h +++ b/cpp/src/arrow/array/builder_base.h @@ -111,6 +111,31 @@ class ARROW_EXPORT ArrayBuilder { int num_children() const { return static_cast(children_.size()); } + /// \brief Return true if value at index is null. Does not boundscheck + bool IsNull(int64_t i) const { return !IsValid(i); } + + /// \brief Return true if value at index is valid (not null). Does not + /// boundscheck. + /// Note that this method does not work for types that do not have a + /// top-level validity bitmap ( Union and Run-End Encoded (RLE) types). + bool IsValid(int64_t i) const { + if (type()->id() == Type::NA) { + return false; + } else if (type()->id() == Type::SPARSE_UNION) { + // Not implemented + return false; + } else if (type()->id() == Type::DENSE_UNION) { + // Not implemented + return false; + } else if (type()->id() == Type::RUN_END_ENCODED) { + // Not implemented + return false; + } else { + // NullType + return bit_util::GetBit(null_bitmap_builder_.data(), i); + } + } + virtual int64_t length() const { return length_; } int64_t null_count() const { return null_count_; } int64_t capacity() const { return capacity_; } From b419a901dd195b7611eb1fd2babbce5ed13f817e Mon Sep 17 00:00:00 2001 From: arash andishgar Date: Tue, 7 Jul 2026 08:39:50 +0330 Subject: [PATCH 2/2] use switch_case instead of if --- cpp/src/arrow/array/builder_base.h | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/cpp/src/arrow/array/builder_base.h b/cpp/src/arrow/array/builder_base.h index 717859fc40e8..f9e9a728a82e 100644 --- a/cpp/src/arrow/array/builder_base.h +++ b/cpp/src/arrow/array/builder_base.h @@ -117,22 +117,16 @@ class ARROW_EXPORT ArrayBuilder { /// \brief Return true if value at index is valid (not null). Does not /// boundscheck. /// Note that this method does not work for types that do not have a - /// top-level validity bitmap ( Union and Run-End Encoded (RLE) types). + /// top-level validity bitmap (Union and Run-End Encoded (RLE) types). bool IsValid(int64_t i) const { - if (type()->id() == Type::NA) { - return false; - } else if (type()->id() == Type::SPARSE_UNION) { - // Not implemented - return false; - } else if (type()->id() == Type::DENSE_UNION) { - // Not implemented - return false; - } else if (type()->id() == Type::RUN_END_ENCODED) { - // Not implemented - return false; - } else { - // NullType - return bit_util::GetBit(null_bitmap_builder_.data(), i); + switch (type()->id()) { + case Type::NA: + case Type::SPARSE_UNION: + case Type::DENSE_UNION: + case Type::RUN_END_ENCODED: + return false; + default: + return bit_util::GetBit(null_bitmap_builder_.data(), i); } }