| 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 | |
| 17 | namespace duckdb { |
| 18 | class BindContext; |
| 19 | class BoundQueryNode; |
| 20 | class ColumnRefExpression; |
| 21 | class SubqueryRef; |
| 22 | class LogicalGet; |
| 23 | class TableCatalogEntry; |
| 24 | class TableFunctionCatalogEntry; |
| 25 | |
| 26 | enum 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. |
| 30 | struct 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 | |
| 39 | public: |
| 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 |
| 47 | struct TableBinding : public Binding { |
| 48 | TableBinding(const string &alias, TableCatalogEntry &table, LogicalGet &get, idx_t index); |
| 49 | |
| 50 | TableCatalogEntry &table; |
| 51 | LogicalGet &get; |
| 52 | |
| 53 | public: |
| 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 |
| 60 | struct 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 | |
| 69 | public: |
| 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 | |