1#include "duckdb/main/relation/table_function_relation.hpp"
2#include "duckdb/parser/tableref/basetableref.hpp"
3#include "duckdb/parser/query_node/select_node.hpp"
4#include "duckdb/parser/expression/star_expression.hpp"
5#include "duckdb/parser/tableref/table_function_ref.hpp"
6#include "duckdb/parser/expression/constant_expression.hpp"
7#include "duckdb/parser/expression/function_expression.hpp"
8#include "duckdb/main/client_context.hpp"
9#include "duckdb/parser/parser.hpp"
10
11namespace duckdb {
12
13TableFunctionRelation::TableFunctionRelation(ClientContext &context, string name_p, vector<Value> parameters_p)
14 : Relation(context, RelationType::TABLE_FUNCTION_RELATION), name(move(name_p)), parameters(move(parameters_p)) {
15 context.TryBindRelation(*this, this->columns);
16}
17
18unique_ptr<QueryNode> TableFunctionRelation::GetQueryNode() {
19 auto result = make_unique<SelectNode>();
20 result->select_list.push_back(make_unique<StarExpression>());
21 result->from_table = GetTableRef();
22 return move(result);
23}
24
25unique_ptr<TableRef> TableFunctionRelation::GetTableRef() {
26 vector<unique_ptr<ParsedExpression>> children;
27 for (auto &parameter : parameters) {
28 children.push_back(make_unique<ConstantExpression>(parameter.GetSQLType(), parameter));
29 }
30
31 auto table_function = make_unique<TableFunctionRef>();
32 auto function = make_unique<FunctionExpression>(name, children);
33 table_function->function = move(function);
34 return move(table_function);
35}
36
37string TableFunctionRelation::GetAlias() {
38 return name;
39}
40
41const vector<ColumnDefinition> &TableFunctionRelation::Columns() {
42 return columns;
43}
44
45string TableFunctionRelation::ToString(idx_t depth) {
46 string function_call = name + "(";
47 for (idx_t i = 0; i < parameters.size(); i++) {
48 if (i > 0) {
49 function_call += ", ";
50 }
51 function_call += parameters[i].ToString();
52 }
53 function_call += ")";
54 return RenderWhitespace(depth) + function_call;
55}
56
57} // namespace duckdb
58