diff --git a/cpp/src/arrow/array/array_test.cc b/cpp/src/arrow/array/array_test.cc index f128f7236f0..69a31266692 100644 --- a/cpp/src/arrow/array/array_test.cc +++ b/cpp/src/arrow/array/array_test.cc @@ -38,6 +38,7 @@ #include "arrow/array/builder_binary.h" #include "arrow/array/builder_decimal.h" #include "arrow/array/builder_dict.h" +#include "arrow/array/builder_nested.h" #include "arrow/array/builder_primitive.h" #include "arrow/array/builder_run_end.h" #include "arrow/array/builder_time.h" @@ -1003,6 +1004,50 @@ TEST_F(TestArray, TestAppendArraySlice) { } } +// Reserve space for `length` empty values in `builder` and its children +static void ReserveEmptyValues(ArrayBuilder* builder, int64_t length) { + ASSERT_OK(builder->Reserve(length)); + if (builder->type()->id() == Type::FIXED_SIZE_LIST) { + // The value builder is not registered as a child + auto list_size = checked_cast(*builder->type()).list_size(); + ReserveEmptyValues(checked_cast(builder)->value_builder(), + length * list_size); + return; + } + for (int i = 0; i < builder->num_children(); ++i) { + ReserveEmptyValues(builder->child(i), length); + } +} + +TEST_F(TestArray, TestUnsafeAppendEmptyValue) { + auto types = TestArrayUtilitiesAgainstTheseTypes(); + types.push_back(struct_({field("a", fixed_size_list(int16(), 2)), + field("b", struct_({field("c", boolean())}))})); + for (const auto& type : types) { + if (type->id() == Type::EXTENSION) continue; // no builder + ARROW_SCOPED_TRACE("type = ", type->ToString()); + std::unique_ptr builder, unsafe_builder; + ASSERT_OK(MakeBuilder(pool_, type, &builder)); + ASSERT_OK(MakeBuilder(pool_, type, &unsafe_builder)); + + ASSERT_OK(builder->AppendEmptyValue()); + ASSERT_OK(builder->AppendEmptyValues(0)); + ASSERT_OK(builder->AppendEmptyValues(3)); + + ReserveEmptyValues(unsafe_builder.get(), 4); + unsafe_builder->UnsafeAppendEmptyValue(); + unsafe_builder->UnsafeAppendEmptyValues(0); + unsafe_builder->UnsafeAppendEmptyValues(3); + ASSERT_EQ(unsafe_builder->length(), 4); + + ASSERT_OK_AND_ASSIGN(auto expected, builder->Finish()); + ASSERT_OK_AND_ASSIGN(auto actual, unsafe_builder->Finish()); + // Not ValidateFull(): empty dictionary values index into an empty dictionary + ASSERT_OK(actual->Validate()); + AssertArraysEqual(*expected, *actual, /*verbose=*/true); + } +} + class TestBuilderAppendArraySlice : public TestArray { public: virtual void AssertResult(const Array& expected, const Array& actual) { diff --git a/cpp/src/arrow/array/builder_base.cc b/cpp/src/arrow/array/builder_base.cc index eea394977f4..7128848584b 100644 --- a/cpp/src/arrow/array/builder_base.cc +++ b/cpp/src/arrow/array/builder_base.cc @@ -316,6 +316,12 @@ struct DerefConstIterator { } // namespace +void ArrayBuilder::UnsafeAppendEmptyValue() { ARROW_CHECK_OK(AppendEmptyValue()); } + +void ArrayBuilder::UnsafeAppendEmptyValues(int64_t length) { + ARROW_CHECK_OK(AppendEmptyValues(length)); +} + Status ArrayBuilder::AppendScalar(const Scalar& scalar, int64_t n_repeats) { if (!scalar.type->Equals(type())) { return Status::Invalid("Cannot append scalar of type ", scalar.type->ToString(), diff --git a/cpp/src/arrow/array/builder_base.h b/cpp/src/arrow/array/builder_base.h index 0cd57ff04da..01bce5c0c26 100644 --- a/cpp/src/arrow/array/builder_base.h +++ b/cpp/src/arrow/array/builder_base.h @@ -167,6 +167,22 @@ class ARROW_EXPORT ArrayBuilder { /// This method is useful when appending null values to a parent nested type. virtual Status AppendEmptyValues(int64_t length) = 0; + /// \brief Append a non-null value to builder, without checking capacity + /// + /// Same as AppendEmptyValue(), but the caller must have reserved enough + /// space beforehand. For nested types, this includes the child builders. + /// + /// The default implementation falls back on AppendEmptyValue(). + virtual void UnsafeAppendEmptyValue(); + + /// \brief Append a number of non-null values to builder, without checking capacity + /// + /// Same as AppendEmptyValues(), but the caller must have reserved enough + /// space beforehand. For nested types, this includes the child builders. + /// + /// The default implementation falls back on AppendEmptyValues(). + virtual void UnsafeAppendEmptyValues(int64_t length); + /// \brief Append a value from a scalar Status AppendScalar(const Scalar& scalar) { return AppendScalar(scalar, 1); } virtual Status AppendScalar(const Scalar& scalar, int64_t n_repeats); diff --git a/cpp/src/arrow/array/builder_binary.cc b/cpp/src/arrow/array/builder_binary.cc index 19c4c2d523f..a6bafe6c21a 100644 --- a/cpp/src/arrow/array/builder_binary.cc +++ b/cpp/src/arrow/array/builder_binary.cc @@ -145,15 +145,13 @@ Status FixedSizeBinaryBuilder::AppendNulls(int64_t length) { Status FixedSizeBinaryBuilder::AppendEmptyValue() { RETURN_NOT_OK(Reserve(1)); - UnsafeAppendToBitmap(true); - byte_builder_.UnsafeAppend(/*num_copies=*/byte_width_, 0); + UnsafeAppendEmptyValue(); return Status::OK(); } Status FixedSizeBinaryBuilder::AppendEmptyValues(int64_t length) { RETURN_NOT_OK(Reserve(length)); - UnsafeAppendToBitmap(length, true); - byte_builder_.UnsafeAppend(/*num_copies=*/length * byte_width_, 0); + UnsafeAppendEmptyValues(length); return Status::OK(); } diff --git a/cpp/src/arrow/array/builder_binary.h b/cpp/src/arrow/array/builder_binary.h index 12f7e45f342..975a73a30c4 100644 --- a/cpp/src/arrow/array/builder_binary.h +++ b/cpp/src/arrow/array/builder_binary.h @@ -125,18 +125,13 @@ class BaseBinaryBuilder Status AppendEmptyValue() final { ARROW_RETURN_NOT_OK(Reserve(1)); - UnsafeAppendNextOffset(); - UnsafeAppendToBitmap(true); + UnsafeAppendEmptyValue(); return Status::OK(); } Status AppendEmptyValues(int64_t length) final { - const int64_t num_bytes = value_data_builder_.length(); ARROW_RETURN_NOT_OK(Reserve(length)); - for (int64_t i = 0; i < length; ++i) { - offsets_builder_.UnsafeAppend(static_cast(num_bytes)); - } - UnsafeAppendToBitmap(length, true); + UnsafeAppendEmptyValues(length); return Status::OK(); } @@ -178,12 +173,20 @@ class BaseBinaryBuilder UnsafeAppendToBitmap(false); } - void UnsafeAppendEmptyValue() { + void UnsafeAppendEmptyValue() final { const int64_t num_bytes = value_data_builder_.length(); offsets_builder_.UnsafeAppend(static_cast(num_bytes)); UnsafeAppendToBitmap(true); } + void UnsafeAppendEmptyValues(int64_t length) final { + const int64_t num_bytes = value_data_builder_.length(); + for (int64_t i = 0; i < length; ++i) { + offsets_builder_.UnsafeAppend(static_cast(num_bytes)); + } + UnsafeAppendToBitmap(length, true); + } + /// \brief Append a sequence of strings in one shot. /// /// \param[in] values a vector of strings @@ -670,16 +673,14 @@ class ARROW_EXPORT BinaryViewBuilder : public ArrayBuilder { /// \brief Append a empty element (length-0 inline string) Status AppendEmptyValue() final { ARROW_RETURN_NOT_OK(Reserve(1)); - data_builder_.UnsafeAppend(BinaryViewType::c_type{}); - UnsafeAppendToBitmap(true); + UnsafeAppendEmptyValue(); return Status::OK(); } /// \brief Append several empty elements Status AppendEmptyValues(int64_t length) final { ARROW_RETURN_NOT_OK(Reserve(length)); - data_builder_.UnsafeAppend(length, BinaryViewType::c_type{}); - UnsafeSetNotNull(length); + UnsafeAppendEmptyValues(length); return Status::OK(); } @@ -688,11 +689,16 @@ class ARROW_EXPORT BinaryViewBuilder : public ArrayBuilder { UnsafeAppendToBitmap(false); } - void UnsafeAppendEmptyValue() { + void UnsafeAppendEmptyValue() final { data_builder_.UnsafeAppend(BinaryViewType::c_type{}); UnsafeAppendToBitmap(true); } + void UnsafeAppendEmptyValues(int64_t length) final { + data_builder_.UnsafeAppend(length, BinaryViewType::c_type{}); + UnsafeSetNotNull(length); + } + /// \brief Append a slice of a BinaryViewArray passed as an ArraySpan. Copies /// the underlying out-of-line string memory to avoid memory lifetime issues Status AppendArraySlice(const ArraySpan& array, int64_t offset, @@ -820,6 +826,16 @@ class ARROW_EXPORT FixedSizeBinaryBuilder : public ArrayBuilder { byte_builder_.UnsafeAppend(/*num_copies=*/byte_width_, 0); } + void UnsafeAppendEmptyValue() final { + UnsafeAppendToBitmap(true); + byte_builder_.UnsafeAppend(/*num_copies=*/byte_width_, 0); + } + + void UnsafeAppendEmptyValues(int64_t length) final { + UnsafeAppendToBitmap(length, true); + byte_builder_.UnsafeAppend(/*num_copies=*/length * byte_width_, 0); + } + Status ValidateOverflow(int64_t new_bytes) const { auto new_size = byte_builder_.length() + new_bytes; if (ARROW_PREDICT_FALSE(new_size > memory_limit())) { diff --git a/cpp/src/arrow/array/builder_nested.cc b/cpp/src/arrow/array/builder_nested.cc index 915fbfbf895..3e480d5777c 100644 --- a/cpp/src/arrow/array/builder_nested.cc +++ b/cpp/src/arrow/array/builder_nested.cc @@ -247,6 +247,16 @@ Status FixedSizeListBuilder::AppendEmptyValues(int64_t length) { return value_builder_->AppendEmptyValues(list_size_ * length); } +void FixedSizeListBuilder::UnsafeAppendEmptyValue() { + UnsafeAppendToBitmap(true); + value_builder_->UnsafeAppendEmptyValues(list_size_); +} + +void FixedSizeListBuilder::UnsafeAppendEmptyValues(int64_t length) { + UnsafeAppendToBitmap(length, true); + value_builder_->UnsafeAppendEmptyValues(list_size_ * length); +} + Status FixedSizeListBuilder::Resize(int64_t capacity) { RETURN_NOT_OK(CheckCapacity(capacity)); return ArrayBuilder::Resize(capacity); diff --git a/cpp/src/arrow/array/builder_nested.h b/cpp/src/arrow/array/builder_nested.h index fdbeb0cd7d1..68c092c2f49 100644 --- a/cpp/src/arrow/array/builder_nested.h +++ b/cpp/src/arrow/array/builder_nested.h @@ -146,7 +146,11 @@ class VarLengthListLikeBuilder : public ArrayBuilder { /// /// \post Another call to Append* or Finish should be made before appending to /// the values builder to ensure list slot remains empty - Status AppendEmptyValue() final { return Append(true, 0); } + Status AppendEmptyValue() final { + ARROW_RETURN_NOT_OK(Reserve(1)); + UnsafeAppendEmptyValue(); + return Status::OK(); + } /// \brief Append an empty list slot /// @@ -154,9 +158,26 @@ class VarLengthListLikeBuilder : public ArrayBuilder { /// the values builder to ensure the last list slot remains empty Status AppendEmptyValues(int64_t length) final { ARROW_RETURN_NOT_OK(Reserve(length)); + UnsafeAppendEmptyValues(length); + return Status::OK(); + } + + /// \brief Append an empty list slot, without checking capacity + /// + /// \post Another call to Append* or Finish should be made before appending to + /// the values builder to ensure list slot remains empty + void UnsafeAppendEmptyValue() final { + UnsafeAppendToBitmap(true); + UnsafeAppendDimensions(/*offset=*/value_builder_->length(), /*size=*/0); + } + + /// \brief Append empty list slots, without checking capacity + /// + /// \post Another call to Append* or Finish should be made before appending to + /// the values builder to ensure the last list slot remains empty + void UnsafeAppendEmptyValues(int64_t length) final { UnsafeAppendToBitmap(length, true); UnsafeAppendEmptyDimensions(/*num_values=*/length); - return Status::OK(); } /// \brief Vector append @@ -700,6 +721,18 @@ class ARROW_EXPORT FixedSizeListBuilder : public ArrayBuilder { Status AppendEmptyValues(int64_t length) final; + /// \brief Append an empty fixed size list, without checking capacity + /// + /// The caller must also have reserved enough space in the child builder, + /// as one list's worth of empty values is appended to it. + void UnsafeAppendEmptyValue() final; + + /// \brief Append empty fixed size lists, without checking capacity + /// + /// The caller must also have reserved enough space in the child builder, + /// as `length` lists' worth of empty values are appended to it. + void UnsafeAppendEmptyValues(int64_t length) final; + Status AppendArraySlice(const ArraySpan& array, int64_t offset, int64_t length) final { const uint8_t* validity = array.MayHaveNulls() ? array.buffers[0].data : NULLPTR; for (int64_t row = offset; row < offset + length; row++) { @@ -807,6 +840,28 @@ class ARROW_EXPORT StructBuilder : public ArrayBuilder { return Status::OK(); } + /// \brief Append an empty struct value, without checking capacity + /// + /// An empty value is also appended to each child builder, so the caller + /// must have reserved space in the child builders as well. + void UnsafeAppendEmptyValue() final { + for (const auto& field : children_) { + field->UnsafeAppendEmptyValue(); + } + UnsafeAppendToBitmap(true); + } + + /// \brief Append empty struct values, without checking capacity + /// + /// Empty values are also appended to each child builder, so the caller + /// must have reserved space in the child builders as well. + void UnsafeAppendEmptyValues(int64_t length) final { + for (const auto& field : children_) { + field->UnsafeAppendEmptyValues(length); + } + UnsafeAppendToBitmap(length, true); + } + Status AppendArraySlice(const ArraySpan& array, int64_t offset, int64_t length) override { for (int i = 0; static_cast(i) < children_.size(); i++) { diff --git a/cpp/src/arrow/array/builder_primitive.h b/cpp/src/arrow/array/builder_primitive.h index 6d79d6e9649..c0621f3f164 100644 --- a/cpp/src/arrow/array/builder_primitive.h +++ b/cpp/src/arrow/array/builder_primitive.h @@ -56,6 +56,13 @@ class ARROW_EXPORT NullBuilder : public ArrayBuilder { Status AppendEmptyValue() final { return AppendEmptyValues(1); } + void UnsafeAppendEmptyValue() final { UnsafeAppendEmptyValues(1); } + + void UnsafeAppendEmptyValues(int64_t length) final { + null_count_ += length; + length_ += length; + } + Status Append(std::nullptr_t) { return AppendNull(); } Status AppendArraySlice(const ArraySpan&, int64_t, int64_t length) override { @@ -127,17 +134,25 @@ class NumericBuilder /// \brief Append a empty element Status AppendEmptyValue() final { ARROW_RETURN_NOT_OK(Reserve(1)); - data_builder_.UnsafeAppend(value_type{}); // zero - UnsafeAppendToBitmap(true); + UnsafeAppendEmptyValue(); return Status::OK(); } /// \brief Append several empty elements Status AppendEmptyValues(int64_t length) final { ARROW_RETURN_NOT_OK(Reserve(length)); + UnsafeAppendEmptyValues(length); + return Status::OK(); + } + + void UnsafeAppendEmptyValue() final { + data_builder_.UnsafeAppend(value_type{}); // zero + UnsafeAppendToBitmap(true); + } + + void UnsafeAppendEmptyValues(int64_t length) final { data_builder_.UnsafeAppend(length, value_type{}); // zero UnsafeSetNotNull(length); - return Status::OK(); } value_type GetValue(int64_t index) const { return data_builder_.data()[index]; } @@ -515,16 +530,21 @@ class ARROW_EXPORT BooleanBuilder Status AppendEmptyValue() final { ARROW_RETURN_NOT_OK(Reserve(1)); - data_builder_.UnsafeAppend(false); - UnsafeSetNotNull(1); + UnsafeAppendEmptyValue(); return Status::OK(); } Status AppendEmptyValues(int64_t length) final { ARROW_RETURN_NOT_OK(Reserve(length)); + UnsafeAppendEmptyValues(length); + return Status::OK(); + } + + void UnsafeAppendEmptyValue() final { UnsafeAppendEmptyValues(1); } + + void UnsafeAppendEmptyValues(int64_t length) final { data_builder_.UnsafeAppend(length, false); UnsafeSetNotNull(length); - return Status::OK(); } /// Scalar append