Debian Codegraph @

lib/arrow.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2017 Brazil
3 Copyright (C) 2019-2025 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "grn.h"cxx . . $ `<file>/lib/grn.h`/
21 #include "grn_arrow.h"cxx . . $ `<file>/lib/grn_arrow.h`/
22 #include "grn_cast.h"cxx . . $ `<file>/lib/grn_cast.h`/
23 #include "grn_column.h"cxx . . $ `<file>/lib/grn_column.h`/
24 #include "grn_ctx.hpp"cxx . . $ `<file>/lib/grn_ctx.hpp`/
25 #include "grn_ctx_impl.h"cxx . . $ `<file>/lib/grn_ctx_impl.h`/
26 #include "grn_db.h"cxx . . $ `<file>/lib/grn_db.h`/
27 #include "grn_output.h"cxx . . $ `<file>/lib/grn_output.h`/
28
29 #ifdef GRN_WITH_APACHE_ARROW
30 # include "grn_arrow.hpp"
31 # include "grn_task_executor.hpp"
32 # include <groonga/arrow.hpp>
33
34 # include <arrow/api.h>
35 # include <arrow/compute/api.h>
36 # include <arrow/io/api.h>
37 # include <arrow/ipc/api.h>
38 # include <arrow/util/base64.h>
39
40 # include <map>
41 # include <sstream>
42
43 namespace grnarrow {
44 grn_rc
45 status_to_rc(const arrow::Status &status)
46 {
47 switch (status.code()) {
48 case arrow::StatusCode::OK:
49 return GRN_SUCCESS;
50 case arrow::StatusCode::OutOfMemory:
51 return GRN_NO_MEMORY_AVAILABLE;
52 case arrow::StatusCode::KeyError:
53 return GRN_INVALID_ARGUMENT; // TODO
54 case arrow::StatusCode::TypeError:
55 return GRN_INVALID_ARGUMENT; // TODO
56 case arrow::StatusCode::Invalid:
57 return GRN_INVALID_ARGUMENT;
58 case arrow::StatusCode::IOError:
59 return GRN_INPUT_OUTPUT_ERROR;
60 case arrow::StatusCode::UnknownError:
61 return GRN_UNKNOWN_ERROR;
62 case arrow::StatusCode::NotImplemented:
63 return GRN_FUNCTION_NOT_IMPLEMENTED;
64 default:
65 return GRN_UNKNOWN_ERROR;
66 }
67 }
68
69 arrow::Status
70 check(grn_ctx *ctx, grn_rc rc, const char *context)
71 {
72 switch (rc) {
73 case GRN_SUCCESS:
74 return arrow::Status::OK();
75 default:
76 // TODO
77 return arrow::Status::UnknownError(context, ": <", rc, ">");
78 }
79 }
80
81 arrow::Status
82 check(grn_ctx *ctx, grn_rc rc, std::string context)
83 {
84 return check(ctx, rc, context.c_str());
85 }
86
87 bool
88 check(grn_ctx *ctx, const arrow::Status &status, const char *context)
89 {
90 if (status.ok()) {
91 return true;
92 } else {
93 auto rc = status_to_rc(status);
94 auto message = status.ToString();
95 ERR(rc, "%s: %s", context, message.c_str());
96 return false;
97 }
98 }
99
100 bool
101 check(grn_ctx *ctx, const arrow::Status &status, const std::string &context)
102 {
103 return check(ctx, status, context.c_str());
104 }
105
106 bool
107 check(grn_ctx *ctx, const arrow::Status &status, std::ostream &output)
108 {
109 return check(ctx, status, static_cast<std::stringstream &>(output).str());
110 }
111
112 class ObjectCache {
113 public:
114 ObjectCache(grn_ctx *ctx) : ctx_(ctx) {}
115
116 ~ObjectCache()
117 {
118 for (auto &it : cached_objects_) {
119 auto object = it.second;
120 if (object) {
121 grn_obj_unref(ctx_, object);
122 }
123 }
124 }
125
126 grn_obj *
127 operator[](grn_id id)
128 {
129 auto it = cached_objects_.find(id);
130 if (it != cached_objects_.end()) {
131 return it->second;
132 } else {
133 auto object = grn_ctx_at(ctx_, id);
134 if (object) {
135 cached_objects_[id] = object;
136 }
137 return object;
138 }
139 }
140
141 private:
142 grn_ctx *ctx_;
143 std::map<grn_id, grn_obj *> cached_objects_;
144 };
145
146 void
147 put_time_value(grn_ctx *ctx,
148 grn_obj *bulk,
149 int64_t time_value,
150 arrow::TimeUnit::type time_unit)
151 {
152 switch (time_unit) {
153 case arrow::TimeUnit::SECOND:
154 GRN_TIME_PUT(ctx, bulk, GRN_TIME_PACK(time_value, 0));
155 break;
156 case arrow::TimeUnit::MILLI:
157 GRN_TIME_PUT(ctx, bulk, GRN_TIME_MSEC_TO_USEC(time_value));
158 break;
159 case arrow::TimeUnit::MICRO:
160 GRN_TIME_PUT(ctx, bulk, time_value);
161 break;
162 case arrow::TimeUnit::NANO:
163 GRN_TIME_PUT(ctx, bulk, GRN_TIME_NSEC_TO_USEC(time_value));
164 break;
165 }
166 }
167
168 std::shared_ptr<arrow::DataType>
169 grn_type_id_to_arrow_type(grn_ctx *ctx, grn_id type_id)
170 {
171 switch (type_id) {
172 case GRN_DB_BOOL:
173 return arrow::boolean();
174 case GRN_DB_UINT8:
175 return arrow::uint8();
176 case GRN_DB_INT8:
177 return arrow::int8();
178 case GRN_DB_UINT16:
179 return arrow::uint16();
180 case GRN_DB_INT16:
181 return arrow::int16();
182 case GRN_DB_UINT32:
183 return arrow::uint32();
184 case GRN_DB_INT32:
185 return arrow::int32();
186 case GRN_DB_UINT64:
187 return arrow::uint64();
188 case GRN_DB_INT64:
189 return arrow::int64();
190 case GRN_DB_FLOAT32:
191 return arrow::float32();
192 case GRN_DB_FLOAT:
193 return arrow::float64();
194 case GRN_DB_TIME:
195 return arrow::timestamp(arrow::TimeUnit::NANO);
196 case GRN_DB_SHORT_TEXT:
197 case GRN_DB_TEXT:
198 case GRN_DB_LONG_TEXT:
199 return arrow::utf8();
200 case GRN_DB_SHORT_BINARY:
201 case GRN_DB_BINARY:
202 case GRN_DB_LONG_BINARY:
203 return arrow::binary();
204 default:
205 return nullptr;
206 }
207 }
208
209 std::shared_ptr<arrow::DataType>
210 grn_column_to_arrow_type(grn_ctx *ctx,
211 grn_obj *column,
212 ObjectCache &object_cache)
213 {
214 switch (column->header.type) {
215 case GRN_TYPE:
216 return grn_type_id_to_arrow_type(ctx, grn_obj_id(ctx, column));
217 case GRN_ACCESSOR:
218 case GRN_COLUMN_FIX_SIZE:
219 case GRN_COLUMN_VAR_SIZE:
220 {
221 grn_id range_id = GRN_ID_NIL;
222 grn_obj_flags range_flags = 0;
223 grn_obj_get_range_info(ctx, column, &range_id, &range_flags);
224 auto arrow_type = grn_type_id_to_arrow_type(ctx, range_id);
225 if (!arrow_type) {
226 auto range = object_cache[range_id];
227 if (grn_obj_is_table_with_key(ctx, range)) {
228 auto domain = object_cache[range->header.domain];
229 arrow_type = grn_column_to_arrow_type(ctx, domain, object_cache);
230 if (arrow_type == arrow::utf8()) {
231 arrow_type = arrow::dictionary(arrow::int32(), arrow_type);
232 }
233 }
234 }
235 if (range_flags & GRN_OBJ_VECTOR) {
236 return arrow::list(arrow_type);
237 } else {
238 return arrow_type;
239 }
240 }
241 break;
242 case GRN_COLUMN_INDEX:
243 return arrow::uint32();
244 default:
245 return nullptr;
246 }
247 return nullptr;
248 }
249
250 const std::shared_ptr<arrow::DataType>
251 undictionary(const std::shared_ptr<arrow::DataType> data_type)
252 {
253 if (data_type->id() != arrow::Type::DICTIONARY) {
254 return data_type;
255 }
256 auto dictionary_data_type =
257 std::static_pointer_cast<arrow::DictionaryType>(data_type);
258 return dictionary_data_type->value_type();
259 }
260
261 class RecordAddVisitor : public arrow::ArrayVisitor {
262 public:
263 RecordAddVisitor(grn_ctx *ctx,
264 grn_loader *grn_loader,
265 std::vector<grn_id> *record_ids,
266 bool is_key)
267 : ctx_(ctx),
268 grn_loader_(grn_loader),
269 record_ids_(record_ids),
270 is_key_(is_key),
271 bulk_(),
272 add_record_data_()
273 {
274 GRN_VOID_INIT(&bulk_);
275 add_record_data_.table = grn_loader_->table;
276 add_record_data_.depth = 0;
277 add_record_data_.record_value = nullptr;
278 add_record_data_.id = GRN_ID_NIL;
279 add_record_data_.key = &bulk_;
280 add_record_data_.current.column_name = nullptr;
281 add_record_data_.current.column_name_size = 0;
282 add_record_data_.current.column = nullptr;
283 add_record_data_.current.value = nullptr;
284 }
285
286 ~RecordAddVisitor() { GRN_OBJ_FIN(ctx_, &bulk_); }
287
288 arrow::Status
289 Visit(const arrow::BooleanArray &array) override
290 {
291 grn_obj_reinit(ctx_, &bulk_, GRN_DB_BOOL, 0);
292 return add_records(array, [&](int64_t i) {
293 const auto &value = array.Value(i);
294 GRN_BOOL_SET(ctx_, &bulk_, value);
295 });
296 }
297
298 arrow::Status
299 Visit(const arrow::Int8Array &array) override
300 {
301 grn_obj_reinit(ctx_, &bulk_, GRN_DB_INT8, 0);
302 return add_records(array, [&](int64_t i) {
303 const auto &value = array.Value(i);
304 GRN_INT8_SET(ctx_, &bulk_, value);
305 });
306 }
307
308 arrow::Status
309 Visit(const arrow::UInt8Array &array) override
310 {
311 grn_obj_reinit(ctx_, &bulk_, GRN_DB_UINT8, 0);
312 return add_records(array, [&](int64_t i) {
313 const auto &value = array.Value(i);
314 GRN_UINT8_SET(ctx_, &bulk_, value);
315 });
316 }
317
318 arrow::Status
319 Visit(const arrow::Int16Array &array) override
320 {
321 grn_obj_reinit(ctx_, &bulk_, GRN_DB_INT16, 0);
322 return add_records(array, [&](int64_t i) {
323 const auto &value = array.Value(i);
324 GRN_INT16_SET(ctx_, &bulk_, value);
325 });
326 }
327
328 arrow::Status
329 Visit(const arrow::UInt16Array &array) override
330 {
331 grn_obj_reinit(ctx_, &bulk_, GRN_DB_UINT16, 0);
332 return add_records(array, [&](int64_t i) {
333 const auto &value = array.Value(i);
334 GRN_UINT16_SET(ctx_, &bulk_, value);
335 });
336 }
337
338 arrow::Status
339 Visit(const arrow::Int32Array &array) override
340 {
341 grn_obj_reinit(ctx_, &bulk_, GRN_DB_INT32, 0);
342 return add_records(array, [&](int64_t i) {
343 const auto &value = array.Value(i);
344 GRN_INT32_SET(ctx_, &bulk_, value);
345 });
346 }
347
348 arrow::Status
349 Visit(const arrow::UInt32Array &array) override
350 {
351 grn_obj_reinit(ctx_, &bulk_, GRN_DB_UINT32, 0);
352 return add_records(array, [&](int64_t i) {
353 const auto &value = array.Value(i);
354 GRN_UINT32_SET(ctx_, &bulk_, value);
355 });
356 }
357
358 arrow::Status
359 Visit(const arrow::Int64Array &array) override
360 {
361 grn_obj_reinit(ctx_, &bulk_, GRN_DB_INT64, 0);
362 return add_records(array, [&](int64_t i) {
363 const auto &value = array.Value(i);
364 GRN_INT64_SET(ctx_, &bulk_, value);
365 });
366 }
367
368 arrow::Status
369 Visit(const arrow::UInt64Array &array) override
370 {
371 grn_obj_reinit(ctx_, &bulk_, GRN_DB_UINT64, 0);
372 return add_records(array, [&](int64_t i) {
373 const auto &value = array.Value(i);
374 GRN_UINT64_SET(ctx_, &bulk_, value);
375 });
376 }
377
378 arrow::Status
379 Visit(const arrow::HalfFloatArray &array) override
380 {
381 grn_obj_reinit(ctx_, &bulk_, GRN_DB_FLOAT32, 0);
382 return add_records(array, [&](int64_t i) {
383 const auto &value = array.Value(i);
384 GRN_FLOAT32_SET(ctx_, &bulk_, value);
385 });
386 }
387
388 arrow::Status
389 Visit(const arrow::FloatArray &array) override
390 {
391 grn_obj_reinit(ctx_, &bulk_, GRN_DB_FLOAT32, 0);
392 return add_records(array, [&](int64_t i) {
393 const auto &value = array.Value(i);
394 GRN_FLOAT32_SET(ctx_, &bulk_, value);
395 });
396 }
397
398 arrow::Status
399 Visit(const arrow::DoubleArray &array) override
400 {
401 grn_obj_reinit(ctx_, &bulk_, GRN_DB_FLOAT, 0);
402 return add_records(array, [&](int64_t i) {
403 const auto &value = array.Value(i);
404 GRN_FLOAT_SET(ctx_, &bulk_, value);
405 });
406 }
407
408 arrow::Status
409 Visit(const arrow::StringArray &array) override
410 {
411 grn_obj_reinit(ctx_, &bulk_, GRN_DB_TEXT, GRN_OBJ_DO_SHALLOW_COPY);
412 return add_records(array, [&](int64_t i) {
413 const auto &value = array.GetView(i);
414 GRN_TEXT_SET(ctx_, &bulk_, value.data(), value.size());
415 });
416 }
417
418 arrow::Status
419 Visit(const arrow::Date64Array &array) override
420 {
421 grn_obj_reinit(ctx_, &bulk_, GRN_DB_TIME, 0);
422 return add_records(array, [&](int64_t i) {
423 const auto &value = array.Value(i);
424 GRN_TIME_SET(ctx_, &bulk_, GRN_TIME_MSEC_TO_USEC(value));
425 });
426 }
427
428 arrow::Status
429 Visit(const arrow::TimestampArray &array) override
430 {
431 grn_obj_reinit(ctx_, &bulk_, GRN_DB_TIME, 0);
432 const auto &arrow_timestamp_type =
433 std::static_pointer_cast<arrow::TimestampType>(array.type());
434 const auto time_unit = arrow_timestamp_type->unit();
435 return add_records(array, [&](int64_t i) {
436 GRN_BULK_REWIND(&bulk_);
437 put_time_value(ctx_, &bulk_, array.Value(i), time_unit);
438 });
439 }
440
441 private:
442 grn_ctx *ctx_;
443 grn_loader *grn_loader_;
444 std::vector<grn_id> *record_ids_;
445 bool is_key_;
446 grn_obj bulk_;
447 grn_loader_add_record_data add_record_data_;
448
449 template <typename Array, typename SetBulk>
450 arrow::Status
451 add_records(const Array &array, SetBulk set_bulk)
452 {
453 const auto n_rows = array.length();
454 for (int64_t i = 0; i < n_rows; ++i) {
455 grn_id record_id;
456 if (array.IsNull(i)) {
457 record_id = GRN_ID_NIL;
458 grn_loader_on_no_identifier_error(ctx_,
459 grn_loader_,
460 grn_loader_->table);
461 } else {
462 set_bulk(i);
463 record_id = add_record();
464 }
465 grn_loader_on_record_added(ctx_, grn_loader_, record_id);
466 record_ids_->push_back(record_id);
467 }
468 return arrow::Status::OK();
469 }
470
471 grn_id
472 add_record()
473 {
474 if (is_key_) {
475 add_record_data_.id = GRN_ID_NIL;
476 return grn_loader_add_record(ctx_, grn_loader_, &add_record_data_);
477 }
478
479 grn_id requested_record_id = GRN_ID_NIL;
480 switch (bulk_.header.domain) {
481 case GRN_DB_UINT32:
482 requested_record_id = GRN_UINT32_VALUE(&bulk_);
483 break;
484 case GRN_DB_INT32:
485 requested_record_id = GRN_INT32_VALUE(&bulk_);
486 break;
487 case GRN_DB_INT64:
488 requested_record_id = static_cast<grn_id>(GRN_INT64_VALUE(&bulk_));
489 break;
490 default:
491 {
492 grn_obj casted_record_id;
493 GRN_UINT32_INIT(&casted_record_id, 0);
494 auto rc = grn_obj_cast(ctx_, &bulk_, &casted_record_id, false);
495 if (rc == GRN_SUCCESS) {
496 requested_record_id = GRN_UINT32_VALUE(&casted_record_id);
497 } else {
498 auto ctx = ctx_;
499 GRN_DEFINE_NAME(grn_loader_->table);
500 grn_obj inspected;
501 GRN_TEXT_INIT(&inspected, 0);
502 grn_inspect(ctx_, &inspected, &bulk_);
503 ERR(GRN_INVALID_ARGUMENT,
504 "[table][load][%.*s][%s] failed to cast to <UInt32>: <%.*s>",
505 name_size,
506 name,
507 GRN_COLUMN_NAME_ID,
508 (int)GRN_TEXT_LEN(&inspected),
509 GRN_TEXT_VALUE(&inspected));
510 GRN_OBJ_FIN(ctx_, &inspected);
511 }
512 GRN_OBJ_FIN(ctx_, &casted_record_id);
513 }
514 break;
515 }
516
517 if (requested_record_id == GRN_ID_NIL) {
518 if (ctx_->rc == GRN_SUCCESS) {
519 return grn_table_add(ctx_, grn_loader_->table, NULL, 0, NULL);
520 } else {
521 return GRN_ID_NIL;
522 }
523 } else {
524 return grn_table_at(ctx_, grn_loader_->table, requested_record_id);
525 }
526 }
527 };
528
529 class ValueLoadVisitor : public arrow::ArrayVisitor {
530 public:
531 ValueLoadVisitor(grn_ctx *ctx,
532 grn_obj *grn_column,
533 grn_obj *bulk,
534 int64_t index,
535 ObjectCache *object_cache)
536 : ctx_(ctx),
537 grn_column_(grn_column),
538 bulk_(bulk),
539 index_(index),
540 buffer_(),
541 loaded_value_(nullptr),
542 object_cache_(object_cache)
543 {
544 GRN_VOID_INIT(&buffer_);
545 }
546
547 ~ValueLoadVisitor() { GRN_OBJ_FIN(ctx_, &buffer_); }
548
549 arrow::Status
550 Visit(const arrow::BooleanArray &array) override
551 {
552 return load_value([&]() {
553 grn_obj_reinit(ctx_, &buffer_, GRN_DB_BOOL, 0);
554 GRN_BOOL_SET(ctx_, &buffer_, array.Value(index_));
555 });
556 }
557
558 arrow::Status
559 Visit(const arrow::Int8Array &array) override
560 {
561 return load_value([&]() {
562 grn_obj_reinit(ctx_, &buffer_, GRN_DB_INT8, 0);
563 GRN_INT8_SET(ctx_, &buffer_, array.Value(index_));
564 });
565 }
566
567 arrow::Status
568 Visit(const arrow::UInt8Array &array) override
569 {
570 return load_value([&]() {
571 grn_obj_reinit(ctx_, &buffer_, GRN_DB_UINT8, 0);
572 GRN_UINT8_SET(ctx_, &buffer_, array.Value(index_));
573 });
574 }
575
576 arrow::Status
577 Visit(const arrow::Int16Array &array) override
578 {
579 return load_value([&]() {
580 grn_obj_reinit(ctx_, &buffer_, GRN_DB_INT16, 0);
581 GRN_INT16_SET(ctx_, &buffer_, array.Value(index_));
582 });
583 }
584
585 arrow::Status
586 Visit(const arrow::UInt16Array &array) override
587 {
588 return load_value([&]() {
589 grn_obj_reinit(ctx_, &buffer_, GRN_DB_UINT16, 0);
590 GRN_UINT16_SET(ctx_, &buffer_, array.Value(index_));
591 });
592 }
593
594 arrow::Status
595 Visit(const arrow::Int32Array &array) override
596 {
597 return load_value([&]() {
598 grn_obj_reinit(ctx_, &buffer_, GRN_DB_INT32, 0);
599 GRN_INT32_SET(ctx_, &buffer_, array.Value(index_));
600 });
601 }
602
603 arrow::Status
604 Visit(const arrow::UInt32Array &array) override
605 {
606 return load_value([&]() {
607 grn_obj_reinit(ctx_, &buffer_, GRN_DB_UINT32, 0);
608 GRN_UINT32_SET(ctx_, &buffer_, array.Value(index_));
609 });
610 }
611
612 arrow::Status
613 Visit(const arrow::Int64Array &array) override
614 {
615 return load_value([&]() {
616 grn_obj_reinit(ctx_, &buffer_, GRN_DB_INT64, 0);
617 GRN_INT64_SET(ctx_, &buffer_, array.Value(index_));
618 });
619 }
620
621 arrow::Status
622 Visit(const arrow::UInt64Array &array) override
623 {
624 return load_value([&]() {
625 grn_obj_reinit(ctx_, &buffer_, GRN_DB_UINT64, 0);
626 GRN_UINT64_SET(ctx_, &buffer_, array.Value(index_));
627 });
628 }
629
630 arrow::Status
631 Visit(const arrow::HalfFloatArray &array) override
632 {
633 return load_value([&]() {
634 grn_obj_reinit(ctx_, &buffer_, GRN_DB_FLOAT32, 0);
635 GRN_FLOAT32_SET(ctx_, &buffer_, array.Value(index_));
636 });
637 }
638
639 arrow::Status
640 Visit(const arrow::FloatArray &array) override
641 {
642 return load_value([&]() {
643 grn_obj_reinit(ctx_, &buffer_, GRN_DB_FLOAT32, 0);
644 GRN_FLOAT32_SET(ctx_, &buffer_, array.Value(index_));
645 });
646 }
647
648 arrow::Status
649 Visit(const arrow::DoubleArray &array) override
650 {
651 return load_value([&]() {
652 grn_obj_reinit(ctx_, &buffer_, GRN_DB_FLOAT, 0);
653 GRN_FLOAT_SET(ctx_, &buffer_, array.Value(index_));
654 });
655 }
656
657 arrow::Status
658 Visit(const arrow::StringArray &array) override
659 {
660 return load_value([&]() {
661 const auto value = array.GetView(index_);
662 grn_obj_reinit(ctx_, &buffer_, GRN_DB_TEXT, GRN_OBJ_DO_SHALLOW_COPY);
663 GRN_TEXT_SET(ctx_, &buffer_, value.data(), value.size());
664 });
665 }
666
667 arrow::Status
668 Visit(const arrow::BinaryArray &array) override
669 {
670 return load_value([&]() {
671 const auto value = array.GetView(index_);
672 grn_obj_reinit(ctx_, &buffer_, GRN_DB_BINARY, GRN_OBJ_DO_SHALLOW_COPY);
673 GRN_BINARY_SET(ctx_, &buffer_, value.data(), value.size());
674 });
675 }
676
677 arrow::Status
678 Visit(const arrow::Date64Array &array) override
679 {
680 return load_value([&]() {
681 const auto value = array.Value(index_);
682 grn_obj_reinit(ctx_, &buffer_, GRN_DB_TIME, 0);
683 GRN_TIME_SET(ctx_, &buffer_, GRN_TIME_MSEC_TO_USEC(value));
684 });
685 }
686
687 arrow::Status
688 Visit(const arrow::TimestampArray &array) override
689 {
690 const auto &arrow_timestamp_type =
691 std::static_pointer_cast<arrow::TimestampType>(array.type());
692 return load_value([&]() {
693 grn_obj_reinit(ctx_, &buffer_, GRN_DB_TIME, 0);
694 put_time_value(ctx_,
695 &buffer_,
696 array.Value(index_),
697 arrow_timestamp_type->unit());
698 });
699 return arrow::Status::OK();
700 }
701
702 arrow::Status
703 Visit(const arrow::ListArray &array) override
704 {
705 const auto &value_array = array.value_slice(index_);
706 switch (value_array->type_id()) {
707 case arrow::Type::STRUCT:
708 for (int64_t i = 0; i < value_array->length(); ++i) {
709 ValueLoadVisitor sub_visitor(ctx_,
710 grn_column_,
711 bulk_,
712 i,
713 object_cache_);
714 ARROW_RETURN_NOT_OK(value_array->Accept(&sub_visitor));
715 }
716 break;
717 case arrow::Type::LIST:
718 for (int64_t i = 0; i < value_array->length(); ++i) {
719 const auto &sub_list_array =
720 std::static_pointer_cast<arrow::ListArray>(value_array);
721 const auto &sub_value_array = sub_list_array->value_slice(i);
722 for (int64_t j = 0; j < sub_value_array->length(); ++j) {
723 ValueLoadVisitor sub_visitor(ctx_,
724 grn_column_,
725 bulk_,
726 j,
727 object_cache_);
728 ARROW_RETURN_NOT_OK(sub_value_array->Accept(&sub_visitor));
729 }
730 }
731 break;
732 default:
733 {
734 grn_obj sub_buffer;
735 if (grn_type_id_is_text_family(ctx_, bulk_->header.domain)) {
736 GRN_TEXT_INIT(&sub_buffer, GRN_OBJ_VECTOR);
737 } else {
738 GRN_VALUE_FIX_SIZE_INIT(&sub_buffer,
739 GRN_OBJ_VECTOR,
740 bulk_->header.domain);
741 }
742 for (int64_t i = 0; i < value_array->length(); ++i) {
743 GRN_BULK_REWIND(&sub_buffer);
744 ValueLoadVisitor sub_visitor(ctx_,
745 grn_column_,
746 &sub_buffer,
747 i,
748 object_cache_);
749 ARROW_RETURN_NOT_OK(value_array->Accept(&sub_visitor));
750 if (ctx_->rc != GRN_SUCCESS) {
751 continue;
752 }
753 auto loaded_value = sub_visitor.loaded_value();
754 if (grn_obj_is_vector(ctx_, bulk_)) {
755 auto original_value = sub_visitor.original_value();
756 if (grn_obj_is_text_family_bulk(ctx_, original_value) &&
757 GRN_TEXT_LEN(original_value) == 0 &&
758 grn_type_id_is_text_family(ctx_, bulk_->header.domain)) {
759 grn_vector_add_element_float(ctx_,
760 bulk_,
761 NULL,
762 0,
763 0.0,
764 bulk_->header.domain);
765 } else {
766 grn_vector_copy(ctx_, loaded_value, bulk_);
767 }
768 } else {
769 auto original_value = sub_visitor.original_value();
770 if (grn_obj_is_text_family_bulk(ctx_, original_value) &&
771 GRN_TEXT_LEN(original_value) == 0 &&
772 grn_id_maybe_table(ctx_, bulk_->header.domain)) {
773 grn_uvector_add_element_record(ctx_, bulk_, GRN_ID_NIL, 0.0);
774 } else {
775 grn_uvector_copy(ctx_, loaded_value, bulk_);
776 }
777 }
778 }
779 GRN_OBJ_FIN(ctx_, &sub_buffer);
780 }
781 break;
782 }
783 return arrow::Status::OK();
784 }
785
786 arrow::Status
787 Visit(const arrow::StructArray &array) override
788 {
789 const auto &value_column = array.GetFieldByName("value");
790 if (!value_column) {
791 return arrow::Status::OK();
792 }
793 if (value_column->type_id() != arrow::Type::STRING) {
794 return arrow::Status::OK();
795 }
796 const auto &weight_column = array.GetFieldByName("weight");
797 if (!weight_column) {
798 return arrow::Status::OK();
799 }
800 float weight;
801 switch (weight_column->type_id()) {
802 case arrow::Type::INT32:
803 weight = std::static_pointer_cast<arrow::Int32Array>(weight_column)
804 ->Value(index_);
805 break;
806 case arrow::Type::FLOAT:
807 weight = std::static_pointer_cast<arrow::FloatArray>(weight_column)
808 ->Value(index_);
809 break;
810 default:
811 return arrow::Status::OK();
812 }
813
814 const auto &value =
815 std::static_pointer_cast<arrow::StringArray>(value_column)
816 ->GetView(index_);
817 const grn_id domain = bulk_->header.domain;
818 const auto raw_value = value.data();
819 const auto raw_value_size = value.size();
820 grn_obj *value_bulk;
821 grn_obj value_buffer;
822 if (grn_type_id_is_text_family(ctx_, domain)) {
823 GRN_TEXT_INIT(&value_buffer, GRN_OBJ_DO_SHALLOW_COPY);
824 GRN_TEXT_SET(ctx_, &value_buffer, raw_value, raw_value_size);
825 value_bulk = &value_buffer;
826 } else {
827 grn_obj raw_value_buffer;
828 GRN_TEXT_INIT(&raw_value_buffer, GRN_OBJ_DO_SHALLOW_COPY);
829 GRN_TEXT_SET(ctx_, &raw_value_buffer, raw_value, raw_value_size);
830 GRN_VALUE_FIX_SIZE_INIT(&value_buffer, 0, domain);
831 value_bulk = grn_column_cast_value(ctx_,
832 grn_column_,
833 &raw_value_buffer,
834 &value_buffer,
835 GRN_OBJ_SET);
836 if (ctx_->rc != GRN_SUCCESS && (grn_obj_is_vector(ctx_, bulk_) ||
837 grn_obj_is_uvector(ctx_, bulk_))) {
838 ERRCLR(ctx_);
839 }
840 GRN_OBJ_FIN(ctx_, &raw_value_buffer);
841 }
842 if (value_bulk && GRN_BULK_VSIZE(value_bulk) > 0) {
843 if (grn_type_id_is_text_family(ctx_, domain)) {
844 const auto value_bulk_size =
845 static_cast<unsigned int>(GRN_BULK_VSIZE(value_bulk));
846 grn_vector_add_element_float(ctx_,
847 bulk_,
848 GRN_BULK_HEAD(value_bulk),
849 value_bulk_size,
850 weight,
851 domain);
852 } else {
853 // TODO: Support weight vector for number such as Int64
854 auto id =
855 grn_uvector_get_element_record(ctx_, value_bulk, 0, nullptr);
856 grn_uvector_add_element_record(ctx_, bulk_, id, weight);
857 }
858 }
859 GRN_OBJ_FIN(ctx_, &value_buffer);
860 return arrow::Status::OK();
861 }
862
863 grn_obj *
864 original_value()
865 {
866 return &buffer_;
867 }
868
869 grn_obj *
870 loaded_value()
871 {
872 if (loaded_value_) {
873 return loaded_value_;
874 }
875 return bulk_;
876 }
877
878 private:
879 grn_ctx *ctx_;
880 grn_obj *grn_column_;
881 grn_obj *bulk_;
882 int64_t index_;
883 grn_obj buffer_;
884 grn_obj *loaded_value_;
885 ObjectCache *object_cache_;
886
887 template <typename LoadBulk>
888 arrow::Status
889 load_value(LoadBulk load_bulk)
890 {
891 load_bulk();
892 loaded_value_ = &buffer_;
893 if (bulk_->header.domain == GRN_DB_VOID) {
894 return arrow::Status::OK();
895 }
896 if (!grn_column_) {
897 return arrow::Status::OK();
898 }
899 if (grn_obj_is_accessor(ctx_, grn_column_)) {
900 if (grn_obj_cast(ctx_, &buffer_, bulk_, true) != GRN_SUCCESS) {
901 auto ctx = ctx_;
902 auto range = (*object_cache_)[bulk_->header.domain];
903 ERR_CAST(grn_column_, range, &buffer_);
904 }
905 loaded_value_ = bulk_;
906 } else {
907 loaded_value_ = grn_column_cast_value(ctx_,
908 grn_column_,
909 &buffer_,
910 bulk_,
911 GRN_OBJ_SET);
912 if (ctx_->rc != GRN_SUCCESS && (grn_obj_is_vector(ctx_, bulk_) ||
913 grn_obj_is_uvector(ctx_, bulk_))) {
914 ERRCLR(ctx_);
915 }
916 }
917 return arrow::Status::OK();
918 }
919 };
920
921 class ColumnLoadVisitor : public arrow::ArrayVisitor {
922 public:
923 ColumnLoadVisitor(grn_ctx *ctx,
924 grn_loader *grn_loader,
925 grn_obj *grn_table,
926 grn_obj *grn_column,
927 const std::shared_ptr<arrow::Field> &arrow_field,
928 const grn_id *record_ids,
929 ObjectCache *object_cache)
930 : ctx_(ctx),
931 grn_loader_(grn_loader),
932 grn_table_(grn_table),
933 grn_column_(grn_column),
934 record_ids_(record_ids),
935 column_name_(arrow_field->name()),
936 buffer_(),
937 object_cache_(object_cache)
938 {
939 const auto &arrow_type = arrow_field->type();
940 grn_id arrow_type_id = GRN_DB_VOID;
941 grn_obj_flags flags = 0;
942 detect_type(arrow_type, &arrow_type_id, &flags);
943 if (arrow_type_id == GRN_DB_VOID) {
944 // TODO
945 GRN_VOID_INIT(&buffer_);
946 return;
947 }
948
949 if (!grn_column_) {
950 if (!grn_loader_) {
951 grn_column_ =
952 grn_column_create(ctx_,
953 grn_table_,
954 column_name_.data(),
955 static_cast<unsigned int>(column_name_.size()),
956 NULL,
957 GRN_OBJ_COLUMN_SCALAR,
958 (*object_cache_)[arrow_type_id]);
959 }
960 }
961 grn_id range_id = GRN_ID_NIL;
962 if (grn_column_) {
963 grn_obj_get_range_info(ctx, grn_column_, &range_id, &flags);
964 }
965 if (range_id == GRN_ID_NIL) {
966 range_id = arrow_type_id;
967 }
968 if (grn_type_id_is_text_family(ctx_, range_id)) {
969 GRN_VALUE_VAR_SIZE_INIT(&buffer_, flags, range_id);
970 } else {
971 GRN_VALUE_FIX_SIZE_INIT(&buffer_, flags, range_id);
972 if (flags & GRN_OBJ_WITH_WEIGHT) {
973 buffer_.header.flags |= GRN_OBJ_WITH_WEIGHT;
974 }
975 }
976 }
977
978 ~ColumnLoadVisitor() { GRN_OBJ_FIN(ctx_, &buffer_); }
979
980 arrow::Status
981 Visit(const arrow::BooleanArray &array) override
982 {
983 return set_values(array);
984 }
985
986 arrow::Status
987 Visit(const arrow::Int8Array &array) override
988 {
989 return set_values(array);
990 }
991
992 arrow::Status
993 Visit(const arrow::UInt8Array &array) override
994 {
995 return set_values(array);
996 }
997
998 arrow::Status
999 Visit(const arrow::Int16Array &array) override
1000 {
1001 return set_values(array);
1002 }
1003
1004 arrow::Status
1005 Visit(const arrow::UInt16Array &array) override
1006 {
1007 return set_values(array);
1008 }
1009
1010 arrow::Status
1011 Visit(const arrow::Int32Array &array) override
1012 {
1013 return set_values(array);
1014 }
1015
1016 arrow::Status
1017 Visit(const arrow::UInt32Array &array) override
1018 {
1019 return set_values(array);
1020 }
1021
1022 arrow::Status
1023 Visit(const arrow::Int64Array &array) override
1024 {
1025 return set_values(array);
1026 }
1027
1028 arrow::Status
1029 Visit(const arrow::UInt64Array &array) override
1030 {
1031 return set_values(array);
1032 }
1033
1034 arrow::Status
1035 Visit(const arrow::HalfFloatArray &array) override
1036 {
1037 return set_values(array);
1038 }
1039
1040 arrow::Status
1041 Visit(const arrow::FloatArray &array) override
1042 {
1043 return set_values(array);
1044 }
1045
1046 arrow::Status
1047 Visit(const arrow::DoubleArray &array) override
1048 {
1049 return set_values(array);
1050 }
1051
1052 arrow::Status
1053 Visit(const arrow::StringArray &array) override
1054 {
1055 return set_values(array);
1056 }
1057
1058 arrow::Status
1059 Visit(const arrow::BinaryArray &array) override
1060 {
1061 return set_values(array);
1062 }
1063
1064 arrow::Status
1065 Visit(const arrow::Date64Array &array) override
1066 {
1067 return set_values(array);
1068 }
1069
1070 arrow::Status
1071 Visit(const arrow::TimestampArray &array) override
1072 {
1073 return set_values(array);
1074 }
1075
1076 arrow::Status
1077 Visit(const arrow::ListArray &array) override
1078 {
1079 return set_values(array);
1080 }
1081
1082 private:
1083 grn_ctx *ctx_;
1084 grn_loader *grn_loader_;
1085 grn_obj *grn_table_;
1086 grn_obj *grn_column_;
1087 const grn_id *record_ids_;
1088 std::string column_name_;
1089 grn_obj buffer_;
1090 ObjectCache *object_cache_;
1091
1092 void
1093 detect_type(const std::shared_ptr<arrow::DataType> &arrow_type,
1094 grn_id *type_id,
1095 grn_obj_flags *flags)
1096 {
1097 switch (arrow_type->id()) {
1098 case arrow::Type::BOOL:
1099 *type_id = GRN_DB_BOOL;
1100 break;
1101 case arrow::Type::UINT8:
1102 *type_id = GRN_DB_UINT8;
1103 break;
1104 case arrow::Type::INT8:
1105 *type_id = GRN_DB_INT8;
1106 break;
1107 case arrow::Type::UINT16:
1108 *type_id = GRN_DB_UINT16;
1109 break;
1110 case arrow::Type::INT16:
1111 *type_id = GRN_DB_INT16;
1112 break;
1113 case arrow::Type::UINT32:
1114 *type_id = GRN_DB_UINT32;
1115 break;
1116 case arrow::Type::INT32:
1117 *type_id = GRN_DB_INT32;
1118 break;
1119 case arrow::Type::UINT64:
1120 *type_id = GRN_DB_UINT64;
1121 break;
1122 case arrow::Type::INT64:
1123 *type_id = GRN_DB_INT64;
1124 break;
1125 case arrow::Type::HALF_FLOAT:
1126 case arrow::Type::FLOAT:
1127 *type_id = GRN_DB_FLOAT32;
1128 break;
1129 case arrow::Type::DOUBLE:
1130 *type_id = GRN_DB_FLOAT;
1131 break;
1132 case arrow::Type::STRING:
1133 *type_id = GRN_DB_TEXT;
1134 break;
1135 case arrow::Type::BINARY:
1136 *type_id = GRN_DB_BINARY;
1137 break;
1138 case arrow::Type::DATE64:
1139 *type_id = GRN_DB_TIME;
1140 break;
1141 case arrow::Type::TIMESTAMP:
1142 *type_id = GRN_DB_TIME;
1143 break;
1144 case arrow::Type::LIST:
1145 *flags |= GRN_OBJ_VECTOR;
1146 {
1147 grn_obj_flags sub_flags = 0;
1148 const auto &arrow_list_type =
1149 std::static_pointer_cast<arrow::ListType>(arrow_type);
1150 detect_type(arrow_list_type->value_type(), type_id, &sub_flags);
1151 *flags |= (sub_flags & GRN_OBJ_WITH_WEIGHT);
1152 }
1153 break;
1154 case arrow::Type::STRUCT:
1155 // Must be weight vector: {"value": string, "weight": int32}
1156 *type_id = GRN_DB_TEXT;
1157 *flags |= GRN_OBJ_WITH_WEIGHT;
1158 break;
1159 case arrow::Type::MAP:
1160 // TODO: Support as weight vector
1161 // *type_id = GRN_DB_TEXT;
1162 // *flags |= GRN_OBJ_VECTOR | GRN_OBJ_WITH_WEIGHT;
1163 default:
1164 *type_id = GRN_DB_VOID;
1165 break;
1166 }
1167 }
1168
1169 template <typename T>
1170 arrow::Status
1171 set_values(const T &array)
1172 {
1173 int64_t n_rows = array.length();
1174 for (int i = 0; i < n_rows; ++i) {
1175 const auto record_id = record_ids_[i];
1176 if (record_id == GRN_ID_NIL) {
1177 continue;
1178 }
1179
1180 GRN_BULK_REWIND(&buffer_);
1181 ValueLoadVisitor visitor(ctx_, grn_column_, &buffer_, i, object_cache_);
1182 ARROW_RETURN_NOT_OK(array.Accept(&visitor));
1183 if (!grn_column_) {
1184 auto ctx = ctx_;
1185 GRN_DEFINE_NAME(grn_table_);
1186 ERR(GRN_INVALID_ARGUMENT,
1187 "[table][load][%.*s] nonexistent column: <%s>",
1188 name_size,
1189 name,
1190 column_name_.data());
1191 if (grn_loader_) {
1192 grn_loader_add_record_data data;
1193 init_grn_loader_add_record_data(&data,
1194 record_id,
1195 visitor.original_value());
1196 grn_loader_on_column_set(ctx_, grn_loader_, &data);
1197 }
1198 continue;
1199 }
1200
1201 if (ctx_->rc != GRN_SUCCESS) {
1202 if (grn_loader_) {
1203 grn_loader_add_record_data data;
1204 init_grn_loader_add_record_data(&data,
1205 record_id,
1206 visitor.original_value());
1207 grn_loader_on_column_set(ctx_, grn_loader_, &data);
1208 }
1209 continue;
1210 }
1211
1212 auto value = visitor.loaded_value();
1213 grn_obj_set_value(ctx_, grn_column_, record_id, value, GRN_OBJ_SET);
1214 if (ctx_->rc != GRN_SUCCESS) {
1215 if (grn_loader_) {
1216 grn_loader_add_record_data data;
1217 init_grn_loader_add_record_data(&data, record_id, value);
1218 grn_loader_on_column_set(ctx_, grn_loader_, &data);
1219 }
1220 }
1221 }
1222 return check(ctx_, ctx_->rc, "[arrow][column-loader][set-value]");
1223 }
1224
1225 void
1226 init_grn_loader_add_record_data(grn_loader_add_record_data *data,
1227 grn_id record_id,
1228 grn_obj *value)
1229 {
1230 data->table = grn_loader_->table;
1231 data->depth = 0;
1232 data->record_value = nullptr;
1233 data->id = record_id;
1234 data->key = nullptr;
1235 data->current.column_name = column_name_.data();
1236 data->current.column_name_size = column_name_.size();
1237 data->current.column = grn_column_;
1238 data->current.value = value;
1239 }
1240 };
1241
1242 class FileLoader {
1243 public:
1244 FileLoader(grn_ctx *ctx, grn_obj *grn_table)
1245 : ctx_(ctx),
1246 grn_table_(grn_table),
1247 key_column_name_(""),
1248 object_cache_(ctx_)
1249 {
1250 }
1251
1252 ~FileLoader() {}
1253
1254 grn_rc
1255 load_table(const std::shared_ptr<arrow::Table> &arrow_table)
1256 {
1257 auto n_columns = arrow_table->num_columns();
1258
1259 if (key_column_name_.empty()) {
1260 grn_obj ids;
1261 GRN_RECORD_INIT(&ids, GRN_OBJ_VECTOR, grn_obj_id(ctx_, grn_table_));
1262 auto n_records = arrow_table->num_rows();
1263 for (int64_t i = 0; i < n_records; ++i) {
1264 auto id = grn_table_add(ctx_, grn_table_, NULL, 0, NULL);
1265 GRN_RECORD_PUT(ctx_, &ids, id);
1266 }
1267 const auto &arrow_schema = arrow_table->schema();
1268 for (int i = 0; i < n_columns; ++i) {
1269 int64_t offset = 0;
1270 const auto &arrow_field = arrow_schema->field(i);
1271 const auto &arrow_chunked_array = arrow_table->column(i);
1272 for (const auto &arrow_array : arrow_chunked_array->chunks()) {
1273 grn_id *sub_ids =
1274 reinterpret_cast<grn_id *>(GRN_BULK_HEAD(&ids)) + offset;
1275 const auto &column_name = arrow_field->name();
1276 auto grn_column = grn_obj_column(ctx_,
1277 grn_table_,
1278 column_name.data(),
1279 column_name.size());
1280 ColumnLoadVisitor visitor(ctx_,
1281 nullptr,
1282 grn_table_,
1283 grn_column,
1284 arrow_field,
1285 sub_ids,
1286 &object_cache_);
1287 auto status = arrow_array->Accept(&visitor);
1288 if (grn_obj_is_accessor(ctx_, grn_column)) {
1289 grn_obj_unlink(ctx_, grn_column);
1290 }
1291 offset += arrow_array->length();
1292 }
1293 }
1294 GRN_OBJ_FIN(ctx_, &ids);
1295 } else {
1296 auto status = arrow::Status::NotImplemented("_key isn't supported yet");
1297 check(ctx_, status, "[arrow][load]");
1298 }
1299 return ctx_->rc;
1300 };
1301
1302 grn_rc
1303 load_record_batch(
1304 const std::shared_ptr<arrow::RecordBatch> &arrow_record_batch)
1305 {
1306 std::vector<std::shared_ptr<arrow::RecordBatch>> arrow_record_batches = {
1307 arrow_record_batch};
1308 auto arrow_table = arrow::Table::FromRecordBatches(arrow_record_batches);
1309 if (!check(ctx_,
1310 arrow_table,
1311 "[arrow][load] "
1312 "failed to convert record batch to table")) {
1313 return ctx_->rc;
1314 }
1315 return load_table(*arrow_table);
1316 };
1317
1318 private:
1319 grn_ctx *ctx_;
1320 grn_obj *grn_table_;
1321 std::string key_column_name_;
1322 ObjectCache object_cache_;
1323 };
1324
1325 class FileDumper {
1326 public:
1327 FileDumper(grn_ctx *ctx, grn_obj *grn_table, grn_obj *grn_columns)
1328 : ctx_(ctx),
1329 grn_table_(grn_table),
1330 grn_columns_(grn_columns),
1331 object_cache_(ctx_),
1332 tag_("[arrow][dump]")
1333 {
1334 }
1335
1336 ~FileDumper() {}
1337
1338 grn_rc
1339 dump(arrow::io::OutputStream *output)
1340 {
1341 std::vector<std::shared_ptr<arrow::Field>> fields;
1342 auto n_columns = GRN_BULK_VSIZE(grn_columns_) / sizeof(grn_obj *);
1343 for (size_t i = 0; i < n_columns; ++i) {
1344 auto column = GRN_PTR_VALUE_AT(grn_columns_, i);
1345
1346 char column_name[GRN_TABLE_MAX_KEY_SIZE];
1347 int column_name_size;
1348 column_name_size =
1349 grn_column_name(ctx_, column, column_name, GRN_TABLE_MAX_KEY_SIZE);
1350 std::string field_name(column_name, column_name_size);
1351 auto range_id = grn_obj_get_range(ctx_, column);
1352 auto range = object_cache_[range_id];
1353 auto field_type = grn_column_to_arrow_type(ctx_, range, object_cache_);
1354 if (!field_type) {
1355 continue;
1356 }
1357 // We can't use dictionary with FileWriter. It doesn't support
1358 // dictionary delta.
1359 field_type = undictionary(field_type);
1360
1361 auto field =
1362 std::make_shared<arrow::Field>(field_name, field_type, false);
1363 fields.push_back(field);
1364 };
1365
1366 auto schema = std::make_shared<arrow::Schema>(fields);
1367 auto writer_result = arrow::ipc::MakeFileWriter(output, schema);
1368
1369 if (!check(ctx_,
1370 writer_result,
1371 tag_ + " failed to create file format writer")) {
1372 return ctx_->rc;
1373 }
1374 auto writer = *writer_result;
1375
1376 std::vector<grn_id> ids;
1377 size_t n_records_per_batch = 1000;
1378 GRN_TABLE_EACH_BEGIN(ctx_, grn_table_, table_cursor, record_id)
1379 {
1380 ids.push_back(record_id);
1381 if (ids.size() == n_records_per_batch) {
1382 write_record_batch(ids, schema, writer);
1383 ids.clear();
1384 if (ctx_->rc != GRN_SUCCESS) {
1385 break;
1386 }
1387 }
1388 }
1389 GRN_TABLE_EACH_END(ctx_, table_cursor);
1390 if (!ids.empty()) {
1391 write_record_batch(ids, schema, writer);
1392 }
1393 if (ctx_->rc != GRN_SUCCESS) {
1394 return ctx_->rc;
1395 }
1396 auto status = writer->Close();
1397 if (!check(ctx_,
1398 status,
1399 tag_ + " failed to close writer file format writer")) {
1400 return ctx_->rc;
1401 }
1402
1403 return ctx_->rc;
1404 }
1405
1406 private:
1407 grn_ctx *ctx_;
1408 grn_obj *grn_table_;
1409 grn_obj *grn_columns_;
1410 ObjectCache object_cache_;
1411 std::string tag_;
1412
1413 void
1414 write_record_batch(std::vector<grn_id> &ids,
1415 std::shared_ptr<arrow::Schema> &schema,
1416 std::shared_ptr<arrow::ipc::RecordBatchWriter> &writer)
1417 {
1418 std::vector<std::shared_ptr<arrow::Array>> columns;
1419 auto n_columns = GRN_BULK_VSIZE(grn_columns_) / sizeof(grn_obj *);
1420 for (size_t i = 0; i < n_columns; ++i) {
1421 auto grn_column = GRN_PTR_VALUE_AT(grn_columns_, i);
1422
1423 arrow::Status status;
1424 std::shared_ptr<arrow::Array> column;
1425
1426 switch (grn_obj_get_range(ctx_, grn_column)) {
1427 case GRN_DB_BOOL:
1428 status = build_boolean_array(ids, grn_column, &column);
1429 break;
1430 case GRN_DB_UINT8:
1431 status = build_uint8_array(ids, grn_column, &column);
1432 break;
1433 case GRN_DB_INT8:
1434 status = build_int8_array(ids, grn_column, &column);
1435 break;
1436 case GRN_DB_UINT16:
1437 status = build_uint16_array(ids, grn_column, &column);
1438 break;
1439 case GRN_DB_INT16:
1440 status = build_int16_array(ids, grn_column, &column);
1441 break;
1442 case GRN_DB_UINT32:
1443 status = build_uint32_array(ids, grn_column, &column);
1444 break;
1445 case GRN_DB_INT32:
1446 status = build_int32_array(ids, grn_column, &column);
1447 break;
1448 case GRN_DB_UINT64:
1449 status = build_uint64_array(ids, grn_column, &column);
1450 break;
1451 case GRN_DB_INT64:
1452 status = build_int64_array(ids, grn_column, &column);
1453 break;
1454 case GRN_DB_FLOAT32:
1455 status = build_float_array(ids, grn_column, &column);
1456 break;
1457 case GRN_DB_FLOAT:
1458 status = build_double_array(ids, grn_column, &column);
1459 break;
1460 case GRN_DB_TIME:
1461 status = build_timestamp_array(ids, grn_column, &column);
1462 break;
1463 case GRN_DB_SHORT_TEXT:
1464 case GRN_DB_TEXT:
1465 case GRN_DB_LONG_TEXT:
1466 status = build_utf8_array(ids, grn_column, &column);
1467 break;
1468 case GRN_DB_SHORT_BINARY:
1469 case GRN_DB_BINARY:
1470 case GRN_DB_LONG_BINARY:
1471 status = build_binary_array(ids, grn_column, &column);
1472 break;
1473 default:
1474 status = arrow::Status::NotImplemented(
1475 "[arrow][dumper] not supported type: TODO");
1476 break;
1477 }
1478 if (!status.ok()) {
1479 continue;
1480 }
1481 columns.push_back(column);
1482 }
1483
1484 auto record_batch = arrow::RecordBatch::Make(schema, ids.size(), columns);
1485 check(ctx_,
1486 writer->WriteRecordBatch(*record_batch),
1487 tag_ + " failed to write record batch");
1488 }
1489
1490 arrow::Status
1491 build_boolean_array(std::vector<grn_id> &ids,
1492 grn_obj *grn_column,
1493 std::shared_ptr<arrow::Array> *array)
1494 {
1495 arrow::BooleanBuilder builder(arrow::default_memory_pool());
1496 for (auto id : ids) {
1497 uint32_t size;
1498 auto data = grn_obj_get_value_(ctx_, grn_column, id, &size);
1499 ARROW_RETURN_NOT_OK(
1500 builder.Append(*(reinterpret_cast<const bool *>(data))));
1501 }
1502 return builder.Finish(array);
1503 }
1504
1505 arrow::Status
1506 build_uint8_array(std::vector<grn_id> &ids,
1507 grn_obj *grn_column,
1508 std::shared_ptr<arrow::Array> *array)
1509 {
1510 arrow::UInt8Builder builder(arrow::default_memory_pool());
1511 for (auto id : ids) {
1512 uint32_t size;
1513 auto data = grn_obj_get_value_(ctx_, grn_column, id, &size);
1514 ARROW_RETURN_NOT_OK(
1515 builder.Append(*(reinterpret_cast<const uint8_t *>(data))));
1516 }
1517 return builder.Finish(array);
1518 }
1519
1520 arrow::Status
1521 build_int8_array(std::vector<grn_id> &ids,
1522 grn_obj *grn_column,
1523 std::shared_ptr<arrow::Array> *array)
1524 {
1525 arrow::Int8Builder builder(arrow::default_memory_pool());
1526 for (auto id : ids) {
1527 uint32_t size;
1528 auto data = grn_obj_get_value_(ctx_, grn_column, id, &size);
1529 ARROW_RETURN_NOT_OK(
1530 builder.Append(*(reinterpret_cast<const int8_t *>(data))));
1531 }
1532 return builder.Finish(array);
1533 }
1534
1535 arrow::Status
1536 build_uint16_array(std::vector<grn_id> &ids,
1537 grn_obj *grn_column,
1538 std::shared_ptr<arrow::Array> *array)
1539 {
1540 arrow::UInt16Builder builder(arrow::default_memory_pool());
1541 for (auto id : ids) {
1542 uint32_t size;
1543 auto data = grn_obj_get_value_(ctx_, grn_column, id, &size);
1544 ARROW_RETURN_NOT_OK(
1545 builder.Append(*(reinterpret_cast<const uint16_t *>(data))));
1546 }
1547 return builder.Finish(array);
1548 }
1549
1550 arrow::Status
1551 build_int16_array(std::vector<grn_id> &ids,
1552 grn_obj *grn_column,
1553 std::shared_ptr<arrow::Array> *array)
1554 {
1555 arrow::Int16Builder builder(arrow::default_memory_pool());
1556 for (auto id : ids) {
1557 uint32_t size;
1558 auto data = grn_obj_get_value_(ctx_, grn_column, id, &size);
1559 ARROW_RETURN_NOT_OK(
1560 builder.Append(*(reinterpret_cast<const int16_t *>(data))));
1561 }
1562 return builder.Finish(array);
1563 }
1564
1565 arrow::Status
1566 build_uint32_array(std::vector<grn_id> &ids,
1567 grn_obj *grn_column,
1568 std::shared_ptr<arrow::Array> *array)
1569 {
1570 arrow::UInt32Builder builder(arrow::default_memory_pool());
1571 for (auto id : ids) {
1572 uint32_t size;
1573 auto data = grn_obj_get_value_(ctx_, grn_column, id, &size);
1574 ARROW_RETURN_NOT_OK(
1575 builder.Append(*(reinterpret_cast<const uint32_t *>(data))));
1576 }
1577 return builder.Finish(array);
1578 }
1579
1580 arrow::Status
1581 build_int32_array(std::vector<grn_id> &ids,
1582 grn_obj *grn_column,
1583 std::shared_ptr<arrow::Array> *array)
1584 {
1585 arrow::Int32Builder builder(arrow::default_memory_pool());
1586 for (auto id : ids) {
1587 uint32_t size;
1588 auto data = grn_obj_get_value_(ctx_, grn_column, id, &size);
1589 ARROW_RETURN_NOT_OK(
1590 builder.Append(*(reinterpret_cast<const int32_t *>(data))));
1591 }
1592 return builder.Finish(array);
1593 }
1594 arrow::Status
1595 build_uint64_array(std::vector<grn_id> &ids,
1596 grn_obj *grn_column,
1597 std::shared_ptr<arrow::Array> *array)
1598 {
1599 arrow::UInt64Builder builder(arrow::default_memory_pool());
1600 for (auto id : ids) {
1601 uint32_t size;
1602 auto data = grn_obj_get_value_(ctx_, grn_column, id, &size);
1603 ARROW_RETURN_NOT_OK(
1604 builder.Append(*(reinterpret_cast<const uint64_t *>(data))));
1605 }
1606 return builder.Finish(array);
1607 }
1608
1609 arrow::Status
1610 build_int64_array(std::vector<grn_id> &ids,
1611 grn_obj *grn_column,
1612 std::shared_ptr<arrow::Array> *array)
1613 {
1614 arrow::Int64Builder builder(arrow::default_memory_pool());
1615 for (auto id : ids) {
1616 uint32_t size;
1617 auto data = grn_obj_get_value_(ctx_, grn_column, id, &size);
1618 ARROW_RETURN_NOT_OK(
1619 builder.Append(*(reinterpret_cast<const int64_t *>(data))));
1620 }
1621 return builder.Finish(array);
1622 }
1623
1624 arrow::Status
1625 build_float_array(std::vector<grn_id> &ids,
1626 grn_obj *grn_column,
1627 std::shared_ptr<arrow::Array> *array)
1628 {
1629 arrow::FloatBuilder builder(arrow::default_memory_pool());
1630 for (auto id : ids) {
1631 uint32_t size;
1632 auto data = grn_obj_get_value_(ctx_, grn_column, id, &size);
1633 ARROW_RETURN_NOT_OK(
1634 builder.Append(*(reinterpret_cast<const float *>(data))));
1635 }
1636 return builder.Finish(array);
1637 }
1638
1639 arrow::Status
1640 build_double_array(std::vector<grn_id> &ids,
1641 grn_obj *grn_column,
1642 std::shared_ptr<arrow::Array> *array)
1643 {
1644 arrow::DoubleBuilder builder(arrow::default_memory_pool());
1645 for (auto id : ids) {
1646 uint32_t size;
1647 auto data = grn_obj_get_value_(ctx_, grn_column, id, &size);
1648 ARROW_RETURN_NOT_OK(
1649 builder.Append(*(reinterpret_cast<const double *>(data))));
1650 }
1651 return builder.Finish(array);
1652 }
1653
1654 arrow::Status
1655 build_timestamp_array(std::vector<grn_id> &ids,
1656 grn_obj *grn_column,
1657 std::shared_ptr<arrow::Array> *array)
1658 {
1659 auto timestamp_ns_data_type =
1660 std::make_shared<arrow::TimestampType>(arrow::TimeUnit::NANO);
1661 arrow::TimestampBuilder builder(timestamp_ns_data_type,
1662 arrow::default_memory_pool());
1663 for (auto id : ids) {
1664 uint32_t size;
1665 auto data = grn_obj_get_value_(ctx_, grn_column, id, &size);
1666 auto timestamp_micro = *(reinterpret_cast<const int64_t *>(data));
1667 ARROW_RETURN_NOT_OK(
1668 builder.Append(GRN_TIME_USEC_TO_NSEC(timestamp_micro)));
1669 }
1670 return builder.Finish(array);
1671 }
1672
1673 arrow::Status
1674 build_utf8_array(std::vector<grn_id> &ids,
1675 grn_obj *grn_column,
1676 std::shared_ptr<arrow::Array> *array)
1677 {
1678 arrow::StringBuilder builder(arrow::default_memory_pool());
1679 for (auto id : ids) {
1680 uint32_t size;
1681 auto data = grn_obj_get_value_(ctx_, grn_column, id, &size);
1682 ARROW_RETURN_NOT_OK(builder.Append(data, size));
1683 }
1684 return builder.Finish(array);
1685 }
1686
1687 arrow::Status
1688 build_binary_array(std::vector<grn_id> &ids,
1689 grn_obj *grn_column,
1690 std::shared_ptr<arrow::Array> *array)
1691 {
1692 arrow::StringBuilder builder(arrow::default_memory_pool());
1693 for (auto id : ids) {
1694 uint32_t size;
1695 auto data = grn_obj_get_value_(ctx_, grn_column, id, &size);
1696 ARROW_RETURN_NOT_OK(builder.Append(data, size));
1697 }
1698 return builder.Finish(array);
1699 }
1700 };
1701
1702 class BufferInputStream : public arrow::io::InputStream,
1703 public arrow::io::Seekable {
1704 public:
1705 BufferInputStream() : buffer_(), offset_(0), closed_(false) {}
1706
1707 ~BufferInputStream() {}
1708
1709 void
1710 feed(const char *data, size_t data_size)
1711 {
1712 if (offset_ == 0) {
1713 buffer_.append(data, data_size);
1714 } else {
1715 buffer_.erase(0, offset_);
1716 buffer_.append(data, data_size);
1717 offset_ = 0;
1718 }
1719 }
1720
1721 arrow::Status
1722 Close() override
1723 {
1724 closed_ = true;
1725 return arrow::Status::OK();
1726 }
1727
1728 arrow::Status
1729 Abort() override
1730 {
1731 closed_ = true;
1732 return arrow::Status::OK();
1733 }
1734
1735 int64_t
1736 tell() const
1737 {
1738 return offset_;
1739 }
1740
1741 arrow::Result<int64_t>
1742 Tell() const override
1743 {
1744 return tell();
1745 }
1746
1747 arrow::Status
1748 Seek(int64_t position) override
1749 {
1750 offset_ = position;
1751 return arrow::Status::OK();
1752 }
1753
1754 bool
1755 closed() const override
1756 {
1757 return closed_;
1758 }
1759
1760 std::string_view
1761 peek(int64_t nbytes)
1762 {
1763 const int64_t bytes_available =
1764 std::min(nbytes, static_cast<int64_t>(buffer_.size() - offset_));
1765 return std::string_view(buffer_.data() + offset_,
1766 static_cast<size_t>(bytes_available));
1767 }
1768
1769 arrow::Result<std::string_view>
1770 Peek(int64_t nbytes) override
1771 {
1772 return peek(nbytes);
1773 }
1774
1775 int64_t
1776 read(int64_t nbytes, void *out)
1777 {
1778 const int64_t bytes_available =
1779 std::min(nbytes, static_cast<int64_t>(buffer_.size() - offset_));
1780 if (bytes_available > 0) {
1781 grn_memcpy(out, buffer_.data() + offset_, bytes_available);
1782 offset_ += bytes_available;
1783 return bytes_available;
1784 } else {
1785 return 0;
1786 }
1787 }
1788
1789 arrow::Result<int64_t>
1790 Read(int64_t nbytes, void *out) override
1791 {
1792 return read(nbytes, out);
1793 }
1794
1795 std::shared_ptr<arrow::Buffer>
1796 read(int64_t nbytes)
1797 {
1798 const int64_t bytes_available =
1799 std::min(nbytes, static_cast<int64_t>(buffer_.size() - offset_));
1800 auto output = std::make_shared<arrow::Buffer>(
1801 reinterpret_cast<const uint8_t *>(buffer_.data() + offset_),
1802 bytes_available);
1803 offset_ += bytes_available;
1804 return output;
1805 }
1806
1807 arrow::Result<std::shared_ptr<arrow::Buffer>>
1808 Read(int64_t nbytes) override
1809 {
1810 return read(nbytes);
1811 }
1812
1813 bool
1814 supports_zero_copy() const override
1815 {
1816 return false;
1817 }
1818
1819 private:
1820 std::string buffer_;
1821 int64_t offset_;
1822 bool closed_;
1823 };
1824
1825 class StreamLoader : public arrow::ipc::Listener {
1826 public:
1827 StreamLoader(grn_ctx *ctx, grn_loader *loader)
1828 : ctx_(ctx),
1829 grn_loader_(loader),
1830 decoder_(std::shared_ptr<StreamLoader>(this, [](void *) {})),
1831 buffer_(nullptr),
1832 object_cache_(ctx_),
1833 tag_("[arrow][stream-loader]")
1834 {
1835 }
1836
1837 grn_rc
1838 consume(const char *data, size_t data_size)
1839 {
1840 if (data_size == 0) {
1841 return GRN_SUCCESS;
1842 }
1843
1844 return consume_decoder(data, data_size);
1845 }
1846
1847 arrow::Status
1848 OnRecordBatchDecoded(
1849 std::shared_ptr<arrow::RecordBatch> record_batch) override
1850 {
1851 return process_record_batch(std::move(record_batch));
1852 }
1853
1854 private:
1855 grn_rc
1856 consume_decoder(const char *data, size_t data_size)
1857 {
1858 if (!buffer_) {
1859 auto buffer = arrow::AllocateResizableBuffer(0);
1860 if (!check(ctx_,
1861 buffer,
1862 tag_ + "[consume] failed to allocate buffer")) {
1863 return ctx_->rc;
1864 }
1865 buffer_ = std::move(*buffer);
1866 }
1867
1868 auto current_buffer_size = buffer_->size();
1869 if (!check(ctx_,
1870 buffer_->Resize(current_buffer_size + data_size),
1871 tag_ + "[consume] failed to resize buffer")) {
1872 return ctx_->rc;
1873 }
1874 grn_memcpy(buffer_->mutable_data() + current_buffer_size,
1875 data,
1876 data_size);
1877
1878 if (buffer_->size() < decoder_.next_required_size()) {
1879 return ctx_->rc;
1880 }
1881
1882 std::shared_ptr<arrow::Buffer> chunk(buffer_.release());
1883 if (!check(ctx_,
1884 decoder_.Consume(chunk),
1885 tag_ + "[consume] failed to consume")) {
1886 return ctx_->rc;
1887 }
1888 return ctx_->rc;
1889 }
1890
1891 arrow::Status
1892 process_record_batch(std::shared_ptr<arrow::RecordBatch> record_batch)
1893 {
1894 const auto tag = tag_ + "[consume][record-batch-decoded]";
1895 auto grn_table = grn_loader_->table;
1896 const auto &schema = record_batch->schema();
1897 const auto key_column_index = schema->GetFieldIndex("_key");
1898 const auto id_column_index = schema->GetFieldIndex("_id");
1899 const auto n_records = record_batch->num_rows();
1900 std::vector<grn_id> record_ids;
1901 if (key_column_index != -1) {
1902 const auto &key_column = record_batch->column(key_column_index);
1903 RecordAddVisitor visitor(ctx_, grn_loader_, &record_ids, true);
1904 ARROW_RETURN_NOT_OK(key_column->Accept(&visitor));
1905 } else if (id_column_index != -1) {
1906 const auto &id_column = record_batch->column(id_column_index);
1907 RecordAddVisitor visitor(ctx_, grn_loader_, &record_ids, false);
1908 ARROW_RETURN_NOT_OK(id_column->Accept(&visitor));
1909 } else {
1910 for (int64_t i = 0; i < n_records; ++i) {
1911 const auto record_id = grn_table_add(ctx_, grn_table, NULL, 0, NULL);
1912 grn_loader_on_record_added(ctx_, grn_loader_, record_id);
1913 record_ids.push_back(record_id);
1914 }
1915 }
1916
1917 const auto n_columns = record_batch->num_columns();
1918 auto task_executor = grn_ctx_get_task_executor(ctx_);
1919 std::mutex loader_merge_mutex;
1920 for (int i = 0; i < n_columns; ++i) {
1921 if (i == key_column_index || i == id_column_index) {
1922 continue;
1923 }
1924 const auto id = static_cast<uintptr_t>(i);
1925 const auto &column_name = schema->field(i)->name();
1926 const auto column_tag =
1927 tag + "[" + column_name + "(" + std::to_string(i) + ")]";
1928 auto *grn_column = grn_loader_get_column(ctx_,
1929 grn_loader_,
1930 column_name.data(),
1931 column_name.size());
1932 auto execute = [&, this, i, grn_column]() {
1933 const auto &column = record_batch->column(i);
1934 const auto &field = schema->field(i);
1935 grn_ctx *ctx = ctx_;
1936 grn_ctx *child_ctx = nullptr;
1937 grn_loader *loader = grn_loader_;
1938 if (task_executor->is_parallel()) {
1939 ctx = child_ctx = grn_ctx_pull_child(ctx_);
1940 loader = &(child_ctx->impl->loader);
1941 loader->table = grn_table;
1942 }
1943 grn::ChildCtxReleaser releaser(ctx_, child_ctx);
1944 ColumnLoadVisitor visitor(ctx,
1945 loader,
1946 grn_table,
1947 grn_column,
1948 field,
1949 record_ids.data(),
1950 &object_cache_);
1951 auto status = column->Accept(&visitor);
1952 if (child_ctx) {
1953 {
1954 std::lock_guard<std::mutex> lock(loader_merge_mutex);
1955 grn_loader_merge(ctx, grn_loader_, loader);
1956 }
1957 grn_ctx_loader_clear(child_ctx);
1958 }
1959 return grnarrow::check(ctx_, status, column_tag);
1960 };
1961 task_executor->execute(id, execute, column_tag.c_str());
1962 }
1963 task_executor->wait_all();
1964 for (const auto record_id : record_ids) {
1965 if (record_id == GRN_ID_NIL) {
1966 continue;
1967 }
1968 grn_loader_apply_each(ctx_, grn_loader_, record_id);
1969 }
1970 return check(ctx_, ctx_->rc, tag);
1971 }
1972
1973 grn_ctx *ctx_;
1974 grn_loader *grn_loader_;
1975 arrow::ipc::StreamDecoder decoder_;
1976 std::unique_ptr<arrow::ResizableBuffer> buffer_;
1977 ObjectCache object_cache_;
1978 std::string tag_;
1979 };
1980
1981 class BulkOutputStream : public arrow::io::OutputStream {
1982 public:
1983 BulkOutputStream(grn_ctx *ctx, grn_obj *bulk)
1984 : arrow::io::OutputStream(),
1985 ctx_(ctx),
1986 bulk_(bulk),
1987 position_(0),
1988 is_open_(true)
1989 {
1990 }
1991
1992 ~BulkOutputStream() override {}
1993
1994 grn_obj *
1995 bulk() const
1996 {
1997 return bulk_;
1998 }
1999
2000 arrow::Status
2001 Close() override
2002 {
2003 is_open_ = false;
2004 return arrow::Status::OK();
2005 }
2006
2007 bool
2008 closed() const override
2009 {
2010 return !is_open_;
2011 }
2012
2013 arrow::Result<int64_t>
2014 Tell() const override
2015 {
2016 return position_;
2017 }
2018
2019 arrow::Status
2020 Write(const void *data, int64_t n_bytes) override
2021 {
2022 if (ARROW_PREDICT_FALSE(!is_open_)) {
2023 return arrow::Status::IOError("BulkOutputStream is closed");
2024 }
2025 if (ARROW_PREDICT_TRUE(n_bytes > 0)) {
2026 auto rc =
2027 grn_bulk_write(ctx_, bulk_, static_cast<const char *>(data), n_bytes);
2028 if (ARROW_PREDICT_TRUE(rc == GRN_SUCCESS)) {
2029 position_ += n_bytes;
2030 return arrow::Status::OK();
2031 } else {
2032 return check(ctx_, rc, "[arrow][bulk-output-stream][write]");
2033 }
2034 } else {
2035 return arrow::Status::OK();
2036 }
2037 }
2038
2039 using arrow::io::OutputStream::Write;
2040
2041 private:
2042 grn_ctx *ctx_;
2043 grn_obj *bulk_;
2044 int64_t position_;
2045 bool is_open_;
2046 };
2047
2048 class StreamWriter {
2049 public:
2050 StreamWriter(grn_ctx *ctx, grn_obj *bulk)
2051 : ctx_(ctx),
2052 output_(ctx, bulk),
2053 metadata_(),
2054 schema_builder_(),
2055 schema_(),
2056 writer_(),
2057 record_batch_builder_(),
2058 n_records_(0),
2059 current_column_index_(0),
2060 current_child_index_(-1),
2061 object_cache_(ctx_),
2062 tag_("[arrow][stream-writer]")
2063 {
2064 }
2065
2066 ~StreamWriter()
2067 {
2068 flush();
2069 if (writer_) {
2070 std::ignore = writer_->Close();
2071 }
2072 std::ignore = output_.Close();
2073 }
2074
2075 void
2076 add_metadata(const char *key, const char *value)
2077 {
2078 metadata_.Append(key, value);
2079 // It is named AddMetadata(), but it "set" metadata, not "add" it.
2080 // This behavior may be changed.
2081 // See also: https://github.com/apache/arrow/issues/46146
2082 auto status = schema_builder_.AddMetadata(metadata_);
2083 if (!status.ok()) {
2084 std::stringstream context;
2085 check(ctx_,
2086 status,
2087 context << tag_ << "[add-meatadata] "
2088 << "failed to add metadata: <" << key << ">: <" << value
2089 << ">");
2090 }
2091 }
2092
2093 void
2094 add_field(const char *name, grn_obj *column)
2095 {
2096 auto type = grn_column_to_arrow_type(ctx_, column, object_cache_);
2097 if (!type) {
2098 auto ctx = ctx_;
2099 grn_obj inspected;
2100 GRN_TEXT_INIT(&inspected, 0);
2101 grn_inspect(ctx_, &inspected, column);
2102 ERR(GRN_FUNCTION_NOT_IMPLEMENTED,
2103 "%s[add-field] unsupported column: <%.*s>",
2104 tag_.c_str(),
2105 (int)GRN_TEXT_LEN(&inspected),
2106 GRN_TEXT_VALUE(&inspected));
2107 GRN_OBJ_FIN(ctx_, &inspected);
2108 return;
2109 }
2110 auto field = arrow::field(name, type);
2111 auto status = schema_builder_.AddField(field);
2112 if (!status.ok()) {
2113 std::stringstream context;
2114 check(ctx_,
2115 status,
2116 context << tag_ << "[add-field] "
2117 << "failed to add field: <" << field->ToString() << ">");
2118 }
2119 }
2120
2121 void
2122 add_field_text_dictionary(const char *name, grn_obj *index_type)
2123 {
2124 auto arrow_index_type =
2125 grn_column_to_arrow_type(ctx_, index_type, object_cache_);
2126 if (!arrow_index_type) {
2127 auto ctx = ctx_;
2128 grn_obj inspected;
2129 GRN_TEXT_INIT(&inspected, 0);
2130 grn_inspect(ctx_, &inspected, index_type);
2131 ERR(GRN_FUNCTION_NOT_IMPLEMENTED,
2132 "%s[add-field-text-dictionary] unsupported index type: <%.*s>",
2133 tag_.c_str(),
2134 (int)GRN_TEXT_LEN(&inspected),
2135 GRN_TEXT_VALUE(&inspected));
2136 GRN_OBJ_FIN(ctx_, &inspected);
2137 return;
2138 }
2139 auto type = arrow::dictionary(arrow_index_type, arrow::utf8());
2140 auto field = arrow::field(name, type);
2141 auto status = schema_builder_.AddField(field);
2142 if (!status.ok()) {
2143 std::stringstream context;
2144 check(ctx_,
2145 status,
2146 context << tag_ << "[add-field-text-dictionary] "
2147 << "failed to add field: <" << field->ToString() << ">");
2148 }
2149 }
2150
2151 void
2152 add_field_union(const char *name, grn_obj **columns, size_t n_columns)
2153 {
2154 std::vector<std::shared_ptr<arrow::Field>> fields;
2155 for (size_t i = 0; i < n_columns; ++i) {
2156 auto column = columns[i];
2157 auto type = grn_column_to_arrow_type(ctx_, column, object_cache_);
2158 if (!type) {
2159 auto ctx = ctx_;
2160 grn_obj inspected;
2161 GRN_TEXT_INIT(&inspected, 0);
2162 grn_inspect(ctx_, &inspected, column);
2163 ERR(GRN_FUNCTION_NOT_IMPLEMENTED,
2164 "%s[add-field-union] unsupported column: <%.*s>",
2165 tag_.c_str(),
2166 (int)GRN_TEXT_LEN(&inspected),
2167 GRN_TEXT_VALUE(&inspected));
2168 GRN_OBJ_FIN(ctx_, &inspected);
2169 return;
2170 }
2171 fields.emplace_back(arrow::field(std::to_string(i), std::move(type)));
2172 }
2173 auto field = arrow::field(name, arrow::dense_union(std::move(fields)));
2174 auto status = schema_builder_.AddField(field);
2175 if (!status.ok()) {
2176 std::stringstream context;
2177 check(ctx_,
2178 status,
2179 context << tag_ << "[add-field[union] "
2180 << "failed to add field: <" << field->ToString() << ">");
2181 }
2182 }
2183
2184 void
2185 write_schema()
2186 {
2187 auto schema_result = schema_builder_.Finish();
2188 if (!check(ctx_,
2189 schema_result,
2190 tag_ + "[write-schema] failed to create schema")) {
2191 return;
2192 }
2193 schema_builder_.Reset();
2194 schema_ = *schema_result;
2195
2196 auto option = arrow::ipc::IpcWriteOptions::Defaults();
2197 option.emit_dictionary_deltas = true;
2198 auto writer_result =
2199 arrow::ipc::MakeStreamWriter(&output_, schema_, option);
2200
2201 if (!check(ctx_,
2202 writer_result,
2203 tag_ + "[write-schema] failed to create writer")) {
2204 return;
2205 }
2206 writer_ = *writer_result;
2207
2208 auto record_batch_builder_result =
2209 arrow::RecordBatchBuilder::Make(schema_, arrow::default_memory_pool());
2210 if (record_batch_builder_result.ok()) {
2211 record_batch_builder_.swap(*record_batch_builder_result);
2212 }
2213 auto status = record_batch_builder_result.status();
2214 check(ctx_,
2215 status,
2216 tag_ + "[write-schema] failed to create record batch builder");
2217 }
2218
2219 void
2220 open_record()
2221 {
2222 current_column_index_ = 0;
2223 current_child_index_ = -1;
2224 }
2225
2226 void
2227 close_record()
2228 {
2229 n_records_++;
2230 if (n_records_ == grn_output_auto_flush_interval) {
2231 flush();
2232 }
2233 }
2234
2235 void
2236 add_column_null()
2237 {
2238 auto column_builder = fetch_current_column_builder<arrow::ArrayBuilder>();
2239 auto status = column_builder->AppendNull();
2240 if (status.ok()) {
2241 return;
2242 }
2243 std::stringstream context;
2244 check(ctx_,
2245 status,
2246 add_column_error_message(context, "null") << ": null");
2247 }
2248
2249 void
2250 add_column_text(const char *value, size_t value_length)
2251 {
2252 auto column_builder =
2253 fetch_current_column_builder<arrow::StringBuilder>();
2254 auto status = column_builder->Append(value, value_length);
2255 if (status.ok()) {
2256 return;
2257 }
2258 std::stringstream context;
2259 check(ctx_,
2260 status,
2261 add_column_error_message(context, "text")
2262 << "<" << std::string_view(value, value_length) << ">");
2263 }
2264
2265 void
2266 add_column_text_dictionary(const char *value, size_t value_length)
2267 {
2268 auto column_builder =
2269 fetch_current_column_builder<arrow::StringDictionaryBuilder>();
2270 auto status = column_builder->Append(value, value_length);
2271 if (status.ok()) {
2272 return;
2273 }
2274 std::stringstream context;
2275 check(ctx_,
2276 status,
2277 add_column_error_message(context, "text-dictionary")
2278 << "<" << std::string_view(value, value_length) << ">");
2279 }
2280
2281 void
2282 add_column_binary(const uint8_t *value, size_t value_length)
2283 {
2284 auto column_builder =
2285 fetch_current_column_builder<arrow::BinaryBuilder>();
2286 auto status = column_builder->Append(value, value_length);
2287 if (status.ok()) {
2288 return;
2289 }
2290 std::stringstream context;
2291 check(ctx_,
2292 status,
2293 add_column_error_message(context, "binary")
2294 << "<"
2295 << arrow::util::base64_encode(
2296 std::string_view(reinterpret_cast<const char *>(value),
2297 value_length))
2298 << ">");
2299 }
2300
2301 void
2302 add_column_int8(int8_t value)
2303 {
2304 auto column_builder = fetch_current_column_builder<arrow::Int8Builder>();
2305 auto status = column_builder->Append(value);
2306 if (status.ok()) {
2307 return;
2308 }
2309 std::stringstream context;
2310 check(ctx_,
2311 status,
2312 add_column_error_message(context, "int8") << "<" << value << ">");
2313 }
2314
2315 void
2316 add_column_uint16(uint16_t value)
2317 {
2318 auto column_builder =
2319 fetch_current_column_builder<arrow::UInt16Builder>();
2320 auto status = column_builder->Append(value);
2321 if (status.ok()) {
2322 return;
2323 }
2324 std::stringstream context;
2325 check(ctx_,
2326 status,
2327 add_column_error_message(context, "uint16") << "<" << value << ">");
2328 }
2329
2330 void
2331 add_column_int32(int32_t value)
2332 {
2333 auto column_builder = fetch_current_column_builder<arrow::Int32Builder>();
2334 auto status = column_builder->Append(value);
2335 if (status.ok()) {
2336 return;
2337 }
2338 std::stringstream context;
2339 check(ctx_,
2340 status,
2341 add_column_error_message(context, "int32") << "<" << value << ">");
2342 }
2343
2344 void
2345 add_column_uint32(uint32_t value)
2346 {
2347 auto column_builder =
2348 fetch_current_column_builder<arrow::UInt32Builder>();
2349 auto status = column_builder->Append(value);
2350 if (status.ok()) {
2351 return;
2352 }
2353 std::stringstream context;
2354 check(ctx_,
2355 status,
2356 add_column_error_message(context, "uint32") << "<" << value << ">");
2357 }
2358
2359 void
2360 add_column_int64(int64_t value)
2361 {
2362 auto column_builder = fetch_current_column_builder<arrow::Int64Builder>();
2363 auto status = column_builder->Append(value);
2364 if (status.ok()) {
2365 return;
2366 }
2367 std::stringstream context;
2368 check(ctx_,
2369 status,
2370 add_column_error_message(context, "int64") << "<" << value << ">");
2371 }
2372
2373 void
2374 add_column_uint64(int64_t value)
2375 {
2376 auto column_builder =
2377 fetch_current_column_builder<arrow::UInt64Builder>();
2378 auto status = column_builder->Append(value);
2379 if (status.ok()) {
2380 return;
2381 }
2382 std::stringstream context;
2383 check(ctx_,
2384 status,
2385 add_column_error_message(context, "uint64") << "<" << value << ">");
2386 }
2387
2388 void
2389 add_column_float32(float value)
2390 {
2391 auto column_builder = fetch_current_column_builder<arrow::FloatBuilder>();
2392 auto status = column_builder->Append(value);
2393 if (status.ok()) {
2394 return;
2395 }
2396 std::stringstream context;
2397 check(ctx_,
2398 status,
2399 add_column_error_message(context, "float32")
2400 << "<" << value << ">");
2401 }
2402
2403 void
2404 add_column_float(double value)
2405 {
2406 auto column_builder =
2407 fetch_current_column_builder<arrow::DoubleBuilder>();
2408 auto status = column_builder->Append(value);
2409 if (status.ok()) {
2410 return;
2411 }
2412 std::stringstream context;
2413 check(ctx_,
2414 status,
2415 add_column_error_message(context, "float") << "<" << value << ">");
2416 }
2417
2418 void
2419 add_column_timestamp(grn_timeval value)
2420 {
2421 auto column_builder =
2422 fetch_current_column_builder<arrow::TimestampBuilder>();
2423 auto status = column_builder->Append(GRN_TIMEVAL_TO_NSEC(&value));
2424 if (status.ok()) {
2425 return;
2426 }
2427 std::stringstream context;
2428 check(ctx_,
2429 status,
2430 add_column_error_message(context, "timestamp")
2431 << "<"
2432 << (value.tv_sec + (value.tv_nsec / GRN_TIME_NSEC_PER_SEC_F))
2433 << ">");
2434 }
2435
2436 void
2437 add_column_record(grn_obj *record)
2438 {
2439 auto table = object_cache_[record->header.domain];
2440 char key[GRN_TABLE_MAX_KEY_SIZE];
2441 auto key_size = grn_table_get_key(ctx_,
2442 table,
2443 GRN_RECORD_VALUE(record),
2444 key,
2445 sizeof(key));
2446 switch (table->header.domain) {
2447 case GRN_DB_INT32:
2448 add_column_int32(*reinterpret_cast<int32_t *>(key));
2449 return;
2450 case GRN_DB_INT64:
2451 add_column_int64(*reinterpret_cast<int64_t *>(key));
2452 return;
2453 default:
2454 break;
2455 }
2456 auto column_builder =
2457 fetch_current_column_builder<arrow::StringDictionaryBuilder>();
2458 auto status = column_builder->Append(std::string_view(key, key_size));
2459 if (status.ok()) {
2460 return;
2461 }
2462 std::stringstream context;
2463 grn_obj inspected;
2464 GRN_TEXT_INIT(&inspected, 0);
2465 grn_inspect(ctx_, &inspected, record);
2466 check(ctx_,
2467 status,
2468 add_column_error_message(context, "record")
2469 << "<"
2470 << std::string_view(GRN_TEXT_VALUE(&inspected),
2471 GRN_TEXT_LEN(&inspected))
2472 << ">");
2473 GRN_OBJ_FIN(ctx_, &inspected);
2474 }
2475
2476 void
2477 add_column_uvector(grn_obj *uvector)
2478 {
2479 auto column_builder = fetch_current_column_builder<arrow::ListBuilder>();
2480 auto status = column_builder->Append();
2481 if (status.ok()) {
2482 auto domain = object_cache_[uvector->header.domain];
2483 auto raw_elements = GRN_BULK_HEAD(uvector);
2484 auto element_size = grn_uvector_element_size(ctx_, uvector);
2485 auto n = GRN_BULK_VSIZE(uvector) / element_size;
2486 auto raw_value_builder = column_builder->value_builder();
2487 if (grn_obj_is_table_with_key(ctx_, domain)) {
2488 auto value_builder =
2489 static_cast<arrow::StringDictionaryBuilder *>(raw_value_builder);
2490 for (size_t i = 0; i < n; ++i) {
2491 auto record_id =
2492 *reinterpret_cast<grn_id *>(raw_elements + (element_size * i));
2493 char key[GRN_TABLE_MAX_KEY_SIZE];
2494 auto key_size =
2495 grn_table_get_key(ctx_, domain, record_id, key, sizeof(key));
2496 status = value_builder->Append(std::string_view(key, key_size));
2497 if (!status.ok()) {
2498 break;
2499 }
2500 }
2501 } else {
2502 auto value_builder =
2503 static_cast<arrow::Int32Builder *>(raw_value_builder);
2504 for (size_t i = 0; i < n; ++i) {
2505 // TODO: check type
2506 auto element =
2507 *reinterpret_cast<int32_t *>(raw_elements + (element_size * i));
2508 status = value_builder->Append(element);
2509 if (!status.ok()) {
2510 break;
2511 }
2512 }
2513 }
2514 }
2515 if (status.ok()) {
2516 return;
2517 }
2518 std::stringstream context;
2519 grn_obj inspected;
2520 GRN_TEXT_INIT(&inspected, 0);
2521 grn_inspect(ctx_, &inspected, uvector);
2522 check(ctx_,
2523 status,
2524 add_column_error_message(context, "uvector")
2525 << "<"
2526 << std::string_view(GRN_TEXT_VALUE(&inspected),
2527 GRN_TEXT_LEN(&inspected))
2528 << ">");
2529 GRN_OBJ_FIN(ctx_, &inspected);
2530 }
2531
2532 void
2533 add_column_union(int8_t type)
2534 {
2535 auto column_builder =
2536 fetch_current_column_builder<arrow::DenseUnionBuilder>();
2537 auto status = column_builder->Append(type);
2538 if (status.ok()) {
2539 current_child_index_ = type;
2540 return;
2541 }
2542 std::stringstream context;
2543 check(ctx_,
2544 status,
2545 add_column_error_message(context, "union") << "<" << type << ">");
2546 }
2547
2548 void
2549 flush()
2550 {
2551 if (n_records_ == 0) {
2552 return;
2553 }
2554
2555 std::shared_ptr<arrow::RecordBatch> record_batch;
2556 auto record_batch_result = record_batch_builder_->Flush();
2557 if (record_batch_result.ok()) {
2558 record_batch = *record_batch_result;
2559 }
2560 auto status = record_batch_result.status();
2561 if (check(ctx_, status, tag_ + "[flush] failed to flush record batch")) {
2562 status = writer_->WriteRecordBatch(*record_batch);
2563 check(ctx_,
2564 status,
2565 tag_ + "[flush] failed to write flushed record batch");
2566 }
2567
2568 auto n_fields = record_batch_builder_->num_fields();
2569 for (int i = 0; i < n_fields; ++i) {
2570 reset_full(record_batch_builder_->GetField(i));
2571 }
2572
2573 n_records_ = 0;
2574 }
2575
2576 private:
2577 grn_ctx *ctx_;
2578 BulkOutputStream output_;
2579 arrow::KeyValueMetadata metadata_;
2580 arrow::SchemaBuilder schema_builder_;
2581 std::shared_ptr<arrow::Schema> schema_;
2582 std::shared_ptr<arrow::ipc::RecordBatchWriter> writer_;
2583 std::unique_ptr<arrow::RecordBatchBuilder> record_batch_builder_;
2584 size_t n_records_;
2585 int current_column_index_;
2586 int current_child_index_;
2587 ObjectCache object_cache_;
2588 std::string tag_;
2589
2590 std::ostream &
2591 add_column_error_message(std::ostream &context, const std::string &type)
2592 {
2593 return context << tag_ << "[add-column]["
2594 << schema_->field(current_column_index_ - 1)->name()
2595 << "][" << type << "] "
2596 << "failed to add a column value: ";
2597 }
2598
2599 // This is a destructive method. This returns the current column
2600 // builder and increment the internal counter. The next call
2601 // returns the next column builder.
2602 template <typename ArrayBuilderType>
2603 ArrayBuilderType *
2604 fetch_current_column_builder()
2605 {
2606 if (current_child_index_ == -1) {
2607 return record_batch_builder_->GetFieldAs<ArrayBuilderType>(
2608 current_column_index_++);
2609 } else {
2610 auto array_builder =
2611 record_batch_builder_->GetField(current_column_index_ - 1);
2612 auto target_builder = static_cast<ArrayBuilderType *>(
2613 array_builder->child(current_child_index_));
2614 current_child_index_ = -1;
2615 return target_builder;
2616 }
2617 }
2618
2619 // We need to execute ResetFull() with an appropriate dictionary length
2620 // in order to avoid memory exhaustion and so on.
2621 // - Dictionary data in DictionaryBuilder are held in memory until
2622 // ResetFull() is executed.
2623 // - Some Apache Arrow implementations (clients) have a 2GB limitation on
2624 // the value size of array: C#.
2625 void
2626 reset_full(arrow::ArrayBuilder *builder)
2627 {
2628 switch (builder->type()->id()) {
2629 case arrow::Type::DICTIONARY:
2630 {
2631 const int64_t dictionary_length_threshold = 10000;
2632
2633 // The value type of Dictionary is always string for now.
2634 auto dictionary_builder =
2635 static_cast<arrow::StringDictionaryBuilder *>(builder);
2636 if (dictionary_builder->dictionary_length() >
2637 dictionary_length_threshold) {
2638 dictionary_builder->ResetFull();
2639 }
2640 }
2641 break;
2642 case arrow::Type::LIST:
2643 {
2644 auto list_builder = static_cast<arrow::ListBuilder *>(builder);
2645 auto value_builder = list_builder->value_builder();
2646 reset_full(value_builder);
2647 }
2648 break;
2649 default:
2650 break;
2651 }
2652 }
2653 };
2654 } // namespace grnarrow
2655
2656 namespace grn {
2657 namespace arrow {
2658 struct ArrayBuilder::Impl {
2659 struct ColumnAppender : ::arrow::TypeVisitor {
2660 grn_ctx *ctx_;
2661 grn_obj *column_;
2662 grn_table_cursor *cursor_;
2663 ::arrow::ArrayBuilder *builder_;
2664
2665 ColumnAppender(grn_ctx *ctx,
2666 grn_obj *column,
2667 grn_table_cursor *cursor,
2668 ::arrow::ArrayBuilder *builder)
2669 : ctx_(ctx),
2670 column_(column),
2671 cursor_(cursor),
2672 builder_(builder)
2673 {
2674 }
2675
2676 # define VISIT(TYPE) \
2677 ::arrow::Status Visit(const TYPE &type) override { return Append(type); }
2678
2679 VISIT(::arrow::BooleanType)
2680 VISIT(::arrow::Int8Type)
2681 VISIT(::arrow::UInt8Type)
2682 VISIT(::arrow::Int16Type)
2683 VISIT(::arrow::UInt16Type)
2684 VISIT(::arrow::Int32Type)
2685 VISIT(::arrow::UInt32Type)
2686 VISIT(::arrow::Int64Type)
2687 VISIT(::arrow::UInt64Type)
2688 VISIT(::arrow::FloatType)
2689 VISIT(::arrow::DoubleType)
2690 VISIT(::arrow::StringType)
2691 VISIT(::arrow::BinaryType)
2692 VISIT(::arrow::TimestampType)
2693
2694 # undef VISIT
2695
2696 template <typename Type>
2697 ::arrow::Status
2698 Append(const Type &type)
2699 {
2700 using Builder = typename ::arrow::TypeTraits<Type>::BuilderType;
2701 using CType = typename ::arrow::TypeTraits<Type>::CType;
2702 auto builder = static_cast<Builder *>(builder_);
2703 grn_id id;
2704 while ((id = grn_table_cursor_next(ctx_, cursor_)) != GRN_ID_NIL) {
2705 uint32_t size;
2706 auto raw_data = grn_obj_get_value_(ctx_, column_, id, &size);
2707 const auto data = *reinterpret_cast<const CType *>(raw_data);
2708 ARROW_RETURN_NOT_OK(builder->Append(data));
2709 }
2710 return ::arrow::Status::OK();
2711 }
2712
2713 ::arrow::Status
2714 Append(const ::arrow::StringType &type)
2715 {
2716 using Builder = ::arrow::StringBuilder;
2717 auto builder = static_cast<Builder *>(builder_);
2718 grn_id id;
2719 while ((id = grn_table_cursor_next(ctx_, cursor_)) != GRN_ID_NIL) {
2720 uint32_t size;
2721 auto raw_data = grn_obj_get_value_(ctx_, column_, id, &size);
2722 ARROW_RETURN_NOT_OK(builder->Append(raw_data, size));
2723 }
2724 return ::arrow::Status::OK();
2725 }
2726
2727 ::arrow::Status
2728 Append(const ::arrow::BinaryType &type)
2729 {
2730 using Builder = ::arrow::BinaryBuilder;
2731 auto builder = static_cast<Builder *>(builder_);
2732 grn_id id;
2733 while ((id = grn_table_cursor_next(ctx_, cursor_)) != GRN_ID_NIL) {
2734 uint32_t size;
2735 auto raw_data = grn_obj_get_value_(ctx_, column_, id, &size);
2736 ARROW_RETURN_NOT_OK(builder->Append(raw_data, size));
2737 }
2738 return ::arrow::Status::OK();
2739 }
2740 };
2741
2742 grn_ctx *ctx_;
2743 std::unique_ptr<::arrow::ArrayBuilder> builder_;
2744 grnarrow::ObjectCache object_cache_;
2745
2746 Impl(grn_ctx *ctx) : ctx_(ctx), builder_(nullptr), object_cache_(ctx_) {}
2747
2748 ::arrow::Status
2749 add_column(grn_obj *column, grn_table_cursor *cursor)
2750 {
2751 auto arrow_type = grn_column_to_arrow_type(ctx_, column, object_cache_);
2752 if (!arrow_type) {
2753 grn::TextBulk inspected(ctx_);
2754 grn_inspect(ctx_, *inspected, column);
2755 return ::arrow::Status::NotImplemented(
2756 "[arrow][array-builder][add-column] "
2757 "unsupported column: ",
2758 inspected.value());
2759 }
2760 arrow_type = grnarrow::undictionary(arrow_type);
2761 if (!builder_) {
2762 ARROW_RETURN_NOT_OK(
2763 ::arrow::MakeBuilder(::arrow::default_memory_pool(),
2764 arrow_type,
2765 &builder_));
2766 }
2767 ColumnAppender appender(ctx_, column, cursor, builder_.get());
2768 return arrow_type->Accept(&appender);
2769 }
2770
2771 ::arrow::Result<std::shared_ptr<::arrow::Array>>
2772 finish()
2773 {
2774 return builder_->Finish();
2775 }
2776 };
2777
2778 ArrayBuilder::ArrayBuilder(grn_ctx *ctx) : impl_(new Impl(ctx)) {}
2779
2780 ArrayBuilder::~ArrayBuilder() = default;
2781
2782 ::arrow::Status
2783 ArrayBuilder::add_column(grn_obj *column, grn_table_cursor *cursor)
2784 {
2785 return impl_->add_column(column, cursor);
2786 }
2787
2788 ::arrow::Result<std::shared_ptr<::arrow::Array>>
2789 ArrayBuilder::finish()
2790 {
2791 return impl_->finish();
2792 }
2793
2794 namespace {
2795 class ArrayValueGetter : public ::arrow::ArrayVisitor {
2796 public:
2797 ArrayValueGetter(grn_ctx *ctx, int64_t index, grn_obj *value)
2798 : ctx_(ctx),
2799 index_(index),
2800 value_(value)
2801 {
2802 }
2803
2804 ::arrow::Status
2805 Visit(const ::arrow::BooleanArray &array) override
2806 {
2807 GRN_BOOL_PUT(ctx_, value_, array.Value(index_));
2808 return ::arrow::Status::OK();
2809 }
2810
2811 ::arrow::Status
2812 Visit(const ::arrow::Int8Array &array) override
2813 {
2814 GRN_INT8_PUT(ctx_, value_, array.Value(index_));
2815 return ::arrow::Status::OK();
2816 }
2817
2818 ::arrow::Status
2819 Visit(const ::arrow::UInt8Array &array) override
2820 {
2821 GRN_UINT8_PUT(ctx_, value_, array.Value(index_));
2822 return ::arrow::Status::OK();
2823 }
2824
2825 ::arrow::Status
2826 Visit(const ::arrow::Int16Array &array) override
2827 {
2828 GRN_INT16_PUT(ctx_, value_, array.Value(index_));
2829 return ::arrow::Status::OK();
2830 }
2831
2832 ::arrow::Status
2833 Visit(const ::arrow::UInt16Array &array) override
2834 {
2835 GRN_UINT16_PUT(ctx_, value_, array.Value(index_));
2836 return ::arrow::Status::OK();
2837 }
2838
2839 ::arrow::Status
2840 Visit(const ::arrow::Int32Array &array) override
2841 {
2842 GRN_INT32_PUT(ctx_, value_, array.Value(index_));
2843 return ::arrow::Status::OK();
2844 }
2845
2846 ::arrow::Status
2847 Visit(const ::arrow::UInt32Array &array) override
2848 {
2849 GRN_UINT32_PUT(ctx_, value_, array.Value(index_));
2850 return ::arrow::Status::OK();
2851 }
2852
2853 ::arrow::Status
2854 Visit(const ::arrow::Int64Array &array) override
2855 {
2856 GRN_INT64_PUT(ctx_, value_, array.Value(index_));
2857 return ::arrow::Status::OK();
2858 }
2859
2860 ::arrow::Status
2861 Visit(const ::arrow::UInt64Array &array) override
2862 {
2863 GRN_UINT64_PUT(ctx_, value_, array.Value(index_));
2864 return ::arrow::Status::OK();
2865 }
2866
2867 ::arrow::Status
2868 Visit(const ::arrow::TimestampArray &array) override
2869 {
2870 const auto &arrow_timestamp_type =
2871 std::static_pointer_cast<::arrow::TimestampType>(array.type());
2872 const auto time_unit = arrow_timestamp_type->unit();
2873 grnarrow::put_time_value(ctx_,
2874 value_,
2875 array.Value(index_),
2876 time_unit);
2877 return ::arrow::Status::OK();
2878 }
2879
2880 ::arrow::Status
2881 Visit(const ::arrow::StringArray &array) override
2882 {
2883 auto raw_value = array.GetView(index_);
2884 grn_bulk_write(ctx_, value_, raw_value.data(), raw_value.length());
2885 return ::arrow::Status::OK();
2886 }
2887
2888 ::arrow::Status
2889 Visit(const ::arrow::BinaryArray &array) override
2890 {
2891 auto raw_value = array.GetView(index_);
2892 grn_bulk_write(ctx_, value_, raw_value.data(), raw_value.length());
2893 return ::arrow::Status::OK();
2894 }
2895
2896 private:
2897 grn_ctx *ctx_;
2898 int64_t index_;
2899 grn_obj *value_;
2900 };
2901 } // namespace
2902
2903 grn_rc
2904 get_value(grn_ctx *ctx,
2905 const ::arrow::Array *array,
2906 int64_t index,
2907 grn_obj *value)
2908 {
2909 ArrayValueGetter getter(ctx, index, value);
2910 auto status = array->Accept(&getter);
2911 grnarrow::check(ctx, status, "[arrow][value][get] failed");
2912 return ctx->rc;
2913 }
2914 } // namespace arrow
2915 } // namespace grn
2916 #endif /* GRN_WITH_APACHE_ARROW */
2917
2918 extern "C" {
2919 void
2920 grn_arrow_init_external_libraries(void)
2921 {
2922 #ifdef GRN_WITH_APACHE_ARROW
2923 # if ARROW_VERSION_MAJOR >= 21
2924 auto status = arrow::compute::Initialize();
2925 if (!status.ok()) {
2926 // TODO: How to report this error? Logger isn't initialized at
2927 // this time.
2928 }
2929 # endif
2930 #endif
2931 }
2932
2933 grn_rc
2934 grn_arrow_load(grn_ctx *ctx, grn_obj *table, const char *path)
2935 {
2936 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
2937 #ifdef GRN_WITH_APACHE_ARROW
2938 auto input_result =
2939 arrow::io::MemoryMappedFile::Open(path, arrow::io::FileMode::READ);
2940 std::ostringstream context;
2941 if (!grnarrow::check(ctx,
2942 input_result,
2943 context << "[arrow][load] failed to open path: "
2944 << "<" << path << ">")) {
2945 GRN_API_RETURN(ctx->rc);
2946 }
2947 auto input = *input_result;
2948 auto reader_result = arrow::ipc::RecordBatchFileReader::Open(input);
2949 if (!grnarrow::check(ctx,
2950 reader_result,
2951 "[arrow][load] "
2952 "failed to create file format reader")) {
2953 GRN_API_RETURN(ctx->rc);
2954 }
2955 auto reader = *reader_result;
2956
2957 grnarrow::FileLoader loader(ctx, table);
2958 int n_record_batches = reader->num_record_batches();
2959 for (int i = 0; i < n_record_batches; ++i) {
2960 auto record_batch_result = reader->ReadRecordBatch(i);
2961 std::ostringstream context;
2962 if (!grnarrow::check(ctx,
2963 record_batch_result,
2964 context << "[arrow][load] failed to get "
2965 << "the " << i << "-th "
2966 << "record")) {
2967 break;
2968 }
2969 auto record_batch = *record_batch_result;
2970 loader.load_record_batch(record_batch);
2971 if (ctx->rc != GRN_SUCCESS) {
2972 break;
2973 }
2974 }
2975 #else /* GRN_WITH_APACHE_ARROW */
2976 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
2977 "[arrow][load] Apache Arrow support isn't enabled");
2978 #endif /* GRN_WITH_APACHE_ARROW */
2979 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
2980 }
2981
2982 grn_rc
2983 grn_arrow_dump(grn_ctx *ctx, grn_obj *table, const char *path)
2984 {
2985 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
2986 #ifdef GRN_WITH_APACHE_ARROW
2987 auto all_columns = grn_hash_create(ctx,
2988 NULL,
2989 sizeof(grn_id),
2990 0,
2991 GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY);
2992 grn_table_columns(ctx,
2993 table,
2994 "",
2995 0,
2996 reinterpret_cast<grn_obj *>(all_columns));
2997
2998 grnarrow::ObjectCache object_cache(ctx);
2999 grn_obj columns;
3000 GRN_PTR_INIT(&columns, GRN_OBJ_VECTOR, GRN_ID_NIL);
3001 GRN_HASH_EACH_BEGIN(ctx, all_columns, cursor, id)
3002 {
3003 void *key;
3004 grn_hash_cursor_get_key(ctx, cursor, &key);
3005 auto column_id = static_cast<grn_id *>(key);
3006 auto column = object_cache[*column_id];
3007 GRN_PTR_PUT(ctx, &columns, column);
3008 }
3009 GRN_HASH_EACH_END(ctx, cursor);
3010 grn_hash_close(ctx, all_columns);
3011
3012 grn_arrow_dump_columns(ctx, table, &columns, path);
3013 GRN_OBJ_FIN(ctx, &columns);
3014 #else /* GRN_WITH_APACHE_ARROW */
3015 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3016 "[arrow][dump] Apache Arrow support isn't enabled");
3017 #endif /* GRN_WITH_APACHE_ARROW */
3018 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3019 }
3020
3021 grn_rc
3022 grn_arrow_dump_columns(grn_ctx *ctx,
3023 grn_obj *table,
3024 grn_obj *columns,
3025 const char *path)
3026 {
3027 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3028 #ifdef GRN_WITH_APACHE_ARROW
3029 auto output_result = arrow::io::FileOutputStream::Open(path);
3030 std::stringstream context;
3031 if (!grnarrow::check(ctx,
3032 output_result,
3033 context << "[arrow][dump] failed to open path: "
3034 << "<" << path << ">")) {
3035 GRN_API_RETURN(ctx->rc);
3036 }
3037 auto output = *output_result;
3038
3039 grnarrow::FileDumper dumper(ctx, table, columns);
3040 dumper.dump(output.get());
3041 #else /* GRN_WITH_APACHE_ARROW */
3042 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3043 "[arrow][dump] Apache Arrow support isn't enabled");
3044 #endif /* GRN_WITH_APACHE_ARROW */
3045 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3046 }
3047
3048 #ifdef GRN_WITH_APACHE_ARROW
3049 struct _grn_arrow_stream_loader {
3050 grnarrow::StreamLoader *loader;
3051 };
3052 #endif
3053
3054 grn_arrow_stream_loader *
3055 grn_arrow_stream_loader_open(grn_ctx *ctx, grn_loader *loader)
3056 {
3057 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3058 #ifdef GRN_WITH_APACHE_ARROW
3059 grn_arrow_stream_loader *arrow_stream_loader;
3060 arrow_stream_loader = static_cast<grn_arrow_stream_loader *>(
3061 GRN_CALLOC(sizeof(grn_arrow_stream_loader)));
3062 arrow_stream_loader->loader = new grnarrow::StreamLoader(ctx, loader);
3063 GRN_API_RETURN(arrow_stream_loader);
3064 #else
3065 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3066 "[arrow][stream-loader][open] Apache Arrow support isn't enabled");
3067 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!);
3068 #endif
3069 }
3070
3071 grn_rc
3072 grn_arrow_stream_loader_close(grn_ctx *ctx, grn_arrow_stream_loader *loader)
3073 {
3074 if (!loader) {
3075 return ctx->rccxx . . $ _grn_ctx#rc.;
3076 }
3077
3078 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3079 #ifdef GRN_WITH_APACHE_ARROW
3080 delete loader->loader;
3081 GRN_FREE(loader);
3082 #else
3083 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3084 "[arrow][stream-loader][close] Apache Arrow support isn't enabled");
3085 #endif
3086 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3087 }
3088
3089 grn_rc
3090 grn_arrow_stream_loader_consume(grn_ctx *ctx,
3091 grn_arrow_stream_loader *loader,
3092 const char *data,
3093 size_t data_size)
3094 {
3095 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3096 #ifdef GRN_WITH_APACHE_ARROW
3097 loader->loader->consume(data, data_size);
3098 #else
3099 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3100 "[arrow][stream-loader][consume] Apache Arrow support isn't enabled");
3101 #endif
3102 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3103 }
3104
3105 #ifdef GRN_WITH_APACHE_ARROW
3106 struct _grn_arrow_stream_writer {
3107 grnarrow::StreamWriter *writer;
3108 };
3109 #endif
3110
3111 grn_arrow_stream_writer *
3112 grn_arrow_stream_writer_open(grn_ctx *ctx, grn_obj *output_buffer)
3113 {
3114 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3115 #ifdef GRN_WITH_APACHE_ARROW
3116 grn_arrow_stream_writer *arrow_stream_writer;
3117 arrow_stream_writer = static_cast<grn_arrow_stream_writer *>(
3118 GRN_CALLOC(sizeof(grn_arrow_stream_writer)));
3119 arrow_stream_writer->writer = new grnarrow::StreamWriter(ctx, output_buffer);
3120 GRN_API_RETURN(arrow_stream_writer);
3121 #else
3122 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3123 "[arrow][stream-writer][open] Apache Arrow support isn't enabled");
3124 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!);
3125 #endif
3126 }
3127
3128 grn_rc
3129 grn_arrow_stream_writer_close(grn_ctx *ctx, grn_arrow_stream_writer *writer)
3130 {
3131 if (!writer) {
3132 return ctx->rccxx . . $ _grn_ctx#rc.;
3133 }
3134
3135 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3136 #ifdef GRN_WITH_APACHE_ARROW
3137 delete writer->writer;
3138 GRN_FREE(writer);
3139 #else
3140 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3141 "[arrow][stream-writer][close] Apache Arrow support isn't enabled");
3142 #endif
3143 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3144 }
3145
3146 grn_rc
3147 grn_arrow_stream_writer_add_field(grn_ctx *ctx,
3148 grn_arrow_stream_writer *writer,
3149 const char *name,
3150 grn_obj *column)
3151 {
3152 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3153 #ifdef GRN_WITH_APACHE_ARROW
3154 writer->writer->add_field(name, column);
3155 #else
3156 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3157 "[arrow][stream-writer][add-field] Apache Arrow support isn't enabled");
3158 #endif
3159 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3160 }
3161
3162 grn_rc
3163 grn_arrow_stream_writer_add_field_text_dictionary(
3164 grn_ctx *ctx,
3165 grn_arrow_stream_writer *writer,
3166 const char *name,
3167 grn_obj *index_type)
3168 {
3169 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3170 #ifdef GRN_WITH_APACHE_ARROW
3171 writer->writer->add_field_text_dictionary(name, index_type);
3172 #else
3173 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3174 "[arrow][stream-writer][add-field-text-dictionary] "
3175 "Apache Arrow support isn't enabled");
3176 #endif
3177 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3178 }
3179
3180 grn_rc
3181 grn_arrow_stream_writer_add_field_union(grn_ctx *ctx,
3182 grn_arrow_stream_writer *writer,
3183 const char *name,
3184 grn_obj **columns,
3185 size_t n_columns)
3186 {
3187 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3188 #ifdef GRN_WITH_APACHE_ARROW
3189 writer->writer->add_field_union(name, columns, n_columns);
3190 #else
3191 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3192 "[arrow][stream-writer][add-field-union] "
3193 "Apache Arrow support isn't enabled");
3194 #endif
3195 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3196 }
3197
3198 grn_rc
3199 grn_arrow_stream_writer_add_metadata(grn_ctx *ctx,
3200 grn_arrow_stream_writer *writer,
3201 const char *key,
3202 const char *value)
3203 {
3204 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3205 #ifdef GRN_WITH_APACHE_ARROW
3206 writer->writer->add_metadata(key, value);
3207 #else
3208 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3209 "[arrow][stream-writer][add-metadata] Apache Arrow support isn't "
3210 "enabled");
3211 #endif
3212 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3213 }
3214
3215 grn_rc
3216 grn_arrow_stream_writer_write_schema(grn_ctx *ctx,
3217 grn_arrow_stream_writer *writer)
3218 {
3219 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3220 #ifdef GRN_WITH_APACHE_ARROW
3221 writer->writer->write_schema();
3222 #else
3223 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3224 "[arrow][stream-writer][write-schema] Apache Arrow support isn't "
3225 "enabled");
3226 #endif
3227 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3228 }
3229
3230 grn_rc
3231 grn_arrow_stream_writer_open_record(grn_ctx *ctx,
3232 grn_arrow_stream_writer *writer)
3233 {
3234 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3235 #ifdef GRN_WITH_APACHE_ARROW
3236 writer->writer->open_record();
3237 #else
3238 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3239 "[arrow][stream-writer][open-record] Apache Arrow support isn't enabled");
3240 #endif
3241 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3242 }
3243
3244 grn_rc
3245 grn_arrow_stream_writer_close_record(grn_ctx *ctx,
3246 grn_arrow_stream_writer *writer)
3247 {
3248 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3249 #ifdef GRN_WITH_APACHE_ARROW
3250 writer->writer->close_record();
3251 #else
3252 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3253 "[arrow][stream-writer][close-record] Apache Arrow support isn't "
3254 "enabled");
3255 #endif
3256 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3257 }
3258
3259 grn_rc
3260 grn_arrow_stream_writer_add_column_null(grn_ctx *ctx,
3261 grn_arrow_stream_writer *writer)
3262 {
3263 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3264 #ifdef GRN_WITH_APACHE_ARROW
3265 writer->writer->add_column_null();
3266 #else
3267 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3268 "[arrow][stream-writer][add-column][null] "
3269 "Apache Arrow support isn't enabled");
3270 #endif
3271 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3272 }
3273
3274 grn_rc
3275 grn_arrow_stream_writer_add_column_text(grn_ctx *ctx,
3276 grn_arrow_stream_writer *writer,
3277 const char *value,
3278 size_t value_length)
3279 {
3280 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3281 #ifdef GRN_WITH_APACHE_ARROW
3282 writer->writer->add_column_text(value, value_length);
3283 #else
3284 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3285 "[arrow][stream-writer][add-column][text] "
3286 "Apache Arrow support isn't enabled");
3287 #endif
3288 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3289 }
3290
3291 grn_rc
3292 grn_arrow_stream_writer_add_column_text_dictionary(
3293 grn_ctx *ctx,
3294 grn_arrow_stream_writer *writer,
3295 const char *value,
3296 size_t value_length)
3297 {
3298 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3299 #ifdef GRN_WITH_APACHE_ARROW
3300 writer->writer->add_column_text_dictionary(value, value_length);
3301 #else
3302 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3303 "[arrow][stream-writer][add-column][text-dictionary] "
3304 "Apache Arrow support isn't enabled");
3305 #endif
3306 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3307 }
3308
3309 grn_rc
3310 grn_arrow_stream_writer_add_column_binary(grn_ctx *ctx,
3311 grn_arrow_stream_writer *writer,
3312 const uint8_tcxx . . $ uint8_t# *value,
3313 size_t value_length)
3314 {
3315 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3316 #ifdef GRN_WITH_APACHE_ARROW
3317 writer->writer->add_column_binary(value, value_length);
3318 #else
3319 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3320 "[arrow][stream-writer][add-column][binary] "
3321 "Apache Arrow support isn't enabled");
3322 #endif
3323 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3324 }
3325
3326 grn_rc
3327 grn_arrow_stream_writer_add_column_int8(grn_ctx *ctx,
3328 grn_arrow_stream_writer *writer,
3329 int8_t value)
3330 {
3331 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3332 #ifdef GRN_WITH_APACHE_ARROW
3333 writer->writer->add_column_int8(value);
3334 #else
3335 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3336 "[arrow][stream-writer][add-column][int8] "
3337 "Apache Arrow support isn't enabled");
3338 #endif
3339 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3340 }
3341
3342 grn_rc
3343 grn_arrow_stream_writer_add_column_uint16(grn_ctx *ctx,
3344 grn_arrow_stream_writer *writer,
3345 uint16_tcxx . . $ uint16_t# value)
3346 {
3347 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3348 #ifdef GRN_WITH_APACHE_ARROW
3349 writer->writer->add_column_uint16(value);
3350 #else
3351 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3352 "[arrow][stream-writer][add-column][uint16] "
3353 "Apache Arrow support isn't enabled");
3354 #endif
3355 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3356 }
3357
3358 grn_rc
3359 grn_arrow_stream_writer_add_column_int32(grn_ctx *ctx,
3360 grn_arrow_stream_writer *writer,
3361 int32_t value)
3362 {
3363 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3364 #ifdef GRN_WITH_APACHE_ARROW
3365 writer->writer->add_column_int32(value);
3366 #else
3367 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3368 "[arrow][stream-writer][add-column][int32] "
3369 "Apache Arrow support isn't enabled");
3370 #endif
3371 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3372 }
3373
3374 grn_rc
3375 grn_arrow_stream_writer_add_column_uint32(grn_ctx *ctx,
3376 grn_arrow_stream_writer *writer,
3377 uint32_tcxx . . $ uint32_t# value)
3378 {
3379 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3380 #ifdef GRN_WITH_APACHE_ARROW
3381 writer->writer->add_column_uint32(value);
3382 #else
3383 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3384 "[arrow][stream-writer][add-column][uint32] "
3385 "Apache Arrow support isn't enabled");
3386 #endif
3387 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3388 }
3389
3390 grn_rc
3391 grn_arrow_stream_writer_add_column_int64(grn_ctx *ctx,
3392 grn_arrow_stream_writer *writer,
3393 int64_t value)
3394 {
3395 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3396 #ifdef GRN_WITH_APACHE_ARROW
3397 writer->writer->add_column_int64(value);
3398 #else
3399 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3400 "[arrow][stream-writer][add-column][int64] "
3401 "Apache Arrow support isn't enabled");
3402 #endif
3403 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3404 }
3405
3406 grn_rc
3407 grn_arrow_stream_writer_add_column_uint64(grn_ctx *ctx,
3408 grn_arrow_stream_writer *writer,
3409 uint64_tcxx . . $ uint64_t# value)
3410 {
3411 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3412 #ifdef GRN_WITH_APACHE_ARROW
3413 writer->writer->add_column_uint64(value);
3414 #else
3415 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3416 "[arrow][stream-writer][add-column][uint64] "
3417 "Apache Arrow support isn't enabled");
3418 #endif
3419 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3420 }
3421
3422 #ifdef GRN_HAVE_BFLOAT16
3423 grn_rc
3424 grn_arrow_stream_writer_add_column_bfloat16(grn_ctx *ctx,
3425 grn_arrow_stream_writer *writer,
3426 grn_bfloat16 value)
3427 {
3428 GRN_API_ENTER;
3429 # ifdef GRN_WITH_APACHE_ARROW
3430 // writer->writer->add_column_bfloat16(value);
3431 ERR(GRN_FUNCTION_NOT_IMPLEMENTED,
3432 "[arrow][stream-writer][add-column][bfloat16] "
3433 "Apache Arrow doesn't support bfloat16 yet");
3434 # else
3435 ERR(GRN_FUNCTION_NOT_IMPLEMENTED,
3436 "[arrow][stream-writer][add-column][bfloat16] "
3437 "Apache Arrow support isn't enabled");
3438 # endif
3439 GRN_API_RETURN(ctx->rc);
3440 }
3441 #endif
3442
3443 grn_rc
3444 grn_arrow_stream_writer_add_column_float32(grn_ctx *ctx,
3445 grn_arrow_stream_writer *writer,
3446 float value)
3447 {
3448 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3449 #ifdef GRN_WITH_APACHE_ARROW
3450 writer->writer->add_column_float32(value);
3451 #else
3452 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3453 "[arrow][stream-writer][add-column][float32] "
3454 "Apache Arrow support isn't enabled");
3455 #endif
3456 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3457 }
3458
3459 grn_rc
3460 grn_arrow_stream_writer_add_column_float(grn_ctx *ctx,
3461 grn_arrow_stream_writer *writer,
3462 double value)
3463 {
3464 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3465 #ifdef GRN_WITH_APACHE_ARROW
3466 writer->writer->add_column_float(value);
3467 #else
3468 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3469 "[arrow][stream-writer][add-column][float] "
3470 "Apache Arrow support isn't enabled");
3471 #endif
3472 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3473 }
3474
3475 grn_rc
3476 grn_arrow_stream_writer_add_column_timestamp(grn_ctx *ctx,
3477 grn_arrow_stream_writer *writer,
3478 grn_timeval value)
3479 {
3480 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3481 #ifdef GRN_WITH_APACHE_ARROW
3482 writer->writer->add_column_timestamp(value);
3483 #else
3484 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3485 "[arrow][stream-writer][add-column][timestamp] "
3486 "Apache Arrow support isn't enabled");
3487 #endif
3488 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3489 }
3490
3491 grn_rc
3492 grn_arrow_stream_writer_add_column_record(grn_ctx *ctx,
3493 grn_arrow_stream_writer *writer,
3494 grn_obj *record)
3495 {
3496 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3497 #ifdef GRN_WITH_APACHE_ARROW
3498 writer->writer->add_column_record(record);
3499 #else
3500 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3501 "[arrow][stream-writer][add-column][record] "
3502 "Apache Arrow support isn't enabled");
3503 #endif
3504 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3505 }
3506
3507 grn_rc
3508 grn_arrow_stream_writer_add_column_uvector(grn_ctx *ctx,
3509 grn_arrow_stream_writer *writer,
3510 grn_obj *uvector)
3511 {
3512 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3513 #ifdef GRN_WITH_APACHE_ARROW
3514 writer->writer->add_column_uvector(uvector);
3515 #else
3516 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3517 "[arrow][stream-writer][add-column][uvector] "
3518 "Apache Arrow support isn't enabled");
3519 #endif
3520 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3521 }
3522
3523 grn_rc
3524 grn_arrow_stream_writer_add_column_union(grn_ctx *ctx,
3525 grn_arrow_stream_writer *writer,
3526 int8_t type)
3527 {
3528 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3529 #ifdef GRN_WITH_APACHE_ARROW
3530 writer->writer->add_column_union(type);
3531 #else
3532 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3533 "[arrow][stream-writer][add-column][union] "
3534 "Apache Arrow support isn't enabled");
3535 #endif
3536 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3537 }
3538
3539 grn_rc
3540 grn_arrow_stream_writer_flush(grn_ctx *ctx, grn_arrow_stream_writer *writer)
3541 {
3542 GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!;
3543 #ifdef GRN_WITH_APACHE_ARROW
3544 writer->writer->flush();
3545 #else
3546 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED,
3547 "[arrow][stream-writer][flush] Apache Arrow support isn't enabled");
3548 #endif
3549 GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc);
3550 }
3551 }