diff --git a/cpp/src/arrow/array/builder_adaptive.h b/cpp/src/arrow/array/builder_adaptive.h index 0cea571be3e3..2b1ce2d6dd0a 100644 --- a/cpp/src/arrow/array/builder_adaptive.h +++ b/cpp/src/arrow/array/builder_adaptive.h @@ -94,6 +94,12 @@ class ARROW_EXPORT AdaptiveIntBuilderBase : public ArrayBuilder { return Status::OK(); } + Type::type type_id() const final { + // This could be more optimal, but it requires minor modifications + // to AdaptiveIntBuilder and AdaptiveUIntBuilder. + return type()->id(); + } + void Reset() override; Status Resize(int64_t capacity) override; diff --git a/cpp/src/arrow/array/builder_base.h b/cpp/src/arrow/array/builder_base.h index 0cd57ff04dac..d14da82f7ed1 100644 --- a/cpp/src/arrow/array/builder_base.h +++ b/cpp/src/arrow/array/builder_base.h @@ -206,6 +206,12 @@ class ARROW_EXPORT ArrayBuilder { /// \brief Return the type of the built Array virtual std::shared_ptr type() const = 0; + /// \brief Return the type ID of the built Array. + /// + /// Use this method when only the type ID is needed, instead of calling + /// type()->id(), to avoid unnecessary allocation and deallocation. + virtual Type::type type_id() const = 0; + protected: /// Set the next length bits to not null (i.e. valid). Status SetNotNull(int64_t length); diff --git a/cpp/src/arrow/array/builder_binary.h b/cpp/src/arrow/array/builder_binary.h index 12f7e45f3421..aab63f847c1e 100644 --- a/cpp/src/arrow/array/builder_binary.h +++ b/cpp/src/arrow/array/builder_binary.h @@ -391,6 +391,8 @@ class BaseBinaryBuilder return std::numeric_limits::max() - 1; } + Type::type type_id() const override { return TypeClass::type_id; } + protected: TypedBufferBuilder offsets_builder_; TypedBufferBuilder value_data_builder_; @@ -711,6 +713,8 @@ class ARROW_EXPORT BinaryViewBuilder : public ArrayBuilder { std::shared_ptr type() const override { return binary_view(); } + Type::type type_id() const override { return Type::BINARY_VIEW; } + protected: TypedBufferBuilder data_builder_; @@ -723,6 +727,7 @@ class ARROW_EXPORT StringViewBuilder : public BinaryViewBuilder { public: using BinaryViewBuilder::BinaryViewBuilder; std::shared_ptr type() const override { return utf8_view(); } + Type::type type_id() const override { return Type::STRING_VIEW; } }; // ---------------------------------------------------------------------- @@ -900,6 +905,8 @@ class ARROW_EXPORT FixedSizeBinaryBuilder : public ArrayBuilder { return fixed_size_binary(byte_width_); } + Type::type type_id() const override { return Type::FIXED_SIZE_BINARY; } + protected: int32_t byte_width_; BufferBuilder byte_builder_; diff --git a/cpp/src/arrow/array/builder_decimal.h b/cpp/src/arrow/array/builder_decimal.h index a0bf0a042208..b4bffd4b7f7a 100644 --- a/cpp/src/arrow/array/builder_decimal.h +++ b/cpp/src/arrow/array/builder_decimal.h @@ -60,6 +60,8 @@ class ARROW_EXPORT Decimal32Builder : public FixedSizeBinaryBuilder { std::shared_ptr type() const override { return decimal_type_; } + Type::type type_id() const override { return Type::DECIMAL32; } + protected: std::shared_ptr decimal_type_; }; @@ -91,6 +93,8 @@ class ARROW_EXPORT Decimal64Builder : public FixedSizeBinaryBuilder { std::shared_ptr type() const override { return decimal_type_; } + Type::type type_id() const override { return Type::DECIMAL64; } + protected: std::shared_ptr decimal_type_; }; @@ -122,6 +126,8 @@ class ARROW_EXPORT Decimal128Builder : public FixedSizeBinaryBuilder { std::shared_ptr type() const override { return decimal_type_; } + Type::type type_id() const override { return Type::DECIMAL128; } + protected: std::shared_ptr decimal_type_; }; diff --git a/cpp/src/arrow/array/builder_dict.h b/cpp/src/arrow/array/builder_dict.h index 4dd4e6ea44fd..f0fc45c755fd 100644 --- a/cpp/src/arrow/array/builder_dict.h +++ b/cpp/src/arrow/array/builder_dict.h @@ -515,6 +515,8 @@ class DictionaryBuilderBase : public ArrayBuilder { ordered_); } + Type::type type_id() const override { return Type::DICTIONARY; } + protected: template Status AppendArraySliceImpl(const typename TypeTraits::ArrayType& dict, @@ -714,6 +716,8 @@ class DictionaryBuilderBase : public ArrayBuilder { ordered_); } + Type::type type_id() const override { return Type::DICTIONARY; } + protected: BuilderType indices_builder_; bool ordered_ = false; diff --git a/cpp/src/arrow/array/builder_nested.h b/cpp/src/arrow/array/builder_nested.h index fdbeb0cd7d17..e67b45009a30 100644 --- a/cpp/src/arrow/array/builder_nested.h +++ b/cpp/src/arrow/array/builder_nested.h @@ -225,6 +225,8 @@ class VarLengthListLikeBuilder : public ArrayBuilder { return std::make_shared(value_field_->WithType(value_builder_->type())); } + Type::type type_id() const override { return TypeClass::type_id; } + private: static constexpr const char* type_name() { if constexpr (is_list_view(TYPE::type_id)) { @@ -617,6 +619,8 @@ class ARROW_EXPORT MapBuilder : public ArrayBuilder { keys_sorted_); } + Type::type type_id() const override { return Type::MAP; } + Status ValidateOverflow(int64_t new_elements) { return list_builder_->ValidateOverflow(new_elements); } @@ -720,6 +724,8 @@ class ARROW_EXPORT FixedSizeListBuilder : public ArrayBuilder { return fixed_size_list(value_field_->WithType(value_builder_->type()), list_size_); } + Type::type type_id() const override { return Type::FIXED_SIZE_LIST; } + // Cannot make this a static attribute because of linking issues static constexpr int64_t maximum_elements() { return std::numeric_limits::max() - 1; @@ -827,6 +833,8 @@ class ARROW_EXPORT StructBuilder : public ArrayBuilder { std::shared_ptr type() const override; + Type::type type_id() const override { return Type::STRUCT; } + private: std::shared_ptr type_; }; diff --git a/cpp/src/arrow/array/builder_primitive.h b/cpp/src/arrow/array/builder_primitive.h index 6d79d6e96499..161df637a174 100644 --- a/cpp/src/arrow/array/builder_primitive.h +++ b/cpp/src/arrow/array/builder_primitive.h @@ -70,6 +70,8 @@ class ARROW_EXPORT NullBuilder : public ArrayBuilder { std::shared_ptr type() const override { return null(); } + Type::type type_id() const override { return Type::NA; } + Status Finish(std::shared_ptr* out) { return FinishTyped(out); } }; @@ -348,6 +350,8 @@ class NumericBuilder std::shared_ptr type() const override { return type_; } + Type::type type_id() const override { return TypeClass::type_id; } + protected: std::shared_ptr type_; TypedBufferBuilder data_builder_; @@ -682,6 +686,8 @@ class ARROW_EXPORT BooleanBuilder std::shared_ptr type() const override { return boolean(); } + Type::type type_id() const override { return Type::BOOL; } + protected: TypedBufferBuilder data_builder_; }; diff --git a/cpp/src/arrow/array/builder_run_end.h b/cpp/src/arrow/array/builder_run_end.h index ac92efbd0dbe..37ebce7b3c28 100644 --- a/cpp/src/arrow/array/builder_run_end.h +++ b/cpp/src/arrow/array/builder_run_end.h @@ -147,6 +147,8 @@ class RunCompressorBuilder : public ArrayBuilder { std::shared_ptr type() const override { return inner_builder_->type(); } + Type::type type_id() const override { return inner_builder_->type_id(); } + bool has_open_run() const { return current_run_length_ > 0; } int64_t open_run_length() const { return current_run_length_; } @@ -258,6 +260,8 @@ class ARROW_EXPORT RunEndEncodedBuilder : public ArrayBuilder { std::shared_ptr type() const override; + Type::type type_id() const override { return Type::RUN_END_ENCODED; } + private: /// \brief Update physical capacity and logical length /// diff --git a/cpp/src/arrow/array/builder_union.h b/cpp/src/arrow/array/builder_union.h index 718ef4c32ceb..520f0a08f520 100644 --- a/cpp/src/arrow/array/builder_union.h +++ b/cpp/src/arrow/array/builder_union.h @@ -64,6 +64,10 @@ class ARROW_EXPORT BasicUnionBuilder : public ArrayBuilder { std::shared_ptr type() const override; + Type::type type_id() const override { + return mode_ == UnionMode::SPARSE ? Type::SPARSE_UNION : Type::DENSE_UNION; + } + int64_t length() const override { return types_builder_.length(); } protected: diff --git a/cpp/src/arrow/builder.cc b/cpp/src/arrow/builder.cc index 1d0a42099b3e..a9839af6c780 100644 --- a/cpp/src/arrow/builder.cc +++ b/cpp/src/arrow/builder.cc @@ -168,6 +168,8 @@ class ARROW_EXPORT TypeErasedIntBuilder : public ArrayBuilder { std::shared_ptr type() const override { return builder_->type(); } + Type::type type_id() const override { return builder_->type_id(); } + private: std::unique_ptr builder_; Type::type type_id_;