Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions cpp/src/arrow/array/array_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<const FixedSizeListType&>(*builder->type()).list_size();
ReserveEmptyValues(checked_cast<FixedSizeListBuilder*>(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<ArrayBuilder> 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) {
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/arrow/array/builder_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
16 changes: 16 additions & 0 deletions cpp/src/arrow/array/builder_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions cpp/src/arrow/array/builder_binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
42 changes: 29 additions & 13 deletions cpp/src/arrow/array/builder_binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<offset_type>(num_bytes));
}
UnsafeAppendToBitmap(length, true);
UnsafeAppendEmptyValues(length);
return Status::OK();
}

Expand Down Expand Up @@ -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<offset_type>(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<offset_type>(num_bytes));
}
UnsafeAppendToBitmap(length, true);
}

/// \brief Append a sequence of strings in one shot.
///
/// \param[in] values a vector of strings
Expand Down Expand Up @@ -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();
}

Expand All @@ -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,
Expand Down Expand Up @@ -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())) {
Expand Down
10 changes: 10 additions & 0 deletions cpp/src/arrow/array/builder_nested.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
59 changes: 57 additions & 2 deletions cpp/src/arrow/array/builder_nested.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,38 @@ 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
///
/// \post Another call to Append* or Finish should be made before appending to
/// 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
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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<size_t>(i) < children_.size(); i++) {
Expand Down
32 changes: 26 additions & 6 deletions cpp/src/arrow/array/builder_primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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]; }
Expand Down Expand Up @@ -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
Expand Down