1#include "duckdb/parser/tableref/subqueryref.hpp"
2#include "duckdb/planner/binder.hpp"
3#include "duckdb/planner/tableref/bound_subqueryref.hpp"
4
5namespace duckdb {
6
7unique_ptr<BoundTableRef> Binder::Bind(SubqueryRef &ref, optional_ptr<CommonTableExpressionInfo> cte) {
8 auto binder = Binder::CreateBinder(context, parent: this);
9 binder->can_contain_nulls = true;
10 if (cte) {
11 binder->bound_ctes.insert(x: *cte);
12 }
13 binder->alias = ref.alias.empty() ? "unnamed_subquery" : ref.alias;
14 auto subquery = binder->BindNode(node&: *ref.subquery->node);
15 idx_t bind_index = subquery->GetRootIndex();
16 string subquery_alias;
17 if (ref.alias.empty()) {
18 subquery_alias = "unnamed_subquery" + to_string(val: bind_index);
19 } else {
20 subquery_alias = ref.alias;
21 }
22 auto result = make_uniq<BoundSubqueryRef>(args: std::move(binder), args: std::move(subquery));
23 bind_context.AddSubquery(index: bind_index, alias: subquery_alias, ref, subquery&: *result->subquery);
24 MoveCorrelatedExpressions(other&: *result->binder);
25 return std::move(result);
26}
27
28} // namespace duckdb
29