-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-15545: [Python][C++] Support casting to extension type #14106
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
Merged
jorisvandenbossche
merged 27 commits into
apache:master
from
milesgranger:ARROW-15545_cast-of-extension-types
Oct 11, 2022
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
e4db00b
Support casting to extension type
milesgranger b9d8cb6
Move extension casting impl from Python to C++
milesgranger 89aa16c
Attempt impl CastToExtension et al. [skip ci]
milesgranger 444fd02
Works with hard-coded extension type [skip ci]
milesgranger 6c05a1d
Static cast to extension type [skip ci]
milesgranger 296b4fb
Move to scalar_cast_extension, need C++ tests [skip ci]
milesgranger 120204b
Impl initial cpp test for casting to extension
milesgranger 34c9998
Update cpp/src/arrow/compute/kernels/scalar_cast_test.cc
milesgranger 20be1fe
Move all but GetExtensionCasts to anonymous namespace [skip ci]
milesgranger c7e76c7
Remove CastToExtension from header file [skip ci]
milesgranger 4cd9b65
Use ARROW_ASSIGN_OR_RAISE during cast [skip ci]
milesgranger 2d18904
Updates tests from review comments [skip ci]
milesgranger 7e8d795
Add entry to compute.rst docs
milesgranger 069bfd0
Move compute.rst docs entry to generic conversions table
milesgranger 3e46cbd
Refactor adding input types in GetCastToExtension [skip ci]
milesgranger 9a5a514
Support casting between extension types
milesgranger 29db94b
Move and use AllTypeIds from gtest_util
milesgranger 2ea1e11
Move AllTypeIds impl -> type.cc
milesgranger e6ab97a
Add test for nested extension types casting
milesgranger 02013e8
Fail cast between incompatible storage types [skip ci]
milesgranger da66bf4
Only convert to ext where input type is same as storage type
milesgranger 374301c
ARROW_EXPORT for AllTypeIds
milesgranger 44cef44
Move result assignments inside if branches
milesgranger 8e5063f
Update error msg and test casting between same ext types
milesgranger 2bc8a83
Update compute.rst doc notes
milesgranger 2cef2ae
Fixup: autopep8 format
milesgranger 135c8a0
Add test for casting to extension w/ extension storage
milesgranger 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // 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 extension types | ||
| #include "arrow/compute/kernels/common.h" | ||
| #include "arrow/compute/kernels/scalar_cast_internal.h" | ||
| #include "arrow/scalar.h" | ||
|
|
||
| namespace arrow { | ||
| namespace compute { | ||
| namespace internal { | ||
|
|
||
| namespace { | ||
| Status CastToExtension(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) { | ||
| const CastOptions& options = checked_cast<const CastState*>(ctx->state())->options; | ||
| const auto& ext_ty = static_cast<const ExtensionType&>(*options.to_type.type); | ||
| auto out_ty = ext_ty.storage_type(); | ||
|
|
||
| DCHECK(batch[0].is_array()); | ||
| std::shared_ptr<Array> array = batch[0].array.ToArray(); | ||
|
|
||
| // Try to prevent user errors by preventing casting between extensions w/ | ||
| // different storage types. Provide a tip on how to accomplish same outcome. | ||
| std::shared_ptr<Array> result; | ||
| if (array->type()->id() == Type::EXTENSION) { | ||
| if (!array->type()->Equals(out_ty)) { | ||
| return Status::TypeError("Casting from '" + array->type()->ToString() + | ||
| "' to different extension type '" + ext_ty.ToString() + | ||
| "' not permitted. One can first cast to the storage " | ||
| "type, then to the extension type."); | ||
| } | ||
| result = array; | ||
milesgranger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| ARROW_ASSIGN_OR_RAISE(result, Cast(*array, out_ty, options, ctx->exec_context())); | ||
| } | ||
|
|
||
| ExtensionArray extension(options.to_type.GetSharedPtr(), result); | ||
| out->value = std::move(extension.data()); | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| std::shared_ptr<CastFunction> GetCastToExtension(std::string name) { | ||
| auto func = std::make_shared<CastFunction>(std::move(name), Type::EXTENSION); | ||
| for (Type::type in_ty : AllTypeIds()) { | ||
| DCHECK_OK( | ||
| func->AddKernel(in_ty, {InputType(in_ty)}, kOutputTargetType, CastToExtension)); | ||
| } | ||
| return func; | ||
| } | ||
|
|
||
| }; // namespace | ||
|
|
||
| std::vector<std::shared_ptr<CastFunction>> GetExtensionCasts() { | ||
milesgranger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| auto func = GetCastToExtension("cast_extension"); | ||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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
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
Oops, something went wrong.
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.