-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathtype_traits.h
More file actions
353 lines (291 loc) · 10 KB
/
type_traits.h
File metadata and controls
353 lines (291 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// Internal header
#pragma once
#include "arrow/python/platform.h"
#include <cstdint>
#include <limits>
#include "arrow/python/numpy_interop.h"
#include "arrow/type_fwd.h"
#include "arrow/util/float16.h"
#include "arrow/util/logging.h"
namespace arrow {
namespace py {
static constexpr int64_t kPandasTimestampNull = std::numeric_limits<int64_t>::min();
constexpr int64_t kNanosecondsInDay = 86400000000000LL;
namespace internal {
//
// Type traits for Numpy -> Arrow equivalence
//
template <int TYPE>
struct npy_traits {};
template <>
struct npy_traits<NPY_BOOL> {
typedef uint8_t value_type;
using TypeClass = BooleanType;
using BuilderClass = BooleanBuilder;
static constexpr bool supports_nulls = false;
static inline bool isnull(uint8_t v) { return false; }
};
#define NPY_INT_DECL(TYPE, CapType, T) \
template <> \
struct npy_traits<NPY_##TYPE> { \
typedef T value_type; \
using TypeClass = CapType##Type; \
using BuilderClass = CapType##Builder; \
\
static constexpr bool supports_nulls = false; \
static inline bool isnull(T v) { return false; } \
};
NPY_INT_DECL(INT8, Int8, int8_t);
NPY_INT_DECL(INT16, Int16, int16_t);
NPY_INT_DECL(INT32, Int32, int32_t);
NPY_INT_DECL(INT64, Int64, int64_t);
NPY_INT_DECL(UINT8, UInt8, uint8_t);
NPY_INT_DECL(UINT16, UInt16, uint16_t);
NPY_INT_DECL(UINT32, UInt32, uint32_t);
NPY_INT_DECL(UINT64, UInt64, uint64_t);
#if !NPY_INT32_IS_INT && NPY_BITSOF_INT == 32
NPY_INT_DECL(INT, Int32, int32_t);
NPY_INT_DECL(UINT, UInt32, uint32_t);
#endif
#if !NPY_INT64_IS_LONG_LONG && NPY_BITSOF_LONGLONG == 64
NPY_INT_DECL(LONGLONG, Int64, int64_t);
NPY_INT_DECL(ULONGLONG, UInt64, uint64_t);
#endif
template <>
struct npy_traits<NPY_FLOAT16> {
typedef uint16_t value_type;
using TypeClass = HalfFloatType;
using BuilderClass = HalfFloatBuilder;
static constexpr uint16_t na_sentinel =
std::numeric_limits<arrow::util::Float16>::quiet_NaN().bits();
static constexpr bool supports_nulls = true;
static inline bool isnull(uint16_t v) {
return arrow::util::Float16::FromBits(v).is_nan();
}
};
template <>
struct npy_traits<NPY_FLOAT32> {
typedef float value_type;
using TypeClass = FloatType;
using BuilderClass = FloatBuilder;
// We need to use quiet_NaN here instead of the NAN macro as on Windows
// the NAN macro leads to "division-by-zero" compile-time error with clang.
static constexpr float na_sentinel = std::numeric_limits<float>::quiet_NaN();
static constexpr bool supports_nulls = true;
static inline bool isnull(float v) { return v != v; }
};
template <>
struct npy_traits<NPY_FLOAT64> {
typedef double value_type;
using TypeClass = DoubleType;
using BuilderClass = DoubleBuilder;
static constexpr double na_sentinel = std::numeric_limits<double>::quiet_NaN();
static constexpr bool supports_nulls = true;
static inline bool isnull(double v) { return v != v; }
};
template <>
struct npy_traits<NPY_DATETIME> {
typedef int64_t value_type;
using TypeClass = TimestampType;
using BuilderClass = TimestampBuilder;
static constexpr bool supports_nulls = true;
static inline bool isnull(int64_t v) {
// NaT = -2**63
// = -0x8000000000000000
// = -9223372036854775808;
// = std::numeric_limits<int64_t>::min()
return v == std::numeric_limits<int64_t>::min();
}
};
template <>
struct npy_traits<NPY_TIMEDELTA> {
typedef int64_t value_type;
using TypeClass = DurationType;
using BuilderClass = DurationBuilder;
static constexpr bool supports_nulls = true;
static inline bool isnull(int64_t v) {
// NaT = -2**63 = std::numeric_limits<int64_t>::min()
return v == std::numeric_limits<int64_t>::min();
}
};
template <>
struct npy_traits<NPY_OBJECT> {
typedef PyObject* value_type;
static constexpr bool supports_nulls = true;
static inline bool isnull(PyObject* v) { return v == Py_None; }
};
//
// Type traits for Arrow -> Numpy equivalence
// Note *supports_nulls* means the equivalent Numpy type support nulls
//
template <int TYPE>
struct arrow_traits {};
template <>
struct arrow_traits<Type::BOOL> {
static constexpr int npy_type = NPY_BOOL;
static constexpr bool supports_nulls = false;
typedef typename npy_traits<NPY_BOOL>::value_type T;
};
#define INT_DECL(TYPE) \
template <> \
struct arrow_traits<Type::TYPE> { \
static constexpr int npy_type = NPY_##TYPE; \
static constexpr bool supports_nulls = false; \
static constexpr double na_value = std::numeric_limits<double>::quiet_NaN(); \
typedef typename npy_traits<NPY_##TYPE>::value_type T; \
};
INT_DECL(INT8);
INT_DECL(INT16);
INT_DECL(INT32);
INT_DECL(INT64);
INT_DECL(UINT8);
INT_DECL(UINT16);
INT_DECL(UINT32);
INT_DECL(UINT64);
template <>
struct arrow_traits<Type::HALF_FLOAT> {
static constexpr int npy_type = NPY_FLOAT16;
static constexpr bool supports_nulls = true;
static constexpr uint16_t na_value =
std::numeric_limits<arrow::util::Float16>::quiet_NaN().bits();
typedef typename npy_traits<NPY_FLOAT16>::value_type T;
};
template <>
struct arrow_traits<Type::FLOAT> {
static constexpr int npy_type = NPY_FLOAT32;
static constexpr bool supports_nulls = true;
static constexpr float na_value = std::numeric_limits<float>::quiet_NaN();
typedef typename npy_traits<NPY_FLOAT32>::value_type T;
};
template <>
struct arrow_traits<Type::DOUBLE> {
static constexpr int npy_type = NPY_FLOAT64;
static constexpr bool supports_nulls = true;
static constexpr double na_value = std::numeric_limits<double>::quiet_NaN();
typedef typename npy_traits<NPY_FLOAT64>::value_type T;
};
template <>
struct arrow_traits<Type::TIMESTAMP> {
static constexpr int npy_type = NPY_DATETIME;
static constexpr int64_t npy_shift = 1;
static constexpr bool supports_nulls = true;
static constexpr int64_t na_value = kPandasTimestampNull;
typedef typename npy_traits<NPY_DATETIME>::value_type T;
};
template <>
struct arrow_traits<Type::DURATION> {
static constexpr int npy_type = NPY_TIMEDELTA;
static constexpr int64_t npy_shift = 1;
static constexpr bool supports_nulls = true;
static constexpr int64_t na_value = kPandasTimestampNull;
typedef typename npy_traits<NPY_TIMEDELTA>::value_type T;
};
template <>
struct arrow_traits<Type::DATE32> {
// Data stores as FR_D day unit
static constexpr int npy_type = NPY_DATETIME;
static constexpr int64_t npy_shift = 1;
static constexpr bool supports_nulls = true;
typedef typename npy_traits<NPY_DATETIME>::value_type T;
static constexpr int64_t na_value = kPandasTimestampNull;
static inline bool isnull(int64_t v) { return npy_traits<NPY_DATETIME>::isnull(v); }
};
template <>
struct arrow_traits<Type::DATE64> {
// Data stores as FR_D day unit
static constexpr int npy_type = NPY_DATETIME;
// There are 1000 * 60 * 60 * 24 = 86400000ms in a day
static constexpr int64_t npy_shift = 86400000;
static constexpr bool supports_nulls = true;
typedef typename npy_traits<NPY_DATETIME>::value_type T;
static constexpr int64_t na_value = kPandasTimestampNull;
static inline bool isnull(int64_t v) { return npy_traits<NPY_DATETIME>::isnull(v); }
};
template <>
struct arrow_traits<Type::TIME32> {
static constexpr int npy_type = NPY_OBJECT;
static constexpr bool supports_nulls = true;
static constexpr int64_t na_value = kPandasTimestampNull;
typedef typename npy_traits<NPY_DATETIME>::value_type T;
};
template <>
struct arrow_traits<Type::TIME64> {
static constexpr int npy_type = NPY_OBJECT;
static constexpr bool supports_nulls = true;
typedef typename npy_traits<NPY_DATETIME>::value_type T;
};
template <>
struct arrow_traits<Type::STRING> {
static constexpr int npy_type = NPY_OBJECT;
static constexpr bool supports_nulls = true;
};
template <>
struct arrow_traits<Type::BINARY> {
static constexpr int npy_type = NPY_OBJECT;
static constexpr bool supports_nulls = true;
};
static inline NPY_DATETIMEUNIT NumPyFrequency(TimeUnit::type unit) {
switch (unit) {
case TimestampType::Unit::SECOND:
return NPY_FR_s;
case TimestampType::Unit::MILLI:
return NPY_FR_ms;
break;
case TimestampType::Unit::MICRO:
return NPY_FR_us;
default:
// NANO
return NPY_FR_ns;
}
}
static inline int NumPyTypeSize(int npy_type) {
npy_type = fix_numpy_type_num(npy_type);
switch (npy_type) {
case NPY_BOOL:
case NPY_INT8:
case NPY_UINT8:
return 1;
case NPY_INT16:
case NPY_UINT16:
return 2;
case NPY_INT32:
case NPY_UINT32:
return 4;
case NPY_INT64:
case NPY_UINT64:
return 8;
case NPY_FLOAT16:
return 2;
case NPY_FLOAT32:
return 4;
case NPY_FLOAT64:
return 8;
case NPY_DATETIME:
return 8;
case NPY_OBJECT:
return sizeof(void*);
default:
ARROW_CHECK(false) << "unhandled numpy type";
break;
}
return -1;
}
} // namespace internal
} // namespace py
} // namespace arrow