From aa9fefce6658eadb32bfc2f7472552d7e2a55765 Mon Sep 17 00:00:00 2001 From: Will Jones Date: Thu, 24 Mar 2022 15:45:03 -0700 Subject: [PATCH 01/10] Start API docs for type traits --- cpp/src/arrow/api.h | 2 + cpp/src/arrow/type_traits.h | 28 ++++++++++ cpp/src/arrow/visit_array_inline.h | 17 ++++++ docs/source/cpp/api/utilities.rst | 85 ++++++++++++++++++++++++++++++ docs/source/cpp/conventions.rst | 15 ++++++ 5 files changed, 147 insertions(+) diff --git a/cpp/src/arrow/api.h b/cpp/src/arrow/api.h index 8958eaf1c9ae..8ff1780995fb 100644 --- a/cpp/src/arrow/api.h +++ b/cpp/src/arrow/api.h @@ -21,6 +21,8 @@ #include "arrow/array.h" // IYWU pragma: export #include "arrow/array/concatenate.h" // IYWU pragma: export +#include "arrow/visit_array_inline.h" // IYWU pragma: export +#include "arrow/visit_scalar_inline.h" // IYWU pragma: export #include "arrow/buffer.h" // IYWU pragma: export #include "arrow/builder.h" // IYWU pragma: export #include "arrow/chunked_array.h" // IYWU pragma: export diff --git a/cpp/src/arrow/type_traits.h b/cpp/src/arrow/type_traits.h index 4b4cb5d15d3d..5b8f3c3cd5d9 100644 --- a/cpp/src/arrow/type_traits.h +++ b/cpp/src/arrow/type_traits.h @@ -85,14 +85,26 @@ TYPE_ID_TRAIT(EXTENSION, ExtensionType) // Per-type type traits // +/// \addtogroup type-traits +/// \brief Base template for type traits of Arrow data types +/// Type traits provide various information about a type at compile time, such +/// as the associated ArrayType, BuilderType, and ScalarType. Not all types +/// provide all information. +/// \tparam T An Arrow data type template struct TypeTraits {}; +/// \brief Base template for type traits of C++ types +/// \tparam T A standard C++ type template struct CTypeTraits {}; +/// \addtogroup type-traits +/// @{ +/// \brief Type traits for null type template <> struct TypeTraits { + /// \brief associated array type using ArrayType = NullArray; using BuilderType = NullBuilder; using ScalarType = NullScalar; @@ -115,7 +127,9 @@ struct TypeTraits { constexpr static bool is_parameter_free = true; static inline std::shared_ptr type_singleton() { return boolean(); } }; +/// @} +/// \addtogroup c-type-traits template <> struct CTypeTraits : public TypeTraits { using ArrowType = BooleanType; @@ -162,6 +176,8 @@ PRIMITIVE_TYPE_TRAITS_DEF(double, Double, float64) #undef PRIMITIVE_TYPE_TRAITS_DEF #undef PRIMITIVE_TYPE_TRAITS_DEF_ +/// \addtogroup type-traits +/// @{ template <> struct TypeTraits { using ArrayType = Date64Array; @@ -367,6 +383,10 @@ struct TypeTraits { static inline std::shared_ptr type_singleton() { return large_utf8(); } }; +/// @} + +/// \addtogroup c-type-traits +/// @{ template <> struct CTypeTraits : public TypeTraits { using ArrowType = StringType; @@ -383,7 +403,10 @@ struct CTypeTraits : public TypeTraits { using ArrowType = DayTimeIntervalType; }; +/// @} +/// \addtogroup type-traits +/// @{ template <> struct TypeTraits { using ArrayType = ListArray; @@ -426,7 +449,9 @@ struct TypeTraits { using ScalarType = FixedSizeListScalar; constexpr static bool is_parameter_free = false; }; +/// @} +/// \addtogroup c-type-traits template struct CTypeTraits> : public TypeTraits { using ArrowType = ListType; @@ -436,6 +461,8 @@ struct CTypeTraits> : public TypeTraits { } }; +/// \addtogroup type-traits +/// @{ template <> struct TypeTraits { using ArrayType = StructArray; @@ -473,6 +500,7 @@ struct TypeTraits { using ScalarType = ExtensionScalar; constexpr static bool is_parameter_free = false; }; +/// @} namespace internal { diff --git a/cpp/src/arrow/visit_array_inline.h b/cpp/src/arrow/visit_array_inline.h index ecb9b6d39c36..4486e355e3ff 100644 --- a/cpp/src/arrow/visit_array_inline.h +++ b/cpp/src/arrow/visit_array_inline.h @@ -29,6 +29,23 @@ namespace arrow { internal::checked_cast::ArrayType&>( \ array)); +/// \brief Apply the visitors Visit() method specialized to the array type +/// +/// \tparam VISITOR Visitor type that implements Visit() for all array types. +/// \return Status +/// +/// A visitor is a type that implements specialized logic for each Arrow type. +/// Example usage: +/// +/// ``` +/// class ExampleVisitor { +/// arrow::Status Visit(arrow::Int32Array arr) { ... } +/// arrow::Status Visit(arrow::Int64Array arr) { ... } +/// ... +/// } +/// ExampleVisitor visitor; +/// VisitArrayInline(some_array, &visitor); +/// ``` template inline Status VisitArrayInline(const Array& array, VISITOR* visitor) { switch (array.type_id()) { diff --git a/docs/source/cpp/api/utilities.rst b/docs/source/cpp/api/utilities.rst index 0c29bab80594..167c222a6eda 100644 --- a/docs/source/cpp/api/utilities.rst +++ b/docs/source/cpp/api/utilities.rst @@ -56,3 +56,88 @@ Compression .. doxygenclass:: arrow::util::Decompressor :members: + +Visitors +======== + +.. doxygenfunction:: arrow::VisitTypeInline + :project: arrow_cpp + +.. doxygenfunction:: arrow::VisitScalarInline + :project: arrow_cpp + +.. doxygenfunction:: arrow::VisitArrayInline + :project: arrow_cpp + + +Type Traits +=========== + +These types provide relationships between Arrow types at compile time. :cpp:type:`TypeTraits` +maps Arrow DataTypes to other types, and :cpp:type:`CTypeTraits ` maps C types to +Arrow types. + +.. TODO: Table of Arrow type and ctype? + +TypeTraits +---------- + +Each specialized type defines the following associated types: + +.. cpp:type:: TypeTraits::ArrayType + + Corresponding :doc:`Arrow array type ` + +.. cpp:type:: TypeTraits::BuilderType + + Corresponding :doc:`array builder type ` + +.. cpp:type:: TypeTraits::ScalarType + + Corresponding :doc:`Arrow scalar type ` + +.. cpp:var:: TypeTraits::is_parameter_free + + Whether the type has any type parameters, such as field types in nested types + or scale and precision in decimal types. + + +In addition, the following are defined for many but not all of the types: + +.. cpp:type:: TypeTraits::CType + + Corresponding C type. For example, ``int64_t`` for ``Int64Array``. + +.. cpp:type:: TypeTraits::TensorType + + Corresponding :doc:`Arrow tensor type ` + +.. cpp:function:: static inline constexpr int64_t bytes_required(int64_t elements) + + Return the number of bytes required for given number of elements. Defined for + types with a fixed size. + +.. cpp:function:: static inline std::shared_ptr TypeTraits::type_singleton() + + For types where is_parameter_free is true, returns a pointer to the type + singleton. + + +.. doxygengroup:: type-traits + :content-only: + :members: + :undoc-members: + +CTypeTraits +----------- + +Each specialized type defines the following associated types: + +.. cpp:type:: CTypeTraits::ArrowType + + Corresponding :doc:`Arrow type ` + +.. doxygengroup:: c-type-traits + :content-only: + :members: + :undoc-members: \ No newline at end of file diff --git a/docs/source/cpp/conventions.rst b/docs/source/cpp/conventions.rst index f4167683eabe..06f110976760 100644 --- a/docs/source/cpp/conventions.rst +++ b/docs/source/cpp/conventions.rst @@ -106,6 +106,21 @@ For example:: .. seealso:: :doc:`API reference for error reporting ` + +Type Traits +----------- + + +.. list-table:: Type traits essentials + :header-rows: 1 + + * - DataType + - CType + - BuilderType + - ScalarType + - ArrayType + * + Visitor Pattern --------------- From 9cd120895aeaef92748015074bad303d89bd8879 Mon Sep 17 00:00:00 2001 From: Will Jones Date: Fri, 25 Mar 2022 09:52:49 -0700 Subject: [PATCH 02/10] Draft prose on type traits and reference predicates --- cpp/src/arrow/type_traits.h | 9 ++++ docs/source/cpp/api/utilities.rst | 26 +++++++++++ docs/source/cpp/conventions.rst | 78 +++++++++++++++++++++++++++---- 3 files changed, 105 insertions(+), 8 deletions(-) diff --git a/cpp/src/arrow/type_traits.h b/cpp/src/arrow/type_traits.h index 5b8f3c3cd5d9..cde75117528b 100644 --- a/cpp/src/arrow/type_traits.h +++ b/cpp/src/arrow/type_traits.h @@ -518,6 +518,9 @@ using void_t = typename make_void::type; // Useful type predicates // +/// \addtogroup type-predicates +/// @{ + // only in C++14 template using enable_if_t = typename std::enable_if::type; @@ -841,6 +844,10 @@ template using enable_if_physical_floating_point = enable_if_t::value, R>; +/// @} + +/// \addtogroup runtime-type-predicates +/// @{ static inline bool is_integer(Type::type type_id) { switch (type_id) { case Type::UINT8: @@ -1085,4 +1092,6 @@ static inline int offset_bit_width(Type::type type_id) { return 0; } +/// @} + } // namespace arrow diff --git a/docs/source/cpp/api/utilities.rst b/docs/source/cpp/api/utilities.rst index 167c222a6eda..0cf4e2bbec88 100644 --- a/docs/source/cpp/api/utilities.rst +++ b/docs/source/cpp/api/utilities.rst @@ -70,6 +70,8 @@ Visitors :project: arrow_cpp +.. _type-traits: + Type Traits =========== @@ -138,6 +140,30 @@ Each specialized type defines the following associated types: Corresponding :doc:`Arrow type ` .. doxygengroup:: c-type-traits + :content-only: + :members: + :undoc-members: + + +.. _type-predicates-api: + +Type Predicates +--------------- + +Type predicates that can be used with templates. + +.. doxygengroup:: type-predicates + :content-only: + :members: + :undoc-members: + + +Runtime Type Predicates +----------------------- + +Type predicates that can be applied at runtime. + +.. doxygengroup:: runtime-type-predicates :content-only: :members: :undoc-members: \ No newline at end of file diff --git a/docs/source/cpp/conventions.rst b/docs/source/cpp/conventions.rst index 06f110976760..a338f39e4602 100644 --- a/docs/source/cpp/conventions.rst +++ b/docs/source/cpp/conventions.rst @@ -107,19 +107,81 @@ For example:: :doc:`API reference for error reporting ` +.. TODO: Maybe move below to Datatypes.rst? + Type Traits ----------- +Writing code that can handle all Arrow types would be verbose, if it weren't for +type traits. They are empty structs with type declarations that map the Arrow +DataTypes to the specialized array, scalar, builder, and other associated types. +For example, boolean type has traits: + +.. TODO: Should code blocks go in a place where they are executed? + +.. code-block:: cpp + + template <> + struct TypeTraits { + using ArrayType = BooleanArray; + using BuilderType = BooleanBuilder; + using ScalarType = BooleanScalar; + using CType = bool; + + static constexpr int64_t bytes_required(int64_t elements) { + return bit_util::BytesForBits(elements); + } + constexpr static bool is_parameter_free = true; + static inline std::shared_ptr type_singleton() { return boolean(); } + }; + +See the :ref:`type-traits` for an explanation of each of these fields. + +Using type traits, one can write template functions that can handle a variety +of Arrow types. For example, to write a function that creates an array of nulls +for any type: + +.. code-block:: cpp + + template + ArrayType make_nulls(int32_t n) { + BuilderType builder = BuilderType::Make(); + for (int32_t i = 0; i < n; ++i) { + builder.AppendNull(); + } + ArrayType out; + builder.Finish(&out); + return out; + } + +For some common cases, there are type associations on the classes themselves. Use: + +* ``Scalar::TypeClass`` to get data type class of a scalar +* ``Array::TypeClass`` to get data type class of an array +* ``DataType::c_type`` to get associated C type of an Arrow data type + +There are also template type definitions for constraining template functions to a +subset of Arrow types. For example, to write a sum function for any numeric +(integer or float) array: + +.. code-block:: cpp + + template + arrow::enable_if_number Visit(const ArrayType& array) { + DataType::c_type sum = 0; + for (arrow::util::optional value : array) { + if (value.has_value()) { + sum += value.value(); + } + } + return sum; + } + +See :ref:`type-predicates-api` for a list of these. -.. list-table:: Type traits essentials - :header-rows: 1 - * - DataType - - CType - - BuilderType - - ScalarType - - ArrayType - * +:ref:`building them yourself `. +.. TODO: test these snippets Visitor Pattern --------------- From 2e3774dfb63841a47518be68e76a719f75fa96b3 Mon Sep 17 00:00:00 2001 From: Will Jones Date: Fri, 25 Mar 2022 11:03:14 -0700 Subject: [PATCH 03/10] Actually test the example C++ code --- docs/source/cpp/conventions.rst | 45 +++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/docs/source/cpp/conventions.rst b/docs/source/cpp/conventions.rst index a338f39e4602..67ebc3f01980 100644 --- a/docs/source/cpp/conventions.rst +++ b/docs/source/cpp/conventions.rst @@ -138,20 +138,28 @@ For example, boolean type has traits: See the :ref:`type-traits` for an explanation of each of these fields. Using type traits, one can write template functions that can handle a variety -of Arrow types. For example, to write a function that creates an array of nulls -for any type: +of Arrow types. For example, to write a function that creates an array of +fibonacci values: .. code-block:: cpp - template - ArrayType make_nulls(int32_t n) { - BuilderType builder = BuilderType::Make(); - for (int32_t i = 0; i < n; ++i) { - builder.AppendNull(); - } - ArrayType out; - builder.Finish(&out); - return out; + template ::BuilderType, + typename ArrayType = typename arrow::TypeTraits::ArrayType, + typename CType = typename arrow::TypeTraits::CType> + arrow::Result> make_fibonacci(int32_t n) { + BuilderType builder; + CType val = 0; + CType next_val = 1; + for (int32_t i = 0; i < n; ++i) { + builder.Append(val); + CType temp = val + next_val; + val = next_val; + next_val = temp; + } + std::shared_ptr out; + ARROW_RETURN_NOT_OK(builder.Finish(&out)); + return out; } For some common cases, there are type associations on the classes themselves. Use: @@ -161,15 +169,17 @@ For some common cases, there are type associations on the classes themselves. Us * ``DataType::c_type`` to get associated C type of an Arrow data type There are also template type definitions for constraining template functions to a -subset of Arrow types. For example, to write a sum function for any numeric +subset of Arrow types using ``std::enable_if_t``, which are useful if other overloads +need to be implemented. For example, to write a sum function for any numeric (integer or float) array: .. code-block:: cpp - template - arrow::enable_if_number Visit(const ArrayType& array) { - DataType::c_type sum = 0; - for (arrow::util::optional value : array) { + template + arrow::enable_if_number sum_array(const ArrayType& array) { + CType sum = 0; + for (arrow::util::optional value : array) { if (value.has_value()) { sum += value.value(); } @@ -180,9 +190,6 @@ subset of Arrow types. For example, to write a sum function for any numeric See :ref:`type-predicates-api` for a list of these. -:ref:`building them yourself `. -.. TODO: test these snippets - Visitor Pattern --------------- From 3970e9f38b85e0b02b60e1f4649e5da7fdc2183b Mon Sep 17 00:00:00 2001 From: Will Jones Date: Fri, 25 Mar 2022 13:35:44 -0700 Subject: [PATCH 04/10] Move docs to types, update visitor docs --- docs/source/cpp/conventions.rst | 98 ----------------------- docs/source/cpp/datatypes.rst | 135 ++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 98 deletions(-) diff --git a/docs/source/cpp/conventions.rst b/docs/source/cpp/conventions.rst index 67ebc3f01980..07af8f0937ab 100644 --- a/docs/source/cpp/conventions.rst +++ b/docs/source/cpp/conventions.rst @@ -106,101 +106,3 @@ For example:: .. seealso:: :doc:`API reference for error reporting ` - -.. TODO: Maybe move below to Datatypes.rst? - -Type Traits ------------ - -Writing code that can handle all Arrow types would be verbose, if it weren't for -type traits. They are empty structs with type declarations that map the Arrow -DataTypes to the specialized array, scalar, builder, and other associated types. -For example, boolean type has traits: - -.. TODO: Should code blocks go in a place where they are executed? - -.. code-block:: cpp - - template <> - struct TypeTraits { - using ArrayType = BooleanArray; - using BuilderType = BooleanBuilder; - using ScalarType = BooleanScalar; - using CType = bool; - - static constexpr int64_t bytes_required(int64_t elements) { - return bit_util::BytesForBits(elements); - } - constexpr static bool is_parameter_free = true; - static inline std::shared_ptr type_singleton() { return boolean(); } - }; - -See the :ref:`type-traits` for an explanation of each of these fields. - -Using type traits, one can write template functions that can handle a variety -of Arrow types. For example, to write a function that creates an array of -fibonacci values: - -.. code-block:: cpp - - template ::BuilderType, - typename ArrayType = typename arrow::TypeTraits::ArrayType, - typename CType = typename arrow::TypeTraits::CType> - arrow::Result> make_fibonacci(int32_t n) { - BuilderType builder; - CType val = 0; - CType next_val = 1; - for (int32_t i = 0; i < n; ++i) { - builder.Append(val); - CType temp = val + next_val; - val = next_val; - next_val = temp; - } - std::shared_ptr out; - ARROW_RETURN_NOT_OK(builder.Finish(&out)); - return out; - } - -For some common cases, there are type associations on the classes themselves. Use: - -* ``Scalar::TypeClass`` to get data type class of a scalar -* ``Array::TypeClass`` to get data type class of an array -* ``DataType::c_type`` to get associated C type of an Arrow data type - -There are also template type definitions for constraining template functions to a -subset of Arrow types using ``std::enable_if_t``, which are useful if other overloads -need to be implemented. For example, to write a sum function for any numeric -(integer or float) array: - -.. code-block:: cpp - - template - arrow::enable_if_number sum_array(const ArrayType& array) { - CType sum = 0; - for (arrow::util::optional value : array) { - if (value.has_value()) { - sum += value.value(); - } - } - return sum; - } - -See :ref:`type-predicates-api` for a list of these. - - -Visitor Pattern ---------------- - -Several types, including :class:`arrow::DataType`, :class:`arrow::Scalar`, and -:class:`arrow::Array`, have sub-types specialized to their corresponding Arrow -types. For example, for the Arrow boolean type there is :class:`arrow::BooleanType`, -:class:`arrow::BooleanScalar`, and :class:`arrow::BooleanArray`. In order to -process these entities, you may need to write write logic that specializes based -on the particular Arrow type. In these cases, use the -`visitor pattern `_. - -Arrow provides abstract visitor classes (:class:`arrow::TypeVisitor`, -:class:`arrow::ScalarVisitor`, :class:`arrow::ArrayVisitor`) and an ``Accept()`` -method on each of the corresponding base types (e.g. :func:`arrow::Array::Accept`). diff --git a/docs/source/cpp/datatypes.rst b/docs/source/cpp/datatypes.rst index 9149420a484f..2322d34a105a 100644 --- a/docs/source/cpp/datatypes.rst +++ b/docs/source/cpp/datatypes.rst @@ -66,3 +66,138 @@ To instantiate data types, it is recommended to call the provided type = arrow::timestamp(arrow::TimeUnit::MICRO); // A list type of single-precision floating-point values type = arrow::list(arrow::float32()); + + + +Type Traits +----------- + +Writing code that can handle concrete :class:`arrow::DataType` subclasses would +be verbose, if it weren't for type traits. They are empty structs with type +declarations that map the Arrow data types to the specialized array, scalar, +builder, and other associated types. For example, boolean type has traits: + +.. code-block:: cpp + + template <> + struct TypeTraits { + using ArrayType = BooleanArray; + using BuilderType = BooleanBuilder; + using ScalarType = BooleanScalar; + using CType = bool; + + static constexpr int64_t bytes_required(int64_t elements) { + return bit_util::BytesForBits(elements); + } + constexpr static bool is_parameter_free = true; + static inline std::shared_ptr type_singleton() { return boolean(); } + }; + +See the :ref:`type-traits` for an explanation of each of these fields. + +Using type traits, one can write template functions that can handle a variety +of Arrow types. For example, to write a function that creates an array of +fibonacci values: + +.. code-block:: cpp + + template ::BuilderType, + typename ArrayType = typename arrow::TypeTraits::ArrayType, + typename CType = typename arrow::TypeTraits::CType> + arrow::Result> make_fibonacci(int32_t n) { + BuilderType builder; + CType val = 0; + CType next_val = 1; + for (int32_t i = 0; i < n; ++i) { + builder.Append(val); + CType temp = val + next_val; + val = next_val; + next_val = temp; + } + std::shared_ptr out; + ARROW_RETURN_NOT_OK(builder.Finish(&out)); + return out; + } + +For some common cases, there are type associations on the classes themselves. Use: + +* ``Scalar::TypeClass`` to get data type class of a scalar +* ``Array::TypeClass`` to get data type class of an array +* ``DataType::c_type`` to get associated C type of an Arrow data type + +There are also template type definitions for constraining template functions to a +subset of Arrow types using ``std::enable_if_t``, which are useful if other overloads +need to be implemented. For example, to write a sum function for any numeric +(integer or float) array: + +.. code-block:: cpp + + template + arrow::enable_if_number sum_array(const ArrayType& array) { + CType sum = 0; + for (arrow::util::optional value : array) { + if (value.has_value()) { + sum += value.value(); + } + } + return sum; + } + +See :ref:`type-predicates-api` for a list of these. + + +Visitor Pattern +--------------- + +In order to process :class:`arrow::DataType`, :class:`arrow::Scalar`, or +:class:`arrow::Array`, you may need to write write logic that specializes based +on the particular Arrow type. In these cases, use the +`visitor pattern `_. Arrow provides +the template functions: + +* :func:`arrow::VisitTypeInline` +* :func:`arrow::VisitScalarInline` +* :func:`arrow::VisitArrayInline` + +To use these, implement ``Status Visit()`` methods for each specialized type, then +pass the class instance to the inline visit function. To avoid repetitive code, +use type traits as documented in the previous section. As a brief example, +here is how one might sum across columns of arbitrary numeric types: + +.. code-block:: cpp + + class TableSummation { + double partial = 0.0; + public: + + arrow::Result Compute(std::shared_ptr batch) { + for (std::shared_ptr array : batch->columns()) { + ARROW_RETURN_NOT_OK(arrow::VisitArrayInline(*array, this)); + } + return partial; + } + + // Default implementation + arrow::Status Visit(const arrow::Array& array) { + return arrow::Status::NotImplemented("Can not compute sum for array of type ", + array.type()->ToString()); + } + + template + arrow::enable_if_number Visit(const ArrayType& array) { + for (arrow::util::optional value : array) { + if (value.has_value()) { + partial += static_cast(value.value()); + } + } + return arrow::Status::OK(); + } + }; + +Arrow also provides abstract visitor classes (:class:`arrow::TypeVisitor`, +:class:`arrow::ScalarVisitor`, :class:`arrow::ArrayVisitor`) and an ``Accept()`` +method on each of the corresponding base types (e.g. :func:`arrow::Array::Accept`). +However, these are not able to be implemented using template functions, so you +will typically prefer using the inline type visitors. \ No newline at end of file From f5c8d9a7dd594c9d6cc61a50167648e1673e2f0a Mon Sep 17 00:00:00 2001 From: Will Jones Date: Fri, 25 Mar 2022 13:35:59 -0700 Subject: [PATCH 05/10] Provide usage examples of type predicates --- docs/source/cpp/api/utilities.rst | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/source/cpp/api/utilities.rst b/docs/source/cpp/api/utilities.rst index 0cf4e2bbec88..86cfebb1fa72 100644 --- a/docs/source/cpp/api/utilities.rst +++ b/docs/source/cpp/api/utilities.rst @@ -150,7 +150,25 @@ Each specialized type defines the following associated types: Type Predicates --------------- -Type predicates that can be used with templates. +Type predicates that can be used with templates. Predicates of the form ``is_XXX`` +resolve to constant boolean values, while predicates of the form ``enable_if_XXX`` +resolve to the second type parameter ``R`` if the first parameter ``T`` passes +the test. + +Example usage: + +.. code-block:: cpp + + template + arrow::enable_if_number my_function(const TypeClass& type) { + .. + } + + template + arrow::enable_if_number my_function(const ArrayType& array) { + .. + } + .. doxygengroup:: type-predicates :content-only: From 2356228e6383c7a8e772721b97ce89e20286c865 Mon Sep 17 00:00:00 2001 From: Will Jones Date: Fri, 25 Mar 2022 14:02:48 -0700 Subject: [PATCH 06/10] More documentation, and some cleanup --- cpp/src/arrow/api.h | 2 +- cpp/src/arrow/type_traits.h | 2 -- cpp/src/arrow/visit_array_inline.h | 4 ++-- cpp/src/arrow/visit_scalar_inline.h | 17 +++++++++++++++++ cpp/src/arrow/visit_type_inline.h | 22 +++++++++++++++++++++- docs/source/cpp/api/utilities.rst | 5 +++-- 6 files changed, 44 insertions(+), 8 deletions(-) diff --git a/cpp/src/arrow/api.h b/cpp/src/arrow/api.h index 8ff1780995fb..53b204cf9dd0 100644 --- a/cpp/src/arrow/api.h +++ b/cpp/src/arrow/api.h @@ -22,7 +22,7 @@ #include "arrow/array.h" // IYWU pragma: export #include "arrow/array/concatenate.h" // IYWU pragma: export #include "arrow/visit_array_inline.h" // IYWU pragma: export -#include "arrow/visit_scalar_inline.h" // IYWU pragma: export +#include "arrow/visit_scalar_inline.h" // IYWU pragma: export #include "arrow/buffer.h" // IYWU pragma: export #include "arrow/builder.h" // IYWU pragma: export #include "arrow/chunked_array.h" // IYWU pragma: export diff --git a/cpp/src/arrow/type_traits.h b/cpp/src/arrow/type_traits.h index cde75117528b..8521649a872a 100644 --- a/cpp/src/arrow/type_traits.h +++ b/cpp/src/arrow/type_traits.h @@ -101,10 +101,8 @@ struct CTypeTraits {}; /// \addtogroup type-traits /// @{ -/// \brief Type traits for null type template <> struct TypeTraits { - /// \brief associated array type using ArrayType = NullArray; using BuilderType = NullBuilder; using ScalarType = NullScalar; diff --git a/cpp/src/arrow/visit_array_inline.h b/cpp/src/arrow/visit_array_inline.h index 4486e355e3ff..f713227777a6 100644 --- a/cpp/src/arrow/visit_array_inline.h +++ b/cpp/src/arrow/visit_array_inline.h @@ -39,8 +39,8 @@ namespace arrow { /// /// ``` /// class ExampleVisitor { -/// arrow::Status Visit(arrow::Int32Array arr) { ... } -/// arrow::Status Visit(arrow::Int64Array arr) { ... } +/// arrow::Status Visit(arrow::NumericArray arr) { ... } +/// arrow::Status Visit(arrow::NumericArray arr) { ... } /// ... /// } /// ExampleVisitor visitor; diff --git a/cpp/src/arrow/visit_scalar_inline.h b/cpp/src/arrow/visit_scalar_inline.h index cad861b7a009..ee89da20e348 100644 --- a/cpp/src/arrow/visit_scalar_inline.h +++ b/cpp/src/arrow/visit_scalar_inline.h @@ -32,6 +32,23 @@ namespace arrow { case TYPE_CLASS##Type::type_id: \ return visitor->Visit(internal::checked_cast(scalar)); +/// \brief Apply the visitors Visit() method specialized to the scalar type +/// +/// \tparam VISITOR Visitor type that implements Visit() for all scalar types. +/// \return Status +/// +/// A visitor is a type that implements specialized logic for each Arrow type. +/// Example usage: +/// +/// ``` +/// class ExampleVisitor { +/// arrow::Status Visit(arrow::Int32Scalar arr) { ... } +/// arrow::Status Visit(arrow::Int64Scalar arr) { ... } +/// ... +/// } +/// ExampleVisitor visitor; +/// VisitScalarInline(some_scalar, &visitor); +/// ``` template inline Status VisitScalarInline(const Scalar& scalar, VISITOR* visitor) { switch (scalar.type->id()) { diff --git a/cpp/src/arrow/visit_type_inline.h b/cpp/src/arrow/visit_type_inline.h index 2b60de2a5e13..9aea08477392 100644 --- a/cpp/src/arrow/visit_type_inline.h +++ b/cpp/src/arrow/visit_type_inline.h @@ -28,6 +28,23 @@ namespace arrow { case TYPE_CLASS##Type::type_id: \ return visitor->Visit(internal::checked_cast(type)); +/// \brief Calls `visitor` with the corresponding concrete type class +/// +/// \tparam VISITOR Visitor type that implements Visit() for all Arrow types. +/// \return Status +/// +/// A visitor is a type that implements specialized logic for each Arrow type. +/// Example usage: +/// +/// ``` +/// class ExampleVisitor { +/// arrow::Status Visit(const arrow::Int32Type& type) { ... } +/// arrow::Status Visit(const arrow::Int64Type& type) { ... } +/// ... +/// } +/// ExampleVisitor visitor; +/// VisitTypeInline(some_type, &visitor); +/// ``` template inline Status VisitTypeInline(const DataType& type, VISITOR* visitor) { switch (type.id()) { @@ -46,7 +63,10 @@ inline Status VisitTypeInline(const DataType& type, VISITOR* visitor) { return visitor->Visit(concrete_ptr); \ } -// Calls `visitor` with a nullptr of the corresponding concrete type class +/// \brief Calls `visitor` with a nullptr of the corresponding concrete type class +/// +/// \tparam VISITOR Visitor type that implements Visit() for all Arrow types. +/// \return Status template inline Status VisitTypeIdInline(Type::type id, VISITOR* visitor) { switch (id) { diff --git a/docs/source/cpp/api/utilities.rst b/docs/source/cpp/api/utilities.rst index 86cfebb1fa72..23900ce427b3 100644 --- a/docs/source/cpp/api/utilities.rst +++ b/docs/source/cpp/api/utilities.rst @@ -63,6 +63,9 @@ Visitors .. doxygenfunction:: arrow::VisitTypeInline :project: arrow_cpp +.. doxygenfunction:: arrow::VisitTypeIdInline + :project: arrow_cpp + .. doxygenfunction:: arrow::VisitScalarInline :project: arrow_cpp @@ -79,8 +82,6 @@ These types provide relationships between Arrow types at compile time. :cpp:type maps Arrow DataTypes to other types, and :cpp:type:`CTypeTraits ` maps C types to Arrow types. -.. TODO: Table of Arrow type and ctype? - TypeTraits ---------- From ba5006ae0e8e3ceafdb1db1f45e3c3a3287d4d76 Mon Sep 17 00:00:00 2001 From: Will Jones Date: Fri, 25 Mar 2022 14:07:30 -0700 Subject: [PATCH 07/10] Format C++ files --- cpp/src/arrow/api.h | 4 ++-- cpp/src/arrow/type_traits.h | 2 +- cpp/src/arrow/visit_array_inline.h | 2 +- cpp/src/arrow/visit_scalar_inline.h | 2 +- cpp/src/arrow/visit_type_inline.h | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cpp/src/arrow/api.h b/cpp/src/arrow/api.h index 53b204cf9dd0..1aabee35b10f 100644 --- a/cpp/src/arrow/api.h +++ b/cpp/src/arrow/api.h @@ -21,8 +21,6 @@ #include "arrow/array.h" // IYWU pragma: export #include "arrow/array/concatenate.h" // IYWU pragma: export -#include "arrow/visit_array_inline.h" // IYWU pragma: export -#include "arrow/visit_scalar_inline.h" // IYWU pragma: export #include "arrow/buffer.h" // IYWU pragma: export #include "arrow/builder.h" // IYWU pragma: export #include "arrow/chunked_array.h" // IYWU pragma: export @@ -40,6 +38,8 @@ #include "arrow/tensor.h" // IYWU pragma: export #include "arrow/type.h" // IYWU pragma: export #include "arrow/util/key_value_metadata.h" // IWYU pragma: export +#include "arrow/visit_array_inline.h" // IYWU pragma: export +#include "arrow/visit_scalar_inline.h" // IYWU pragma: export #include "arrow/visitor.h" // IYWU pragma: export /// \brief Top-level namespace for Apache Arrow C++ API diff --git a/cpp/src/arrow/type_traits.h b/cpp/src/arrow/type_traits.h index 8521649a872a..80d7d66f2fdd 100644 --- a/cpp/src/arrow/type_traits.h +++ b/cpp/src/arrow/type_traits.h @@ -88,7 +88,7 @@ TYPE_ID_TRAIT(EXTENSION, ExtensionType) /// \addtogroup type-traits /// \brief Base template for type traits of Arrow data types /// Type traits provide various information about a type at compile time, such -/// as the associated ArrayType, BuilderType, and ScalarType. Not all types +/// as the associated ArrayType, BuilderType, and ScalarType. Not all types /// provide all information. /// \tparam T An Arrow data type template diff --git a/cpp/src/arrow/visit_array_inline.h b/cpp/src/arrow/visit_array_inline.h index f713227777a6..895cc3744577 100644 --- a/cpp/src/arrow/visit_array_inline.h +++ b/cpp/src/arrow/visit_array_inline.h @@ -36,7 +36,7 @@ namespace arrow { /// /// A visitor is a type that implements specialized logic for each Arrow type. /// Example usage: -/// +/// /// ``` /// class ExampleVisitor { /// arrow::Status Visit(arrow::NumericArray arr) { ... } diff --git a/cpp/src/arrow/visit_scalar_inline.h b/cpp/src/arrow/visit_scalar_inline.h index ee89da20e348..f3e8108e9c36 100644 --- a/cpp/src/arrow/visit_scalar_inline.h +++ b/cpp/src/arrow/visit_scalar_inline.h @@ -39,7 +39,7 @@ namespace arrow { /// /// A visitor is a type that implements specialized logic for each Arrow type. /// Example usage: -/// +/// /// ``` /// class ExampleVisitor { /// arrow::Status Visit(arrow::Int32Scalar arr) { ... } diff --git a/cpp/src/arrow/visit_type_inline.h b/cpp/src/arrow/visit_type_inline.h index 9aea08477392..6897d12b3cf9 100644 --- a/cpp/src/arrow/visit_type_inline.h +++ b/cpp/src/arrow/visit_type_inline.h @@ -35,7 +35,7 @@ namespace arrow { /// /// A visitor is a type that implements specialized logic for each Arrow type. /// Example usage: -/// +/// /// ``` /// class ExampleVisitor { /// arrow::Status Visit(const arrow::Int32Type& type) { ... } From 31c6038f9bfa664e85074928b9799d5da5940687 Mon Sep 17 00:00:00 2001 From: Will Jones Date: Mon, 28 Mar 2022 09:26:44 -0700 Subject: [PATCH 08/10] Apply suggestions from code review Co-authored-by: David Li --- docs/source/cpp/datatypes.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/source/cpp/datatypes.rst b/docs/source/cpp/datatypes.rst index 2322d34a105a..802e2c30f202 100644 --- a/docs/source/cpp/datatypes.rst +++ b/docs/source/cpp/datatypes.rst @@ -75,7 +75,7 @@ Type Traits Writing code that can handle concrete :class:`arrow::DataType` subclasses would be verbose, if it weren't for type traits. They are empty structs with type declarations that map the Arrow data types to the specialized array, scalar, -builder, and other associated types. For example, boolean type has traits: +builder, and other associated types. For example, the Boolean type has traits: .. code-block:: cpp @@ -97,7 +97,7 @@ See the :ref:`type-traits` for an explanation of each of these fields. Using type traits, one can write template functions that can handle a variety of Arrow types. For example, to write a function that creates an array of -fibonacci values: +Fibonacci values for any Arrow numeric type: .. code-block:: cpp From e5c4e10654d9e3ec6f641d7f4cba472cd4a37a63 Mon Sep 17 00:00:00 2001 From: Will Jones Date: Mon, 28 Mar 2022 09:40:43 -0700 Subject: [PATCH 09/10] Address more PR feedback --- docs/source/cpp/api/utilities.rst | 3 +-- docs/source/cpp/datatypes.rst | 9 +++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/source/cpp/api/utilities.rst b/docs/source/cpp/api/utilities.rst index 23900ce427b3..27625d745155 100644 --- a/docs/source/cpp/api/utilities.rst +++ b/docs/source/cpp/api/utilities.rst @@ -122,8 +122,7 @@ In addition, the following are defined for many but not all of the types: .. cpp:function:: static inline std::shared_ptr TypeTraits::type_singleton() - For types where is_parameter_free is true, returns a pointer to the type - singleton. + For types where is_parameter_free is true, returns an instance of the data type. .. doxygengroup:: type-traits diff --git a/docs/source/cpp/datatypes.rst b/docs/source/cpp/datatypes.rst index 802e2c30f202..c4e947599526 100644 --- a/docs/source/cpp/datatypes.rst +++ b/docs/source/cpp/datatypes.rst @@ -73,8 +73,9 @@ Type Traits ----------- Writing code that can handle concrete :class:`arrow::DataType` subclasses would -be verbose, if it weren't for type traits. They are empty structs with type -declarations that map the Arrow data types to the specialized array, scalar, +be verbose, if it weren't for type traits. Similar to the type traits provided +in `std::type_traits `_, +Arrow's type traits map the Arrow data types to the specialized array, scalar, builder, and other associated types. For example, the Boolean type has traits: .. code-block:: cpp @@ -105,7 +106,7 @@ Fibonacci values for any Arrow numeric type: typename BuilderType = typename arrow::TypeTraits::BuilderType, typename ArrayType = typename arrow::TypeTraits::ArrayType, typename CType = typename arrow::TypeTraits::CType> - arrow::Result> make_fibonacci(int32_t n) { + arrow::Result> MakeFibonacci(int32_t n) { BuilderType builder; CType val = 0; CType next_val = 1; @@ -135,7 +136,7 @@ need to be implemented. For example, to write a sum function for any numeric template - arrow::enable_if_number sum_array(const ArrayType& array) { + arrow::enable_if_number SumArray(const ArrayType& array) { CType sum = 0; for (arrow::util::optional value : array) { if (value.has_value()) { From 31b6f91fbebc7245cbdd94dad8a4b806315a8bb7 Mon Sep 17 00:00:00 2001 From: Will Jones Date: Mon, 28 Mar 2022 10:42:51 -0700 Subject: [PATCH 10/10] Move std::type_traits ref to more relevant location --- docs/source/cpp/api/utilities.rst | 4 ++-- docs/source/cpp/datatypes.rst | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/docs/source/cpp/api/utilities.rst b/docs/source/cpp/api/utilities.rst index 27625d745155..a0043ff9ca9a 100644 --- a/docs/source/cpp/api/utilities.rst +++ b/docs/source/cpp/api/utilities.rst @@ -160,12 +160,12 @@ Example usage: .. code-block:: cpp template - arrow::enable_if_number my_function(const TypeClass& type) { + arrow::enable_if_number MyFunction(const TypeClass& type) { .. } template - arrow::enable_if_number my_function(const ArrayType& array) { + arrow::enable_if_number MyFunction(const ArrayType& array) { .. } diff --git a/docs/source/cpp/datatypes.rst b/docs/source/cpp/datatypes.rst index c4e947599526..5428fbd008cf 100644 --- a/docs/source/cpp/datatypes.rst +++ b/docs/source/cpp/datatypes.rst @@ -73,10 +73,9 @@ Type Traits ----------- Writing code that can handle concrete :class:`arrow::DataType` subclasses would -be verbose, if it weren't for type traits. Similar to the type traits provided -in `std::type_traits `_, -Arrow's type traits map the Arrow data types to the specialized array, scalar, -builder, and other associated types. For example, the Boolean type has traits: +be verbose, if it weren't for type traits. Arrow's type traits map the Arrow +data types to the specialized array, scalar, builder, and other associated types. +For example, the Boolean type has traits: .. code-block:: cpp @@ -127,10 +126,13 @@ For some common cases, there are type associations on the classes themselves. Us * ``Array::TypeClass`` to get data type class of an array * ``DataType::c_type`` to get associated C type of an Arrow data type -There are also template type definitions for constraining template functions to a -subset of Arrow types using ``std::enable_if_t``, which are useful if other overloads -need to be implemented. For example, to write a sum function for any numeric -(integer or float) array: +Similar to the type traits provided in +`std::type_traits `_, +Arrow provides type predicates such as ``is_number_type`` as well as +corresponding templates that wrap ``std::enable_if_t`` such as ``enable_if_number``. +These can constrain template functions to only compile for relevant types, which +is useful if other overloads need to be implemented. For example, to write a sum +function for any numeric (integer or float) array: .. code-block:: cpp