-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathtable.cpp
More file actions
331 lines (274 loc) · 11 KB
/
table.cpp
File metadata and controls
331 lines (274 loc) · 11 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
// 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 "./arrow_types.h"
#include <arrow/array/array_base.h>
#include <arrow/table.h>
#include <arrow/util/byte_size.h>
#include <arrow/util/key_value_metadata.h>
// [[arrow::export]]
int Table__num_columns(const std::shared_ptr<arrow::Table>& x) {
return x->num_columns();
}
// [[arrow::export]]
r_vec_size Table__num_rows(const std::shared_ptr<arrow::Table>& x) {
return r_vec_size(x->num_rows());
}
// [[arrow::export]]
std::shared_ptr<arrow::Schema> Table__schema(const std::shared_ptr<arrow::Table>& x) {
return x->schema();
}
// [[arrow::export]]
std::shared_ptr<arrow::Table> Table__ReplaceSchemaMetadata(
const std::shared_ptr<arrow::Table>& x, cpp11::strings metadata) {
auto vec_metadata = cpp11::as_cpp<std::vector<std::string>>(metadata);
auto names_metadata = cpp11::as_cpp<std::vector<std::string>>(metadata.names());
auto kv = std::shared_ptr<arrow::KeyValueMetadata>(
new arrow::KeyValueMetadata(names_metadata, vec_metadata));
return x->ReplaceSchemaMetadata(kv);
}
// [[arrow::export]]
std::shared_ptr<arrow::ChunkedArray> Table__column(
const std::shared_ptr<arrow::Table>& table, int i) {
arrow::r::validate_index(i, table->num_columns());
return table->column(i);
}
// [[arrow::export]]
std::shared_ptr<arrow::Field> Table__field(const std::shared_ptr<arrow::Table>& table,
int i) {
arrow::r::validate_index(i, table->num_columns());
return table->field(i);
}
// [[arrow::export]]
cpp11::list Table__columns(const std::shared_ptr<arrow::Table>& table) {
auto nc = table->num_columns();
std::vector<std::shared_ptr<arrow::ChunkedArray>> res(nc);
for (int i = 0; i < nc; i++) {
res[i] = table->column(i);
}
return arrow::r::to_r_list(res);
}
// [[arrow::export]]
std::vector<std::string> Table__ColumnNames(const std::shared_ptr<arrow::Table>& table) {
return table->ColumnNames();
}
// [[arrow::export]]
std::shared_ptr<arrow::Table> Table__RenameColumns(
const std::shared_ptr<arrow::Table>& table, const std::vector<std::string>& names) {
return ValueOrStop(table->RenameColumns(names));
}
// [[arrow::export]]
std::shared_ptr<arrow::Table> Table__Slice1(const std::shared_ptr<arrow::Table>& table,
R_xlen_t offset) {
arrow::r::validate_slice_offset(offset, table->num_rows());
return table->Slice(offset);
}
// [[arrow::export]]
std::shared_ptr<arrow::Table> Table__Slice2(const std::shared_ptr<arrow::Table>& table,
R_xlen_t offset, R_xlen_t length) {
arrow::r::validate_slice_offset(offset, table->num_rows());
arrow::r::validate_slice_length(length, table->num_rows() - offset);
return table->Slice(offset, length);
}
// [[arrow::export]]
bool Table__Equals(const std::shared_ptr<arrow::Table>& lhs,
const std::shared_ptr<arrow::Table>& rhs, bool check_metadata) {
return lhs->Equals(*rhs.get(), check_metadata);
}
// [[arrow::export]]
bool Table__Validate(const std::shared_ptr<arrow::Table>& table) {
StopIfNotOk(table->Validate());
return true;
}
// [[arrow::export]]
bool Table__ValidateFull(const std::shared_ptr<arrow::Table>& table) {
StopIfNotOk(table->ValidateFull());
return true;
}
// [[arrow::export]]
std::shared_ptr<arrow::ChunkedArray> Table__GetColumnByName(
const std::shared_ptr<arrow::Table>& table, const std::string& name) {
return table->GetColumnByName(name);
}
// [[arrow::export]]
std::shared_ptr<arrow::Table> Table__RemoveColumn(
const std::shared_ptr<arrow::Table>& table, int i) {
return ValueOrStop(table->RemoveColumn(i));
}
// [[arrow::export]]
std::shared_ptr<arrow::Table> Table__AddColumn(
const std::shared_ptr<arrow::Table>& table, int i,
const std::shared_ptr<arrow::Field>& field,
const std::shared_ptr<arrow::ChunkedArray>& column) {
return ValueOrStop(table->AddColumn(i, field, column));
}
// [[arrow::export]]
std::shared_ptr<arrow::Table> Table__SetColumn(
const std::shared_ptr<arrow::Table>& table, int i,
const std::shared_ptr<arrow::Field>& field,
const std::shared_ptr<arrow::ChunkedArray>& column) {
return ValueOrStop(table->SetColumn(i, field, column));
}
// [[arrow::export]]
std::shared_ptr<arrow::Table> Table__SelectColumns(
const std::shared_ptr<arrow::Table>& table, const std::vector<int>& indices) {
return ValueOrStop(table->SelectColumns(indices));
}
namespace arrow {
namespace r {
arrow::Status InferSchemaFromDots(SEXP lst, SEXP schema_sxp, int num_fields,
std::shared_ptr<arrow::Schema>& schema) {
// maybe a schema was given
if (Rf_inherits(schema_sxp, "Schema")) {
schema = cpp11::as_cpp<std::shared_ptr<arrow::Schema>>(schema_sxp);
return arrow::Status::OK();
}
if (!Rf_isNull(schema_sxp)) {
return arrow::Status::RError("`schema` must be an arrow::Schema or NULL");
}
// infer the schema from the `...`
std::vector<std::shared_ptr<arrow::Field>> fields(num_fields);
auto extract_one_field = [&fields](int j, SEXP x, std::string name) {
if (Rf_inherits(x, "ChunkedArray")) {
fields[j] = arrow::field(
name, cpp11::as_cpp<std::shared_ptr<arrow::ChunkedArray>>(x)->type());
} else if (Rf_inherits(x, "Array")) {
fields[j] =
arrow::field(name, cpp11::as_cpp<std::shared_ptr<arrow::Array>>(x)->type());
} else {
// TODO: we just need the type at this point
fields[j] = arrow::field(name, arrow::r::InferArrowType(x));
}
};
arrow::r::TraverseDots(lst, num_fields, extract_one_field);
schema = std::make_shared<arrow::Schema>(std::move(fields));
return arrow::Status::OK();
}
SEXP arrow_attributes(SEXP x, bool only_top_level) {
SEXP call = PROTECT(
Rf_lang3(arrow::r::symbols::arrow_attributes, x, Rf_ScalarLogical(only_top_level)));
SEXP att = Rf_eval(call, arrow::r::ns::arrow);
UNPROTECT(1);
return att;
}
cpp11::writable::list CollectColumnMetadata(SEXP lst, int num_fields) {
// Preallocate for the lambda to fill in
cpp11::writable::list metadata_columns(num_fields);
cpp11::writable::strings metadata_columns_names(num_fields);
auto extract_one_metadata = [&metadata_columns, &metadata_columns_names](
int j, SEXP x, std::string name) {
metadata_columns_names[j] = name;
// no metadata for arrow R6 objects
if (Rf_inherits(x, "ArrowObject")) {
return;
}
metadata_columns[j] = arrow_attributes(x, false);
};
arrow::r::TraverseDots(lst, num_fields, extract_one_metadata);
metadata_columns.names() = metadata_columns_names;
return metadata_columns;
}
arrow::Status AddMetadataFromDots(SEXP lst, int num_fields,
std::shared_ptr<arrow::Schema>& schema) {
// Preallocate the r_metadata object: list(attributes=list(), columns=namedList(fields))
cpp11::writable::list metadata(2);
metadata.names() = arrow::r::data::names_metadata;
bool has_top_level_metadata = false;
// "top level" attributes, only relevant if the first object is not named and a data
// frame
cpp11::strings names = Rf_getAttrib(lst, R_NamesSymbol);
if (names[0] == "" && Rf_inherits(VECTOR_ELT(lst, 0), "data.frame") &&
Rf_xlength(lst) == 1) {
SEXP top_level = metadata[0] = arrow_attributes(VECTOR_ELT(lst, 0), true);
if (!Rf_isNull(top_level) && XLENGTH(top_level) > 0) {
has_top_level_metadata = true;
}
}
// recurse to get all columns metadata
cpp11::writable::list metadata_columns = CollectColumnMetadata(lst, num_fields);
// Remove metadata for ExtensionType columns, because these have their own mechanism for
// preserving R type information
for (int i = 0; i < schema->num_fields(); i++) {
if (schema->field(i)->type()->id() == Type::EXTENSION) {
metadata_columns[i] = R_NilValue;
}
}
// If all metadata_columns are NULL and there is no top-level metadata, set has_metadata
// to false
bool has_metadata = has_top_level_metadata;
for (R_xlen_t i = 0; i < metadata_columns.size(); i++) {
if (metadata_columns[i] != R_NilValue) {
has_metadata = true;
break;
}
}
// Assign to the output metadata
metadata[1] = metadata_columns;
if (has_metadata) {
SEXP serialise_call =
PROTECT(Rf_lang2(arrow::r::symbols::serialize_arrow_r_metadata, metadata));
SEXP serialised = PROTECT(Rf_eval(serialise_call, arrow::r::ns::arrow));
schema = schema->WithMetadata(
arrow::key_value_metadata({"r"}, {CHAR(STRING_ELT(serialised, 0))}));
UNPROTECT(2);
}
return arrow::Status::OK();
}
} // namespace r
} // namespace arrow
// [[arrow::export]]
bool all_record_batches(SEXP lst) {
R_xlen_t n = XLENGTH(lst);
for (R_xlen_t i = 0; i < n; i++) {
if (!Rf_inherits(VECTOR_ELT(lst, i), "RecordBatch")) return false;
}
return true;
}
// [[arrow::export]]
std::shared_ptr<arrow::Table> Table__from_record_batches(
const std::vector<std::shared_ptr<arrow::RecordBatch>>& batches, SEXP schema_sxp) {
bool infer_schema = !Rf_inherits(schema_sxp, "Schema");
std::shared_ptr<arrow::Table> tab;
if (infer_schema) {
tab = ValueOrStop(arrow::Table::FromRecordBatches(std::move(batches)));
} else {
auto schema = cpp11::as_cpp<std::shared_ptr<arrow::Schema>>(schema_sxp);
tab = ValueOrStop(arrow::Table::FromRecordBatches(schema, std::move(batches)));
}
return tab;
}
// [[arrow::export]]
std::shared_ptr<arrow::Table> Table__from_schema(
const std::shared_ptr<arrow::Schema>& schema) {
int64_t num_fields = schema->num_fields();
std::vector<std::shared_ptr<arrow::ChunkedArray>> columns(num_fields);
for (int i = 0; i < num_fields; i++) {
auto maybe_column = arrow::ChunkedArray::Make({}, schema->field(i)->type());
columns[i] = ValueOrStop(maybe_column);
}
return (arrow::Table::Make(schema, std::move(columns)));
}
// [[arrow::export]]
r_vec_size Table__ReferencedBufferSize(const std::shared_ptr<arrow::Table>& table) {
return r_vec_size(ValueOrStop(arrow::util::ReferencedBufferSize(*table)));
}
// [[arrow::export]]
std::shared_ptr<arrow::Table> Table__ConcatenateTables(
const std::vector<std::shared_ptr<arrow::Table>>& tables, bool unify_schemas) {
arrow::ConcatenateTablesOptions options;
options.unify_schemas = unify_schemas;
return ValueOrStop(arrow::ConcatenateTables(tables, options));
}