1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
|
#ifndef RFL_PARSING_TABULAR_ARROWTYPES_HPP_
#define RFL_PARSING_TABULAR_ARROWTYPES_HPP_
#include <arrow/api.h>
#include <array>
#include <cinttypes>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <type_traits>
#include "../../Box.hpp"
#include "../../NamedTuple.hpp"
#include "../../Ref.hpp"
#include "../../Rename.hpp"
#include "../../Timestamp.hpp"
#include "../../Tuple.hpp"
#include "../../concepts.hpp"
#include "../../enums.hpp"
#include "../../internal/StringLiteral.hpp"
#include "../../internal/has_reflection_type_v.hpp"
#include "../../internal/ptr_cast.hpp"
#include "../../named_tuple_t.hpp"
namespace rfl::parsing::tabular {
enum class SerializationType { csv, parquet };
template <class T, SerializationType _s>
struct ArrowTypes;
template <class T, SerializationType _s>
Result<Ref<typename ArrowTypes<T, _s>::ArrayType>> transform_numerical_array(
const std::shared_ptr<arrow::Array>& _arr) noexcept;
template <SerializationType _s>
struct ArrowTypes<bool, _s> {
using ArrayType = arrow::BooleanArray;
using BuilderType = arrow::BooleanBuilder;
static auto data_type() { return arrow::boolean(); }
static void add_to_builder(const bool _val, BuilderType* _builder) {
const auto status = _builder->Append(_val);
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
if (_arr->type()->Equals(data_type())) {
return Ref<ArrayType>::make(std::static_pointer_cast<ArrayType>(_arr));
} else {
return error("Expected boolean array, got " + _arr->type()->ToString() +
".");
}
}
static Result<bool> get_value(const Ref<ArrayType>& _chunk,
const int64_t _ix) {
return _chunk->Value(_ix);
}
static auto make_builder() { return BuilderType(); }
};
template <SerializationType _s>
struct ArrowTypes<uint8_t, _s> {
using ArrayType = arrow::UInt8Array;
using BuilderType = arrow::UInt8Builder;
using T = uint8_t;
static auto data_type() { return arrow::uint8(); }
static void add_to_builder(const auto _val, BuilderType* _builder) {
const auto status = _builder->Append(_val);
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return transform_numerical_array<T, _s>(_arr);
}
static Result<uint8_t> get_value(const Ref<ArrayType>& _chunk,
const int64_t _ix) {
return _chunk->Value(_ix);
}
static auto make_builder() { return BuilderType(); }
};
template <SerializationType _s>
struct ArrowTypes<uint16_t, _s> {
using ArrayType = arrow::UInt16Array;
using BuilderType = arrow::UInt16Builder;
using T = uint16_t;
static auto data_type() { return arrow::uint16(); }
static void add_to_builder(const auto _val, BuilderType* _builder) {
const auto status = _builder->Append(_val);
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return transform_numerical_array<T, _s>(_arr);
}
static Result<uint16_t> get_value(const Ref<ArrayType>& _chunk,
const int64_t _ix) {
return _chunk->Value(_ix);
}
static auto make_builder() { return BuilderType(); }
};
template <SerializationType _s>
struct ArrowTypes<uint32_t, _s> {
using ArrayType = arrow::UInt32Array;
using BuilderType = arrow::UInt32Builder;
using T = uint32_t;
static auto data_type() { return arrow::uint32(); }
static void add_to_builder(const auto _val, BuilderType* _builder) {
const auto status = _builder->Append(_val);
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return transform_numerical_array<T, _s>(_arr);
}
static Result<uint32_t> get_value(const Ref<ArrayType>& _chunk,
const int64_t _ix) {
return _chunk->Value(_ix);
}
static auto make_builder() { return BuilderType(); }
};
template <SerializationType _s>
struct ArrowTypes<uint64_t, _s> {
using ArrayType = arrow::UInt64Array;
using BuilderType = arrow::UInt64Builder;
using T = uint64_t;
static auto data_type() { return arrow::uint64(); }
static void add_to_builder(const auto _val, BuilderType* _builder) {
const auto status = _builder->Append(_val);
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return transform_numerical_array<T, _s>(_arr);
}
static Result<uint64_t> get_value(const Ref<ArrayType>& _chunk,
const int64_t _ix) {
return _chunk->Value(_ix);
}
static auto make_builder() { return BuilderType(); }
};
template <SerializationType _s>
struct ArrowTypes<int8_t, _s> {
using ArrayType = arrow::Int8Array;
using BuilderType = arrow::Int8Builder;
using T = int8_t;
static auto data_type() { return arrow::int8(); }
static void add_to_builder(const auto _val, BuilderType* _builder) {
const auto status = _builder->Append(_val);
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return transform_numerical_array<T, _s>(_arr);
}
static Result<int8_t> get_value(const Ref<ArrayType>& _chunk,
const int64_t _ix) {
return _chunk->Value(_ix);
}
static auto make_builder() { return BuilderType(); }
};
template <SerializationType _s>
struct ArrowTypes<int16_t, _s> {
using ArrayType = arrow::Int16Array;
using BuilderType = arrow::Int16Builder;
using T = int16_t;
static auto data_type() { return arrow::int16(); }
static void add_to_builder(const auto _val, BuilderType* _builder) {
const auto status = _builder->Append(_val);
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return transform_numerical_array<T, _s>(_arr);
}
static Result<int16_t> get_value(const Ref<ArrayType>& _chunk,
const int64_t _ix) {
return _chunk->Value(_ix);
}
static auto make_builder() { return BuilderType(); }
};
template <SerializationType _s>
struct ArrowTypes<int32_t, _s> {
using ArrayType = arrow::Int32Array;
using BuilderType = arrow::Int32Builder;
using T = int32_t;
static auto data_type() { return arrow::int32(); }
static void add_to_builder(const auto _val, BuilderType* _builder) {
const auto status = _builder->Append(_val);
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return transform_numerical_array<T, _s>(_arr);
}
static Result<int32_t> get_value(const Ref<ArrayType>& _chunk,
const int64_t _ix) {
return _chunk->Value(_ix);
}
static auto make_builder() { return BuilderType(); }
};
template <SerializationType _s>
struct ArrowTypes<int64_t, _s> {
using ArrayType = arrow::Int64Array;
using BuilderType = arrow::Int64Builder;
using T = int64_t;
static auto data_type() { return arrow::int64(); }
static void add_to_builder(const auto _val, BuilderType* _builder) {
const auto status = _builder->Append(_val);
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return transform_numerical_array<T, _s>(_arr);
}
static Result<int64_t> get_value(const Ref<ArrayType>& _chunk,
const int64_t _ix) {
return _chunk->Value(_ix);
}
static auto make_builder() { return BuilderType(); }
};
template <SerializationType _s>
struct ArrowTypes<float, _s> {
using ArrayType = arrow::FloatArray;
using BuilderType = arrow::FloatBuilder;
using T = float;
static auto data_type() { return arrow::float32(); }
static void add_to_builder(const auto _val, BuilderType* _builder) {
const auto status = _builder->Append(_val);
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return transform_numerical_array<T, _s>(_arr);
}
static Result<float> get_value(const Ref<ArrayType>& _chunk,
const int64_t _ix) {
return _chunk->Value(_ix);
}
static auto make_builder() { return BuilderType(); }
};
template <SerializationType _s>
struct ArrowTypes<double, _s> {
using ArrayType = arrow::DoubleArray;
using BuilderType = arrow::DoubleBuilder;
using T = double;
static auto data_type() { return arrow::float64(); }
static void add_to_builder(const auto _val, BuilderType* _builder) {
const auto status = _builder->Append(_val);
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return transform_numerical_array<T, _s>(_arr);
}
static Result<double> get_value(const Ref<ArrayType>& _chunk,
const int64_t _ix) {
return _chunk->Value(_ix);
}
static auto make_builder() { return BuilderType(); }
};
template <SerializationType _s>
struct ArrowTypes<std::string, _s> {
using ArrayType = arrow::StringArray;
using BuilderType = arrow::StringBuilder;
static auto data_type() { return arrow::utf8(); }
static void add_to_builder(const auto& _val, BuilderType* _builder) {
const auto status = _builder->Append(_val);
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
if (_arr->type()->Equals(data_type())) {
return Ref<ArrayType>::make(std::static_pointer_cast<ArrayType>(_arr));
} else {
return error("Expected string array, got " + _arr->type()->ToString() +
".");
}
}
static Result<std::string> get_value(const Ref<ArrayType>& _chunk,
const int64_t _ix) {
return std::string(_chunk->Value(_ix));
}
static auto make_builder() { return BuilderType(); }
};
template <class T, SerializationType _s>
requires enchantum::Enum<T>
struct ArrowTypes<T, _s> {
using ArrayType = arrow::StringArray;
using BuilderType = arrow::StringBuilder;
static auto data_type() { return arrow::utf8(); }
static void add_to_builder(const T _val, BuilderType* _builder) {
const auto status = _builder->Append(enum_to_string(_val));
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return ArrowTypes<std::string, _s>::get_array(_arr);
}
static Result<T> get_value(const Ref<ArrayType>& _chunk, const int64_t _ix) {
return string_to_enum<T>(std::string(_chunk->Value(_ix)));
}
static auto make_builder() { return BuilderType(); }
};
template <class T, SerializationType _s>
requires concepts::ContiguousByteContainer<T>
struct ArrowTypes<T, _s> {
using ArrayType = arrow::BinaryArray;
using BuilderType = arrow::BinaryBuilder;
static auto data_type() { return arrow::binary(); }
static void add_to_builder(const auto& _val, BuilderType* _builder) {
const auto status = _builder->Append(
internal::ptr_cast<const uint8_t*>(_val.data()), _val.size());
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
if (_arr->type()->Equals(data_type())) {
return Ref<ArrayType>::make(std::static_pointer_cast<ArrayType>(_arr));
} else if (_arr->type()->Equals(arrow::utf8())) {
return transform_string(
std::static_pointer_cast<arrow::StringArray>(_arr));
} else {
return error("Expected binary or string array, got " +
_arr->type()->ToString() + ".");
}
}
static Result<T> get_value(const Ref<ArrayType>& _chunk, const int64_t _ix) {
const auto begin = internal::ptr_cast<const typename T::value_type*>(
_chunk->Value(_ix).data());
return T(begin, begin + _chunk->Value(_ix).size());
}
static auto make_builder() { return BuilderType(); }
static Result<Ref<arrow::BinaryArray>> transform_string(
const std::shared_ptr<arrow::StringArray>& _arr) noexcept {
if (!_arr) {
return error(
"transform_string: std::shared_ptr not set. This is a "
"bug, please report.");
}
auto builder = arrow::BinaryBuilder();
for (int64_t i = 0; i < _arr->length(); ++i) {
if (_arr->IsNull(i)) {
const auto status = builder.AppendNull();
if (!status.ok()) {
return error(status.message());
}
} else {
const std::string_view s = _arr->Value(i);
const auto status = builder.Append(
internal::ptr_cast<const uint8_t*>(s.data()), s.size());
if (!status.ok()) {
return error(status.message());
}
}
}
std::shared_ptr<arrow::Array> res;
const auto status = builder.Finish(&res);
return Ref<arrow::BinaryArray>::make(
std::static_pointer_cast<arrow::BinaryArray>(res));
}
};
template <internal::StringLiteral _format, SerializationType _s>
struct ArrowTypes<Timestamp<_format>, _s> {
enum class TimeUnit { day, second, milli, micro, nano, string };
using ArrayType = arrow::TimestampArray;
using BuilderType = arrow::TimestampBuilder;
static auto data_type() { return arrow::timestamp(arrow::TimeUnit::MILLI); }
static void add_to_builder(const auto& _val, BuilderType* _builder) {
const auto status = _builder->Append(_val.to_time_t() * 1000);
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
if (_arr->type()->Equals(data_type())) {
return Ref<ArrayType>::make(std::static_pointer_cast<ArrayType>(_arr));
} else if (_arr->type()->Equals(
arrow::timestamp(arrow::TimeUnit::SECOND))) {
return transform_time_stamp<TimeUnit::second>(
std::static_pointer_cast<arrow::TimestampArray>(_arr));
} else if (_arr->type()->Equals(arrow::timestamp(arrow::TimeUnit::MICRO))) {
return transform_time_stamp<TimeUnit::micro>(
std::static_pointer_cast<arrow::TimestampArray>(_arr));
} else if (_arr->type()->Equals(arrow::timestamp(arrow::TimeUnit::NANO))) {
return transform_time_stamp<TimeUnit::nano>(
std::static_pointer_cast<arrow::TimestampArray>(_arr));
} else if (_arr->type()->Equals(arrow::date32())) {
return transform_time_stamp<TimeUnit::day>(
std::static_pointer_cast<arrow::Date32Array>(_arr));
} else if (_arr->type()->Equals(arrow::date64())) {
return transform_time_stamp<TimeUnit::milli>(
std::static_pointer_cast<arrow::Date64Array>(_arr));
} else if (_arr->type()->Equals(arrow::utf8())) {
return transform_time_stamp<TimeUnit::string>(
std::static_pointer_cast<arrow::StringArray>(_arr));
} else {
return error("Expected timestamp, date32, date64 or string array, got " +
_arr->type()->ToString() + ".");
}
}
static Result<Timestamp<_format>> get_value(const Ref<ArrayType>& _chunk,
const int64_t _ix) {
return Timestamp<_format>(_chunk->Value(_ix) / 1000);
}
static auto make_builder() {
return BuilderType(data_type(), arrow::default_memory_pool());
}
template <TimeUnit _unit, class SourceArrayType>
static Result<Ref<arrow::TimestampArray>> transform_time_stamp(
const std::shared_ptr<SourceArrayType>& _arr) noexcept {
if (!_arr) {
return error(
"transform_time_stamp: std::shared_ptr not set. This is a "
"bug, please report.");
}
auto builder =
arrow::TimestampBuilder(data_type(), arrow::default_memory_pool());
for (int64_t i = 0; i < _arr->length(); ++i) {
if (_arr->IsNull(i)) {
const auto status = builder.AppendNull();
if (!status.ok()) {
return error(status.message());
}
} else {
if constexpr (_unit == TimeUnit::day) {
const auto status = builder.Append(
static_cast<int64_t>(_arr->Value(i)) * 1000 * 24 * 60 * 60);
if (!status.ok()) {
return error(status.message());
}
} else if constexpr (_unit == TimeUnit::second) {
const auto status =
builder.Append(static_cast<int64_t>(_arr->Value(i) * 1000));
if (!status.ok()) {
return error(status.message());
}
} else if constexpr (_unit == TimeUnit::milli) {
const auto status =
builder.Append(static_cast<int64_t>(_arr->Value(i)));
if (!status.ok()) {
return error(status.message());
}
} else if constexpr (_unit == TimeUnit::micro) {
const auto status =
builder.Append(static_cast<int64_t>(_arr->Value(i) / 1000));
if (!status.ok()) {
return error(status.message());
}
} else if constexpr (_unit == TimeUnit::nano) {
const auto status =
builder.Append(static_cast<int64_t>(_arr->Value(i) / 1000000));
if (!status.ok()) {
return error(status.message());
}
} else if constexpr (_unit == TimeUnit::string) {
const auto ts = Timestamp<_format>::make(std::string(_arr->Value(i)));
if (!ts) {
return error(ts.error().what());
}
const auto status = builder.Append(ts->to_time_t() * 1000);
if (!status.ok()) {
return error(status.message());
}
} else {
static_assert(rfl::always_false_v<SourceArrayType>,
"Unsupported time unit.");
}
}
}
std::shared_ptr<arrow::Array> res;
const auto status = builder.Finish(&res);
return Ref<arrow::TimestampArray>::make(
std::static_pointer_cast<arrow::TimestampArray>(res));
}
};
template <internal::StringLiteral _format>
struct ArrowTypes<Timestamp<_format>, SerializationType::csv> {
using ArrayType = arrow::TimestampArray;
using BuilderType = arrow::StringBuilder;
static auto data_type() { return arrow::timestamp(arrow::TimeUnit::MILLI); }
static void add_to_builder(const Timestamp<_format>& _val,
BuilderType* _builder) {
const auto status = _builder->Append(_val.str());
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return ArrowTypes<Timestamp<_format>,
SerializationType::parquet>::get_array(_arr);
}
static Result<Timestamp<_format>> get_value(const Ref<ArrayType>& _chunk,
const int64_t _ix) {
return ArrowTypes<Timestamp<_format>,
SerializationType::parquet>::get_value(_chunk, _ix);
}
static auto make_builder() { return BuilderType(); }
};
template <class T, SerializationType _s>
requires internal::has_reflection_type_v<T>
struct ArrowTypes<T, _s> {
using ArrayType =
typename ArrowTypes<typename T::ReflectionType, _s>::ArrayType;
using BuilderType =
typename ArrowTypes<typename T::ReflectionType, _s>::BuilderType;
static auto data_type() {
return ArrowTypes<typename T::ReflectionType, _s>::data_type();
}
static void add_to_builder(const auto& _val, BuilderType* _builder) {
ArrowTypes<typename T::ReflectionType, _s>::add_to_builder(
_val.reflection(), _builder);
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return ArrowTypes<typename T::ReflectionType, _s>::get_array(_arr);
}
static Result<T> get_value(const Ref<ArrayType>& _chunk, const int64_t _ix) {
return ArrowTypes<std::remove_cvref_t<typename T::ReflectionType>,
_s>::get_value(_chunk, _ix)
.and_then([](const auto& _v) -> Result<T> {
try {
return T(_v);
} catch (const std::exception& e) {
return error(e.what());
}
});
}
static auto make_builder() {
return ArrowTypes<typename T::ReflectionType, _s>::make_builder();
}
};
template <class T, SerializationType _s>
struct ArrowTypes<std::optional<T>, _s> {
using ArrayType = typename ArrowTypes<std::remove_cvref_t<T>, _s>::ArrayType;
using BuilderType =
typename ArrowTypes<std::remove_cvref_t<T>, _s>::BuilderType;
static auto data_type() { return ArrowTypes<T, _s>::data_type(); }
static void add_to_builder(const auto& _val, BuilderType* _builder) {
if (_val) {
ArrowTypes<T, _s>::add_to_builder(*_val, _builder);
} else {
const auto status = _builder->AppendNull();
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return ArrowTypes<T, _s>::get_array(_arr);
}
static auto get_value(const Ref<ArrayType>& _chunk, const int64_t _ix) {
return ArrowTypes<std::remove_cvref_t<T>, _s>::get_value(_chunk, _ix)
.transform([](const auto& _v) { return std::make_optional<T>(_v); });
}
static auto make_builder() { return ArrowTypes<T, _s>::make_builder(); }
};
template <class T, SerializationType _s>
struct ArrowTypes<std::shared_ptr<T>, _s> {
using ArrayType = typename ArrowTypes<std::remove_cvref_t<T>, _s>::ArrayType;
using BuilderType =
typename ArrowTypes<std::remove_cvref_t<T>, _s>::BuilderType;
static auto data_type() { return ArrowTypes<T, _s>::data_type(); }
static void add_to_builder(const auto& _val, BuilderType* _builder) {
if (_val) {
ArrowTypes<T, _s>::add_to_builder(*_val, _builder);
} else {
const auto status = _builder->AppendNull();
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return ArrowTypes<T, _s>::get_array(_arr);
}
static auto get_value(const Ref<ArrayType>& _chunk, const int64_t _ix) {
return ArrowTypes<std::remove_cvref_t<T>, _s>::get_value(_chunk, _ix)
.transform([](const auto& _v) { return std::make_shared<T>(_v); });
}
static auto make_builder() { return ArrowTypes<T, _s>::make_builder(); }
};
template <class T, SerializationType _s>
struct ArrowTypes<std::unique_ptr<T>, _s> {
using ArrayType = typename ArrowTypes<std::remove_cvref_t<T>, _s>::ArrayType;
using BuilderType =
typename ArrowTypes<std::remove_cvref_t<T>, _s>::BuilderType;
static auto data_type() { return ArrowTypes<T, _s>::data_type(); }
static void add_to_builder(const auto& _val, BuilderType* _builder) {
if (_val) {
ArrowTypes<T, _s>::add_to_builder(*_val, _builder);
} else {
const auto status = _builder->AppendNull();
if (!status.ok()) {
throw std::runtime_error(status.message());
}
}
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return ArrowTypes<T, _s>::get_array(_arr);
}
static auto get_value(const Ref<ArrayType>& _chunk, const int64_t _ix) {
return ArrowTypes<std::remove_cvref_t<T>, _s>::get_value(_chunk, _ix)
.transform([](const auto& _v) { return std::make_unique<T>(_v); });
}
static auto make_builder() { return ArrowTypes<T, _s>::make_builder(); }
};
template <class T, SerializationType _s>
struct ArrowTypes<Box<T>, _s> {
using ArrayType = typename ArrowTypes<std::remove_cvref_t<T>, _s>::ArrayType;
using BuilderType =
typename ArrowTypes<std::remove_cvref_t<T>, _s>::BuilderType;
static auto data_type() { return ArrowTypes<T, _s>::data_type(); }
static void add_to_builder(const auto& _val, BuilderType* _builder) {
ArrowTypes<T, _s>::add_to_builder(*_val, _builder);
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return ArrowTypes<T, _s>::get_array(_arr);
}
static auto get_value(const Ref<ArrayType>& _chunk, const int64_t _ix) {
return ArrowTypes<std::remove_cvref_t<T>, _s>::get_value(_chunk, _ix)
.transform([](const auto& _v) { return Box<T>::make(_v); });
}
static auto make_builder() { return ArrowTypes<T, _s>::make_builder(); }
};
template <class T, SerializationType _s>
struct ArrowTypes<Ref<T>, _s> {
using ArrayType = typename ArrowTypes<std::remove_cvref_t<T>, _s>::ArrayType;
using BuilderType =
typename ArrowTypes<std::remove_cvref_t<T>, _s>::BuilderType;
static auto data_type() { return ArrowTypes<T, _s>::data_type(); }
static void add_to_builder(const auto& _val, BuilderType* _builder) {
ArrowTypes<T, _s>::add_to_builder(*_val, _builder);
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return ArrowTypes<T, _s>::get_array(_arr);
}
static auto get_value(const Ref<ArrayType>& _chunk, const int64_t _ix) {
return ArrowTypes<std::remove_cvref_t<T>, _s>::get_value(_chunk, _ix)
.transform([](const auto& _v) { return Ref<T>::make(_v); });
}
static auto make_builder() { return ArrowTypes<T, _s>::make_builder(); }
};
template <internal::StringLiteral _name, class T, SerializationType _s>
struct ArrowTypes<Rename<_name, T>, _s> {
using ArrayType = typename ArrowTypes<std::remove_cvref_t<T>, _s>::ArrayType;
using BuilderType =
typename ArrowTypes<std::remove_cvref_t<T>, _s>::BuilderType;
static auto data_type() { return ArrowTypes<T, _s>::data_type(); }
static void add_to_builder(const auto& _val, BuilderType* _builder) {
ArrowTypes<T, _s>::add_to_builder(_val.value(), _builder);
}
static Result<Ref<ArrayType>> get_array(
const std::shared_ptr<arrow::Array>& _arr) {
return ArrowTypes<T, _s>::get_array(_arr);
}
static auto get_value(const Ref<ArrayType>& _chunk, const int64_t _ix) {
return ArrowTypes<std::remove_cvref_t<T>, _s>::get_value(_chunk, _ix)
.transform([](const auto& _v) { return Rename<_name, T>(_v); });
}
static auto make_builder() { return ArrowTypes<T, _s>::make_builder(); }
};
template <class T, SerializationType _s, class SourceArrayType>
Result<Ref<typename ArrowTypes<T, _s>::ArrayType>>
transform_numerical_array_impl(
const std::shared_ptr<SourceArrayType>& _arr) noexcept {
if (!_arr) {
return error(
"transform_numerical_array_impl: std::shared_ptr not set. This is a "
"bug, please report.");
}
auto builder = ArrowTypes<T, _s>::make_builder();
for (int64_t i = 0; i < _arr->length(); ++i) {
if (_arr->IsNull(i)) {
const auto status = builder.AppendNull();
if (!status.ok()) {
return error(status.message());
}
} else {
const auto status = builder.Append(static_cast<T>(_arr->Value(i)));
if (!status.ok()) {
return error(status.message());
}
}
}
using TargetArrayType = typename ArrowTypes<T, _s>::ArrayType;
std::shared_ptr<arrow::Array> res;
const auto status = builder.Finish(&res);
return Ref<TargetArrayType>::make(
std::static_pointer_cast<TargetArrayType>(res));
}
template <class T, SerializationType _s>
Result<Ref<typename ArrowTypes<T, _s>::ArrayType>> transform_numerical_array(
const std::shared_ptr<arrow::Array>& _arr) noexcept {
if (!_arr) {
return error(
"Could not transform the numerical array. std::shared_ptr not set.");
}
using ArrayType = typename ArrowTypes<T, _s>::ArrayType;
if (_arr->type()->Equals(ArrowTypes<T, _s>::data_type())) {
return Ref<ArrayType>::make(std::static_pointer_cast<ArrayType>(_arr));
} else if (_arr->type()->Equals(ArrowTypes<uint8_t, _s>::data_type())) {
return transform_numerical_array_impl<T, _s>(
std::static_pointer_cast<typename ArrowTypes<uint8_t, _s>::ArrayType>(
_arr));
} else if (_arr->type()->Equals(ArrowTypes<uint16_t, _s>::data_type())) {
return transform_numerical_array_impl<T, _s>(
std::static_pointer_cast<typename ArrowTypes<uint16_t, _s>::ArrayType>(
_arr));
} else if (_arr->type()->Equals(ArrowTypes<uint32_t, _s>::data_type())) {
return transform_numerical_array_impl<T, _s>(
std::static_pointer_cast<typename ArrowTypes<uint32_t, _s>::ArrayType>(
_arr));
} else if (_arr->type()->Equals(ArrowTypes<uint64_t, _s>::data_type())) {
return transform_numerical_array_impl<T, _s>(
std::static_pointer_cast<typename ArrowTypes<uint64_t, _s>::ArrayType>(
_arr));
} else if (_arr->type()->Equals(ArrowTypes<int8_t, _s>::data_type())) {
return transform_numerical_array_impl<T, _s>(
std::static_pointer_cast<typename ArrowTypes<int8_t, _s>::ArrayType>(
_arr));
} else if (_arr->type()->Equals(ArrowTypes<int16_t, _s>::data_type())) {
return transform_numerical_array_impl<T, _s>(
std::static_pointer_cast<typename ArrowTypes<int16_t, _s>::ArrayType>(
_arr));
} else if (_arr->type()->Equals(ArrowTypes<int32_t, _s>::data_type())) {
return transform_numerical_array_impl<T, _s>(
std::static_pointer_cast<typename ArrowTypes<int32_t, _s>::ArrayType>(
_arr));
} else if (_arr->type()->Equals(ArrowTypes<int64_t, _s>::data_type())) {
return transform_numerical_array_impl<T, _s>(
std::static_pointer_cast<typename ArrowTypes<int64_t, _s>::ArrayType>(
_arr));
} else if (_arr->type()->Equals(ArrowTypes<float, _s>::data_type())) {
return transform_numerical_array_impl<T, _s>(
std::static_pointer_cast<typename ArrowTypes<float, _s>::ArrayType>(
_arr));
} else if (_arr->type()->Equals(ArrowTypes<double, _s>::data_type())) {
return transform_numerical_array_impl<T, _s>(
std::static_pointer_cast<typename ArrowTypes<double, _s>::ArrayType>(
_arr));
} else {
return error("Expected numerical array, got " + _arr->type()->ToString() +
".");
}
}
} // namespace rfl::parsing::tabular
#endif
|