| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #pragma once |
|
|
| #include <algorithm> |
| #include <cstdint> |
| #include <limits> |
| #include <memory> |
| #include <utility> |
| #include <vector> |
|
|
| #include "arrow/array/array_base.h" |
| #include "arrow/array/array_primitive.h" |
| #include "arrow/buffer.h" |
| #include "arrow/buffer_builder.h" |
| #include "arrow/result.h" |
| #include "arrow/status.h" |
| #include "arrow/type_fwd.h" |
| #include "arrow/util/macros.h" |
| #include "arrow/util/visibility.h" |
|
|
| namespace arrow { |
|
|
| namespace internal { |
|
|
| template <class Builder, class V> |
| class ArrayBuilderExtraOps { |
| public: |
| |
| Status AppendOrNull(const std::optional<V>& value) { |
| auto* self = static_cast<Builder*>(this); |
| return value.has_value() ? self->Append(*value) : self->AppendNull(); |
| } |
|
|
| |
| |
| |
| void UnsafeAppendOrNull(const std::optional<V>& value) { |
| auto* self = static_cast<Builder*>(this); |
| return value.has_value() ? self->UnsafeAppend(*value) : self->UnsafeAppendNull(); |
| } |
| }; |
|
|
| } |
|
|
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| constexpr int64_t kMinBuilderCapacity = 1 << 5; |
| constexpr int64_t kListMaximumElements = std::numeric_limits<int32_t>::max() - 1; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| class ARROW_EXPORT ArrayBuilder { |
| public: |
| explicit ArrayBuilder(MemoryPool* pool, int64_t alignment = kDefaultBufferAlignment) |
| : pool_(pool), alignment_(alignment), null_bitmap_builder_(pool, alignment) {} |
|
|
| ARROW_DEFAULT_MOVE_AND_ASSIGN(ArrayBuilder); |
|
|
| virtual ~ArrayBuilder() = default; |
|
|
| |
| |
| ArrayBuilder* child(int i) { return children_[i].get(); } |
|
|
| const std::shared_ptr<ArrayBuilder>& child_builder(int i) const { return children_[i]; } |
|
|
| int num_children() const { return static_cast<int>(children_.size()); } |
|
|
| virtual int64_t length() const { return length_; } |
| int64_t null_count() const { return null_count_; } |
| int64_t capacity() const { return capacity_; } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| virtual Status Resize(int64_t capacity); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Status Reserve(int64_t additional_capacity) { |
| auto current_capacity = capacity(); |
| auto min_capacity = length() + additional_capacity; |
| if (min_capacity <= current_capacity) return Status::OK(); |
|
|
| |
| auto new_capacity = BufferBuilder::GrowByFactor(current_capacity, min_capacity); |
| return Resize(new_capacity); |
| } |
|
|
| |
| virtual void Reset(); |
|
|
| |
| virtual Status AppendNull() = 0; |
| |
| virtual Status AppendNulls(int64_t length) = 0; |
|
|
| |
| |
| |
| |
| |
| virtual Status AppendEmptyValue() = 0; |
|
|
| |
| |
| |
| |
| |
| virtual Status AppendEmptyValues(int64_t length) = 0; |
|
|
| |
| Status AppendScalar(const Scalar& scalar) { return AppendScalar(scalar, 1); } |
| virtual Status AppendScalar(const Scalar& scalar, int64_t n_repeats); |
| virtual Status AppendScalars(const ScalarVector& scalars); |
|
|
| |
| |
| |
| virtual Status AppendArraySlice(const ArraySpan& ARROW_ARG_UNUSED(array), |
| int64_t ARROW_ARG_UNUSED(offset), |
| int64_t ARROW_ARG_UNUSED(length)) { |
| return Status::NotImplemented("AppendArraySlice for builder for ", *type()); |
| } |
|
|
| |
| |
| |
| |
| |
| virtual Status FinishInternal(std::shared_ptr<ArrayData>* out) = 0; |
|
|
| |
| |
| |
| |
| |
| |
| Status Finish(std::shared_ptr<Array>* out); |
|
|
| |
| |
| |
| |
| |
| Result<std::shared_ptr<Array>> Finish(); |
|
|
| |
| virtual std::shared_ptr<DataType> type() const = 0; |
|
|
| protected: |
| |
| Status AppendToBitmap(bool is_valid); |
|
|
| |
| |
| Status AppendToBitmap(const uint8_t* valid_bytes, int64_t length); |
|
|
| |
| Status AppendToBitmap(int64_t num_bits, bool value); |
|
|
| |
| Status SetNotNull(int64_t length); |
|
|
| |
|
|
| void UnsafeAppendNull() { UnsafeAppendToBitmap(false); } |
|
|
| |
| void UnsafeAppendToBitmap(bool is_valid) { |
| null_bitmap_builder_.UnsafeAppend(is_valid); |
| ++length_; |
| if (!is_valid) ++null_count_; |
| } |
|
|
| |
| |
| void UnsafeAppendToBitmap(const uint8_t* valid_bytes, int64_t length) { |
| if (valid_bytes == NULLPTR) { |
| return UnsafeSetNotNull(length); |
| } |
| null_bitmap_builder_.UnsafeAppend(valid_bytes, length); |
| length_ += length; |
| null_count_ = null_bitmap_builder_.false_count(); |
| } |
|
|
| |
| |
| void UnsafeAppendToBitmap(const uint8_t* bitmap, int64_t offset, int64_t length) { |
| if (bitmap == NULLPTR) { |
| return UnsafeSetNotNull(length); |
| } |
| null_bitmap_builder_.UnsafeAppend(bitmap, offset, length); |
| length_ += length; |
| null_count_ = null_bitmap_builder_.false_count(); |
| } |
|
|
| |
| void UnsafeAppendToBitmap(const int64_t num_bits, bool value) { |
| if (value) { |
| UnsafeSetNotNull(num_bits); |
| } else { |
| UnsafeSetNull(num_bits); |
| } |
| } |
|
|
| void UnsafeAppendToBitmap(const std::vector<bool>& is_valid); |
|
|
| |
| void UnsafeSetNotNull(int64_t length); |
|
|
| |
| void UnsafeSetNull(int64_t length); |
|
|
| static Status TrimBuffer(const int64_t bytes_filled, ResizableBuffer* buffer); |
|
|
| |
| template <typename ArrayType> |
| Status FinishTyped(std::shared_ptr<ArrayType>* out) { |
| std::shared_ptr<Array> out_untyped; |
| ARROW_RETURN_NOT_OK(Finish(&out_untyped)); |
| *out = std::static_pointer_cast<ArrayType>(std::move(out_untyped)); |
| return Status::OK(); |
| } |
|
|
| |
| Status CheckCapacity(int64_t new_capacity) { |
| if (ARROW_PREDICT_FALSE(new_capacity < 0)) { |
| return Status::Invalid( |
| "Resize capacity must be positive (requested: ", new_capacity, ")"); |
| } |
|
|
| if (ARROW_PREDICT_FALSE(new_capacity < length_)) { |
| return Status::Invalid("Resize cannot downsize (requested: ", new_capacity, |
| ", current length: ", length_, ")"); |
| } |
|
|
| return Status::OK(); |
| } |
|
|
| |
| Status CheckArrayType(const std::shared_ptr<DataType>& expected_type, |
| const Array& array, const char* message); |
| Status CheckArrayType(Type::type expected_type, const Array& array, |
| const char* message); |
|
|
| MemoryPool* pool_; |
| int64_t alignment_; |
|
|
| TypedBufferBuilder<bool> null_bitmap_builder_; |
| int64_t null_count_ = 0; |
|
|
| |
| int64_t length_ = 0; |
| int64_t capacity_ = 0; |
|
|
| |
| std::vector<std::shared_ptr<ArrayBuilder>> children_; |
|
|
| private: |
| ARROW_DISALLOW_COPY_AND_ASSIGN(ArrayBuilder); |
| }; |
|
|
| |
| |
| |
| |
| |
| ARROW_EXPORT |
| Status MakeBuilder(MemoryPool* pool, const std::shared_ptr<DataType>& type, |
| std::unique_ptr<ArrayBuilder>* out); |
|
|
| inline Result<std::unique_ptr<ArrayBuilder>> MakeBuilder( |
| const std::shared_ptr<DataType>& type, MemoryPool* pool = default_memory_pool()) { |
| std::unique_ptr<ArrayBuilder> out; |
| ARROW_RETURN_NOT_OK(MakeBuilder(pool, type, &out)); |
| return out; |
| } |
|
|
| |
| |
| |
| ARROW_EXPORT |
| Status MakeBuilderExactIndex(MemoryPool* pool, const std::shared_ptr<DataType>& type, |
| std::unique_ptr<ArrayBuilder>* out); |
|
|
| inline Result<std::unique_ptr<ArrayBuilder>> MakeBuilderExactIndex( |
| const std::shared_ptr<DataType>& type, MemoryPool* pool = default_memory_pool()) { |
| std::unique_ptr<ArrayBuilder> out; |
| ARROW_RETURN_NOT_OK(MakeBuilderExactIndex(pool, type, &out)); |
| return out; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| ARROW_EXPORT |
| Status MakeDictionaryBuilder(MemoryPool* pool, const std::shared_ptr<DataType>& type, |
| const std::shared_ptr<Array>& dictionary, |
| std::unique_ptr<ArrayBuilder>* out); |
|
|
| inline Result<std::unique_ptr<ArrayBuilder>> MakeDictionaryBuilder( |
| const std::shared_ptr<DataType>& type, const std::shared_ptr<Array>& dictionary, |
| MemoryPool* pool = default_memory_pool()) { |
| std::unique_ptr<ArrayBuilder> out; |
| ARROW_RETURN_NOT_OK(MakeDictionaryBuilder(pool, type, dictionary, &out)); |
| return out; |
| } |
|
|
| } |
|
|