1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/planner/table_binding.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/common.hpp"
12#include "duckdb/common/unordered_map.hpp"
13#include "duckdb/parser/column_definition.hpp"
14#include "duckdb/parser/parsed_expression.hpp"
15#include "duckdb/planner/expression_binder.hpp"
16
17namespace duckdb {
18class BindContext;
19class BoundQueryNode;
20class ColumnRefExpression;
21class SubqueryRef;
22class LogicalGet;
23class TableCatalogEntry;
24class TableFunctionCatalogEntry;
25
26enum class BindingType : uint8_t { TABLE = 0, SUBQUERY = 1, TABLE_FUNCTION = 2, GENERIC = 3 };
27
28//! A Binding represents a binding to a table, table-producing function or subquery with a specified table index. Used
29//! in the binder.
30struct Binding {
31 Binding(BindingType type, const string &alias, idx_t index) : type(type), alias(alias), index(index) {
32 }
33 virtual ~Binding() = default;
34
35 BindingType type;
36 string alias;
37 idx_t index;
38
39public:
40 virtual bool HasMatchingBinding(const string &column_name) = 0;
41 virtual BindResult Bind(ColumnRefExpression &colref, idx_t depth) = 0;
42 virtual void GenerateAllColumnExpressions(BindContext &context,
43 vector<unique_ptr<ParsedExpression>> &select_list) = 0;
44};
45
46//! Represents a binding to a base table
47struct TableBinding : public Binding {
48 TableBinding(const string &alias, TableCatalogEntry &table, LogicalGet &get, idx_t index);
49
50 TableCatalogEntry &table;
51 LogicalGet &get;
52
53public:
54 bool HasMatchingBinding(const string &column_name) override;
55 BindResult Bind(ColumnRefExpression &colref, idx_t depth) override;
56 void GenerateAllColumnExpressions(BindContext &context, vector<unique_ptr<ParsedExpression>> &select_list) override;
57};
58
59//! Represents a generic binding with types and names
60struct GenericBinding : public Binding {
61 GenericBinding(const string &alias, vector<SQLType> types, vector<string> names, idx_t index);
62
63 vector<SQLType> types;
64 //! Column names of the subquery
65 vector<string> names;
66 //! Name -> index for the names
67 unordered_map<string, uint64_t> name_map;
68
69public:
70 bool HasMatchingBinding(const string &column_name) override;
71 BindResult Bind(ColumnRefExpression &colref, idx_t depth) override;
72 void GenerateAllColumnExpressions(BindContext &context, vector<unique_ptr<ParsedExpression>> &select_list) override;
73};
74
75} // namespace duckdb
76