-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnumpy_type.cpp
More file actions
149 lines (140 loc) · 5.06 KB
/
numpy_type.cpp
File metadata and controls
149 lines (140 loc) · 5.06 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
#include "numpy/numpy_type.h"
#include "common/string_utils.h"
namespace lbug {
using namespace lbug::common;
static bool isDateTime(NumpyNullableType type) {
switch (type) {
case NumpyNullableType::DATETIME_US:
case NumpyNullableType::DATETIME_S:
case NumpyNullableType::DATETIME_NS:
case NumpyNullableType::DATETIME_MS:
return true;
default:
return false;
}
}
static NumpyNullableType convertNumpyTypeInternal(const std::string& colTypeStr) {
if (colTypeStr == "bool" || colTypeStr == "boolean") {
return NumpyNullableType::BOOL;
}
if (colTypeStr == "uint8" || colTypeStr == "UInt8") {
return NumpyNullableType::UINT_8;
}
if (colTypeStr == "uint16" || colTypeStr == "UInt16") {
return NumpyNullableType::UINT_16;
}
if (colTypeStr == "uint32" || colTypeStr == "UInt32") {
return NumpyNullableType::UINT_32;
}
if (colTypeStr == "uint64" || colTypeStr == "UInt64") {
return NumpyNullableType::UINT_64;
}
if (colTypeStr == "int8" || colTypeStr == "Int8") {
return NumpyNullableType::INT_8;
}
if (colTypeStr == "int16" || colTypeStr == "Int16") {
return NumpyNullableType::INT_16;
}
if (colTypeStr == "int32" || colTypeStr == "Int32") {
return NumpyNullableType::INT_32;
}
if (colTypeStr == "int64" || colTypeStr == "Int64") {
return NumpyNullableType::INT_64;
}
if (colTypeStr == "float16" || colTypeStr == "Float16") {
return NumpyNullableType::FLOAT_16;
}
if (colTypeStr == "float32" || colTypeStr == "Float32") {
return NumpyNullableType::FLOAT_32;
}
if (colTypeStr == "float64" || colTypeStr == "Float64") {
return NumpyNullableType::FLOAT_64;
}
if (colTypeStr == "timedelta64[ns]") {
return NumpyNullableType::TIMEDELTA;
}
// We use 'StartsWith' because it might have ', tz' at the end, indicating timezone.
if (colTypeStr.starts_with("datetime64[us")) {
return NumpyNullableType::DATETIME_US;
}
if (colTypeStr.starts_with("datetime64[ns")) {
return NumpyNullableType::DATETIME_NS;
}
if (colTypeStr.starts_with("datetime64[ms")) {
return NumpyNullableType::DATETIME_MS;
}
if (colTypeStr.starts_with("datetime64[s")) {
return NumpyNullableType::DATETIME_S;
}
// LCOV_EXCL_START
// Legacy datetime type indicators
if (colTypeStr.starts_with("<M8[us")) {
return NumpyNullableType::DATETIME_US;
}
if (colTypeStr.starts_with("<M8[ns")) {
return NumpyNullableType::DATETIME_NS;
}
// LCOV_EXCL_STOP
if (colTypeStr == "object" || colTypeStr == "string") {
return NumpyNullableType::OBJECT;
}
UNREACHABLE_CODE;
}
NumpyType NumpyTypeUtils::convertNumpyType(const py::handle& colType) {
auto colTypeStr = std::string(py::str(colType));
NumpyType resultNPType;
resultNPType.type = convertNumpyTypeInternal(colTypeStr);
if (isDateTime(resultNPType.type)) {
if (hasattr(colType, "tz")) {
resultNPType.hasTimezone = true;
}
}
return resultNPType;
}
LogicalType NumpyTypeUtils::numpyToLogicalType(const NumpyType& col_type) {
// TODO(Ziyi): timestamp with timezone is currently not supported.
switch (col_type.type) {
case NumpyNullableType::BOOL:
return LogicalType(LogicalTypeID::BOOL);
case NumpyNullableType::INT_8:
return LogicalType(LogicalTypeID::INT8);
case NumpyNullableType::INT_16:
return LogicalType(LogicalTypeID::INT16);
case NumpyNullableType::INT_32:
return LogicalType(LogicalTypeID::INT32);
case NumpyNullableType::INT_64:
return LogicalType(LogicalTypeID::INT64);
case NumpyNullableType::UINT_8:
return LogicalType(LogicalTypeID::UINT8);
case NumpyNullableType::UINT_16:
return LogicalType(LogicalTypeID::UINT16);
case NumpyNullableType::UINT_32:
return LogicalType(LogicalTypeID::UINT32);
case NumpyNullableType::UINT_64:
return LogicalType(LogicalTypeID::UINT64);
case NumpyNullableType::FLOAT_16:
case NumpyNullableType::FLOAT_32:
return LogicalType(LogicalTypeID::FLOAT);
case NumpyNullableType::FLOAT_64:
return LogicalType(LogicalTypeID::DOUBLE);
case NumpyNullableType::TIMEDELTA:
return LogicalType(LogicalTypeID::INTERVAL);
case NumpyNullableType::DATETIME_US:
// because we currently don't support object, only US with timezone is allowed
if (col_type.hasTimezone) {
return LogicalType(LogicalTypeID::TIMESTAMP_TZ);
}
return LogicalType(LogicalTypeID::TIMESTAMP);
case NumpyNullableType::DATETIME_NS:
return LogicalType(LogicalTypeID::TIMESTAMP_NS);
case NumpyNullableType::DATETIME_MS:
return LogicalType(LogicalTypeID::TIMESTAMP_MS);
case NumpyNullableType::DATETIME_S:
return LogicalType(LogicalTypeID::TIMESTAMP_SEC);
case NumpyNullableType::OBJECT:
return LogicalType(LogicalTypeID::STRING);
default:
UNREACHABLE_CODE;
}
}
} // namespace lbug