From 0a5f90ad02a1fbfe18f6a1e374d682e250691a90 Mon Sep 17 00:00:00 2001 From: Benjamin Kietzman Date: Tue, 7 May 2019 14:14:43 -0500 Subject: [PATCH] ARROW-3475: [C++] Allow builders to finish to the corresponding array type Allows `Int64Builder` to finish into `shared_ptr`, `ListBuilder` to finish into `shared_ptr`, etc. Author: Benjamin Kietzman Closes #4262 from bkietz/3475-Int64Builder-FinishNumericArrayInt64Type and squashes the following commits: b96f56f54 revert unnecessary change to builder.h 0b93527ec exclude usings from doxygen declaration list b2f77b710 IWYU include eefa577c0 adding typed Finish method to other builders a59638b23 adding a typed Finish method to primitive builders --- cpp/src/arrow/array-test.cc | 20 ++++++++++++++++++++ cpp/src/arrow/array/builder_base.h | 10 ++++++++++ cpp/src/arrow/array/builder_binary.h | 18 ++++++++++++++++++ cpp/src/arrow/array/builder_decimal.h | 6 ++++++ cpp/src/arrow/array/builder_dict.h | 12 ++++++++++++ cpp/src/arrow/array/builder_nested.h | 12 ++++++++++++ cpp/src/arrow/array/builder_primitive.h | 20 ++++++++++++++++++++ cpp/src/arrow/array/builder_union.h | 6 ++++++ 8 files changed, 104 insertions(+) diff --git a/cpp/src/arrow/array-test.cc b/cpp/src/arrow/array-test.cc index 71183fb2cb52d..2d1ab48858182 100644 --- a/cpp/src/arrow/array-test.cc +++ b/cpp/src/arrow/array-test.cc @@ -748,6 +748,26 @@ TYPED_TEST(TestPrimitiveBuilder, TestAppendValues) { this->Check(this->builder_nn_, false); } +TYPED_TEST(TestPrimitiveBuilder, TestTypedFinish) { + DECL_T(); + + int64_t size = 1000; + this->RandomData(size); + + std::vector& draws = this->draws_; + std::vector& valid_bytes = this->valid_bytes_; + + ASSERT_OK(this->builder_->AppendValues(draws.data(), size, valid_bytes.data())); + std::shared_ptr result_untyped; + ASSERT_OK(this->builder_->Finish(&result_untyped)); + + ASSERT_OK(this->builder_->AppendValues(draws.data(), size, valid_bytes.data())); + std::shared_ptr result; + ASSERT_OK(this->builder_->Finish(&result)); + + AssertArraysEqual(*result_untyped, *result); +} + TYPED_TEST(TestPrimitiveBuilder, TestAppendValuesIter) { int64_t size = 10000; this->RandomData(size); diff --git a/cpp/src/arrow/array/builder_base.h b/cpp/src/arrow/array/builder_base.h index 4f04866429a5e..36f6c7a2a4da8 100644 --- a/cpp/src/arrow/array/builder_base.h +++ b/cpp/src/arrow/array/builder_base.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include "arrow/buffer-builder.h" @@ -177,6 +178,15 @@ class ARROW_EXPORT ArrayBuilder { static Status TrimBuffer(const int64_t bytes_filled, ResizableBuffer* buffer); + /// \brief Finish to an array of the specified ArrayType + template + Status FinishTyped(std::shared_ptr* out) { + std::shared_ptr out_untyped; + ARROW_RETURN_NOT_OK(Finish(&out_untyped)); + *out = std::static_pointer_cast(std::move(out_untyped)); + return Status::OK(); + } + static Status CheckCapacity(int64_t new_capacity, int64_t old_capacity) { if (new_capacity < 0) { return Status::Invalid("Resize capacity must be positive"); diff --git a/cpp/src/arrow/array/builder_binary.h b/cpp/src/arrow/array/builder_binary.h index c849572f4076a..facaf4a241969 100644 --- a/cpp/src/arrow/array/builder_binary.h +++ b/cpp/src/arrow/array/builder_binary.h @@ -120,6 +120,12 @@ class ARROW_EXPORT BinaryBuilder : public ArrayBuilder { Status FinishInternal(std::shared_ptr* out) override; + /// \cond FALSE + using ArrayBuilder::Finish; + /// \endcond + + Status Finish(std::shared_ptr* out) { return FinishTyped(out); } + /// \return size of values buffer so far int64_t value_data_length() const { return value_data_builder_.length(); } /// \return capacity of values buffer @@ -186,6 +192,12 @@ class ARROW_EXPORT StringBuilder : public BinaryBuilder { /// \return Status Status AppendValues(const char** values, int64_t length, const uint8_t* valid_bytes = NULLPTR); + + /// \cond FALSE + using ArrayBuilder::Finish; + /// \endcond + + Status Finish(std::shared_ptr* out) { return FinishTyped(out); } }; // ---------------------------------------------------------------------- @@ -254,6 +266,12 @@ class ARROW_EXPORT FixedSizeBinaryBuilder : public ArrayBuilder { Status Resize(int64_t capacity) override; Status FinishInternal(std::shared_ptr* out) override; + /// \cond FALSE + using ArrayBuilder::Finish; + /// \endcond + + Status Finish(std::shared_ptr* out) { return FinishTyped(out); } + /// \return size of values buffer so far int64_t value_data_length() const { return byte_builder_.length(); } diff --git a/cpp/src/arrow/array/builder_decimal.h b/cpp/src/arrow/array/builder_decimal.h index fb40a7950abbd..d5a26ff42f580 100644 --- a/cpp/src/arrow/array/builder_decimal.h +++ b/cpp/src/arrow/array/builder_decimal.h @@ -38,6 +38,12 @@ class ARROW_EXPORT Decimal128Builder : public FixedSizeBinaryBuilder { Status Append(const Decimal128& val); Status FinishInternal(std::shared_ptr* out) override; + + /// \cond FALSE + using ArrayBuilder::Finish; + /// \endcond + + Status Finish(std::shared_ptr* out) { return FinishTyped(out); } }; using DecimalBuilder = Decimal128Builder; diff --git a/cpp/src/arrow/array/builder_dict.h b/cpp/src/arrow/array/builder_dict.h index 4d31cdb9d549c..e7f44b9d09f76 100644 --- a/cpp/src/arrow/array/builder_dict.h +++ b/cpp/src/arrow/array/builder_dict.h @@ -106,6 +106,12 @@ class ARROW_EXPORT DictionaryBuilder : public ArrayBuilder { Status Resize(int64_t capacity) override; Status FinishInternal(std::shared_ptr* out) override; + /// \cond FALSE + using ArrayBuilder::Finish; + /// \endcond + + Status Finish(std::shared_ptr* out) { return FinishTyped(out); } + /// is the dictionary builder in the delta building mode bool is_building_delta() { return delta_offset_ > 0; } @@ -139,6 +145,12 @@ class ARROW_EXPORT DictionaryBuilder : public ArrayBuilder { Status Resize(int64_t capacity) override; Status FinishInternal(std::shared_ptr* out) override; + /// \cond FALSE + using ArrayBuilder::Finish; + /// \endcond + + Status Finish(std::shared_ptr* out) { return FinishTyped(out); } + protected: AdaptiveIntBuilder values_builder_; }; diff --git a/cpp/src/arrow/array/builder_nested.h b/cpp/src/arrow/array/builder_nested.h index 79e8c1bf0efeb..c5c89f89bdaa9 100644 --- a/cpp/src/arrow/array/builder_nested.h +++ b/cpp/src/arrow/array/builder_nested.h @@ -52,6 +52,12 @@ class ARROW_EXPORT ListBuilder : public ArrayBuilder { void Reset() override; Status FinishInternal(std::shared_ptr* out) override; + /// \cond FALSE + using ArrayBuilder::Finish; + /// \endcond + + Status Finish(std::shared_ptr* out) { return FinishTyped(out); } + /// \brief Vector append /// /// If passed, valid_bytes is of equal length to values, and any zero byte @@ -96,6 +102,12 @@ class ARROW_EXPORT StructBuilder : public ArrayBuilder { Status FinishInternal(std::shared_ptr* out) override; + /// \cond FALSE + using ArrayBuilder::Finish; + /// \endcond + + Status Finish(std::shared_ptr* out) { return FinishTyped(out); } + /// Null bitmap is of equal length to every child field, and any zero byte /// will be considered as a null for that field, but users must using app- /// end methods or advance methods of the child builders' independently to diff --git a/cpp/src/arrow/array/builder_primitive.h b/cpp/src/arrow/array/builder_primitive.h index d8b2a5f914c46..4c7cbf2c25e20 100644 --- a/cpp/src/arrow/array/builder_primitive.h +++ b/cpp/src/arrow/array/builder_primitive.h @@ -46,6 +46,12 @@ class ARROW_EXPORT NullBuilder : public ArrayBuilder { Status Append(std::nullptr_t) { return AppendNull(); } Status FinishInternal(std::shared_ptr* out) override; + + /// \cond FALSE + using ArrayBuilder::Finish; + /// \endcond + + Status Finish(std::shared_ptr* out) { return FinishTyped(out); } }; /// Base class for all Builders that emit an Array of a scalar numerical type. @@ -53,6 +59,7 @@ template class NumericBuilder : public ArrayBuilder { public: using value_type = typename T::c_type; + using ArrayType = typename TypeTraits::ArrayType; using ArrayBuilder::ArrayBuilder; template @@ -159,6 +166,12 @@ class NumericBuilder : public ArrayBuilder { return Status::OK(); } + /// \cond FALSE + using ArrayBuilder::Finish; + /// \endcond + + Status Finish(std::shared_ptr* out) { return FinishTyped(out); } + /// \brief Append a sequence of elements in one shot /// \param[in] values_begin InputIterator to the beginning of the values /// \param[in] values_end InputIterator pointing to the end of the values @@ -402,6 +415,13 @@ class ARROW_EXPORT BooleanBuilder : public ArrayBuilder { } Status FinishInternal(std::shared_ptr* out) override; + + /// \cond FALSE + using ArrayBuilder::Finish; + /// \endcond + + Status Finish(std::shared_ptr* out) { return FinishTyped(out); } + void Reset() override; Status Resize(int64_t capacity) override; diff --git a/cpp/src/arrow/array/builder_union.h b/cpp/src/arrow/array/builder_union.h index 5764d5b9d1c05..aac2e54f9a261 100644 --- a/cpp/src/arrow/array/builder_union.h +++ b/cpp/src/arrow/array/builder_union.h @@ -76,6 +76,12 @@ class ARROW_EXPORT DenseUnionBuilder : public ArrayBuilder { Status FinishInternal(std::shared_ptr* out) override; + /// \cond FALSE + using ArrayBuilder::Finish; + /// \endcond + + Status Finish(std::shared_ptr* out) { return FinishTyped(out); } + /// \brief Make a new child builder available to the UnionArray /// /// \param[in] child the child builder