1919#define ARROW_TYPE_TRAITS_H
2020
2121#include < memory>
22+ #include < string>
2223#include < type_traits>
24+ #include < vector>
2325
2426#include " arrow/type_fwd.h"
2527#include " arrow/util/bit-util.h"
@@ -33,6 +35,9 @@ namespace arrow {
3335template <typename T>
3436struct TypeTraits {};
3537
38+ template <typename T>
39+ struct CTypeTraits {};
40+
3641template <>
3742struct TypeTraits <NullType> {
3843 using ArrayType = NullArray;
@@ -41,102 +46,61 @@ struct TypeTraits<NullType> {
4146};
4247
4348template <>
44- struct TypeTraits <UInt8Type> {
45- using ArrayType = UInt8Array;
46- using BuilderType = UInt8Builder;
47- using TensorType = UInt8Tensor;
48- static constexpr int64_t bytes_required (int64_t elements) { return elements; }
49- constexpr static bool is_parameter_free = true ;
50- static inline std::shared_ptr<DataType> type_singleton () { return uint8 (); }
51- };
52-
53- template <>
54- struct TypeTraits <Int8Type> {
55- using ArrayType = Int8Array;
56- using BuilderType = Int8Builder;
57- using TensorType = Int8Tensor;
58- static constexpr int64_t bytes_required (int64_t elements) { return elements; }
59- constexpr static bool is_parameter_free = true ;
60- static inline std::shared_ptr<DataType> type_singleton () { return int8 (); }
61- };
62-
63- template <>
64- struct TypeTraits <UInt16Type> {
65- using ArrayType = UInt16Array;
66- using BuilderType = UInt16Builder;
67- using TensorType = UInt16Tensor;
68-
69- static constexpr int64_t bytes_required (int64_t elements) {
70- return elements * sizeof (uint16_t );
71- }
72- constexpr static bool is_parameter_free = true ;
73- static inline std::shared_ptr<DataType> type_singleton () { return uint16 (); }
74- };
75-
76- template <>
77- struct TypeTraits <Int16Type> {
78- using ArrayType = Int16Array;
79- using BuilderType = Int16Builder;
80- using TensorType = Int16Tensor;
81-
82- static constexpr int64_t bytes_required (int64_t elements) {
83- return elements * sizeof (int16_t );
84- }
85- constexpr static bool is_parameter_free = true ;
86- static inline std::shared_ptr<DataType> type_singleton () { return int16 (); }
87- };
88-
89- template <>
90- struct TypeTraits <UInt32Type> {
91- using ArrayType = UInt32Array;
92- using BuilderType = UInt32Builder;
93- using TensorType = UInt32Tensor;
94-
95- static constexpr int64_t bytes_required (int64_t elements) {
96- return elements * sizeof (uint32_t );
97- }
98- constexpr static bool is_parameter_free = true ;
99- static inline std::shared_ptr<DataType> type_singleton () { return uint32 (); }
100- };
101-
102- template <>
103- struct TypeTraits <Int32Type> {
104- using ArrayType = Int32Array;
105- using BuilderType = Int32Builder;
106- using TensorType = Int32Tensor;
107-
108- static constexpr int64_t bytes_required (int64_t elements) {
109- return elements * sizeof (int32_t );
110- }
111- constexpr static bool is_parameter_free = true ;
112- static inline std::shared_ptr<DataType> type_singleton () { return int32 (); }
113- };
114-
115- template <>
116- struct TypeTraits <UInt64Type> {
117- using ArrayType = UInt64Array;
118- using BuilderType = UInt64Builder;
119- using TensorType = UInt64Tensor;
49+ struct TypeTraits <BooleanType> {
50+ using ArrayType = BooleanArray;
51+ using BuilderType = BooleanBuilder;
52+ using CType = bool ;
12053
12154 static constexpr int64_t bytes_required (int64_t elements) {
122- return elements * sizeof ( uint64_t );
55+ return BitUtil::BytesForBits (elements );
12356 }
12457 constexpr static bool is_parameter_free = true ;
125- static inline std::shared_ptr<DataType> type_singleton () { return uint64 (); }
58+ static inline std::shared_ptr<DataType> type_singleton () { return boolean (); }
12659};
12760
12861template <>
129- struct TypeTraits <Int64Type> {
130- using ArrayType = Int64Array;
131- using BuilderType = Int64Builder;
132- using TensorType = Int64Tensor;
62+ struct CTypeTraits <bool > : public TypeTraits<BooleanType> {
63+ using ArrowType = BooleanType;
64+ };
65+
66+ #define PRIMITIVE_TYPE_TRAITS_DEF_ (CType_, ArrowType_, ArrowArrayType, ArrowBuilderType, \
67+ ArrowTensorType, SingletonFn) \
68+ template <> \
69+ struct TypeTraits <ArrowType_> { \
70+ using ArrayType = ArrowArrayType; \
71+ using BuilderType = ArrowBuilderType; \
72+ using TensorType = ArrowTensorType; \
73+ using CType = CType_; \
74+ static constexpr int64_t bytes_required (int64_t elements) { \
75+ return elements * sizeof (CType_); \
76+ } \
77+ constexpr static bool is_parameter_free = true ; \
78+ static inline std::shared_ptr<DataType> type_singleton () { return SingletonFn (); } \
79+ }; \
80+ \
81+ template <> \
82+ struct CTypeTraits <CType_> : public TypeTraits<ArrowType_> { \
83+ using ArrowType = ArrowType_; \
84+ };
13385
134- static constexpr int64_t bytes_required (int64_t elements) {
135- return elements * sizeof (int64_t );
136- }
137- constexpr static bool is_parameter_free = true ;
138- static inline std::shared_ptr<DataType> type_singleton () { return int64 (); }
139- };
86+ #define PRIMITIVE_TYPE_TRAITS_DEF (CType, ArrowShort, SingletonFn ) \
87+ PRIMITIVE_TYPE_TRAITS_DEF_ ( \
88+ CType, ARROW_CONCAT (ArrowShort, Type), ARROW_CONCAT (ArrowShort, Array), \
89+ ARROW_CONCAT (ArrowShort, Builder), ARROW_CONCAT (ArrowShort, Tensor), SingletonFn)
90+
91+ PRIMITIVE_TYPE_TRAITS_DEF (uint8_t , UInt8, uint8)
92+ PRIMITIVE_TYPE_TRAITS_DEF (int8_t , Int8, int8)
93+ PRIMITIVE_TYPE_TRAITS_DEF (uint16_t , UInt16, uint16)
94+ PRIMITIVE_TYPE_TRAITS_DEF (int16_t , Int16, int16)
95+ PRIMITIVE_TYPE_TRAITS_DEF (uint32_t , UInt32, uint32)
96+ PRIMITIVE_TYPE_TRAITS_DEF (int32_t , Int32, int32)
97+ PRIMITIVE_TYPE_TRAITS_DEF (uint64_t , UInt64, uint64)
98+ PRIMITIVE_TYPE_TRAITS_DEF (int64_t , Int64, int64)
99+ PRIMITIVE_TYPE_TRAITS_DEF (float , Float, float32)
100+ PRIMITIVE_TYPE_TRAITS_DEF (double , Double, float64)
101+
102+ #undef PRIMITIVE_TYPE_TRAITS_DEF
103+ #undef PRIMITIVE_TYPE_TRAITS_DEF_
140104
141105template <>
142106struct TypeTraits <Date64Type> {
@@ -208,51 +172,13 @@ struct TypeTraits<HalfFloatType> {
208172 static inline std::shared_ptr<DataType> type_singleton () { return float16 (); }
209173};
210174
211- template <>
212- struct TypeTraits <FloatType> {
213- using ArrayType = FloatArray;
214- using BuilderType = FloatBuilder;
215- using TensorType = FloatTensor;
216-
217- static constexpr int64_t bytes_required (int64_t elements) {
218- return static_cast <int64_t >(elements * sizeof (float ));
219- }
220- constexpr static bool is_parameter_free = true ;
221- static inline std::shared_ptr<DataType> type_singleton () { return float32 (); }
222- };
223-
224- template <>
225- struct TypeTraits <DoubleType> {
226- using ArrayType = DoubleArray;
227- using BuilderType = DoubleBuilder;
228- using TensorType = DoubleTensor;
229-
230- static constexpr int64_t bytes_required (int64_t elements) {
231- return static_cast <int64_t >(elements * sizeof (double ));
232- }
233- constexpr static bool is_parameter_free = true ;
234- static inline std::shared_ptr<DataType> type_singleton () { return float64 (); }
235- };
236-
237175template <>
238176struct TypeTraits <Decimal128Type> {
239177 using ArrayType = Decimal128Array;
240178 using BuilderType = Decimal128Builder;
241179 constexpr static bool is_parameter_free = false ;
242180};
243181
244- template <>
245- struct TypeTraits <BooleanType> {
246- using ArrayType = BooleanArray;
247- using BuilderType = BooleanBuilder;
248-
249- static constexpr int64_t bytes_required (int64_t elements) {
250- return BitUtil::BytesForBits (elements);
251- }
252- constexpr static bool is_parameter_free = true ;
253- static inline std::shared_ptr<DataType> type_singleton () { return boolean (); }
254- };
255-
256182template <>
257183struct TypeTraits <StringType> {
258184 using ArrayType = StringArray;
@@ -261,6 +187,16 @@ struct TypeTraits<StringType> {
261187 static inline std::shared_ptr<DataType> type_singleton () { return utf8 (); }
262188};
263189
190+ template <>
191+ struct CTypeTraits <std::string> : public TypeTraits<StringType> {
192+ using ArrowType = StringType;
193+ };
194+
195+ template <>
196+ struct CTypeTraits <char *> : public TypeTraits<StringType> {
197+ using ArrowType = StringType;
198+ };
199+
264200template <>
265201struct TypeTraits <BinaryType> {
266202 using ArrayType = BinaryArray;
@@ -283,6 +219,15 @@ struct TypeTraits<ListType> {
283219 constexpr static bool is_parameter_free = false ;
284220};
285221
222+ template <typename CType>
223+ struct CTypeTraits <std::vector<CType>> : public TypeTraits<ListType> {
224+ using ArrowType = ListType;
225+
226+ static inline std::shared_ptr<DataType> type_singleton () {
227+ return list (CTypeTraits<CType>::type_singleton ());
228+ }
229+ };
230+
286231template <>
287232struct TypeTraits <StructType> {
288233 using ArrayType = StructArray;
0 commit comments