1 | #include "duckdb/catalog/catalog.hpp" |
2 | #include "duckdb/catalog/catalog_entry/scalar_function_catalog_entry.hpp" |
3 | #include "duckdb/execution/expression_executor.hpp" |
4 | #include "duckdb/parser/expression/function_expression.hpp" |
5 | #include "duckdb/planner/expression/bound_cast_expression.hpp" |
6 | #include "duckdb/planner/expression/bound_function_expression.hpp" |
7 | #include "duckdb/planner/expression_binder.hpp" |
8 | |
9 | using namespace duckdb; |
10 | using namespace std; |
11 | |
12 | BindResult ExpressionBinder::BindExpression(FunctionExpression &function, idx_t depth) { |
13 | // lookup the function in the catalog |
14 | |
15 | if (function.function_name == "unnest" || function.function_name == "unlist" ) { |
16 | // special case, not in catalog |
17 | // TODO make sure someone does not create such a function OR |
18 | // have unnest live in catalog, too |
19 | return BindUnnest(function, depth); |
20 | } |
21 | |
22 | auto func = Catalog::GetCatalog(context).GetEntry(context, CatalogType::SCALAR_FUNCTION, function.schema, |
23 | function.function_name); |
24 | if (func->type == CatalogType::SCALAR_FUNCTION) { |
25 | // scalar function |
26 | return BindFunction(function, (ScalarFunctionCatalogEntry *)func, depth); |
27 | } else { |
28 | // aggregate function |
29 | return BindAggregate(function, (AggregateFunctionCatalogEntry *)func, depth); |
30 | } |
31 | } |
32 | |
33 | BindResult ExpressionBinder::BindFunction(FunctionExpression &function, ScalarFunctionCatalogEntry *func, idx_t depth) { |
34 | // bind the children of the function expression |
35 | string error; |
36 | for (idx_t i = 0; i < function.children.size(); i++) { |
37 | BindChild(function.children[i], depth, error); |
38 | } |
39 | if (!error.empty()) { |
40 | return BindResult(error); |
41 | } |
42 | // all children bound successfully |
43 | // extract the children and types |
44 | vector<SQLType> arguments; |
45 | vector<unique_ptr<Expression>> children; |
46 | for (idx_t i = 0; i < function.children.size(); i++) { |
47 | auto &child = (BoundExpression &)*function.children[i]; |
48 | arguments.push_back(child.sql_type); |
49 | children.push_back(move(child.expr)); |
50 | } |
51 | |
52 | auto result = ScalarFunction::BindScalarFunction(context, *func, arguments, move(children), function.is_operator); |
53 | auto sql_return_type = result->sql_return_type; |
54 | return BindResult(move(result), sql_return_type); |
55 | } |
56 | |
57 | BindResult ExpressionBinder::BindAggregate(FunctionExpression &expr, AggregateFunctionCatalogEntry *function, |
58 | idx_t depth) { |
59 | return BindResult(UnsupportedAggregateMessage()); |
60 | } |
61 | |
62 | BindResult ExpressionBinder::BindUnnest(FunctionExpression &expr, idx_t depth) { |
63 | return BindResult(UnsupportedUnnestMessage()); |
64 | } |
65 | |
66 | string ExpressionBinder::UnsupportedAggregateMessage() { |
67 | return "Aggregate functions are not supported here" ; |
68 | } |
69 | |
70 | string ExpressionBinder::UnsupportedUnnestMessage() { |
71 | return "UNNEST not supported here" ; |
72 | } |
73 | |