Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
type_traits.h
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #ifndef ARROW_TYPE_TRAITS_H
19 #define ARROW_TYPE_TRAITS_H
20 
21 #include <memory>
22 #include <type_traits>
23 
24 #include "arrow/type_fwd.h"
25 #include "arrow/util/bit-util.h"
26 
27 namespace arrow {
28 
29 template <typename T>
30 struct TypeTraits {};
31 
32 template <>
36  constexpr static bool is_parameter_free = false;
37 };
38 
39 template <>
41  using ArrayType = UInt8Array;
43  using TensorType = UInt8Tensor;
44  static inline int64_t bytes_required(int64_t elements) { return elements; }
45  constexpr static bool is_parameter_free = true;
46  static inline std::shared_ptr<DataType> type_singleton() { return uint8(); }
47 };
48 
49 template <>
51  using ArrayType = Int8Array;
53  using TensorType = Int8Tensor;
54  static inline int64_t bytes_required(int64_t elements) { return elements; }
55  constexpr static bool is_parameter_free = true;
56  static inline std::shared_ptr<DataType> type_singleton() { return int8(); }
57 };
58 
59 template <>
61  using ArrayType = UInt16Array;
63  using TensorType = UInt16Tensor;
64 
65  static inline int64_t bytes_required(int64_t elements) {
66  return elements * sizeof(uint16_t);
67  }
68  constexpr static bool is_parameter_free = true;
69  static inline std::shared_ptr<DataType> type_singleton() { return uint16(); }
70 };
71 
72 template <>
74  using ArrayType = Int16Array;
76  using TensorType = Int16Tensor;
77 
78  static inline int64_t bytes_required(int64_t elements) {
79  return elements * sizeof(int16_t);
80  }
81  constexpr static bool is_parameter_free = true;
82  static inline std::shared_ptr<DataType> type_singleton() { return int16(); }
83 };
84 
85 template <>
87  using ArrayType = UInt32Array;
89  using TensorType = UInt32Tensor;
90 
91  static inline int64_t bytes_required(int64_t elements) {
92  return elements * sizeof(uint32_t);
93  }
94  constexpr static bool is_parameter_free = true;
95  static inline std::shared_ptr<DataType> type_singleton() { return uint32(); }
96 };
97 
98 template <>
100  using ArrayType = Int32Array;
102  using TensorType = Int32Tensor;
103 
104  static inline int64_t bytes_required(int64_t elements) {
105  return elements * sizeof(int32_t);
106  }
107  constexpr static bool is_parameter_free = true;
108  static inline std::shared_ptr<DataType> type_singleton() { return int32(); }
109 };
110 
111 template <>
113  using ArrayType = UInt64Array;
115  using TensorType = UInt64Tensor;
116 
117  static inline int64_t bytes_required(int64_t elements) {
118  return elements * sizeof(uint64_t);
119  }
120  constexpr static bool is_parameter_free = true;
121  static inline std::shared_ptr<DataType> type_singleton() { return uint64(); }
122 };
123 
124 template <>
126  using ArrayType = Int64Array;
128  using TensorType = Int64Tensor;
129 
130  static inline int64_t bytes_required(int64_t elements) {
131  return elements * sizeof(int64_t);
132  }
133  constexpr static bool is_parameter_free = true;
134  static inline std::shared_ptr<DataType> type_singleton() { return int64(); }
135 };
136 
137 template <>
141 
142  static inline int64_t bytes_required(int64_t elements) {
143  return elements * sizeof(int64_t);
144  }
145  constexpr static bool is_parameter_free = true;
146  static inline std::shared_ptr<DataType> type_singleton() { return date64(); }
147 };
148 
149 template <>
153 
154  static inline int64_t bytes_required(int64_t elements) {
155  return elements * sizeof(int32_t);
156  }
157  constexpr static bool is_parameter_free = true;
158  static inline std::shared_ptr<DataType> type_singleton() { return date32(); }
159 };
160 
161 template <>
165 
166  static inline int64_t bytes_required(int64_t elements) {
167  return elements * sizeof(int64_t);
168  }
169  constexpr static bool is_parameter_free = false;
170 };
171 
172 template <>
176 
177  static inline int64_t bytes_required(int64_t elements) {
178  return elements * sizeof(int32_t);
179  }
180  constexpr static bool is_parameter_free = false;
181 };
182 
183 template <>
187 
188  static inline int64_t bytes_required(int64_t elements) {
189  return elements * sizeof(int64_t);
190  }
191  constexpr static bool is_parameter_free = false;
192 };
193 
194 template <>
196  using ArrayType = HalfFloatArray;
198  using TensorType = HalfFloatTensor;
199 
200  static inline int64_t bytes_required(int64_t elements) {
201  return elements * sizeof(uint16_t);
202  }
203  constexpr static bool is_parameter_free = true;
204  static inline std::shared_ptr<DataType> type_singleton() { return float16(); }
205 };
206 
207 template <>
209  using ArrayType = FloatArray;
211  using TensorType = FloatTensor;
212 
213  static inline int64_t bytes_required(int64_t elements) {
214  return static_cast<int64_t>(elements * sizeof(float));
215  }
216  constexpr static bool is_parameter_free = true;
217  static inline std::shared_ptr<DataType> type_singleton() { return float32(); }
218 };
219 
220 template <>
222  using ArrayType = DoubleArray;
224  using TensorType = DoubleTensor;
225 
226  static inline int64_t bytes_required(int64_t elements) {
227  return static_cast<int64_t>(elements * sizeof(double));
228  }
229  constexpr static bool is_parameter_free = true;
230  static inline std::shared_ptr<DataType> type_singleton() { return float64(); }
231 };
232 
233 template <>
237  constexpr static bool is_parameter_free = false;
238 };
239 
240 template <>
244 
245  static inline int64_t bytes_required(int64_t elements) {
246  return BitUtil::BytesForBits(elements);
247  }
248  constexpr static bool is_parameter_free = true;
249  static inline std::shared_ptr<DataType> type_singleton() { return boolean(); }
250 };
251 
252 template <>
256  constexpr static bool is_parameter_free = true;
257  static inline std::shared_ptr<DataType> type_singleton() { return utf8(); }
258 };
259 
260 template <>
264  constexpr static bool is_parameter_free = true;
265  static inline std::shared_ptr<DataType> type_singleton() { return binary(); }
266 };
267 
268 template <>
272  constexpr static bool is_parameter_free = false;
273 };
274 
275 template <>
279  constexpr static bool is_parameter_free = false;
280 };
281 
282 template <>
286  constexpr static bool is_parameter_free = false;
287 };
288 
289 template <>
292  constexpr static bool is_parameter_free = false;
293 };
294 
295 template <>
298  constexpr static bool is_parameter_free = false;
299 };
300 
301 namespace detail {
302 
303 // Not all type classes have a c_type
304 template <typename T>
305 struct as_void {
306  using type = void;
307 };
308 
309 // The partial specialization will match if T has the ATTR_NAME member
310 #define GET_ATTR(ATTR_NAME, DEFAULT) \
311  template <typename T, typename Enable = void> \
312  struct GetAttr_##ATTR_NAME { \
313  using type = DEFAULT; \
314  }; \
315  \
316  template <typename T> \
317  struct GetAttr_##ATTR_NAME<T, typename as_void<typename T::ATTR_NAME>::type> { \
318  using type = typename T::ATTR_NAME; \
319  };
320 
321 GET_ATTR(c_type, void);
322 GET_ATTR(TypeClass, void);
323 
324 #undef GET_ATTR
325 
326 } // namespace detail
327 
328 #define PRIMITIVE_TRAITS(T) \
329  using TypeClass = \
330  typename std::conditional<std::is_base_of<DataType, T>::value, T, \
331  typename detail::GetAttr_TypeClass<T>::type>::type; \
332  using c_type = typename detail::GetAttr_c_type<TypeClass>::type
333 
334 template <typename T>
336  PRIMITIVE_TRAITS(T);
337  static constexpr bool value =
338  std::is_integral<c_type>::value && std::is_unsigned<c_type>::value;
339 };
340 
341 template <typename T>
342 struct IsSignedInt {
343  PRIMITIVE_TRAITS(T);
344  static constexpr bool value =
345  std::is_integral<c_type>::value && std::is_signed<c_type>::value;
346 };
347 
348 template <typename T>
349 struct IsInteger {
350  PRIMITIVE_TRAITS(T);
351  static constexpr bool value = std::is_integral<c_type>::value;
352 };
353 
354 template <typename T>
356  PRIMITIVE_TRAITS(T);
357  static constexpr bool value = std::is_floating_point<c_type>::value;
358 };
359 
360 template <typename T>
361 struct IsNumeric {
362  PRIMITIVE_TRAITS(T);
363  static constexpr bool value = std::is_arithmetic<c_type>::value;
364 };
365 
366 static inline bool is_integer(Type::type type_id) {
367  switch (type_id) {
368  case Type::UINT8:
369  case Type::INT8:
370  case Type::UINT16:
371  case Type::INT16:
372  case Type::UINT32:
373  case Type::INT32:
374  case Type::UINT64:
375  case Type::INT64:
376  return true;
377  default:
378  break;
379  }
380  return false;
381 }
382 
383 static inline bool is_floating(Type::type type_id) {
384  switch (type_id) {
385  case Type::HALF_FLOAT:
386  case Type::FLOAT:
387  case Type::DOUBLE:
388  return true;
389  default:
390  break;
391  }
392  return false;
393 }
394 
395 static inline bool is_primitive(Type::type type_id) {
396  switch (type_id) {
397  case Type::NA:
398  case Type::BOOL:
399  case Type::UINT8:
400  case Type::INT8:
401  case Type::UINT16:
402  case Type::INT16:
403  case Type::UINT32:
404  case Type::INT32:
405  case Type::UINT64:
406  case Type::INT64:
407  case Type::HALF_FLOAT:
408  case Type::FLOAT:
409  case Type::DOUBLE:
410  case Type::DATE32:
411  case Type::DATE64:
412  case Type::TIME32:
413  case Type::TIME64:
414  case Type::TIMESTAMP:
415  case Type::INTERVAL:
416  return true;
417  default:
418  break;
419  }
420  return false;
421 }
422 
423 static inline bool is_binary_like(Type::type type_id) {
424  switch (type_id) {
425  case Type::BINARY:
426  case Type::STRING:
427  return true;
428  default:
429  break;
430  }
431  return false;
432 }
433 
434 static inline bool is_dictionary(Type::type type_id) {
435  return type_id == Type::DICTIONARY;
436 }
437 
438 } // namespace arrow
439 
440 #endif // ARROW_TYPE_TRAITS_H
Definition: type.h:469
Definition: array.h:566
Definition: array.h:369
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:213
DoubleTensor TensorType
Definition: type_traits.h:224
NumericArray< Time32Type > Time32Array
Definition: type_fwd.h:122
Builder class for UTF8 strings.
Definition: builder.h:763
NumericBuilder< FloatType > FloatBuilder
Definition: builder.h:331
A NULL type having no physical storage.
Definition: type.h:48
NumericBuilder< Time32Type > Time32Builder
Definition: builder.h:325
Definition: type.h:364
Builder class for variable-length binary data.
Definition: builder.h:718
NumericBuilder< Int64Type > Int64Builder
Definition: builder.h:323
UInt32Tensor TensorType
Definition: type_traits.h:89
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:265
std::shared_ptr< DataType > date64()
Definition: type.h:316
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:200
type
Definition: type.h:46
static constexpr bool value
Definition: type_traits.h:363
Unsigned 16-bit little-endian integer.
Definition: type.h:60
UInt8Array ArrayType
Definition: type_traits.h:41
Definition: type.h:378
Unsigned 32-bit little-endian integer.
Definition: type.h:66
std::shared_ptr< DataType > int16()
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:104
std::shared_ptr< DataType > uint8()
std::shared_ptr< DataType > binary()
Unsigned 8-bit little-endian integer.
Definition: type.h:54
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:166
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:188
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:142
UInt32Array ArrayType
Definition: type_traits.h:87
Int16Array ArrayType
Definition: type_traits.h:74
Definition: array.h:397
Signed 8-bit little-endian integer.
Definition: type.h:57
NumericArray< Date64Type > Date64Array
Definition: type_fwd.h:114
Date as int64_t milliseconds since UNIX epoch.
Definition: type.h:581
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:91
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:257
Definition: type_traits.h:349
Definition: type_traits.h:361
std::shared_ptr< DataType > uint16()
std::shared_ptr< DataType > int32()
static constexpr bool value
Definition: type_traits.h:344
NumericBuilder< Int16Type > Int16Builder
Definition: builder.h:321
static constexpr bool value
Definition: type_traits.h:337
HalfFloatTensor TensorType
Definition: type_traits.h:198
NumericBuilder< UInt16Type > UInt16Builder
Definition: builder.h:316
UInt64Tensor TensorType
Definition: type_traits.h:115
#define GET_ATTR(ATTR_NAME, DEFAULT)
Definition: type_traits.h:310
Dictionary aka Category type.
Definition: type.h:130
Time as signed 64-bit integer, representing either microseconds or nanoseconds since midnight...
Definition: type.h:111
NumericBuilder< Int8Type > Int8Builder
Definition: builder.h:320
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:95
std::shared_ptr< DataType > uint32()
NumericBuilder< Int32Type > Int32Builder
Definition: builder.h:322
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:56
Signed 32-bit little-endian integer.
Definition: type.h:69
UInt64Array ArrayType
Definition: type_traits.h:113
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:249
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:121
NumericBuilder< UInt32Type > UInt32Builder
Definition: builder.h:317
Base class for all Builders that emit an Array of a scalar numerical type.
Definition: builder.h:273
Time as signed 32-bit integer, representing either seconds or milliseconds since midnight.
Definition: type.h:107
Int32Tensor TensorType
Definition: type_traits.h:102
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:154
Int32Array ArrayType
Definition: type_traits.h:100
int32_t days since the UNIX epoch
Definition: type.h:96
Definition: builder.h:543
Definition: type_traits.h:30
FloatTensor TensorType
Definition: type_traits.h:211
std::shared_ptr< DataType > uint64()
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:245
4-byte floating point value
Definition: type.h:81
Signed 16-bit little-endian integer.
Definition: type.h:63
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:158
NumericBuilder< UInt8Type > UInt8Builder
Definition: builder.h:315
std::shared_ptr< DataType > int8()
Definition: type_traits.h:355
Int16Tensor TensorType
Definition: type_traits.h:76
DoubleArray ArrayType
Definition: type_traits.h:222
Int64Array ArrayType
Definition: type_traits.h:126
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:130
Definition: type.h:328
Definition: type.h:658
std::shared_ptr< DataType > float32()
UInt16Array ArrayType
Definition: type_traits.h:61
NumericBuilder< HalfFloatType > HalfFloatBuilder
Definition: builder.h:330
Boolean as 1 bit, LSB bit-packed ordering.
Definition: type.h:51
Signed 64-bit little-endian integer.
Definition: type.h:75
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:65
Definition: type.h:340
Variable-length bytes (no guarantee of UTF8-ness)
Definition: type.h:90
Definition: type.h:291
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:78
NumericBuilder< Time64Type > Time64Builder
Definition: builder.h:326
Definition: type.h:421
FloatArray ArrayType
Definition: type_traits.h:209
Definition: type.h:303
Definition: type.h:385
Exact timestamp encoded with int64 since UNIX epoch Default unit millisecond.
Definition: type.h:103
Definition: type.h:458
static constexpr bool value
Definition: type_traits.h:357
std::shared_ptr< DataType > date32()
std::shared_ptr< DataType > int64()
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:134
Definition: builder.h:803
Int8Array ArrayType
Definition: type_traits.h:51
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:82
Definition: array.h:543
NumericBuilder< Date32Type > Date32Builder
Definition: builder.h:327
UInt16Tensor TensorType
Definition: type_traits.h:63
Top-level namespace for Apache Arrow C++ API.
Definition: adapter.h:32
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:54
std::shared_ptr< DataType > boolean()
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:204
NumericArray< Time64Type > Time64Array
Definition: type_fwd.h:126
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:177
Definition: array.h:419
Definition: type.h:508
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:69
Definition: type.h:371
Definition: type.h:334
Append, Resize and Reserve methods are acting on StructBuilder.
Definition: builder.h:873
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:226
8-byte floating point value
Definition: type.h:84
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:44
HalfFloatArray ArrayType
Definition: type_traits.h:196
Definition: type.h:346
NumericArray< TimestampType > TimestampArray
Definition: type_fwd.h:130
Date as int32_t days since UNIX epoch.
Definition: type.h:564
std::shared_ptr< DataType > float64()
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:46
Definition: array.h:476
UInt8Tensor TensorType
Definition: type_traits.h:43
int64_t milliseconds since the UNIX epoch
Definition: type.h:99
Definition: builder.h:850
Definition: type_traits.h:342
Definition: array.h:643
NumericBuilder< DoubleType > DoubleBuilder
Definition: builder.h:332
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:217
NumericBuilder< Date64Type > Date64Builder
Definition: builder.h:328
std::shared_ptr< DataType > utf8()
UTF8 variable-length string as List<Char>
Definition: type.h:87
NumericBuilder< UInt64Type > UInt64Builder
Definition: builder.h:318
Unsigned 64-bit little-endian integer.
Definition: type.h:72
Definition: array.h:595
Definition: array.h:736
std::shared_ptr< DataType > float16()
Definition: type.h:710
Definition: type.h:643
NumericArray< Date32Type > Date32Array
Definition: type_fwd.h:118
Definition: type.h:352
Int64Tensor TensorType
Definition: type_traits.h:128
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:146
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:108
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:117
Definition: array.h:613
Definition: type.h:322
2-byte floating point value
Definition: type.h:78
Definition: type.h:628
Definition: builder.h:175
Definition: type.h:524
YEAR_MONTH or DAY_TIME interval in SQL style.
Definition: type.h:114
static constexpr bool value
Definition: type_traits.h:351
Definition: type.h:358
Definition: type_traits.h:335
Builder class for variable-length list array value types.
Definition: builder.h:671
Int8Tensor TensorType
Definition: type_traits.h:53
NumericBuilder< TimestampType > TimestampBuilder
Definition: builder.h:324
Definition: type.h:437
Degenerate null type Array.
Definition: array.h:326
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:230