-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-11673 - [C++] Casting dictionary type to use different index type #10721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
523c3b8
add initial impl
nirandaperera d450567
fixing non-int index_type
nirandaperera 35ab8e8
cast only when types differ
nirandaperera 110e0d8
extending test cases
nirandaperera 0a6255c
fixing null scalar issue
nirandaperera 37a9bfe
adding PR comments
nirandaperera ee9ddb3
lint fix
nirandaperera 4d1a26b
test fix
nirandaperera 3f10dba
removing unused var
nirandaperera ec58508
adding float test case
nirandaperera 45c26a6
updating with JIRA
nirandaperera 93c018d
lint fix
nirandaperera 26ffdb3
rebasing and removing todo
nirandaperera 1bad25f
Python test case
kszucs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
cpp/src/arrow/compute/kernels/scalar_cast_dictionary.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| // Implementation of casting to dictionary type | ||
|
|
||
| #include <arrow/util/bitmap_ops.h> | ||
| #include <arrow/util/checked_cast.h> | ||
|
|
||
| #include "arrow/array/builder_primitive.h" | ||
| #include "arrow/compute/cast_internal.h" | ||
| #include "arrow/compute/kernels/scalar_cast_internal.h" | ||
| #include "arrow/compute/kernels/util_internal.h" | ||
| #include "arrow/util/int_util.h" | ||
|
|
||
| namespace arrow { | ||
| using internal::CopyBitmap; | ||
|
|
||
| namespace compute { | ||
| namespace internal { | ||
|
|
||
| Status CastDictionary(KernelContext* ctx, const ExecBatch& batch, Datum* out) { | ||
| const CastOptions& options = CastState::Get(ctx); | ||
| auto out_type = std::static_pointer_cast<DictionaryType>(out->type()); | ||
|
|
||
| // if out type is same as in type, return input | ||
| if (out_type->Equals(batch[0].type())) { | ||
| *out = batch[0]; | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| if (batch[0].is_scalar()) { // if input is scalar | ||
| auto in_scalar = checked_cast<const DictionaryScalar&>(*batch[0].scalar()); | ||
|
|
||
| // if invalid scalar, return null scalar | ||
| if (!in_scalar.is_valid) { | ||
| *out = MakeNullScalar(out_type); | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| Datum casted_index, casted_dict; | ||
| if (in_scalar.value.index->type->Equals(out_type->index_type())) { | ||
| casted_index = in_scalar.value.index; | ||
| } else { | ||
| ARROW_ASSIGN_OR_RAISE(casted_index, | ||
| Cast(in_scalar.value.index, out_type->index_type(), options, | ||
| ctx->exec_context())); | ||
| } | ||
|
|
||
| if (in_scalar.value.dictionary->type()->Equals(out_type->value_type())) { | ||
| casted_dict = in_scalar.value.dictionary; | ||
| } else { | ||
| ARROW_ASSIGN_OR_RAISE( | ||
| casted_dict, Cast(in_scalar.value.dictionary, out_type->value_type(), options, | ||
| ctx->exec_context())); | ||
| } | ||
|
|
||
| *out = std::static_pointer_cast<Scalar>( | ||
| DictionaryScalar::Make(casted_index.scalar(), casted_dict.make_array())); | ||
|
|
||
| return Status::OK(); | ||
| } | ||
|
|
||
| // if input is array | ||
| const std::shared_ptr<ArrayData>& in_array = batch[0].array(); | ||
| const auto& in_type = checked_cast<const DictionaryType&>(*in_array->type); | ||
|
|
||
| ArrayData* out_array = out->mutable_array(); | ||
|
|
||
| if (in_type.index_type()->Equals(out_type->index_type())) { | ||
| out_array->buffers[0] = in_array->buffers[0]; | ||
| out_array->buffers[1] = in_array->buffers[1]; | ||
| out_array->null_count = in_array->GetNullCount(); | ||
| out_array->offset = in_array->offset; | ||
| } else { | ||
| // for indices, create a dummy ArrayData with index_type() | ||
| const std::shared_ptr<ArrayData>& indices_arr = | ||
| ArrayData::Make(in_type.index_type(), in_array->length, in_array->buffers, | ||
| in_array->GetNullCount(), in_array->offset); | ||
| ARROW_ASSIGN_OR_RAISE(auto casted_indices, Cast(indices_arr, out_type->index_type(), | ||
| options, ctx->exec_context())); | ||
| out_array->buffers[0] = std::move(casted_indices.array()->buffers[0]); | ||
| out_array->buffers[1] = std::move(casted_indices.array()->buffers[1]); | ||
| } | ||
|
|
||
| // data (dict) | ||
| if (in_type.value_type()->Equals(out_type->value_type())) { | ||
| out_array->dictionary = in_array->dictionary; | ||
| } else { | ||
| const std::shared_ptr<Array>& dict_arr = MakeArray(in_array->dictionary); | ||
| ARROW_ASSIGN_OR_RAISE(auto casted_data, Cast(dict_arr, out_type->value_type(), | ||
| options, ctx->exec_context())); | ||
| out_array->dictionary = casted_data.array(); | ||
| } | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| std::vector<std::shared_ptr<CastFunction>> GetDictionaryCasts() { | ||
| auto func = std::make_shared<CastFunction>("cast_dictionary", Type::DICTIONARY); | ||
|
|
||
| AddCommonCasts(Type::DICTIONARY, kOutputTargetType, func.get()); | ||
| ScalarKernel kernel({InputType(Type::DICTIONARY)}, kOutputTargetType, CastDictionary); | ||
| kernel.null_handling = NullHandling::COMPUTED_NO_PREALLOCATE; | ||
| kernel.mem_allocation = MemAllocation::NO_PREALLOCATE; | ||
|
|
||
| DCHECK_OK(func->AddKernel(Type::DICTIONARY, std::move(kernel))); | ||
|
|
||
| return {func}; | ||
| } | ||
|
|
||
| } // namespace internal | ||
| } // namespace compute | ||
| } // namespace arrow | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.