1#include "duckdb/planner/table_binding.hpp"
2
3#include "duckdb/common/string_util.hpp"
4#include "duckdb/catalog/catalog_entry/table_catalog_entry.hpp"
5#include "duckdb/catalog/catalog_entry/table_function_catalog_entry.hpp"
6#include "duckdb/parser/expression/columnref_expression.hpp"
7#include "duckdb/parser/tableref/subqueryref.hpp"
8#include "duckdb/planner/bind_context.hpp"
9#include "duckdb/planner/bound_query_node.hpp"
10#include "duckdb/planner/expression/bound_columnref_expression.hpp"
11#include "duckdb/planner/operator/logical_get.hpp"
12
13using namespace duckdb;
14using namespace std;
15
16TableBinding::TableBinding(const string &alias, TableCatalogEntry &table, LogicalGet &get, idx_t index)
17 : Binding(BindingType::TABLE, alias, index), table(table), get(get) {
18}
19
20bool TableBinding::HasMatchingBinding(const string &column_name) {
21 return table.ColumnExists(column_name);
22}
23
24BindResult TableBinding::Bind(ColumnRefExpression &colref, idx_t depth) {
25 auto entry = table.name_map.find(colref.column_name);
26 if (entry == table.name_map.end()) {
27 return BindResult(StringUtil::Format("Table \"%s\" does not have a column named \"%s\"",
28 colref.table_name.c_str(), colref.column_name.c_str()));
29 }
30 auto col_index = entry->second;
31 // fetch the type of the column
32 SQLType col_type;
33 if (entry->second == COLUMN_IDENTIFIER_ROW_ID) {
34 // row id: BIGINT type
35 col_type = SQLType::BIGINT;
36 } else {
37 // normal column: fetch type from base column
38 auto &col = table.columns[col_index];
39 col_type = col.type;
40 }
41
42 auto &column_ids = get.column_ids;
43 // check if the entry already exists in the column list for the table
44 ColumnBinding binding;
45
46 binding.column_index = column_ids.size();
47 for (idx_t i = 0; i < column_ids.size(); i++) {
48 if (column_ids[i] == col_index) {
49 binding.column_index = i;
50 break;
51 }
52 }
53 if (binding.column_index == column_ids.size()) {
54 // column binding not found: add it to the list of bindings
55 column_ids.push_back(col_index);
56 }
57 binding.table_index = index;
58 return BindResult(
59 make_unique<BoundColumnRefExpression>(colref.GetName(), GetInternalType(col_type), binding, depth), col_type);
60}
61
62void TableBinding::GenerateAllColumnExpressions(BindContext &context,
63 vector<unique_ptr<ParsedExpression>> &select_list) {
64 for (auto &column : table.columns) {
65 if (context.BindingIsHidden(alias, column.name)) {
66 continue;
67 }
68 assert(!column.name.empty());
69 select_list.push_back(make_unique<ColumnRefExpression>(column.name, alias));
70 }
71}
72
73GenericBinding::GenericBinding(const string &alias, vector<SQLType> coltypes, vector<string> colnames, idx_t index)
74 : Binding(BindingType::GENERIC, alias, index), types(move(coltypes)), names(move(colnames)) {
75 assert(types.size() == names.size());
76 for (idx_t i = 0; i < names.size(); i++) {
77 auto &name = names[i];
78 assert(!name.empty());
79 if (name_map.find(name) != name_map.end()) {
80 throw BinderException("table \"%s\" has duplicate column name \"%s\"", alias.c_str(), name.c_str());
81 }
82 name_map[name] = i;
83 }
84}
85
86bool GenericBinding::HasMatchingBinding(const string &column_name) {
87 auto entry = name_map.find(column_name);
88 return entry != name_map.end();
89}
90
91BindResult GenericBinding::Bind(ColumnRefExpression &colref, idx_t depth) {
92 auto column_entry = name_map.find(colref.column_name);
93 if (column_entry == name_map.end()) {
94 return BindResult(StringUtil::Format("Values list \"%s\" does not have a column named \"%s\"", alias.c_str(),
95 colref.column_name.c_str()));
96 }
97 ColumnBinding binding;
98 binding.table_index = index;
99 binding.column_index = column_entry->second;
100 SQLType sql_type = types[column_entry->second];
101 return BindResult(
102 make_unique<BoundColumnRefExpression>(colref.GetName(), GetInternalType(sql_type), binding, depth), sql_type);
103}
104
105void GenericBinding::GenerateAllColumnExpressions(BindContext &context,
106 vector<unique_ptr<ParsedExpression>> &select_list) {
107 for (auto &column_name : names) {
108 assert(!column_name.empty());
109 if (context.BindingIsHidden(alias, column_name)) {
110 continue;
111 }
112 select_list.push_back(make_unique<ColumnRefExpression>(column_name, alias));
113 }
114}
115