| 608 | |
| 609 | public: |
| 610 | explicit Converter_Dictionary(const std::shared_ptr<ChunkedArray>& chunked_array) |
| 611 | : Converter(chunked_array), |
| 612 | need_unification_(DictionaryChunkArrayNeedUnification(chunked_array)) { |
| 613 | if (need_unification_) { |
| 614 | const auto& arr_type = checked_cast<const DictionaryType&>(*chunked_array->type()); |
| 615 | unifier_ = ValueOrStop(DictionaryUnifier::Make(arr_type.value_type())); |
| 616 | |
| 617 | int n_arrays = chunked_array->num_chunks(); |
| 618 | arrays_transpose_.resize(n_arrays); |
| 619 | |
| 620 | for (int i = 0; i < n_arrays; i++) { |
| 621 | const auto& dict_i = |
| 622 | *checked_cast<const DictionaryArray&>(*chunked_array->chunk(i)).dictionary(); |
| 623 | StopIfNotOk(unifier_->Unify(dict_i, &arrays_transpose_[i])); |
| 624 | } |
| 625 | |
| 626 | StopIfNotOk(unifier_->GetResult(&out_type_, &dictionary_)); |
| 627 | } else { |
| 628 | const auto& dict_type = checked_cast<const DictionaryType&>(*chunked_array->type()); |
| 629 | |
| 630 | const auto& indices_type = *dict_type.index_type(); |
| 631 | switch (indices_type.id()) { |
| 632 | case Type::UINT8: |
| 633 | case Type::INT8: |
| 634 | case Type::UINT16: |
| 635 | case Type::INT16: |
| 636 | case Type::INT32: |
| 637 | case Type::UINT32: |
| 638 | case Type::INT64: |
| 639 | case Type::UINT64: |
| 640 | break; |
| 641 | default: |
| 642 | cpp11::stop("Cannot convert Dictionary Array of type `%s` to R", |
| 643 | dict_type.ToString().c_str()); |
| 644 | } |
| 645 | |
| 646 | if (chunked_array->num_chunks() > 0) { |
| 647 | // DictionaryChunkArrayNeedUnification() returned false so we can safely assume |
| 648 | // the dictionary of the first chunk applies everywhere |
| 649 | const auto& dict_array = |
| 650 | checked_cast<const DictionaryArray&>(*chunked_array->chunk(0)); |
| 651 | dictionary_ = dict_array.dictionary(); |
| 652 | } else { |
| 653 | dictionary_ = CreateEmptyArray(dict_type.value_type()); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | // R factors store their codes in 32-bit integers, so dictionary arrays with |
| 658 | // more levels than that cannot be represented safely. |
| 659 | if (dictionary_->length() > std::numeric_limits<int>::max()) { |
| 660 | const auto& dict_type = checked_cast<const DictionaryType&>(*chunked_array->type()); |
| 661 | cpp11::stop( |
| 662 | "Cannot convert Dictionary Array of type `%s` to R: dictionary has " |
| 663 | "more levels than an R factor can represent", |
| 664 | dict_type.ToString().c_str()); |
| 665 | } |
| 666 | } |
| 667 |
nothing calls this directly
no test coverage detected