Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.3k
ARROW-8919: [C++][Compute][Dataset] Add Function::DispatchBest to accomodate implicit casts#9294
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
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
1fb628b
ARROW-8919: [C++][Compute] Add Function::DispatchBest
bkietz e6f0840
support implicit casts in Function::Execute, CallFunction
bkietz 5cdd710
first pass at integrating DispatchBest into Expressions
bkietz 6385032
add DispatchBest to SetLookup kernels
bkietz 058e15a
repair implicit cast is_in execution test
bkietz b8525a4
add support for null -> * cast to arithmetic and compare
bkietz e60e555
use explicit schema to avoid inferring bool as str
bkietz 2528d95
apply implicit casts to R binding
bkietz ecd778c
ensure value_set is cast to the input type
bkietz 003ef40
always check for an exact match first
bkietz 7ebb067
add implicit cast between timestamp-like types to comparison
bkietz 8100d21
support dictionary(X) -> Y casts if X -> Y
bkietz c1de51d
describe implicit cast behavior in compute.rst
bkietz db5ae2f
msvc: linkage fix
bkietz 0852305
review comments
bkietz ff9cde2
unskip implicit casting comparison test
bkietz c761233
Revert "unskip implicit casting comparison test"
bkietz dd68342
review comments
bkietz 282dac5
expand common numeric type when signed/unsigned
bkietz 66aa801
add test case for stripping casts from uint32 to signed integer types
bkietz 62a6b5e
Nits + fix compile error (hopefully)
pitrou 6ded65f
inline InitKernelState, ensure KernelInitArgs::inputs is bound to a n…
bkietz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -124,26 +124,15 @@ void RegisterScalarCast(FunctionRegistry* registry) { | ||
| } // namespace internal | ||
| struct CastFunction::CastFunctionImpl { | ||
| Type::type out_type; | ||
| std::unordered_set<int> in_types; | ||
| }; | ||
| CastFunction::CastFunction(std::string name, Type::type out_type) | ||
| : ScalarFunction(std::move(name), Arity::Unary(), /*doc=*/nullptr) { | ||
| impl_.reset(new CastFunctionImpl()); | ||
| impl_->out_type = out_type; | ||
| } | ||
| CastFunction::~CastFunction() = default; | ||
| Type::type CastFunction::out_type_id() const { return impl_->out_type; } | ||
| CastFunction::CastFunction(std::string name, Type::type out_type_id) | ||
| : ScalarFunction(std::move(name), Arity::Unary(), /*doc=*/nullptr), | ||
| out_type_id_(out_type_id) {} | ||
| Status CastFunction::AddKernel(Type::type in_type_id, ScalarKernel kernel) { | ||
| // We use the same KernelInit for every cast | ||
| kernel.init = internal::CastState::Init; | ||
| RETURN_NOT_OK(ScalarFunction::AddKernel(kernel)); | ||
| impl_->in_types.insert(static_cast<int>(in_type_id)); | ||
| in_type_ids_.push_back(in_type_id); | ||
| return Status::OK(); | ||
| } | ||
| @@ -159,19 +148,10 @@ Status CastFunction::AddKernel(Type::type in_type_id, std::vector<InputType> in_ | ||
| return AddKernel(in_type_id, std::move(kernel)); | ||
| } | ||
| bool CastFunction::CanCastTo(const DataType& out_type) const { | ||
| return impl_->in_types.find(static_cast<int>(out_type.id())) != impl_->in_types.end(); | ||
| } | ||
| Result<const Kernel*> CastFunction::DispatchExact( | ||
| const std::vector<ValueDescr>& values) const { | ||
| const int passed_num_args = static_cast<int>(values.size()); | ||
| RETURN_NOT_OK(CheckArity(values)); | ||
| // Validate arity | ||
| if (passed_num_args != 1) { | ||
| return Status::Invalid("Cast functions accept 1 argument but passed ", | ||
| passed_num_args); | ||
| } | ||
| std::vector<const ScalarKernel*> candidate_kernels; | ||
| for (const auto& kernel : kernels_) { | ||
| if (kernel.signature->MatchesInputs(values)) { | ||
| @@ -181,25 +161,28 @@ Result<const Kernel*> CastFunction::DispatchExact( | ||
| if (candidate_kernels.size() == 0) { | ||
| return Status::NotImplemented("Unsupported cast from ", values[0].type->ToString(), | ||
| " to ", ToTypeName(impl_->out_type), " using function ", | ||
| " to ", ToTypeName(out_type_id_), " using function ", | ||
| this->name()); | ||
| } else if (candidate_kernels.size() == 1) { | ||
| } | ||
| if (candidate_kernels.size() == 1) { | ||
| // One match, return it | ||
| return candidate_kernels[0]; | ||
| } else { | ||
| // Now we are in a casting scenario where we may have both a EXACT_TYPE and | ||
| // a SAME_TYPE_ID. So we will see if there is an exact match among the | ||
| // candidate kernels and if not we will just return the first one | ||
| for (auto kernel : candidate_kernels) { | ||
| const InputType& arg0 = kernel->signature->in_types()[0]; | ||
| if (arg0.kind() == InputType::EXACT_TYPE) { | ||
| // Bingo. Return it | ||
| return kernel; | ||
| } | ||
| } | ||
| // Now we are in a casting scenario where we may have both a EXACT_TYPE and | ||
| // a SAME_TYPE_ID. So we will see if there is an exact match among the | ||
| // candidate kernels and if not we will just return the first one | ||
| for (auto kernel : candidate_kernels) { | ||
| const InputType& arg0 = kernel->signature->in_types()[0]; | ||
| if (arg0.kind() == InputType::EXACT_TYPE) { | ||
| // Bingo. Return it | ||
| return kernel; | ||
| } | ||
| // We didn't find an exact match. So just return some kernel that matches | ||
| return candidate_kernels[0]; | ||
| } | ||
| // We didn't find an exact match. So just return some kernel that matches | ||
| return candidate_kernels[0]; | ||
| } | ||
| Result<Datum> Cast(const Datum& value, const CastOptions& options, ExecContext* ctx) { | ||
| @@ -225,13 +208,37 @@ Result<std::shared_ptr<CastFunction>> GetCastFunction( | ||
| } | ||
| bool CanCast(const DataType& from_type, const DataType& to_type) { | ||
| // TODO | ||
| internal::EnsureInitCastTable(); | ||
| auto it = internal::g_cast_table.find(static_cast<int>(from_type.id())); | ||
| auto it = internal::g_cast_table.find(static_cast<int>(to_type.id())); | ||
| if (it == internal::g_cast_table.end()) { | ||
| return false; | ||
| } | ||
| return it->second->CanCastTo(to_type); | ||
| const CastFunction* function = it->second.get(); | ||
| DCHECK_EQ(function->out_type_id(), to_type.id()); | ||
| for (auto from_id : function->in_type_ids()) { | ||
| // XXX should probably check the output type as well | ||
pitrou marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| if (from_type.id() == from_id) return true; | ||
| } | ||
| return false; | ||
| } | ||
| Result<std::vector<Datum>> Cast(std::vector<Datum> datums, std::vector<ValueDescr> descrs, | ||
| ExecContext* ctx) { | ||
| for (size_t i = 0; i != datums.size(); ++i) { | ||
| if (descrs[i] != datums[i].descr()) { | ||
| if (descrs[i].shape != datums[i].shape()) { | ||
| return Status::NotImplemented("casting between Datum shapes"); | ||
| } | ||
| ARROW_ASSIGN_OR_RAISE(datums[i], | ||
| Cast(datums[i], CastOptions::Safe(descrs[i].type), ctx)); | ||
| } | ||
| } | ||
| return datums; | ||
| } | ||
| } // namespace compute | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.