forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_traits.h
More file actions
299 lines (248 loc) · 8.04 KB
/
type_traits.h
File metadata and controls
299 lines (248 loc) · 8.04 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
// 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
#include "arrow/python/platform.h"
#include <cstdint>
#include <limits>
#include "arrow/python/numpy_interop.h"
#include <numpy/halffloat.h>
#include "arrow/builder.h"
#include "arrow/type.h"
#include "arrow/util/logging.h"
namespace arrow {
namespace py {
namespace internal {
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_INT64 != NPY_LONGLONG
NPY_INT_DECL(LONGLONG, Int64, int64_t);
NPY_INT_DECL(ULONGLONG, UInt64, uint64_t);
#endif
template <>
struct npy_traits<NPY_FLOAT16> {
typedef npy_half value_type;
using TypeClass = HalfFloatType;
using BuilderClass = HalfFloatBuilder;
static constexpr bool supports_nulls = true;
static inline bool isnull(npy_half v) { return v == NPY_HALF_NAN; }
};
template <>
struct npy_traits<NPY_FLOAT32> {
typedef float value_type;
using TypeClass = FloatType;
using BuilderClass = FloatBuilder;
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 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_OBJECT> {
typedef PyObject* value_type;
static constexpr bool supports_nulls = true;
};
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;
};
#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 = 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 = NPY_HALF_NAN;
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 = 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 = NAN;
typedef typename npy_traits<NPY_FLOAT64>::value_type T;
};
static constexpr int64_t kPandasTimestampNull = std::numeric_limits<int64_t>::min();
constexpr int64_t kNanosecondsInDay = 86400000000000LL;
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::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 int NumPyTypeSize(int npy_type) {
switch (npy_type) {
case NPY_BOOL:
return 1;
case NPY_INT8:
return 1;
case NPY_INT16:
return 2;
case NPY_INT32:
return 4;
case NPY_INT64:
return 8;
#if (NPY_INT64 != NPY_LONGLONG)
case NPY_LONGLONG:
return 8;
#endif
case NPY_UINT8:
return 1;
case NPY_UINT16:
return 2;
case NPY_UINT32:
return 4;
case NPY_UINT64:
return 8;
#if (NPY_UINT64 != NPY_ULONGLONG)
case NPY_ULONGLONG:
return 8;
#endif
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:
DCHECK(false) << "unhandled numpy type";
break;
}
return -1;
}
} // namespace internal
} // namespace py
} // namespace arrow