| 1 | #include "duckdb/catalog/catalog_entry/table_catalog_entry.hpp" |
| 2 | #include "duckdb/catalog/catalog_entry/view_catalog_entry.hpp" |
| 3 | #include "duckdb/parser/tableref/basetableref.hpp" |
| 4 | #include "duckdb/parser/tableref/subqueryref.hpp" |
| 5 | #include "duckdb/parser/query_node/select_node.hpp" |
| 6 | #include "duckdb/planner/binder.hpp" |
| 7 | #include "duckdb/planner/tableref/bound_basetableref.hpp" |
| 8 | #include "duckdb/planner/tableref/bound_subqueryref.hpp" |
| 9 | #include "duckdb/planner/tableref/bound_cteref.hpp" |
| 10 | #include "duckdb/planner/operator/logical_get.hpp" |
| 11 | #include "duckdb/parser/statement/select_statement.hpp" |
| 12 | #include "duckdb/common/string_util.hpp" |
| 13 | #include "duckdb/parser/tableref/table_function_ref.hpp" |
| 14 | #include "duckdb/main/config.hpp" |
| 15 | #include "duckdb/planner/tableref/bound_dummytableref.hpp" |
| 16 | #include "duckdb/main/client_context.hpp" |
| 17 | |
| 18 | namespace duckdb { |
| 19 | |
| 20 | unique_ptr<BoundTableRef> Binder::Bind(BaseTableRef &ref) { |
| 21 | QueryErrorContext error_context(root_statement, ref.query_location); |
| 22 | // CTEs and views are also referred to using BaseTableRefs, hence need to distinguish here |
| 23 | // check if the table name refers to a CTE |
| 24 | auto found_cte = FindCTE(name: ref.table_name, skip: ref.table_name == alias); |
| 25 | if (found_cte) { |
| 26 | // Check if there is a CTE binding in the BindContext |
| 27 | auto &cte = *found_cte; |
| 28 | auto ctebinding = bind_context.GetCTEBinding(ctename: ref.table_name); |
| 29 | if (!ctebinding) { |
| 30 | if (CTEIsAlreadyBound(cte)) { |
| 31 | throw BinderException("Circular reference to CTE \"%s\", use WITH RECURSIVE to use recursive CTEs" , |
| 32 | ref.table_name); |
| 33 | } |
| 34 | // Move CTE to subquery and bind recursively |
| 35 | SubqueryRef subquery(unique_ptr_cast<SQLStatement, SelectStatement>(src: cte.query->Copy())); |
| 36 | subquery.alias = ref.alias.empty() ? ref.table_name : ref.alias; |
| 37 | subquery.column_name_alias = cte.aliases; |
| 38 | for (idx_t i = 0; i < ref.column_name_alias.size(); i++) { |
| 39 | if (i < subquery.column_name_alias.size()) { |
| 40 | subquery.column_name_alias[i] = ref.column_name_alias[i]; |
| 41 | } else { |
| 42 | subquery.column_name_alias.push_back(x: ref.column_name_alias[i]); |
| 43 | } |
| 44 | } |
| 45 | return Bind(ref&: subquery, cte: found_cte); |
| 46 | } else { |
| 47 | // There is a CTE binding in the BindContext. |
| 48 | // This can only be the case if there is a recursive CTE present. |
| 49 | auto index = GenerateTableIndex(); |
| 50 | auto result = make_uniq<BoundCTERef>(args&: index, args&: ctebinding->index); |
| 51 | auto b = ctebinding; |
| 52 | auto alias = ref.alias.empty() ? ref.table_name : ref.alias; |
| 53 | auto names = BindContext::AliasColumnNames(table_name: alias, names: b->names, column_aliases: ref.column_name_alias); |
| 54 | |
| 55 | bind_context.AddGenericBinding(index, alias, names, types: b->types); |
| 56 | // Update references to CTE |
| 57 | auto cteref = bind_context.cte_references[ref.table_name]; |
| 58 | (*cteref)++; |
| 59 | |
| 60 | result->types = b->types; |
| 61 | result->bound_columns = std::move(names); |
| 62 | return std::move(result); |
| 63 | } |
| 64 | } |
| 65 | // not a CTE |
| 66 | // extract a table or view from the catalog |
| 67 | BindSchemaOrCatalog(catalog_name&: ref.catalog_name, schema_name&: ref.schema_name); |
| 68 | auto table_or_view = Catalog::GetEntry(context, type: CatalogType::TABLE_ENTRY, catalog: ref.catalog_name, schema: ref.schema_name, |
| 69 | name: ref.table_name, if_not_found: OnEntryNotFound::RETURN_NULL, error_context); |
| 70 | // we still didn't find the table |
| 71 | if (GetBindingMode() == BindingMode::EXTRACT_NAMES) { |
| 72 | if (!table_or_view || table_or_view->type == CatalogType::TABLE_ENTRY) { |
| 73 | // if we are in EXTRACT_NAMES, we create a dummy table ref |
| 74 | AddTableName(table_name: ref.table_name); |
| 75 | |
| 76 | // add a bind context entry |
| 77 | auto table_index = GenerateTableIndex(); |
| 78 | auto alias = ref.alias.empty() ? ref.table_name : ref.alias; |
| 79 | vector<LogicalType> types {LogicalType::INTEGER}; |
| 80 | vector<string> names {"__dummy_col" + to_string(val: table_index)}; |
| 81 | bind_context.AddGenericBinding(index: table_index, alias, names, types); |
| 82 | return make_uniq_base<BoundTableRef, BoundEmptyTableRef>(args&: table_index); |
| 83 | } |
| 84 | } |
| 85 | if (!table_or_view) { |
| 86 | string table_name = ref.catalog_name; |
| 87 | if (!ref.schema_name.empty()) { |
| 88 | table_name += (!table_name.empty() ? "." : "" ) + ref.schema_name; |
| 89 | } |
| 90 | table_name += (!table_name.empty() ? "." : "" ) + ref.table_name; |
| 91 | // table could not be found: try to bind a replacement scan |
| 92 | auto &config = DBConfig::GetConfig(context); |
| 93 | if (context.config.use_replacement_scans) { |
| 94 | for (auto &scan : config.replacement_scans) { |
| 95 | auto replacement_function = scan.function(context, table_name, scan.data.get()); |
| 96 | if (replacement_function) { |
| 97 | if (!ref.alias.empty()) { |
| 98 | // user-provided alias overrides the default alias |
| 99 | replacement_function->alias = ref.alias; |
| 100 | } else if (replacement_function->alias.empty()) { |
| 101 | // if the replacement scan itself did not provide an alias we use the table name |
| 102 | replacement_function->alias = ref.table_name; |
| 103 | } |
| 104 | if (replacement_function->type == TableReferenceType::TABLE_FUNCTION) { |
| 105 | auto &table_function = replacement_function->Cast<TableFunctionRef>(); |
| 106 | table_function.column_name_alias = ref.column_name_alias; |
| 107 | } else if (replacement_function->type == TableReferenceType::SUBQUERY) { |
| 108 | auto &subquery = replacement_function->Cast<SubqueryRef>(); |
| 109 | subquery.column_name_alias = ref.column_name_alias; |
| 110 | } else { |
| 111 | throw InternalException("Replacement scan should return either a table function or a subquery" ); |
| 112 | } |
| 113 | return Bind(ref&: *replacement_function); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // could not find an alternative: bind again to get the error |
| 119 | table_or_view = Catalog::GetEntry(context, type: CatalogType::TABLE_ENTRY, catalog: ref.catalog_name, schema: ref.schema_name, |
| 120 | name: ref.table_name, if_not_found: OnEntryNotFound::THROW_EXCEPTION, error_context); |
| 121 | } |
| 122 | switch (table_or_view->type) { |
| 123 | case CatalogType::TABLE_ENTRY: { |
| 124 | // base table: create the BoundBaseTableRef node |
| 125 | auto table_index = GenerateTableIndex(); |
| 126 | auto &table = table_or_view->Cast<TableCatalogEntry>(); |
| 127 | |
| 128 | unique_ptr<FunctionData> bind_data; |
| 129 | auto scan_function = table.GetScanFunction(context, bind_data); |
| 130 | auto alias = ref.alias.empty() ? ref.table_name : ref.alias; |
| 131 | // TODO: bundle the type and name vector in a struct (e.g PackedColumnMetadata) |
| 132 | vector<LogicalType> table_types; |
| 133 | vector<string> table_names; |
| 134 | vector<TableColumnType> table_categories; |
| 135 | |
| 136 | vector<LogicalType> return_types; |
| 137 | vector<string> return_names; |
| 138 | for (auto &col : table.GetColumns().Logical()) { |
| 139 | table_types.push_back(x: col.Type()); |
| 140 | table_names.push_back(x: col.Name()); |
| 141 | return_types.push_back(x: col.Type()); |
| 142 | return_names.push_back(x: col.Name()); |
| 143 | } |
| 144 | table_names = BindContext::AliasColumnNames(table_name: alias, names: table_names, column_aliases: ref.column_name_alias); |
| 145 | |
| 146 | auto logical_get = make_uniq<LogicalGet>(args&: table_index, args&: scan_function, args: std::move(bind_data), |
| 147 | args: std::move(return_types), args: std::move(return_names)); |
| 148 | bind_context.AddBaseTable(index: table_index, alias, names: table_names, types: table_types, bound_column_ids&: logical_get->column_ids, |
| 149 | entry: logical_get->GetTable().get()); |
| 150 | return make_uniq_base<BoundTableRef, BoundBaseTableRef>(args&: table, args: std::move(logical_get)); |
| 151 | } |
| 152 | case CatalogType::VIEW_ENTRY: { |
| 153 | // the node is a view: get the query that the view represents |
| 154 | auto &view_catalog_entry = table_or_view->Cast<ViewCatalogEntry>(); |
| 155 | // We need to use a new binder for the view that doesn't reference any CTEs |
| 156 | // defined for this binder so there are no collisions between the CTEs defined |
| 157 | // for the view and for the current query |
| 158 | bool inherit_ctes = false; |
| 159 | auto view_binder = Binder::CreateBinder(context, parent: this, inherit_ctes); |
| 160 | view_binder->can_contain_nulls = true; |
| 161 | SubqueryRef subquery(unique_ptr_cast<SQLStatement, SelectStatement>(src: view_catalog_entry.query->Copy())); |
| 162 | subquery.alias = ref.alias.empty() ? ref.table_name : ref.alias; |
| 163 | subquery.column_name_alias = |
| 164 | BindContext::AliasColumnNames(table_name: subquery.alias, names: view_catalog_entry.aliases, column_aliases: ref.column_name_alias); |
| 165 | // bind the child subquery |
| 166 | view_binder->AddBoundView(view&: view_catalog_entry); |
| 167 | auto bound_child = view_binder->Bind(ref&: subquery); |
| 168 | if (!view_binder->correlated_columns.empty()) { |
| 169 | throw BinderException("Contents of view were altered - view bound correlated columns" ); |
| 170 | } |
| 171 | |
| 172 | D_ASSERT(bound_child->type == TableReferenceType::SUBQUERY); |
| 173 | // verify that the types and names match up with the expected types and names |
| 174 | auto &bound_subquery = bound_child->Cast<BoundSubqueryRef>(); |
| 175 | if (GetBindingMode() != BindingMode::EXTRACT_NAMES && |
| 176 | bound_subquery.subquery->types != view_catalog_entry.types) { |
| 177 | throw BinderException("Contents of view were altered: types don't match!" ); |
| 178 | } |
| 179 | bind_context.AddView(index: bound_subquery.subquery->GetRootIndex(), alias: subquery.alias, ref&: subquery, |
| 180 | subquery&: *bound_subquery.subquery, view: &view_catalog_entry); |
| 181 | return bound_child; |
| 182 | } |
| 183 | default: |
| 184 | throw InternalException("Catalog entry type" ); |
| 185 | } |
| 186 | } |
| 187 | } // namespace duckdb |
| 188 | |