Skip to content

Commit cb4d261

Browse files
committed
add recursive cte bindings
1 parent 8e5aca2 commit cb4d261

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/planner/binder/tableref/bind_basetableref.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ unique_ptr<BoundTableRef> Binder::Bind(BaseTableRef &ref) {
189189
"referenced from this part of the query.",
190190
ref.table_name);
191191
}
192+
// Move CTE to subquery and bind recursively
193+
SubqueryRef subquery(unique_ptr_cast<SQLStatement, SelectStatement>(cte.query->Copy()));
194+
subquery.alias = ref.alias.empty() ? ref.table_name : ref.alias;
195+
subquery.column_name_alias = cte.aliases;
196+
for (idx_t i = 0; i < ref.column_name_alias.size(); i++) {
197+
if (i < subquery.column_name_alias.size()) {
198+
subquery.column_name_alias[i] = ref.column_name_alias[i];
199+
} else {
200+
subquery.column_name_alias.push_back(ref.column_name_alias[i]);
201+
}
202+
}
203+
return Bind(subquery, &found_cte.get());
192204
}
193205
}
194206
if (circular_cte) {

0 commit comments

Comments
 (0)