Skip to content

Commit 1d05bfb

Browse files
committed
Append IsNull and IsVaili with relevant tests
1 parent dd69699 commit 1d05bfb

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

cpp/src/arrow/array/array_test.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,33 @@ TEST_F(TestBuilder, TestResizeDownsize) {
12521252
ASSERT_GE(500, builder.capacity());
12531253
ASSERT_EQ(500, builder.length());
12541254
}
1255+
namespace {
1256+
1257+
void AssertBuilderValidityAtIndices(const ArrayBuilder& builder,
1258+
const std::vector<int64_t>& indices,
1259+
const std::vector<bool>& expected_validity) {
1260+
for (size_t i = 0; i < indices.size(); ++i) {
1261+
ASSERT_EQ(expected_validity[i], builder.IsValid(indices[i]));
1262+
}
1263+
}
1264+
1265+
} // namespace
1266+
1267+
TEST_F(TestBuilder, TestIsNull) {
1268+
NullBuilder null_builder(pool_);
1269+
ASSERT_OK(null_builder.AppendNull());
1270+
ASSERT_OK(null_builder.Append(nullptr));
1271+
AssertBuilderValidityAtIndices(null_builder, {0, 1}, {false, false});
1272+
1273+
Int32Builder int32_builder(pool_);
1274+
ASSERT_OK(int32_builder.Append(0));
1275+
ASSERT_OK(int32_builder.AppendNull());
1276+
ASSERT_OK(int32_builder.Append(2));
1277+
ASSERT_OK(int32_builder.AppendNulls(3));
1278+
ASSERT_OK(int32_builder.Append(6));
1279+
ASSERT_OK(int32_builder.AppendEmptyValues(3));
1280+
AssertBuilderValidityAtIndices(int32_builder, {0, 1, 4, 7}, {true, false, false, true});
1281+
}
12551282

12561283
template <typename Attrs>
12571284
class TestPrimitiveBuilder : public TestBuilder {

cpp/src/arrow/array/builder_base.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,31 @@ class ARROW_EXPORT ArrayBuilder {
111111

112112
int num_children() const { return static_cast<int>(children_.size()); }
113113

114+
/// \brief Return true if value at index is null. Does not boundscheck
115+
bool IsNull(int64_t i) const { return !IsValid(i); }
116+
117+
/// \brief Return true if value at index is valid (not null). Does not
118+
/// boundscheck.
119+
/// Note that this method does not work for types that do not have a
120+
/// top-level validity bitmap ( Union and Run-End Encoded (RLE) types).
121+
bool IsValid(int64_t i) const {
122+
if (type()->id() == Type::NA) {
123+
return false;
124+
} else if (type()->id() == Type::SPARSE_UNION) {
125+
// Not implemented
126+
return false;
127+
} else if (type()->id() == Type::DENSE_UNION) {
128+
// Not implemented
129+
return false;
130+
} else if (type()->id() == Type::RUN_END_ENCODED) {
131+
// Not implemented
132+
return false;
133+
} else {
134+
// NullType
135+
return bit_util::GetBit(null_bitmap_builder_.data(), i);
136+
}
137+
}
138+
114139
virtual int64_t length() const { return length_; }
115140
int64_t null_count() const { return null_count_; }
116141
int64_t capacity() const { return capacity_; }

0 commit comments

Comments
 (0)