From 19b01f94acf711032f1e03421f209f36146f9048 Mon Sep 17 00:00:00 2001 From: Jin Shang Date: Sun, 20 Aug 2023 22:22:08 +0800 Subject: [PATCH] add util test --- cpp/src/arrow/array/array_test.cc | 13 +++++++++++++ cpp/src/arrow/array/util.cc | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/cpp/src/arrow/array/array_test.cc b/cpp/src/arrow/array/array_test.cc index e9d478f108584..2d33c2777a060 100644 --- a/cpp/src/arrow/array/array_test.cc +++ b/cpp/src/arrow/array/array_test.cc @@ -552,6 +552,19 @@ TEST_F(TestArray, TestMakeArrayOfNullUnion) { ASSERT_EQ(typed_union.raw_value_offsets()[i], 0); } } + + // zero-length nested types should have union children with length zero + auto list_union = list(dense_union({field("a", utf8()), field("b", int32())}, {0, 1})); + ASSERT_OK_AND_ASSIGN(auto list_union_nulls, MakeArrayOfNull(list_union, 0)); + ASSERT_OK(list_union_nulls->ValidateFull()); + ASSERT_EQ(list_union_nulls->null_count(), 0); + { + const auto& typed_list = checked_cast(*list_union_nulls); + const auto& typed_union = checked_cast(*typed_list.values()); + // Child field has length 0 + ASSERT_EQ(typed_union.field(0)->length(), 0); + ASSERT_EQ(typed_union.field(0)->null_count(), 0); + } } TEST_F(TestArray, TestValidateNullCount) { diff --git a/cpp/src/arrow/array/util.cc b/cpp/src/arrow/array/util.cc index 25525e7a67ca0..150a7901a33ff 100644 --- a/cpp/src/arrow/array/util.cc +++ b/cpp/src/arrow/array/util.cc @@ -572,7 +572,7 @@ class NullArrayFactory { int64_t child_length = length_; if (type.mode() == UnionMode::DENSE) { // For dense unions, we set the offsets to all zero and create children - // with length 1 + // with length 1 if length_ is non-zero out_->buffers.resize(3); out_->buffers[2] = buffer_;