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 <type_traits>
22 
23 #include "arrow/type_fwd.h"
24 #include "arrow/util/bit-util.h"
25 
26 namespace arrow {
27 
28 template <typename T>
29 struct TypeTraits {};
30 
31 template <>
35  constexpr static bool is_parameter_free = false;
36 };
37 
38 template <>
40  using ArrayType = UInt8Array;
42  using TensorType = UInt8Tensor;
43  static inline int64_t bytes_required(int64_t elements) { return elements; }
44  constexpr static bool is_parameter_free = true;
45  static inline std::shared_ptr<DataType> type_singleton() { return uint8(); }
46 };
47 
48 template <>
50  using ArrayType = Int8Array;
52  using TensorType = Int8Tensor;
53  static inline int64_t bytes_required(int64_t elements) { return elements; }
54  constexpr static bool is_parameter_free = true;
55  static inline std::shared_ptr<DataType> type_singleton() { return int8(); }
56 };
57 
58 template <>
60  using ArrayType = UInt16Array;
62  using TensorType = UInt16Tensor;
63 
64  static inline int64_t bytes_required(int64_t elements) {
65  return elements * sizeof(uint16_t);
66  }
67  constexpr static bool is_parameter_free = true;
68  static inline std::shared_ptr<DataType> type_singleton() { return uint16(); }
69 };
70 
71 template <>
73  using ArrayType = Int16Array;
75  using TensorType = Int16Tensor;
76 
77  static inline int64_t bytes_required(int64_t elements) {
78  return elements * sizeof(int16_t);
79  }
80  constexpr static bool is_parameter_free = true;
81  static inline std::shared_ptr<DataType> type_singleton() { return int16(); }
82 };
83 
84 template <>
86  using ArrayType = UInt32Array;
88  using TensorType = UInt32Tensor;
89 
90  static inline int64_t bytes_required(int64_t elements) {
91  return elements * sizeof(uint32_t);
92  }
93  constexpr static bool is_parameter_free = true;
94  static inline std::shared_ptr<DataType> type_singleton() { return uint32(); }
95 };
96 
97 template <>
99  using ArrayType = Int32Array;
101  using TensorType = Int32Tensor;
102 
103  static inline int64_t bytes_required(int64_t elements) {
104  return elements * sizeof(int32_t);
105  }
106  constexpr static bool is_parameter_free = true;
107  static inline std::shared_ptr<DataType> type_singleton() { return int32(); }
108 };
109 
110 template <>
112  using ArrayType = UInt64Array;
114  using TensorType = UInt64Tensor;
115 
116  static inline int64_t bytes_required(int64_t elements) {
117  return elements * sizeof(uint64_t);
118  }
119  constexpr static bool is_parameter_free = true;
120  static inline std::shared_ptr<DataType> type_singleton() { return uint64(); }
121 };
122 
123 template <>
125  using ArrayType = Int64Array;
127  using TensorType = Int64Tensor;
128 
129  static inline int64_t bytes_required(int64_t elements) {
130  return elements * sizeof(int64_t);
131  }
132  constexpr static bool is_parameter_free = true;
133  static inline std::shared_ptr<DataType> type_singleton() { return int64(); }
134 };
135 
136 template <>
140 
141  static inline int64_t bytes_required(int64_t elements) {
142  return elements * sizeof(int64_t);
143  }
144  constexpr static bool is_parameter_free = true;
145  static inline std::shared_ptr<DataType> type_singleton() { return date64(); }
146 };
147 
148 template <>
152 
153  static inline int64_t bytes_required(int64_t elements) {
154  return elements * sizeof(int32_t);
155  }
156  constexpr static bool is_parameter_free = true;
157  static inline std::shared_ptr<DataType> type_singleton() { return date32(); }
158 };
159 
160 template <>
164 
165  static inline int64_t bytes_required(int64_t elements) {
166  return elements * sizeof(int64_t);
167  }
168  constexpr static bool is_parameter_free = false;
169 };
170 
171 template <>
175 
176  static inline int64_t bytes_required(int64_t elements) {
177  return elements * sizeof(int32_t);
178  }
179  constexpr static bool is_parameter_free = false;
180 };
181 
182 template <>
186 
187  static inline int64_t bytes_required(int64_t elements) {
188  return elements * sizeof(int64_t);
189  }
190  constexpr static bool is_parameter_free = false;
191 };
192 
193 template <>
195  using ArrayType = HalfFloatArray;
197  using TensorType = HalfFloatTensor;
198 
199  static inline int64_t bytes_required(int64_t elements) {
200  return elements * sizeof(uint16_t);
201  }
202  constexpr static bool is_parameter_free = true;
203  static inline std::shared_ptr<DataType> type_singleton() { return float16(); }
204 };
205 
206 template <>
208  using ArrayType = FloatArray;
210  using TensorType = FloatTensor;
211 
212  static inline int64_t bytes_required(int64_t elements) {
213  return static_cast<int64_t>(elements * sizeof(float));
214  }
215  constexpr static bool is_parameter_free = true;
216  static inline std::shared_ptr<DataType> type_singleton() { return float32(); }
217 };
218 
219 template <>
221  using ArrayType = DoubleArray;
223  using TensorType = DoubleTensor;
224 
225  static inline int64_t bytes_required(int64_t elements) {
226  return static_cast<int64_t>(elements * sizeof(double));
227  }
228  constexpr static bool is_parameter_free = true;
229  static inline std::shared_ptr<DataType> type_singleton() { return float64(); }
230 };
231 
232 template <>
236  constexpr static bool is_parameter_free = false;
237 };
238 
239 template <>
243 
244  static inline int64_t bytes_required(int64_t elements) {
245  return BitUtil::BytesForBits(elements);
246  }
247  constexpr static bool is_parameter_free = true;
248  static inline std::shared_ptr<DataType> type_singleton() { return boolean(); }
249 };
250 
251 template <>
255  constexpr static bool is_parameter_free = true;
256  static inline std::shared_ptr<DataType> type_singleton() { return utf8(); }
257 };
258 
259 template <>
263  constexpr static bool is_parameter_free = true;
264  static inline std::shared_ptr<DataType> type_singleton() { return binary(); }
265 };
266 
267 template <>
271  constexpr static bool is_parameter_free = false;
272 };
273 
274 template <>
278  constexpr static bool is_parameter_free = false;
279 };
280 
281 template <>
285  constexpr static bool is_parameter_free = false;
286 };
287 
288 template <>
291  constexpr static bool is_parameter_free = false;
292 };
293 
294 template <>
297  constexpr static bool is_parameter_free = false;
298 };
299 
300 namespace detail {
301 
302 // Not all type classes have a c_type
303 template <typename T>
304 struct as_void {
305  using type = void;
306 };
307 
308 // The partial specialization will match if T has the ATTR_NAME member
309 #define GET_ATTR(ATTR_NAME, DEFAULT) \
310  template <typename T, typename Enable = void> \
311  struct GetAttr_##ATTR_NAME { \
312  using type = DEFAULT; \
313  }; \
314  \
315  template <typename T> \
316  struct GetAttr_##ATTR_NAME<T, typename as_void<typename T::ATTR_NAME>::type> { \
317  using type = typename T::ATTR_NAME; \
318  };
319 
320 GET_ATTR(c_type, void);
321 GET_ATTR(TypeClass, void);
322 
323 #undef GET_ATTR
324 
325 } // namespace detail
326 
327 #define PRIMITIVE_TRAITS(T) \
328  using TypeClass = \
329  typename std::conditional<std::is_base_of<DataType, T>::value, T, \
330  typename detail::GetAttr_TypeClass<T>::type>::type; \
331  using c_type = typename detail::GetAttr_c_type<TypeClass>::type
332 
333 template <typename T>
335  PRIMITIVE_TRAITS(T);
336  static constexpr bool value =
337  std::is_integral<c_type>::value && std::is_unsigned<c_type>::value;
338 };
339 
340 template <typename T>
341 struct IsSignedInt {
342  PRIMITIVE_TRAITS(T);
343  static constexpr bool value =
344  std::is_integral<c_type>::value && std::is_signed<c_type>::value;
345 };
346 
347 template <typename T>
348 struct IsInteger {
349  PRIMITIVE_TRAITS(T);
350  static constexpr bool value = std::is_integral<c_type>::value;
351 };
352 
353 template <typename T>
355  PRIMITIVE_TRAITS(T);
356  static constexpr bool value = std::is_floating_point<c_type>::value;
357 };
358 
359 template <typename T>
360 struct IsNumeric {
361  PRIMITIVE_TRAITS(T);
362  static constexpr bool value = std::is_arithmetic<c_type>::value;
363 };
364 
365 static inline bool is_integer(Type::type type_id) {
366  switch (type_id) {
367  case Type::UINT8:
368  case Type::INT8:
369  case Type::UINT16:
370  case Type::INT16:
371  case Type::UINT32:
372  case Type::INT32:
373  case Type::UINT64:
374  case Type::INT64:
375  return true;
376  default:
377  break;
378  }
379  return false;
380 }
381 
382 static inline bool is_floating(Type::type type_id) {
383  switch (type_id) {
384  case Type::HALF_FLOAT:
385  case Type::FLOAT:
386  case Type::DOUBLE:
387  return true;
388  default:
389  break;
390  }
391  return false;
392 }
393 
394 static inline bool is_primitive(Type::type type_id) {
395  switch (type_id) {
396  case Type::NA:
397  case Type::BOOL:
398  case Type::UINT8:
399  case Type::INT8:
400  case Type::UINT16:
401  case Type::INT16:
402  case Type::UINT32:
403  case Type::INT32:
404  case Type::UINT64:
405  case Type::INT64:
406  case Type::HALF_FLOAT:
407  case Type::FLOAT:
408  case Type::DOUBLE:
409  case Type::DATE32:
410  case Type::DATE64:
411  case Type::TIME32:
412  case Type::TIME64:
413  case Type::TIMESTAMP:
414  case Type::INTERVAL:
415  return true;
416  default:
417  break;
418  }
419  return false;
420 }
421 
422 static inline bool is_binary_like(Type::type type_id) {
423  switch (type_id) {
424  case Type::BINARY:
425  case Type::STRING:
426  return true;
427  default:
428  break;
429  }
430  return false;
431 }
432 
433 static inline bool is_dictionary(Type::type type_id) {
434  switch (type_id) {
435  case Type::DICTIONARY:
436  return true;
437  default:
438  break;
439  }
440 
441  return false;
442 }
443 
444 } // namespace arrow
445 
446 #endif // ARROW_TYPE_TRAITS_H
Definition: type.h:485
Definition: array.h:536
Definition: array.h:339
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:212
DoubleTensor TensorType
Definition: type_traits.h:223
NumericArray< Time32Type > Time32Array
Definition: type_fwd.h:116
Builder class for UTF8 strings.
Definition: builder.h:707
NumericBuilder< FloatType > FloatBuilder
Definition: builder.h:311
A NULL type having no physical storage.
Definition: type.h:47
NumericBuilder< Time32Type > Time32Builder
Definition: builder.h:305
Definition: type.h:387
Builder class for variable-length binary data.
Definition: builder.h:665
NumericBuilder< Int64Type > Int64Builder
Definition: builder.h:303
UInt32Tensor TensorType
Definition: type_traits.h:88
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:264
std::shared_ptr< DataType > date64()
Definition: type.h:339
Definition: array.h:563
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:199
type
Definition: type.h:45
Unsigned 16-bit little-endian integer.
Definition: type.h:59
UInt8Array ArrayType
Definition: type_traits.h:40
Definition: type.h:401
Unsigned 32-bit little-endian integer.
Definition: type.h:65
std::shared_ptr< DataType > int16()
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:103
std::shared_ptr< DataType > uint8()
std::shared_ptr< DataType > binary()
Unsigned 8-bit little-endian integer.
Definition: type.h:53
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:165
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:187
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:141
UInt32Array ArrayType
Definition: type_traits.h:86
Int16Array ArrayType
Definition: type_traits.h:73
Definition: array.h:367
Signed 8-bit little-endian integer.
Definition: type.h:56
NumericArray< Date64Type > Date64Array
Definition: type_fwd.h:108
Date as int64_t milliseconds since UNIX epoch.
Definition: type.h:580
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:90
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:256
Definition: type_traits.h:348
Definition: type_traits.h:360
std::shared_ptr< DataType > uint16()
std::shared_ptr< DataType > int32()
NumericBuilder< Int16Type > Int16Builder
Definition: builder.h:301
HalfFloatTensor TensorType
Definition: type_traits.h:197
NumericBuilder< UInt16Type > UInt16Builder
Definition: builder.h:296
UInt64Tensor TensorType
Definition: type_traits.h:114
#define GET_ATTR(ATTR_NAME, DEFAULT)
Definition: type_traits.h:309
Dictionary aka Category type.
Definition: type.h:129
Time as signed 64-bit integer, representing either microseconds or nanoseconds since midnight...
Definition: type.h:110
NumericBuilder< Int8Type > Int8Builder
Definition: builder.h:300
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:94
std::shared_ptr< DataType > uint32()
NumericBuilder< Int32Type > Int32Builder
Definition: builder.h:302
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:55
Signed 32-bit little-endian integer.
Definition: type.h:68
UInt64Array ArrayType
Definition: type_traits.h:112
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:248
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:120
NumericBuilder< UInt32Type > UInt32Builder
Definition: builder.h:297
Base class for all Builders that emit an Array of a scalar numerical type.
Definition: builder.h:254
Time as signed 32-bit integer, representing either seconds or milliseconds since midnight.
Definition: type.h:106
Int32Tensor TensorType
Definition: type_traits.h:101
Definition: builder.h:756
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:153
Int32Array ArrayType
Definition: type_traits.h:99
int32_t days since the UNIX epoch
Definition: type.h:95
Definition: builder.h:515
Definition: type_traits.h:29
FloatTensor TensorType
Definition: type_traits.h:210
std::shared_ptr< DataType > uint64()
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:244
4-byte floating point value
Definition: type.h:80
Signed 16-bit little-endian integer.
Definition: type.h:62
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:157
NumericBuilder< UInt8Type > UInt8Builder
Definition: builder.h:295
std::shared_ptr< DataType > int8()
Definition: type_traits.h:354
Int16Tensor TensorType
Definition: type_traits.h:75
DoubleArray ArrayType
Definition: type_traits.h:221
Int64Array ArrayType
Definition: type_traits.h:125
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:129
Definition: type.h:351
Definition: type.h:657
std::shared_ptr< DataType > float32()
UInt16Array ArrayType
Definition: type_traits.h:60
NumericBuilder< HalfFloatType > HalfFloatBuilder
Definition: builder.h:310
Boolean as 1 bit, LSB bit-packed ordering.
Definition: type.h:50
Signed 64-bit little-endian integer.
Definition: type.h:74
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:64
Definition: type.h:363
Variable-length bytes (no guarantee of UTF8-ness)
Definition: type.h:89
Definition: type.h:312
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:77
NumericBuilder< Time64Type > Time64Builder
Definition: builder.h:306
Definition: type.h:433
FloatArray ArrayType
Definition: type_traits.h:208
Definition: type.h:326
Definition: type.h:408
Exact timestamp encoded with int64 since UNIX epoch Default unit millisecond.
Definition: type.h:102
Definition: type.h:474
std::shared_ptr< DataType > date32()
std::shared_ptr< DataType > int64()
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:133
Definition: builder.h:720
Definition: type.h:501
Int8Array ArrayType
Definition: type_traits.h:50
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:81
Definition: array.h:513
NumericBuilder< Date32Type > Date32Builder
Definition: builder.h:307
UInt16Tensor TensorType
Definition: type_traits.h:62
Top-level namespace for Apache Arrow C++ API.
Definition: allocator.h:29
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:53
std::shared_ptr< DataType > boolean()
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:203
NumericArray< Time64Type > Time64Array
Definition: type_fwd.h:120
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:176
Definition: array.h:389
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:68
Definition: type.h:394
Definition: type.h:357
Append, Resize and Reserve methods are acting on StructBuilder.
Definition: builder.h:776
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:225
8-byte floating point value
Definition: type.h:83
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:43
HalfFloatArray ArrayType
Definition: type_traits.h:195
Definition: type.h:369
NumericArray< TimestampType > TimestampArray
Definition: type_fwd.h:124
Date as int32_t days since UNIX epoch.
Definition: type.h:563
std::shared_ptr< DataType > float64()
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:45
Definition: array.h:446
UInt8Tensor TensorType
Definition: type_traits.h:42
int64_t milliseconds since the UNIX epoch
Definition: type.h:98
Definition: type_traits.h:341
Definition: array.h:601
NumericBuilder< DoubleType > DoubleBuilder
Definition: builder.h:312
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:216
NumericBuilder< Date64Type > Date64Builder
Definition: builder.h:308
std::shared_ptr< DataType > utf8()
UTF8 variable-length string as List<Char>
Definition: type.h:86
NumericBuilder< UInt64Type > UInt64Builder
Definition: builder.h:298
Unsigned 64-bit little-endian integer.
Definition: type.h:71
Definition: array.h:659
std::shared_ptr< DataType > float16()
Definition: type.h:709
Definition: type.h:642
NumericArray< Date32Type > Date32Array
Definition: type_fwd.h:112
Definition: type.h:375
Int64Tensor TensorType
Definition: type_traits.h:127
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:145
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:107
static int64_t bytes_required(int64_t elements)
Definition: type_traits.h:116
Definition: array.h:578
Definition: type.h:345
#define PRIMITIVE_TRAITS(T)
Definition: type_traits.h:327
2-byte floating point value
Definition: type.h:77
Definition: type.h:627
Definition: builder.h:171
Definition: type.h:522
YEAR_MONTH or DAY_TIME interval in SQL style.
Definition: type.h:113
Definition: type.h:381
Definition: type_traits.h:334
Builder class for variable-length list array value types.
Definition: builder.h:622
Int8Tensor TensorType
Definition: type_traits.h:52
NumericBuilder< TimestampType > TimestampBuilder
Definition: builder.h:304
Definition: type.h:451
Degenerate null type Array.
Definition: array.h:293
static std::shared_ptr< DataType > type_singleton()
Definition: type_traits.h:229