Skip to content

Commit

Permalink
Fix missing materialized CTE aliases bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kryonix committed Mar 7, 2024
1 parent 04c85b7 commit 94a3bd3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/planner/binder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ unique_ptr<BoundCTENode> Binder::BindMaterializedCTE(CommonTableExpressionMap &c
auto mat_cte = make_uniq<CTENode>();
mat_cte->ctename = cte.first;
mat_cte->query = cte_entry->query->node->Copy();
mat_cte->aliases = cte_entry->aliases;
materialized_ctes.push_back(std::move(mat_cte));
}
}
Expand Down Expand Up @@ -117,6 +118,15 @@ BoundStatement Binder::BindWithCTE(T &statement) {

bound_statement = tail->child_binder->Bind(statement.template Cast<T>());

tail->types = bound_statement.types;
tail->names = bound_statement.names;

for (auto &c : tail->query_binder->correlated_columns) {
tail->child_binder->AddCorrelatedColumn(c);
}

MoveCorrelatedExpressions(*tail->child_binder);

// extract operator below root operation
auto plan = std::move(bound_statement.plan->children[0]);
bound_statement.plan->children.clear();
Expand Down
8 changes: 8 additions & 0 deletions test/sql/cte/materialized/dml_materialized_cte.test
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ statement ok
WITH t(x) AS MATERIALIZED (SELECT 1),
u(x) AS MATERIALIZED (SELECT 2 UNION ALL SELECT * FROM t)
INSERT INTO a (SELECT * FROM u);

statement ok
WITH a(x) AS MATERIALIZED (SELECT 1)
DELETE FROM t WHERE x IN (WITH s(x) AS MATERIALIZED (SELECT x + 41 FROM a) SELECT * FROM a);

statement ok
WITH a(x) AS MATERIALIZED (SELECT 1)
DELETE FROM t WHERE x IN (WITH s(x) AS MATERIALIZED (SELECT x + 41 FROM a) SELECT * FROM s);

0 comments on commit 94a3bd3

Please sign in to comment.