MCPcopy Create free account
hub / github.com/apache/arrow / CastToDictionary

Function CastToDictionary

cpp/src/arrow/compute/kernels/scalar_cast_dictionary.cc:38–90  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36namespace {
37
38Status 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
92template <typename SrcType>
93void AddDictionaryCast(CastFunction* func) {

Callers

nothing calls this directly

Calls 15

is_base_binary_likeFunction · 0.85
DictionaryEncodeFunction · 0.85
ToArrayDataMethod · 0.80
array_dataMethod · 0.80
index_typeMethod · 0.80
GetNullCountMethod · 0.80
MakeArrayFunction · 0.70
GetFunction · 0.50
OKFunction · 0.50
MakeFunction · 0.50
typeMethod · 0.45
EqualsMethod · 0.45

Tested by

no test coverage detected