forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_infer.cpp
234 lines (205 loc) · 7.25 KB
/
type_infer.cpp
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
// 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.
#include <memory>
#include "./arrow_types.h"
#include <arrow/array/array_base.h>
#include <arrow/chunked_array.h>
namespace arrow {
namespace r {
static inline std::shared_ptr<arrow::DataType> IndexTypeForFactors(int n_factors) {
if (n_factors < INT8_MAX) {
return arrow::int8();
} else if (n_factors < INT16_MAX) {
return arrow::int16();
} else {
return arrow::int32();
}
}
std::shared_ptr<arrow::DataType> InferArrowTypeFromFactor(SEXP factor) {
SEXP factors = Rf_getAttrib(factor, R_LevelsSymbol);
auto index_type = IndexTypeForFactors(Rf_length(factors));
bool is_ordered = Rf_inherits(factor, "ordered");
return dictionary(index_type, arrow::utf8(), is_ordered);
}
template <int VectorType>
std::shared_ptr<arrow::DataType> InferArrowTypeFromVector(SEXP x) {
cpp11::stop("Unknown vector type: ", VectorType);
}
template <>
std::shared_ptr<arrow::DataType> InferArrowTypeFromVector<ENVSXP>(SEXP x) {
if (Rf_inherits(x, "Array")) {
return cpp11::as_cpp<std::shared_ptr<arrow::Array>>(x)->type();
}
cpp11::stop("Unrecognized vector instance for type ENVSXP");
}
template <>
std::shared_ptr<arrow::DataType> InferArrowTypeFromVector<LGLSXP>(SEXP x) {
return Rf_inherits(x, "vctrs_unspecified") ? null() : boolean();
}
template <>
std::shared_ptr<arrow::DataType> InferArrowTypeFromVector<INTSXP>(SEXP x) {
if (Rf_isFactor(x)) {
return InferArrowTypeFromFactor(x);
} else if (Rf_inherits(x, "Date")) {
return date32();
} else if (Rf_inherits(x, "POSIXct")) {
auto tzone_sexp = Rf_getAttrib(x, symbols::tzone);
if (Rf_isNull(tzone_sexp)) {
auto systzone_sexp = cpp11::package("base")["Sys.timezone"];
return timestamp(TimeUnit::MICRO, CHAR(STRING_ELT(systzone_sexp(), 0)));
} else {
return timestamp(TimeUnit::MICRO, CHAR(STRING_ELT(tzone_sexp, 0)));
}
}
return int32();
}
template <>
std::shared_ptr<arrow::DataType> InferArrowTypeFromVector<REALSXP>(SEXP x) {
if (Rf_inherits(x, "Date")) {
return date32();
}
if (Rf_inherits(x, "POSIXct")) {
auto tzone_sexp = Rf_getAttrib(x, symbols::tzone);
if (Rf_isNull(tzone_sexp)) {
auto systzone_sexp = cpp11::package("base")["Sys.timezone"];
return timestamp(TimeUnit::MICRO, CHAR(STRING_ELT(systzone_sexp(), 0)));
} else {
return timestamp(TimeUnit::MICRO, CHAR(STRING_ELT(tzone_sexp, 0)));
}
}
if (Rf_inherits(x, "integer64")) {
return int64();
}
if (Rf_inherits(x, "hms")) {
return time32(TimeUnit::SECOND);
}
if (Rf_inherits(x, "difftime")) {
return duration(TimeUnit::SECOND);
}
return float64();
}
template <>
std::shared_ptr<arrow::DataType> InferArrowTypeFromVector<STRSXP>(SEXP x) {
return cpp11::unwind_protect([&] {
R_xlen_t n = XLENGTH(x);
int64_t size = 0;
for (R_xlen_t i = 0; i < n; i++) {
size += arrow::r::unsafe::r_string_size(STRING_ELT(x, i));
if (size > arrow::kBinaryMemoryLimit) {
// Exceeds 2GB capacity of utf8 type, so use large
return large_utf8();
}
}
return utf8();
});
}
static inline std::shared_ptr<arrow::DataType> InferArrowTypeFromDataFrame(
cpp11::list x) {
R_xlen_t n = x.size();
cpp11::strings names(x.attr(R_NamesSymbol));
std::vector<std::shared_ptr<arrow::Field>> fields(n);
for (R_xlen_t i = 0; i < n; i++) {
fields[i] = arrow::field(names[i], InferArrowType(x[i]));
}
return arrow::struct_(std::move(fields));
}
template <>
std::shared_ptr<arrow::DataType> InferArrowTypeFromVector<VECSXP>(SEXP x) {
if (Rf_inherits(x, "data.frame") || Rf_inherits(x, "POSIXlt")) {
return InferArrowTypeFromDataFrame(x);
} else {
// some known special cases
if (Rf_inherits(x, "arrow_fixed_size_binary")) {
SEXP byte_width = Rf_getAttrib(x, symbols::byte_width);
if (Rf_isNull(byte_width) || TYPEOF(byte_width) != INTSXP ||
XLENGTH(byte_width) != 1) {
cpp11::stop("malformed arrow_fixed_size_binary object");
}
return arrow::fixed_size_binary(INTEGER(byte_width)[0]);
}
if (Rf_inherits(x, "arrow_binary")) {
return arrow::binary();
}
if (Rf_inherits(x, "arrow_large_binary")) {
return arrow::large_binary();
}
// Check attr(x, "ptype") for an appropriate R prototype
SEXP ptype = Rf_getAttrib(x, symbols::ptype);
if (!Rf_isNull(ptype)) {
arrow::list(InferArrowType(ptype));
}
// If unspecified, iterate through the vector until we get a non-null result
// special case raw() vectors, since we want list(raw()) to result in
// a binary() array
for (R_xlen_t i = 0; i < XLENGTH(x); i++) {
ptype = VECTOR_ELT(x, i);
if (Rf_isNull(ptype)) {
continue;
}
if (!Rf_isObject(ptype) && TYPEOF(ptype) == RAWSXP) {
return arrow::binary();
} else {
return arrow::list(InferArrowType(ptype));
}
}
return arrow::list(arrow::null());
}
}
std::shared_ptr<arrow::DataType> InferArrowType(SEXP x) {
if (arrow::r::altrep::is_unmaterialized_arrow_altrep(x)) {
return arrow::r::altrep::vec_to_arrow_altrep_bypass(x)->type();
}
// If we handle the conversion in C++ we do so here; otherwise we call
// the type() S3 generic to infer the type of the object. For data.frame,
// this code is sufficiently recursive such that it correctly calls into
// R to infer column types where can_convert_native() is false.
if (can_convert_native(x) || Rf_inherits(x, "data.frame")) {
switch (TYPEOF(x)) {
case ENVSXP:
return InferArrowTypeFromVector<ENVSXP>(x);
case LGLSXP:
return InferArrowTypeFromVector<LGLSXP>(x);
case INTSXP:
return InferArrowTypeFromVector<INTSXP>(x);
case REALSXP:
return InferArrowTypeFromVector<REALSXP>(x);
case RAWSXP:
return uint8();
case STRSXP:
return InferArrowTypeFromVector<STRSXP>(x);
case VECSXP:
return InferArrowTypeFromVector<VECSXP>(x);
case NILSXP:
return null();
default:
cpp11::stop("Cannot infer type from vector");
}
} else {
cpp11::sexp type_result = cpp11::package("arrow")["infer_type"](
x, cpp11::named_arg("from_array_infer_type") = true);
if (!Rf_inherits(type_result, "DataType")) {
cpp11::stop("type() did not return an object of type DataType");
}
return cpp11::as_cpp<std::shared_ptr<arrow::DataType>>(type_result);
}
}
} // namespace r
} // namespace arrow
// [[arrow::export]]
std::shared_ptr<arrow::DataType> Array__infer_type(SEXP x) {
return arrow::r::InferArrowType(x);
}