This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
carrow.cc
662 lines (545 loc) · 15.6 KB
/
carrow.cc
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#include <arrow/api.h>
#include <arrow/io/api.h>
#include <arrow/ipc/api.h>
#include <plasma/client.h>
#include <iostream>
#include <sstream>
#include <vector>
#include "carrow.h"
#ifdef __cplusplus
extern "C" {
#endif
const int BOOL_DTYPE = arrow::Type::BOOL;
const int FLOAT64_DTYPE = arrow::Type::DOUBLE;
const int INTEGER64_DTYPE = arrow::Type::INT64;
const int STRING_DTYPE = arrow::Type::STRING;
const int TIMESTAMP_DTYPE = arrow::Type::TIMESTAMP;
/*
static void debug_mark(std::string msg = "HERE") {
std::cout << "\033[1;31m";
std::cout << "<< " << msg << " >>\n";
std::cout << "\033[0m";
std::cout.flush();
}
*/
#define CARROW_RETURN_IF_ERROR(status) \
do { \
if (!status.ok()) { \
return result_t{status.message().c_str(), nullptr}; \
} \
} while (false)
std::shared_ptr<arrow::DataType> data_type(int dtype) {
switch (dtype) {
case BOOL_DTYPE:
return arrow::boolean();
case FLOAT64_DTYPE:
return arrow::float64();
case INTEGER64_DTYPE:
return arrow::int64();
case STRING_DTYPE:
return arrow::utf8();
case TIMESTAMP_DTYPE:
return arrow::timestamp(arrow::TimeUnit::NANO);
}
return nullptr;
}
/* TODO: Do it with template (currently not possible under extern "C")
so we can unite with Array
e.g.
template <class T>
struct Shared<T> {
std::shared_ptr<T> ptr;
};
*/
struct Metadata {
std::shared_ptr<arrow::KeyValueMetadata> ptr;
};
struct Schema {
std::shared_ptr<arrow::Schema> ptr;
};
struct Array {
std::shared_ptr<arrow::Array> ptr;
};
struct Table {
std::shared_ptr<arrow::Table> ptr;
};
void *field_new(char *name, int dtype) {
auto dt = data_type(dtype);
return new arrow::Field(name, dt);
}
const char *field_name(void *vp) {
auto field = (arrow::Field *)vp;
return field->name().c_str();
}
int field_dtype(void *vp) {
auto field = (arrow::Field *)vp;
return field->type()->id();
}
void field_free(void *vp) {
if (vp == nullptr) {
return;
}
auto field = (arrow::Field *)vp;
delete field;
}
void *schema_new(void *vp, size_t count) {
auto fields = (arrow::Field **)vp;
auto vec = std::vector<std::shared_ptr<arrow::Field>>();
for (size_t i = 0; i < count; i++) {
vec.push_back(std::shared_ptr<arrow::Field>(fields[i]));
}
auto schema = new Schema;
schema->ptr = std::make_shared<arrow::Schema>(vec);
return schema;
}
result_t schema_set_meta(void *vp, void *mp) {
result_t res = {nullptr, nullptr};
auto schema = (Schema *)vp;
if (schema == nullptr) {
res.err = strdup("null schema");
return res;
}
auto meta = (Metadata *)mp;
if (meta == nullptr) {
res.err = strdup("null meta");
return res;
}
schema->ptr = schema->ptr->WithMetadata(meta->ptr);
return res;
}
result_t schema_meta(void *vp) {
result_t res = {nullptr, nullptr};
auto schema = (Schema *)vp;
if (schema == nullptr) {
res.err = strdup("null schema");
return res;
}
auto meta = new Metadata;
meta->ptr = schema->ptr->metadata()->Copy();
res.ptr = meta;
return res;
}
void schema_free(void *vp) {
if (vp == nullptr) {
return;
}
auto schema = (Schema *)vp;
delete schema;
}
result_t array_builder_new(int dtype) {
result_t res = {nullptr, nullptr};
switch (dtype) {
case BOOL_DTYPE:
res.ptr = new arrow::BooleanBuilder();
break;
case FLOAT64_DTYPE:
res.ptr = new arrow::DoubleBuilder();
break;
case INTEGER64_DTYPE:
res.ptr = new arrow::Int64Builder();
break;
case STRING_DTYPE:
res.ptr = new arrow::StringBuilder();
break;
case TIMESTAMP_DTYPE:
res.ptr = new arrow::TimestampBuilder(data_type(TIMESTAMP_DTYPE), nullptr);
break;
default:
std::ostringstream oss;
oss << "unknown dtype: " << dtype;
res.err = oss.str().c_str();
}
return res;
}
// TODO: Check for nulls in all append
result_t array_builder_append_bool(void *vp, uint8_t value) {
auto builder = (arrow::BooleanBuilder *)vp;
auto status = builder->Append(value);
CARROW_RETURN_IF_ERROR(status);
return result_t{nullptr, nullptr};
}
result_t array_builder_append_bools(void *vp, uint8_t *values, int64_t length) {
auto builder = (arrow::BooleanBuilder *)vp;
auto status = builder->AppendValues(values, length);
CARROW_RETURN_IF_ERROR(status);
return result_t{nullptr, nullptr};
}
result_t array_builder_append_float(void *vp, double value) {
auto builder = (arrow::DoubleBuilder *)vp;
auto status = builder->Append(value);
CARROW_RETURN_IF_ERROR(status);
return result_t{nullptr, nullptr};
}
result_t array_builder_append_floats(void *vp, double *values, int64_t length) {
auto builder = (arrow::DoubleBuilder *)vp;
auto status = builder->AppendValues(values, length);
CARROW_RETURN_IF_ERROR(status);
return result_t{nullptr, nullptr};
}
result_t array_builder_append_int(void *vp, int64_t value) {
auto builder = (arrow::Int64Builder *)vp;
auto status = builder->Append(value);
CARROW_RETURN_IF_ERROR(status);
return result_t{nullptr, nullptr};
}
result_t array_builder_append_ints(void *vp, int64_t *values, int64_t length) {
auto builder = (arrow::Int64Builder *)vp;
auto status = builder->AppendValues(values, length);
CARROW_RETURN_IF_ERROR(status);
return result_t{nullptr, nullptr};
}
result_t array_builder_append_string(void *vp, char *cp, size_t length) {
auto builder = (arrow::StringBuilder *)vp;
auto status = builder->Append(cp, length);
CARROW_RETURN_IF_ERROR(status);
return result_t{nullptr, nullptr};
}
result_t array_builder_append_strings(void *vp, char **cp, int64_t length) {
auto builder = (arrow::StringBuilder *)vp;
auto status = builder->AppendValues((const char **)cp, length);
CARROW_RETURN_IF_ERROR(status);
return result_t{nullptr, nullptr};
}
result_t array_builder_append_timestamp(void *vp, long value) {
auto builder = (arrow::TimestampBuilder *)vp;
auto status = builder->Append(value);
CARROW_RETURN_IF_ERROR(status);
return result_t{nullptr, nullptr};
}
result_t array_builder_append_timestamps(void *vp, long *values,
int64_t length) {
auto builder = (arrow::TimestampBuilder *)vp;
auto status = builder->AppendValues(values, length);
CARROW_RETURN_IF_ERROR(status);
return result_t{nullptr, nullptr};
}
result_t array_builder_finish(void *vp) {
auto builder = (arrow::ArrayBuilder *)vp;
std::shared_ptr<arrow::Array> array;
auto status = builder->Finish(&array);
CARROW_RETURN_IF_ERROR(status);
delete builder;
auto wrapper = new Array;
wrapper->ptr = array;
return result_t{nullptr, wrapper};
}
int array_dtype(void *vp) {
if (vp == nullptr) {
return -1;
}
auto wrapper = (Array *)vp;
return wrapper->ptr->type_id();
}
int64_t array_length(void *vp) {
if (vp == nullptr) {
return -1;
}
auto wrapper = (Array *)vp;
return wrapper->ptr->length();
}
int array_bool_at(void *vp, long long i) {
auto wrapper = (Array *)vp;
if (wrapper == nullptr) {
return -1;
}
if (wrapper->ptr->type_id() != BOOL_DTYPE) {
return -1;
}
auto arr = (arrow::BooleanArray *)(wrapper->ptr.get());
return arr->Value(i) ? 1 : 0;
}
double array_float_at(void *vp, long long i) {
auto wrapper = (Array *)vp;
if (wrapper == nullptr) {
return -1;
}
if (wrapper->ptr->type_id() != FLOAT64_DTYPE) {
return -1;
}
auto arr = (arrow::DoubleArray *)(wrapper->ptr.get());
return arr->Value(i);
}
int64_t array_int_at(void *vp, long long i) {
auto wrapper = (Array *)vp;
if (wrapper == nullptr) {
return -1;
}
if (wrapper->ptr->type_id() != INTEGER64_DTYPE) {
return -1;
}
auto arr = (arrow::Int64Array *)(wrapper->ptr.get());
return arr->Value(i);
}
const char *array_str_at(void *vp, long long i) {
auto wrapper = (Array *)vp;
if (wrapper == nullptr) {
return nullptr;
}
if (wrapper->ptr->type_id() != STRING_DTYPE) {
return nullptr;
}
auto arr = (arrow::StringArray *)(wrapper->ptr.get());
auto str = arr->GetString(i);
return strdup(str.c_str());
}
int64_t array_timestamp_at(void *vp, long long i) {
auto wrapper = (Array *)vp;
if (wrapper == nullptr) {
return -1;
}
if (wrapper->ptr->type_id() != TIMESTAMP_DTYPE) {
return -1;
}
auto arr = (arrow::TimestampArray *)(wrapper->ptr.get());
return arr->Value(i);
}
void array_free(void *vp) {
if (vp == nullptr) {
return;
}
delete (Array *)vp;
}
void *table_new(void *sp, void *ap, size_t ncols) {
auto schema = (Schema *)sp;
auto arrays = (Array**)ap;
auto vec = std::vector<std::shared_ptr<arrow::Array>>();
for (size_t i = 0; i < ncols; i++) {
vec.push_back(arrays[i]->ptr);
}
auto table = arrow::Table::Make(schema->ptr, vec);
if (table == nullptr) {
return nullptr;
}
auto wrapper = new Table;
wrapper->ptr = table;
return wrapper;
}
long long table_num_cols(void *vp) {
auto wrapper = (Table *)vp;
return wrapper->ptr->num_columns();
}
long long table_num_rows(void *vp) {
auto wrapper = (Table *)vp;
return wrapper->ptr->num_rows();
}
void *table_schema(void *vp) {
auto wrapper = (Table *)vp;
auto ptr = wrapper->ptr->schema();
if (ptr == NULL) {
return NULL;
}
auto schema = new Schema;
schema->ptr = ptr;
return schema;
}
void *table_column(void *vp, int i) {
auto wrapper = (Table *)vp;
auto arr = wrapper->ptr->column(i);
if (arr == NULL) {
return NULL;
}
auto array = new Array;
array->ptr = arr->chunk(0); // FIXME: Rethink Array
return array;
}
void *table_field(void *vp, int i) {
auto wrapper = (Table *)vp;
auto field = wrapper->ptr->field(i);
if (field == NULL) {
return NULL;
}
return field.get();
}
void table_free(void *vp) {
if (vp == nullptr) {
return;
}
delete (Table *)vp;
}
void *table_slice(void *vp, int64_t offset, int64_t length) {
auto wrapper = (Table *)vp;
auto ptr = wrapper->ptr->Slice(offset, length);
auto table = new Table;
table->ptr = ptr;
return table;
}
void *meta_new() {
auto meta = new Metadata;
meta->ptr = std::make_shared<arrow::KeyValueMetadata>();
return meta;
}
result_t meta_set(void *vp, const char *key, const char *value) {
result_t res = {nullptr, nullptr};
auto meta = (Metadata *)vp;
if (meta == nullptr) {
res.err = strdup("null pointer");
return res;
}
meta->ptr->Append(key, value);
return res;
}
result_t meta_size(void *vp) {
result_t res = {nullptr, nullptr};
auto meta = (Metadata *)vp;
if (meta == nullptr) {
res.err = strdup("null pointer");
return res;
}
res.i = meta->ptr->size();
return res;
}
result_t meta_key(void *vp, int64_t i) {
result_t res = {nullptr, nullptr};
auto meta = (Metadata *)vp;
if (meta == nullptr) {
res.err = strdup("null pointer");
return res;
}
res.ptr = strdup(meta->ptr->key(i).c_str());
return res;
}
result_t meta_value(void *vp, int64_t i) {
result_t res = {nullptr, nullptr};
auto meta = (Metadata *)vp;
if (meta == nullptr) {
res.err = strdup("null pointer");
return res;
}
res.ptr = strdup(meta->ptr->value(i).c_str());
return res;
}
result_t plasma_connect(char *path) {
plasma::PlasmaClient *client = new plasma::PlasmaClient();
auto status = client->Connect(path, "", 0);
if (!status.ok()) {
client->Disconnect();
delete client;
}
CARROW_RETURN_IF_ERROR(status);
return result_t{nullptr, client};
}
arrow::Status
write_table(std::shared_ptr<arrow::Table> table,
std::shared_ptr<arrow::ipc::RecordBatchWriter> writer) {
arrow::TableBatchReader rdr(*table);
while (true) {
std::shared_ptr<arrow::RecordBatch> batch;
auto status = rdr.ReadNext(&batch);
if (!status.ok()) {
return status;
}
if (batch == nullptr) {
break;
}
status = writer->WriteRecordBatch(*batch);
if (!status.ok()) {
return status;
}
}
return arrow::Status::OK();
}
result_t table_size(std::shared_ptr<arrow::Table> table) {
arrow::TableBatchReader rdr(*table);
std::shared_ptr<arrow::RecordBatch> batch;
arrow::io::MockOutputStream stream;
std::shared_ptr<arrow::ipc::RecordBatchWriter> writer;
auto status = arrow::ipc::RecordBatchStreamWriter::Open(
&stream, table->schema(), &writer);
CARROW_RETURN_IF_ERROR(status);
status = write_table(table, writer);
CARROW_RETURN_IF_ERROR(status);
status = writer->Close();
CARROW_RETURN_IF_ERROR(status);
auto num_written = stream.GetExtentBytesWritten();
return result_t{nullptr, (void *)num_written};
}
result_t plasma_write(void *cp, void *tp, char *oid) {
if ((cp == nullptr) || (tp == nullptr) || (oid == nullptr)) {
return result_t{"null pointer", nullptr};
}
auto client = (plasma::PlasmaClient *)(cp);
auto wrapper = (Table *)(tp);
auto table = wrapper->ptr;
auto res = table_size(table);
if (res.err != nullptr) {
return res;
}
auto size = int64_t(res.ptr);
plasma::ObjectID id = plasma::ObjectID::from_binary(oid);
std::shared_ptr<arrow::Buffer> buf;
// TODO: Check padding
auto status = client->Create(id, size, nullptr, 0, &buf);
CARROW_RETURN_IF_ERROR(status);
arrow::io::FixedSizeBufferWriter bw(buf);
std::shared_ptr<arrow::ipc::RecordBatchWriter> writer;
status =
arrow::ipc::RecordBatchStreamWriter::Open(&bw, table->schema(), &writer);
CARROW_RETURN_IF_ERROR(status);
status = write_table(table, writer);
CARROW_RETURN_IF_ERROR(status);
status = client->Seal(id);
CARROW_RETURN_IF_ERROR(status);
return result_t{nullptr, (void *)size};
}
result_t plasma_disconnect(void *vp) {
if (vp == nullptr) {
return result_t{nullptr, nullptr};
}
auto client = (plasma::PlasmaClient *)(vp);
auto status = client->Disconnect();
CARROW_RETURN_IF_ERROR(status);
delete client;
return result_t{nullptr, nullptr};
}
// TODO: Do we want allowing multiple IDs? (like the client Get)
result_t plasma_read(void *cp, char *oid, int64_t timeout_ms) {
if ((cp == nullptr) || (oid == nullptr)) {
return result_t{"null pointer", nullptr};
}
auto client = (plasma::PlasmaClient *)(cp);
plasma::ObjectID id = plasma::ObjectID::from_binary(oid);
std::vector<plasma::ObjectID> ids;
ids.push_back(id);
std::vector<plasma::ObjectBuffer> buffers;
auto status = client->Get(ids, timeout_ms, &buffers);
CARROW_RETURN_IF_ERROR(status);
// TODO: Support multiple buffers
if (buffers.size() != 1) {
std::ostringstream oss;
oss << "more than one buffer for " << oid;
return result_t{oss.str().c_str(), nullptr};
}
auto buf_reader = std::make_shared<arrow::io::BufferReader>(buffers[0].data);
std::shared_ptr<arrow::ipc::RecordBatchReader> reader;
status = arrow::ipc::RecordBatchStreamReader::Open(buf_reader, &reader);
CARROW_RETURN_IF_ERROR(status);
std::vector<std::shared_ptr<arrow::RecordBatch>> batches;
while (true) {
std::shared_ptr<arrow::RecordBatch> batch;
status = reader->ReadNext(&batch);
CARROW_RETURN_IF_ERROR(status);
if (batch == nullptr) {
break;
}
batches.push_back(batch);
}
std::shared_ptr<arrow::Table> table;
status = arrow::Table::FromRecordBatches(batches, &table);
CARROW_RETURN_IF_ERROR(status);
auto wrapper = new Table;
wrapper->ptr = table;
return result_t{nullptr, wrapper};
}
result_t plasma_release(void *cp, char *oid) {
if ((cp == nullptr) || (oid == nullptr)) {
return result_t{"null pointer", nullptr};
}
auto client = (plasma::PlasmaClient *)(cp);
plasma::ObjectID id = plasma::ObjectID::from_binary(oid);
auto status = client->Release(id);
CARROW_RETURN_IF_ERROR(status);
return result_t{nullptr, nullptr};
}
#ifdef __cplusplus
} // extern "C"
#endif