Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional check for overlapping CTE names #12305

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions src/planner/binder/tableref/bind_basetableref.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include "duckdb/catalog/catalog_entry/table_catalog_entry.hpp"
#include "duckdb/catalog/catalog_entry/view_catalog_entry.hpp"
#include "duckdb/common/string_util.hpp"
#include "duckdb/main/client_context.hpp"
#include "duckdb/main/config.hpp"
#include "duckdb/main/extension_helper.hpp"
#include "duckdb/parser/query_node/select_node.hpp"
#include "duckdb/parser/statement/select_statement.hpp"
#include "duckdb/parser/tableref/basetableref.hpp"
#include "duckdb/parser/tableref/subqueryref.hpp"
#include "duckdb/parser/query_node/select_node.hpp"
#include "duckdb/parser/tableref/table_function_ref.hpp"
#include "duckdb/planner/binder.hpp"
#include "duckdb/planner/operator/logical_get.hpp"
#include "duckdb/planner/tableref/bound_basetableref.hpp"
#include "duckdb/planner/tableref/bound_subqueryref.hpp"
#include "duckdb/planner/tableref/bound_cteref.hpp"
#include "duckdb/planner/operator/logical_get.hpp"
#include "duckdb/parser/statement/select_statement.hpp"
#include "duckdb/common/string_util.hpp"
#include "duckdb/main/extension_helper.hpp"
#include "duckdb/parser/tableref/table_function_ref.hpp"
#include "duckdb/main/config.hpp"
#include "duckdb/planner/tableref/bound_dummytableref.hpp"
#include "duckdb/main/client_context.hpp"
#include "duckdb/planner/tableref/bound_subqueryref.hpp"

namespace duckdb {

Expand Down Expand Up @@ -93,26 +93,8 @@ unique_ptr<BoundTableRef> Binder::Bind(BaseTableRef &ref) {
for (auto found_cte : found_ctes) {
auto &cte = found_cte.get();
auto ctebinding = bind_context.GetCTEBinding(ref.table_name);
if (!ctebinding) {
if (CTEIsAlreadyBound(cte)) {
// remember error state
circular_cte = true;
// retry with next candidate CTE
continue;
}
// Move CTE to subquery and bind recursively
SubqueryRef subquery(unique_ptr_cast<SQLStatement, SelectStatement>(cte.query->Copy()));
subquery.alias = ref.alias.empty() ? ref.table_name : ref.alias;
subquery.column_name_alias = cte.aliases;
for (idx_t i = 0; i < ref.column_name_alias.size(); i++) {
if (i < subquery.column_name_alias.size()) {
subquery.column_name_alias[i] = ref.column_name_alias[i];
} else {
subquery.column_name_alias.push_back(ref.column_name_alias[i]);
}
}
return Bind(subquery, &found_cte.get());
} else {
if (ctebinding && (cte.query->node->type == QueryNodeType::RECURSIVE_CTE_NODE ||
cte.materialized == CTEMaterialize::CTE_MATERIALIZE_ALWAYS)) {
// There is a CTE binding in the BindContext.
// This can only be the case if there is a recursive CTE,
// or a materialized CTE present.
Expand All @@ -137,6 +119,25 @@ unique_ptr<BoundTableRef> Binder::Bind(BaseTableRef &ref) {
result->types = ctebinding->types;
result->bound_columns = std::move(names);
return std::move(result);
} else {
if (CTEIsAlreadyBound(cte)) {
// remember error state
circular_cte = true;
// retry with next candidate CTE
continue;
}
// Move CTE to subquery and bind recursively
SubqueryRef subquery(unique_ptr_cast<SQLStatement, SelectStatement>(cte.query->Copy()));
subquery.alias = ref.alias.empty() ? ref.table_name : ref.alias;
subquery.column_name_alias = cte.aliases;
for (idx_t i = 0; i < ref.column_name_alias.size(); i++) {
if (i < subquery.column_name_alias.size()) {
subquery.column_name_alias[i] = ref.column_name_alias[i];
} else {
subquery.column_name_alias.push_back(ref.column_name_alias[i]);
}
}
return Bind(subquery, &found_cte.get());
}
}
if (circular_cte) {
Expand Down
5 changes: 5 additions & 0 deletions test/sql/cte/materialized/test_cte_in_cte_materialized.test
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ with cte1 as MATERIALIZED (Select i as j from a) select * from cte1 where j = (w
----
42

query I
with cte as materialized (Select i as j from a) select * from cte where j = (with cte as (select max(j) as j from cte) select j from cte);
----
42

require noalternativeverify

# refer to same-named CTE in a subquery expression
Expand Down
Loading