Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::FunctionBinder Class Reference

The FunctionBinder class is responsible for binding functions. More...

Collaboration diagram for duckdb::FunctionBinder:

Public Member Functions

DUCKDB_API FunctionBinder (Binder &binder)
DUCKDB_API FunctionBinder (ClientContext &context)
DUCKDB_API optional_idx BindFunction (const string &name, ScalarFunctionSet &functions, const vector< LogicalType > &arguments, ErrorData &error)
DUCKDB_API optional_idx BindFunction (const string &name, ScalarFunctionSet &functions, vector< unique_ptr< Expression > > &arguments, ErrorData &error)
DUCKDB_API optional_idx BindFunction (const string &name, AggregateFunctionSet &functions, const vector< LogicalType > &arguments, ErrorData &error)
DUCKDB_API optional_idx BindFunction (const string &name, AggregateFunctionSet &functions, vector< unique_ptr< Expression > > &arguments, ErrorData &error)
DUCKDB_API optional_idx BindFunction (const string &name, TableFunctionSet &functions, const vector< LogicalType > &arguments, ErrorData &error)
DUCKDB_API optional_idx BindFunction (const string &name, TableFunctionSet &functions, vector< unique_ptr< Expression > > &arguments, ErrorData &error)
DUCKDB_API optional_idx BindFunction (const string &name, PragmaFunctionSet &functions, vector< Value > &parameters, ErrorData &error)
 Bind a pragma function from the set of functions and input arguments.
DUCKDB_API unique_ptr< Expression > BindScalarFunction (const string &schema, const string &name, vector< unique_ptr< Expression > > children, ErrorData &error, bool is_operator=false, optional_ptr< Binder > binder=nullptr)
DUCKDB_API unique_ptr< Expression > BindScalarFunction (ScalarFunctionCatalogEntry &function, vector< unique_ptr< Expression > > children, ErrorData &error, bool is_operator=false, optional_ptr< Binder > binder=nullptr)
DUCKDB_API unique_ptr< Expression > BindScalarFunction (ScalarFunction bound_function, vector< unique_ptr< Expression > > children, bool is_operator=false, optional_ptr< Binder > binder=nullptr)
DUCKDB_API unique_ptr< BoundAggregateExpression > BindAggregateFunction (AggregateFunction bound_function, vector< unique_ptr< Expression > > children, unique_ptr< Expression > filter=nullptr, AggregateType aggr_type=AggregateType::NON_DISTINCT)
void CastToFunctionArguments (SimpleFunction &function, vector< unique_ptr< Expression > > &children)
 Cast a set of expressions to the arguments of this function.
void ResolveTemplateTypes (BaseScalarFunction &bound_function, const vector< unique_ptr< Expression > > &children)
void CheckTemplateTypesResolved (const BaseScalarFunction &bound_function)

Static Public Member Functions

static DUCKDB_API void BindSortedAggregate (ClientContext &context, BoundAggregateExpression &expr, const vector< unique_ptr< Expression > > &groups, optional_ptr< vector< GroupingSet > > grouping_sets)
static DUCKDB_API void BindSortedAggregate (ClientContext &context, BoundWindowExpression &expr)

Public Attributes

optional_ptr< Binder > binder
ClientContext & context

Private Member Functions

optional_idx BindVarArgsFunctionCost (const SimpleFunction &func, const vector< LogicalType > &arguments)
optional_idx BindFunctionCost (const SimpleFunction &func, const vector< LogicalType > &arguments)
template<class T>
vector< idx_t > BindFunctionsFromArguments (const string &name, FunctionSet< T > &functions, const vector< LogicalType > &arguments, ErrorData &error)
template<class T>
optional_idx MultipleCandidateException (const string &catalog_name, const string &schema_name, const string &name, FunctionSet< T > &functions, vector< idx_t > &candidate_functions, const vector< LogicalType > &arguments, ErrorData &error)
template<class T>
optional_idx BindFunctionFromArguments (const string &name, FunctionSet< T > &functions, const vector< LogicalType > &arguments, ErrorData &error)
vector< LogicalType > GetLogicalTypesFromExpressions (vector< unique_ptr< Expression > > &arguments)

Detailed Description

The FunctionBinder class is responsible for binding functions.

Constructor & Destructor Documentation

◆ FunctionBinder() [1/2]

duckdb::FunctionBinder::FunctionBinder ( Binder & binder)
explicit
221035 : binder(&binder_p), context(binder_p.context) {
221036}

◆ FunctionBinder() [2/2]

duckdb::FunctionBinder::FunctionBinder ( ClientContext & context)
explicit
221033 : binder(nullptr), context(context_p) {
221034}

Member Function Documentation

◆ BindFunction() [1/7]

optional_idx duckdb::FunctionBinder::BindFunction ( const string & name,
ScalarFunctionSet & functions,
const vector< LogicalType > & arguments,
ErrorData & error )

Bind a scalar function from the set of functions and input arguments. Returns the index of the chosen function, returns optional_idx() and sets error if none could be found

221188 {
221189 return BindFunctionFromArguments(name, functions, arguments, error);
221190}
Here is the caller graph for this function:

◆ BindFunction() [2/7]

optional_idx duckdb::FunctionBinder::BindFunction ( const string & name,
ScalarFunctionSet & functions,
vector< unique_ptr< Expression > > & arguments,
ErrorData & error )
221232 {
221233 auto types = GetLogicalTypesFromExpressions(arguments);
221234 return BindFunction(name, functions, types, error);
221235}
DUCKDB_API optional_idx BindFunction(const string &name, ScalarFunctionSet &functions, const vector< LogicalType > &arguments, ErrorData &error)
Definition duckdb.cpp:221187

◆ BindFunction() [3/7]

optional_idx duckdb::FunctionBinder::BindFunction ( const string & name,
AggregateFunctionSet & functions,
const vector< LogicalType > & arguments,
ErrorData & error )

Bind an aggregate function from the set of functions and input arguments. Returns the index of the chosen function, returns optional_idx() and sets error if none could be found

221193 {
221194 return BindFunctionFromArguments(name, functions, arguments, error);
221195}

◆ BindFunction() [4/7]

optional_idx duckdb::FunctionBinder::BindFunction ( const string & name,
AggregateFunctionSet & functions,
vector< unique_ptr< Expression > > & arguments,
ErrorData & error )
221238 {
221239 auto types = GetLogicalTypesFromExpressions(arguments);
221240 return BindFunction(name, functions, types, error);
221241}

◆ BindFunction() [5/7]

optional_idx duckdb::FunctionBinder::BindFunction ( const string & name,
TableFunctionSet & functions,
const vector< LogicalType > & arguments,
ErrorData & error )

Bind a table function from the set of functions and input arguments. Returns the index of the chosen function, returns optional_idx() and sets error if none could be found

221198 {
221199 return BindFunctionFromArguments(name, functions, arguments, error);
221200}

◆ BindFunction() [6/7]

optional_idx duckdb::FunctionBinder::BindFunction ( const string & name,
TableFunctionSet & functions,
vector< unique_ptr< Expression > > & arguments,
ErrorData & error )
221244 {
221245 auto types = GetLogicalTypesFromExpressions(arguments);
221246 return BindFunction(name, functions, types, error);
221247}

◆ BindFunction() [7/7]

optional_idx duckdb::FunctionBinder::BindFunction ( const string & name,
PragmaFunctionSet & functions,
vector< Value > & parameters,
ErrorData & error )

Bind a pragma function from the set of functions and input arguments.

221203 {
221204 vector<LogicalType> types;
221205 for (auto &value : parameters) {
221206 types.push_back(value.type());
221207 }
221208 auto entry = BindFunctionFromArguments(name, functions, types, error);
221209 if (!entry.IsValid()) {
221210 error.Throw();
221211 }
221212 auto candidate_function = functions.GetFunctionByOffset(entry.GetIndex());
221213 // cast the input parameters
221214 for (idx_t i = 0; i < parameters.size(); i++) {
221215 auto target_type =
221216 i < candidate_function.arguments.size() ? candidate_function.arguments[i] : candidate_function.varargs;
221217 parameters[i] = parameters[i].CastAs(context, target_type);
221218 }
221219 return entry;
221220}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ BindScalarFunction() [1/3]

unique_ptr< Expression > duckdb::FunctionBinder::BindScalarFunction ( const string & schema,
const string & name,
vector< unique_ptr< Expression > > children,
ErrorData & error,
bool is_operator = false,
optional_ptr< Binder > binder = nullptr )
221325 {
221326 // bind the function
221327 auto &function = Catalog::GetSystemCatalog(context).GetEntry<ScalarFunctionCatalogEntry>(context, schema, name);
221328 D_ASSERT(function.type == CatalogType::SCALAR_FUNCTION_ENTRY);
221329 return BindScalarFunction(function, std::move(children), error, is_operator, binder);
221330}
static DUCKDB_API Catalog & GetSystemCatalog(ClientContext &context)
Get the SystemCatalog from the ClientContext.
Definition duckdb.cpp:7173
DUCKDB_API optional_ptr< CatalogEntry > GetEntry(ClientContext &context, const string &schema, const EntryLookupInfo &lookup_info, OnEntryNotFound if_not_found)
Gets the "schema.name" entry of the specified type, if entry does not exist behavior depends on OnEnt...
Definition duckdb.cpp:8156

◆ BindScalarFunction() [2/3]

unique_ptr< Expression > duckdb::FunctionBinder::BindScalarFunction ( ScalarFunctionCatalogEntry & function,
vector< unique_ptr< Expression > > children,
ErrorData & error,
bool is_operator = false,
optional_ptr< Binder > binder = nullptr )
221334 {
221335 // bind the function
221336 auto best_function = BindFunction(func.name, func.functions, children, error);
221337 if (!best_function.IsValid()) {
221338 return nullptr;
221339 }
221340
221341 // found a matching function!
221342 auto bound_function = func.functions.GetFunctionByOffset(best_function.GetIndex());
221343
221344 // If any of the parameters are NULL, the function will just be replaced with a NULL constant.
221345 // We try to give the NULL constant the correct type, but we have to do this without binding the function,
221346 // because functions with DEFAULT_NULL_HANDLING should not have to deal with NULL inputs in their bind code.
221347 // Some functions may have an invalid default return type, as they must be bound to infer the return type.
221348 // In those cases, we default to SQLNULL.
221349 const auto return_type_if_null =
221350 bound_function.GetReturnType().IsComplete() ? bound_function.GetReturnType() : LogicalType::SQLNULL;
221351 if (bound_function.GetNullHandling() == FunctionNullHandling::DEFAULT_NULL_HANDLING) {
221352 for (auto &child : children) {
221353 if (child->return_type == LogicalTypeId::SQLNULL) {
221354 return make_uniq<BoundConstantExpression>(Value(return_type_if_null));
221355 }
221356 if (!child->IsFoldable()) {
221357 continue;
221358 }
221359 Value result;
221360 if (!ExpressionExecutor::TryEvaluateScalar(context, *child, result)) {
221361 continue;
221362 }
221363 if (result.IsNull()) {
221364 return make_uniq<BoundConstantExpression>(Value(return_type_if_null));
221365 }
221366 }
221367 }
221368 return BindScalarFunction(bound_function, std::move(children), is_operator, binder);
221369}
static DUCKDB_API bool TryEvaluateScalar(ClientContext &context, const Expression &expr, Value &result)
Try to evaluate a scalar expression and fold it into a single value, returns false if an exception is...
Definition duckdb.cpp:150008

◆ BindScalarFunction() [3/3]

unique_ptr< Expression > duckdb::FunctionBinder::BindScalarFunction ( ScalarFunction bound_function,
vector< unique_ptr< Expression > > children,
bool is_operator = false,
optional_ptr< Binder > binder = nullptr )
221659 {
221660 // Attempt to resolve template types, before we call the "Bind" callback.
221661 ResolveTemplateTypes(bound_function, children);
221662
221663 unique_ptr<FunctionData> bind_info;
221664
221665 if (bound_function.HasBindCallback()) {
221666 bind_info = bound_function.GetBindCallback()(context, bound_function, children);
221667 } else if (bound_function.HasBindExtendedCallback()) {
221668 if (!binder) {
221669 throw InternalException("Function '%s' has a 'bind_extended' but the FunctionBinder was created without "
221670 "a reference to a Binder",
221671 bound_function.name);
221672 }
221673 ScalarFunctionBindInput bind_input(*binder);
221674 bind_info = bound_function.GetBindExtendedCallback()(bind_input, bound_function, children);
221675 }
221676
221677 // After the "bind" callback, we verify that all template types are bound to concrete types.
221678 CheckTemplateTypesResolved(bound_function);
221679
221680 if (bound_function.HasModifiedDatabasesCallback() && binder) {
221681 auto &properties = binder->GetStatementProperties();
221682 FunctionModifiedDatabasesInput input(bind_info, properties);
221683 bound_function.GetModifiedDatabasesCallback()(context, input);
221684 }
221685
221686 HandleCollations(context, bound_function, children);
221687
221688 // check if we need to add casts to the children
221689 CastToFunctionArguments(bound_function, children);
221690
221691 auto return_type = bound_function.GetReturnType();
221692 unique_ptr<Expression> result;
221693 auto result_func = make_uniq<BoundFunctionExpression>(std::move(return_type), std::move(bound_function),
221694 std::move(children), std::move(bind_info), is_operator);
221695 if (result_func->function.HasBindExpressionCallback()) {
221696 // if a bind_expression callback is registered - call it and emit the resulting expression
221697 FunctionBindExpressionInput input(context, result_func->bind_info.get(), result_func->children);
221698 result = result_func->function.GetBindExpressionCallback()(input);
221699 }
221700 if (!result) {
221701 result = std::move(result_func);
221702 }
221703 return result;
221704}
void CastToFunctionArguments(SimpleFunction &function, vector< unique_ptr< Expression > > &children)
Cast a set of expressions to the arguments of this function.
Definition duckdb.cpp:221294

◆ BindAggregateFunction()

unique_ptr< BoundAggregateExpression > duckdb::FunctionBinder::BindAggregateFunction ( AggregateFunction bound_function,
vector< unique_ptr< Expression > > children,
unique_ptr< Expression > filter = nullptr,
AggregateType aggr_type = AggregateType::NON_DISTINCT )
221709 {
221710 ResolveTemplateTypes(bound_function, children);
221711
221712 unique_ptr<FunctionData> bind_info;
221713 if (bound_function.HasBindCallback()) {
221714 bind_info = bound_function.GetBindCallback()(context, bound_function, children);
221715 // we may have lost some arguments in the bind
221716 children.resize(MinValue(bound_function.arguments.size(), children.size()));
221717 }
221718
221719 CheckTemplateTypesResolved(bound_function);
221720
221721 // check if we need to add casts to the children
221722 CastToFunctionArguments(bound_function, children);
221723
221724 return make_uniq<BoundAggregateExpression>(std::move(bound_function), std::move(children), std::move(filter),
221725 std::move(bind_info), aggr_type);
221726}

◆ BindSortedAggregate() [1/2]

void duckdb::FunctionBinder::BindSortedAggregate ( ClientContext & context,
BoundAggregateExpression & expr,
const vector< unique_ptr< Expression > > & groups,
optional_ptr< vector< GroupingSet > > grouping_sets )
static
211565 {
211566 if (!expr.order_bys || expr.order_bys->orders.empty() || expr.children.empty()) {
211567 // not a sorted aggregate: return
211568 return;
211569 }
211570 // Remove unnecessary ORDER BY clauses and return if nothing remains
211571 if (context.config.enable_optimizer) {
211572 if (expr.order_bys->Simplify(groups, grouping_sets)) {
211573 expr.order_bys.reset();
211574 return;
211575 }
211576 }
211577 auto &bound_function = expr.function;
211578 auto &children = expr.children;
211579 auto &order_bys = *expr.order_bys;
211580 auto sorted_bind = make_uniq<SortedAggregateBindData>(context, expr);
211581
211582 if (!sorted_bind->sorted_on_args) {
211583 // The arguments are the children plus the sort columns.
211584 for (auto &order : order_bys.orders) {
211585 children.emplace_back(std::move(order.expression));
211586 }
211587 }
211588
211589 vector<LogicalType> arguments;
211590 arguments.reserve(children.size());
211591 for (const auto &child : children) {
211592 arguments.emplace_back(child->return_type);
211593 }
211594
211595 // Replace the aggregate with the wrapper
211596 AggregateFunction ordered_aggregate(
211597 bound_function.name, arguments, bound_function.GetReturnType(),
211598 AggregateFunction::StateSize<SortedAggregateState>,
211599 AggregateFunction::StateInitialize<SortedAggregateState, SortedAggregateFunction,
211600 AggregateDestructorType::LEGACY>,
211601 SortedAggregateFunction::ScatterUpdate,
211602 AggregateFunction::StateCombine<SortedAggregateState, SortedAggregateFunction>,
211603 SortedAggregateFunction::Finalize, bound_function.GetNullHandling(), SortedAggregateFunction::SimpleUpdate,
211604 nullptr, AggregateFunction::StateDestroy<SortedAggregateState, SortedAggregateFunction>, nullptr,
211605 SortedAggregateFunction::Window);
211606
211607 expr.function = std::move(ordered_aggregate);
211608 expr.bind_info = std::move(sorted_bind);
211609 expr.order_bys.reset();
211610}

◆ BindSortedAggregate() [2/2]

void duckdb::FunctionBinder::BindSortedAggregate ( ClientContext & context,
BoundWindowExpression & expr )
static
211612 {
211613 // Make implicit orderings explicit
211614 auto &aggregate = *expr.aggregate;
211615 if (aggregate.GetOrderDependent() == AggregateOrderDependent::ORDER_DEPENDENT && expr.arg_orders.empty()) {
211616 for (auto &order : expr.orders) {
211617 const auto type = order.type;
211618 const auto null_order = order.null_order;
211619 auto expression = order.expression->Copy();
211620 expr.arg_orders.emplace_back(type, null_order, std::move(expression));
211621 }
211622 }
211623
211624 if (expr.arg_orders.empty() || expr.children.empty()) {
211625 // not a sorted aggregate: return
211626 return;
211627 }
211628 // Remove unnecessary ORDER BY clauses and return if nothing remains
211629 if (context.config.enable_optimizer) {
211630 if (BoundOrderModifier::Simplify(expr.arg_orders, expr.partitions, nullptr)) {
211631 expr.arg_orders.clear();
211632 return;
211633 }
211634 }
211635 auto &children = expr.children;
211636 auto &arg_orders = expr.arg_orders;
211637 auto sorted_bind = make_uniq<SortedAggregateBindData>(context, expr);
211638
211639 if (!sorted_bind->sorted_on_args) {
211640 // The arguments are the children plus the sort columns.
211641 for (auto &order : arg_orders) {
211642 children.emplace_back(std::move(order.expression));
211643 }
211644 }
211645
211646 vector<LogicalType> arguments;
211647 arguments.reserve(children.size());
211648 for (const auto &child : children) {
211649 arguments.emplace_back(child->return_type);
211650 }
211651
211652 // Replace the aggregate with the wrapper
211653 AggregateFunction ordered_aggregate(
211654 aggregate.name, arguments, aggregate.GetReturnType(), AggregateFunction::StateSize<SortedAggregateState>,
211655 AggregateFunction::StateInitialize<SortedAggregateState, SortedAggregateFunction,
211656 AggregateDestructorType::LEGACY>,
211657 SortedAggregateFunction::ScatterUpdate,
211658 AggregateFunction::StateCombine<SortedAggregateState, SortedAggregateFunction>,
211659 SortedAggregateFunction::Finalize, aggregate.GetNullHandling(), SortedAggregateFunction::SimpleUpdate, nullptr,
211660 AggregateFunction::StateDestroy<SortedAggregateState, SortedAggregateFunction>, nullptr,
211661 SortedAggregateFunction::Window);
211662
211663 aggregate = std::move(ordered_aggregate);
211664 expr.bind_info = std::move(sorted_bind);
211665 expr.arg_orders.clear();
211666}
static bool Simplify(vector< BoundOrderByNode > &orders, const vector< unique_ptr< Expression > > &groups, optional_ptr< vector< GroupingSet > > grouping_sets)
Definition duckdb.cpp:375082

◆ CastToFunctionArguments()

void duckdb::FunctionBinder::CastToFunctionArguments ( SimpleFunction & function,
vector< unique_ptr< Expression > > & children )

Cast a set of expressions to the arguments of this function.

221294 {
221295 for (auto &arg : function.arguments) {
221296 PrepareTypeForCast(arg);
221297 }
221298 PrepareTypeForCast(function.varargs);
221299
221300 for (idx_t i = 0; i < children.size(); i++) {
221301 auto target_type = i < function.arguments.size() ? function.arguments[i] : function.varargs;
221302 if (target_type.id() == LogicalTypeId::STRING_LITERAL || target_type.id() == LogicalTypeId::INTEGER_LITERAL) {
221303 throw InternalException(
221304 "Function %s returned a STRING_LITERAL or INTEGER_LITERAL type - return an explicit type instead",
221305 function.name);
221306 }
221307 target_type.Verify();
221308 // don't cast lambda children, they get removed before execution
221309 if (children[i]->return_type.id() == LogicalTypeId::LAMBDA) {
221310 continue;
221311 }
221312 // check if the type of child matches the type of function argument
221313 // if not we need to add a cast
221314 auto cast_result = RequiresCast(children[i]->return_type, target_type);
221315 // except for one special case: if the function accepts ANY argument
221316 // in that case we don't add a cast
221317 if (cast_result == LogicalTypeComparisonResult::DIFFERENT_TYPES) {
221318 children[i] = BoundCastExpression::AddCastToType(context, std::move(children[i]), target_type);
221319 }
221320 }
221321}
static DUCKDB_API unique_ptr< Expression > AddCastToType(ClientContext &context, unique_ptr< Expression > expr, const LogicalType &target_type, bool try_cast=false)
Cast an expression to the specified SQL type if required.
Definition duckdb.cpp:375869
internal::named_arg< T, Char > arg(const S &name, const T &arg)
Definition duckdb.cpp:16242
Here is the call graph for this function:

◆ ResolveTemplateTypes()

void duckdb::FunctionBinder::ResolveTemplateTypes ( BaseScalarFunction & bound_function,
const vector< unique_ptr< Expression > > & children )
221599 {
221600 case_insensitive_map_t<vector<LogicalType>> bindings;
221601 vector<reference<LogicalType>> to_substitute;
221602
221603 // First, we need to infer the template types from the children.
221604 for (idx_t i = 0; i < bound_function.arguments.size(); i++) {
221605 auto &param = bound_function.arguments[i];
221606
221607 // If the parameter is not templated, we can skip it.
221608 if (param.IsTemplated()) {
221609 auto actual = ExpressionBinder::GetExpressionReturnType(*children[i]);
221610 InferTemplateType(context, param, actual, bindings, *children[i], bound_function);
221611
221612 to_substitute.emplace_back(param);
221613 }
221614 }
221615
221616 // If the function has a templated varargs, we need to infer its type too
221617 if (bound_function.varargs.IsTemplated()) {
221618 // All remaining children are considered varargs.
221619 for (idx_t i = bound_function.arguments.size(); i < children.size(); i++) {
221620 auto actual = ExpressionBinder::GetExpressionReturnType(*children[i]);
221621 InferTemplateType(context, bound_function.varargs, actual, bindings, *children[i], bound_function);
221622 }
221623 to_substitute.emplace_back(bound_function.varargs);
221624 }
221625
221626 // If the return type is templated, we need to subsitute it as well
221627 if (bound_function.GetReturnType().IsTemplated()) {
221628 to_substitute.emplace_back(bound_function.GetReturnType());
221629 }
221630
221631 // Finally, substitute all template types in the bound function with their concrete types.
221632 for (auto &templated_type : to_substitute) {
221633 SubstituteTemplateType(templated_type, bindings, bound_function.name);
221634 }
221635}

◆ CheckTemplateTypesResolved()

void duckdb::FunctionBinder::CheckTemplateTypesResolved ( const BaseScalarFunction & bound_function)
221649 {
221650 for (const auto &arg : bound_function.arguments) {
221651 VerifyTemplateType(arg, bound_function.name);
221652 }
221653 VerifyTemplateType(bound_function.varargs, bound_function.name);
221654 VerifyTemplateType(bound_function.GetReturnType(), bound_function.name);
221655}

◆ BindVarArgsFunctionCost()

optional_idx duckdb::FunctionBinder::BindVarArgsFunctionCost ( const SimpleFunction & func,
const vector< LogicalType > & arguments )
private
221038 {
221039 if (arguments.size() < func.arguments.size()) {
221040 // not enough arguments to fulfill the non-vararg part of the function
221041 return optional_idx();
221042 }
221043 idx_t cost = 0;
221044 for (idx_t i = 0; i < arguments.size(); i++) {
221045 LogicalType arg_type = i < func.arguments.size() ? func.arguments[i] : func.varargs;
221046 if (arguments[i] == arg_type) {
221047 // arguments match: do nothing
221048 continue;
221049 }
221050 int64_t cast_cost = CastFunctionSet::ImplicitCastCost(context, arguments[i], arg_type);
221051 if (cast_cost >= 0) {
221052 // we can implicitly cast, add the cost to the total cost
221053 cost += idx_t(cast_cost);
221054 } else {
221055 // we can't implicitly cast: throw an error
221056 return optional_idx();
221057 }
221058 }
221059 return cost;
221060}
DUCKDB_API int64_t ImplicitCastCost(optional_ptr< ClientContext > context, const LogicalType &source, const LogicalType &target)
Returns the implicit cast cost of casting from source -> target -1 means an implicit cast is not poss...
Definition duckdb.cpp:213249
::int64_t int64_t

◆ BindFunctionCost()

optional_idx duckdb::FunctionBinder::BindFunctionCost ( const SimpleFunction & func,
const vector< LogicalType > & arguments )
private
221062 {
221063 if (func.HasVarArgs()) {
221064 // special case varargs function
221065 return BindVarArgsFunctionCost(func, arguments);
221066 }
221067 if (func.arguments.size() != arguments.size()) {
221068 // invalid argument count: check the next function
221069 return optional_idx();
221070 }
221071 idx_t cost = 0;
221072 bool has_parameter = false;
221073 for (idx_t i = 0; i < arguments.size(); i++) {
221074 if (arguments[i].id() == LogicalTypeId::UNKNOWN) {
221075 has_parameter = true;
221076 continue;
221077 }
221078 int64_t cast_cost = CastFunctionSet::ImplicitCastCost(context, arguments[i], func.arguments[i]);
221079 if (cast_cost >= 0) {
221080 // we can implicitly cast, add the cost to the total cost
221081 cost += idx_t(cast_cost);
221082 } else {
221083 // we can't implicitly cast: throw an error
221084 return optional_idx();
221085 }
221086 }
221087 if (has_parameter) {
221088 // all arguments are implicitly castable and there is a parameter - return 0 as cost
221089 return 0;
221090 }
221091 return cost;
221092}

◆ BindFunctionsFromArguments()

template<class T>
vector< idx_t > duckdb::FunctionBinder::BindFunctionsFromArguments ( const string & name,
FunctionSet< T > & functions,
const vector< LogicalType > & arguments,
ErrorData & error )
private
221096 {
221097 optional_idx best_function;
221098 idx_t lowest_cost = NumericLimits<idx_t>::Maximum();
221099 vector<idx_t> candidate_functions;
221100 for (idx_t f_idx = 0; f_idx < functions.functions.size(); f_idx++) {
221101 auto &func = functions.functions[f_idx];
221102 // check the arguments of the function
221103 auto bind_cost = BindFunctionCost(func, arguments);
221104 if (!bind_cost.IsValid()) {
221105 // auto casting was not possible
221106 continue;
221107 }
221108 auto cost = bind_cost.GetIndex();
221109 if (cost == lowest_cost) {
221110 candidate_functions.push_back(f_idx);
221111 continue;
221112 }
221113 if (cost > lowest_cost) {
221114 continue;
221115 }
221116 candidate_functions.clear();
221117 lowest_cost = cost;
221118 best_function = f_idx;
221119 }
221120 if (!best_function.IsValid()) {
221121 // no matching function was found, throw an error
221122 vector<string> candidates;
221123 string catalog_name;
221124 string schema_name;
221125 for (auto &f : functions.functions) {
221126 if (catalog_name.empty() && !f.catalog_name.empty()) {
221127 catalog_name = f.catalog_name;
221128 }
221129 if (schema_name.empty() && !f.schema_name.empty()) {
221130 schema_name = f.schema_name;
221131 }
221132 candidates.push_back(f.ToString());
221133 }
221134 error = ErrorData(BinderException::NoMatchingFunction(catalog_name, schema_name, name, arguments, candidates));
221135 return candidate_functions;
221136 }
221137 candidate_functions.push_back(best_function.GetIndex());
221138 return candidate_functions;
221139}

◆ MultipleCandidateException()

template<class T>
optional_idx duckdb::FunctionBinder::MultipleCandidateException ( const string & catalog_name,
const string & schema_name,
const string & name,
FunctionSet< T > & functions,
vector< idx_t > & candidate_functions,
const vector< LogicalType > & arguments,
ErrorData & error )
private
221145 {
221146 D_ASSERT(functions.functions.size() > 1);
221147 // there are multiple possible function definitions
221148 // throw an exception explaining which overloads are there
221149 string call_str = Function::CallToString(catalog_name, schema_name, name, arguments);
221150 string candidate_str;
221151 for (auto &conf : candidate_functions) {
221152 T f = functions.GetFunctionByOffset(conf);
221153 candidate_str += "\t" + f.ToString() + "\n";
221154 }
221155 error = ErrorData(
221156 ExceptionType::BINDER,
221157 StringUtil::Format("Could not choose a best candidate function for the function call \"%s\". In order to "
221158 "select one, please add explicit type casts.\n\tCandidate functions:\n%s",
221159 call_str, candidate_str));
221160 return optional_idx();
221161}
static DUCKDB_API string CallToString(const string &catalog_name, const string &schema_name, const string &name, const vector< LogicalType > &arguments, const LogicalType &varargs=LogicalType::INVALID)
Returns the formatted string name(arg1, arg2, ...).
Definition duckdb.cpp:220899
static string Format(const string fmt_str, ARGS... params)
Format a string using printf semantics.
Definition duckdb.hpp:4002

◆ BindFunctionFromArguments()

template<class T>
optional_idx duckdb::FunctionBinder::BindFunctionFromArguments ( const string & name,
FunctionSet< T > & functions,
const vector< LogicalType > & arguments,
ErrorData & error )
private
221165 {
221166 auto candidate_functions = BindFunctionsFromArguments<T>(name, functions, arguments, error);
221167 if (candidate_functions.empty()) {
221168 // No candidates, return an invalid index.
221169 return optional_idx();
221170 }
221171 if (candidate_functions.size() > 1) {
221172 // Multiple candidates, check if there are any unknown arguments.
221173 for (auto &arg_type : arguments) {
221174 if (arg_type.IsUnknown()) {
221175 // We cannot resolve the parameters to a function.
221176 throw ParameterNotResolvedException();
221177 }
221178 }
221179 auto catalog_name = functions.functions.size() > 0 ? functions.functions[0].catalog_name : "";
221180 auto schema_name = functions.functions.size() > 0 ? functions.functions[0].schema_name : "";
221181 return MultipleCandidateException(catalog_name, schema_name, name, functions, candidate_functions, arguments,
221182 error);
221183 }
221184 return candidate_functions[0];
221185}

◆ GetLogicalTypesFromExpressions()

vector< LogicalType > duckdb::FunctionBinder::GetLogicalTypesFromExpressions ( vector< unique_ptr< Expression > > & arguments)
private
221222 {
221223 vector<LogicalType> types;
221224 types.reserve(arguments.size());
221225 for (auto &argument : arguments) {
221226 types.push_back(ExpressionBinder::GetExpressionReturnType(*argument));
221227 }
221228 return types;
221229}

The documentation for this class was generated from the following file: