| 36 | namespace { |
| 37 | |
| 38 | Status CastToDictionary(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) { |
| 39 | const CastOptions& options = CastState::Get(ctx); |
| 40 | const auto& out_type = checked_cast<const DictionaryType&>(*out->type()); |
| 41 | |
| 42 | std::shared_ptr<ArrayData> in_array = batch[0].array.ToArrayData(); |
| 43 | |
| 44 | // if out type is same as in type, return input |
| 45 | if (out_type.Equals(*batch[0].type())) { |
| 46 | /// XXX: This is the wrong place to do a zero-copy optimization |
| 47 | out->value = in_array; |
| 48 | return Status::OK(); |
| 49 | } |
| 50 | |
| 51 | // If the input type is string or binary-like, it is first encoded as a dictionary to |
| 52 | // facilitate processing. This approach allows the subsequent code to uniformly handle |
| 53 | // string or binary-like inputs as if they were originally provided in dictionary |
| 54 | // format. Encoding as a dictionary helps in reusing the same logic for dictionary |
| 55 | // operations. |
| 56 | if (is_base_binary_like(in_array->type->id())) { |
| 57 | in_array = DictionaryEncode(in_array)->array(); |
| 58 | } |
| 59 | const auto& in_type = checked_cast<const DictionaryType&>(*in_array->type); |
| 60 | |
| 61 | ArrayData* out_array = out->array_data().get(); |
| 62 | |
| 63 | /// XXX: again, maybe the wrong place for zero-copy optimizations |
| 64 | if (in_type.index_type()->Equals(out_type.index_type())) { |
| 65 | out_array->buffers[0] = in_array->buffers[0]; |
| 66 | out_array->buffers[1] = in_array->buffers[1]; |
| 67 | out_array->null_count = in_array->GetNullCount(); |
| 68 | out_array->offset = in_array->offset; |
| 69 | } else { |
| 70 | // for indices, create a dummy ArrayData with index_type() |
| 71 | std::shared_ptr<ArrayData> indices_arr = |
| 72 | ArrayData::Make(in_type.index_type(), in_array->length, in_array->buffers, |
| 73 | in_array->GetNullCount(), in_array->offset); |
| 74 | ARROW_ASSIGN_OR_RAISE(auto casted_indices, Cast(indices_arr, out_type.index_type(), |
| 75 | options, ctx->exec_context())); |
| 76 | out_array->buffers[0] = std::move(casted_indices.array()->buffers[0]); |
| 77 | out_array->buffers[1] = std::move(casted_indices.array()->buffers[1]); |
| 78 | } |
| 79 | |
| 80 | // data (dict) |
| 81 | if (in_type.value_type()->Equals(out_type.value_type())) { |
| 82 | out_array->dictionary = in_array->dictionary; |
| 83 | } else { |
| 84 | const std::shared_ptr<Array>& dict_arr = MakeArray(in_array->dictionary); |
| 85 | ARROW_ASSIGN_OR_RAISE(auto casted_data, Cast(dict_arr, out_type.value_type(), options, |
| 86 | ctx->exec_context())); |
| 87 | out_array->dictionary = casted_data.array(); |
| 88 | } |
| 89 | return Status::OK(); |
| 90 | } |
| 91 | |
| 92 | template <typename SrcType> |
| 93 | void AddDictionaryCast(CastFunction* func) { |
nothing calls this directly
no test coverage detected