-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
GH-38589: [C++][Gandiva] Support registering external C functions #38632
Changes from all commits
0a86c38
2eb0237
23ae3b5
3871aac
93d8ac0
217e916
427556d
e9e248b
ec24d23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,11 +25,12 @@ | |
|
||
#include "gandiva/annotator.h" | ||
#include "gandiva/dex.h" | ||
#include "gandiva/function_holder_registry.h" | ||
#include "gandiva/function_holder_maker_registry.h" | ||
#include "gandiva/function_registry.h" | ||
#include "gandiva/function_signature.h" | ||
#include "gandiva/in_holder.h" | ||
#include "gandiva/node.h" | ||
#include "gandiva/regex_functions_holder.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this include needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found |
||
|
||
namespace gandiva { | ||
|
||
|
@@ -81,9 +82,10 @@ Status ExprDecomposer::Visit(const FunctionNode& in_node) { | |
std::shared_ptr<FunctionHolder> holder; | ||
int holder_idx = -1; | ||
if (native_function->NeedsFunctionHolder()) { | ||
auto status = FunctionHolderRegistry::Make(desc->name(), node, &holder); | ||
auto function_holder_maker_registry = registry_.GetFunctionHolderMakerRegistry(); | ||
ARROW_ASSIGN_OR_RAISE(holder, | ||
function_holder_maker_registry.Make(desc->name(), node)); | ||
holder_idx = annotator_.AddHolderPointer(holder.get()); | ||
ARROW_RETURN_NOT_OK(status); | ||
} | ||
|
||
if (native_function->result_nullable_type() == kResultNullIfNull) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// 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 | ||
|
||
#include <llvm/IR/Type.h> | ||
|
||
#include "gandiva/engine.h" | ||
#include "gandiva/exported_funcs.h" | ||
|
||
namespace { | ||
// calculate the number of arguments for a function signature | ||
size_t GetNumArgs(const gandiva::FunctionSignature& sig, | ||
const gandiva::NativeFunction& func) { | ||
auto num_args = 0; | ||
num_args += func.NeedsContext() ? 1 : 0; | ||
num_args += func.NeedsFunctionHolder() ? 1 : 0; | ||
for (auto const& arg : sig.param_types()) { | ||
num_args += arg->id() == arrow::Type::STRING ? 2 : 1; | ||
} | ||
num_args += sig.ret_type()->id() == arrow::Type::STRING ? 1 : 0; | ||
return num_args; | ||
} | ||
|
||
// map from a NativeFunction's signature to the corresponding LLVM signature | ||
arrow::Result<std::pair<std::vector<llvm::Type*>, llvm::Type*>> MapToLLVMSignature( | ||
const gandiva::FunctionSignature& sig, const gandiva::NativeFunction& func, | ||
gandiva::LLVMTypes* types) { | ||
std::vector<llvm::Type*> arg_llvm_types; | ||
arg_llvm_types.reserve(GetNumArgs(sig, func)); | ||
|
||
if (func.NeedsContext()) { | ||
arg_llvm_types.push_back(types->i64_type()); | ||
} | ||
if (func.NeedsFunctionHolder()) { | ||
arg_llvm_types.push_back(types->i64_type()); | ||
} | ||
for (auto const& arg : sig.param_types()) { | ||
arg_llvm_types.push_back(types->IRType(arg->id())); | ||
if (arg->id() == arrow::Type::STRING) { | ||
// string type needs an additional length argument | ||
arg_llvm_types.push_back(types->i32_type()); | ||
} | ||
} | ||
if (sig.ret_type()->id() == arrow::Type::STRING) { | ||
// for string output, the last arg is the output length | ||
arg_llvm_types.push_back(types->i32_ptr_type()); | ||
} | ||
auto ret_llvm_type = types->IRType(sig.ret_type()->id()); | ||
return std::make_pair(std::move(arg_llvm_types), ret_llvm_type); | ||
} | ||
} // namespace | ||
|
||
namespace gandiva { | ||
Status ExternalCFunctions::AddMappings(Engine* engine) const { | ||
auto const& c_funcs = function_registry_->GetCFunctions(); | ||
auto const types = engine->types(); | ||
for (auto& [func, func_ptr] : c_funcs) { | ||
for (auto const& sig : func.signatures()) { | ||
ARROW_ASSIGN_OR_RAISE(auto llvm_signature, MapToLLVMSignature(sig, func, types)); | ||
auto& [args, ret_llvm_type] = llvm_signature; | ||
engine->AddGlobalMappingForFunc(func.pc_name(), ret_llvm_type, args, func_ptr); | ||
} | ||
} | ||
return Status::OK(); | ||
} | ||
} // namespace gandiva |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// 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. | ||
|
||
#include "gandiva/function_holder_maker_registry.h" | ||
|
||
#include <functional> | ||
|
||
#include "arrow/util/string.h" | ||
#include "gandiva/function_holder.h" | ||
#include "gandiva/interval_holder.h" | ||
#include "gandiva/random_generator_holder.h" | ||
#include "gandiva/regex_functions_holder.h" | ||
#include "gandiva/to_date_holder.h" | ||
|
||
namespace gandiva { | ||
|
||
using arrow::internal::AsciiToLower; | ||
|
||
FunctionHolderMakerRegistry::FunctionHolderMakerRegistry() | ||
: function_holder_makers_(DefaultHolderMakers()) {} | ||
|
||
arrow::Status FunctionHolderMakerRegistry::Register(const std::string& name, | ||
FunctionHolderMaker holder_maker) { | ||
function_holder_makers_.emplace(AsciiToLower(name), std::move(holder_maker)); | ||
return arrow::Status::OK(); | ||
} | ||
|
||
template <typename HolderType> | ||
static arrow::Result<FunctionHolderPtr> HolderMaker(const FunctionNode& node) { | ||
std::shared_ptr<HolderType> derived_instance; | ||
ARROW_RETURN_NOT_OK(HolderType::Make(node, &derived_instance)); | ||
return derived_instance; | ||
} | ||
Comment on lines
+42
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. If I make this change, we need to change some existing classes, like |
||
|
||
arrow::Result<FunctionHolderPtr> FunctionHolderMakerRegistry::Make( | ||
const std::string& name, const FunctionNode& node) { | ||
auto lowered_name = AsciiToLower(name); | ||
auto found = function_holder_makers_.find(lowered_name); | ||
if (found == function_holder_makers_.end()) { | ||
return Status::Invalid("function holder not registered for function " + name); | ||
} | ||
|
||
return found->second(node); | ||
} | ||
|
||
FunctionHolderMakerRegistry::MakerMap FunctionHolderMakerRegistry::DefaultHolderMakers() { | ||
static const MakerMap maker_map = { | ||
{"like", HolderMaker<LikeHolder>}, | ||
{"to_date", HolderMaker<ToDateHolder>}, | ||
{"random", HolderMaker<RandomGeneratorHolder>}, | ||
{"rand", HolderMaker<RandomGeneratorHolder>}, | ||
{"regexp_replace", HolderMaker<ReplaceHolder>}, | ||
{"regexp_extract", HolderMaker<ExtractHolder>}, | ||
{"castintervalday", HolderMaker<IntervalDaysHolder>}, | ||
{"castintervalyear", HolderMaker<IntervalYearsHolder>}}; | ||
return maker_map; | ||
} | ||
} // namespace gandiva |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For external C interface functions, they may cause some error when adding mapping for them, so I change the
AddGlobalMappings
andExportedFuncsBase::AddMappings
functions to return arrow::Status to represent the result