1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/function/function_binder.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/function/function.hpp"
12#include "duckdb/function/cast/cast_function_set.hpp"
13#include "duckdb/function/scalar_function.hpp"
14#include "duckdb/function/aggregate_function.hpp"
15#include "duckdb/function/function_set.hpp"
16
17namespace duckdb {
18
19//! The FunctionBinder class is responsible for binding functions
20class FunctionBinder {
21public:
22 DUCKDB_API explicit FunctionBinder(ClientContext &context);
23
24 ClientContext &context;
25
26public:
27 //! Bind a scalar function from the set of functions and input arguments. Returns the index of the chosen function,
28 //! returns DConstants::INVALID_INDEX and sets error if none could be found
29 DUCKDB_API idx_t BindFunction(const string &name, ScalarFunctionSet &functions,
30 const vector<LogicalType> &arguments, string &error);
31 DUCKDB_API idx_t BindFunction(const string &name, ScalarFunctionSet &functions,
32 vector<unique_ptr<Expression>> &arguments, string &error);
33 //! Bind an aggregate function from the set of functions and input arguments. Returns the index of the chosen
34 //! function, returns DConstants::INVALID_INDEX and sets error if none could be found
35 DUCKDB_API idx_t BindFunction(const string &name, AggregateFunctionSet &functions,
36 const vector<LogicalType> &arguments, string &error);
37 DUCKDB_API idx_t BindFunction(const string &name, AggregateFunctionSet &functions,
38 vector<unique_ptr<Expression>> &arguments, string &error);
39 //! Bind a table function from the set of functions and input arguments. Returns the index of the chosen
40 //! function, returns DConstants::INVALID_INDEX and sets error if none could be found
41 DUCKDB_API idx_t BindFunction(const string &name, TableFunctionSet &functions, const vector<LogicalType> &arguments,
42 string &error);
43 DUCKDB_API idx_t BindFunction(const string &name, TableFunctionSet &functions,
44 vector<unique_ptr<Expression>> &arguments, string &error);
45 //! Bind a pragma function from the set of functions and input arguments
46 DUCKDB_API idx_t BindFunction(const string &name, PragmaFunctionSet &functions, PragmaInfo &info, string &error);
47
48 DUCKDB_API unique_ptr<Expression> BindScalarFunction(const string &schema, const string &name,
49 vector<unique_ptr<Expression>> children, string &error,
50 bool is_operator = false, Binder *binder = nullptr);
51 DUCKDB_API unique_ptr<Expression> BindScalarFunction(ScalarFunctionCatalogEntry &function,
52 vector<unique_ptr<Expression>> children, string &error,
53 bool is_operator = false, Binder *binder = nullptr);
54
55 DUCKDB_API unique_ptr<BoundFunctionExpression> BindScalarFunction(ScalarFunction bound_function,
56 vector<unique_ptr<Expression>> children,
57 bool is_operator = false);
58
59 DUCKDB_API unique_ptr<BoundAggregateExpression>
60 BindAggregateFunction(AggregateFunction bound_function, vector<unique_ptr<Expression>> children,
61 unique_ptr<Expression> filter = nullptr,
62 AggregateType aggr_type = AggregateType::NON_DISTINCT);
63
64 DUCKDB_API static void BindSortedAggregate(ClientContext &context, BoundAggregateExpression &expr,
65 const vector<unique_ptr<Expression>> &groups);
66
67private:
68 //! Cast a set of expressions to the arguments of this function
69 void CastToFunctionArguments(SimpleFunction &function, vector<unique_ptr<Expression>> &children);
70 int64_t BindVarArgsFunctionCost(const SimpleFunction &func, const vector<LogicalType> &arguments);
71 int64_t BindFunctionCost(const SimpleFunction &func, const vector<LogicalType> &arguments);
72
73 template <class T>
74 vector<idx_t> BindFunctionsFromArguments(const string &name, FunctionSet<T> &functions,
75 const vector<LogicalType> &arguments, string &error);
76
77 template <class T>
78 idx_t MultipleCandidateException(const string &name, FunctionSet<T> &functions, vector<idx_t> &candidate_functions,
79 const vector<LogicalType> &arguments, string &error);
80
81 template <class T>
82 idx_t BindFunctionFromArguments(const string &name, FunctionSet<T> &functions, const vector<LogicalType> &arguments,
83 string &error);
84
85 vector<LogicalType> GetLogicalTypesFromExpressions(vector<unique_ptr<Expression>> &arguments);
86};
87
88} // namespace duckdb
89