diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 4b89273cd86..1158007d957 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -294,7 +294,7 @@ InterpreterSelectQuery::InterpreterSelectQuery( } // Only propagate WITH elements to subqueries if we're not a subquery - if (options.subquery_depth == 0) + if (!options.is_subquery) { if (context->getSettingsRef().enable_global_with_statement) ApplyWithAliasVisitor().visit(query_ptr); diff --git a/src/Interpreters/SelectQueryOptions.h b/src/Interpreters/SelectQueryOptions.h index 611c2b1601e..124b5a6daa0 100644 --- a/src/Interpreters/SelectQueryOptions.h +++ b/src/Interpreters/SelectQueryOptions.h @@ -33,6 +33,7 @@ struct SelectQueryOptions bool ignore_quota = false; bool ignore_limits = false; bool is_internal = false; + bool is_subquery = false; // non-subquery can also have subquery_depth > 0, e.g. insert select SelectQueryOptions(QueryProcessingStage::Enum stage = QueryProcessingStage::Complete, size_t depth = 0) : to_stage(stage), subquery_depth(depth) @@ -46,6 +47,7 @@ struct SelectQueryOptions SelectQueryOptions out = *this; out.to_stage = QueryProcessingStage::Complete; ++out.subquery_depth; + out.is_subquery = true; return out; } diff --git a/tests/queries/0_stateless/01711_cte_subquery_fix.reference b/tests/queries/0_stateless/01711_cte_subquery_fix.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/01711_cte_subquery_fix.sql b/tests/queries/0_stateless/01711_cte_subquery_fix.sql new file mode 100644 index 00000000000..19eed6cc612 --- /dev/null +++ b/tests/queries/0_stateless/01711_cte_subquery_fix.sql @@ -0,0 +1,2 @@ +create or replace table t engine = Memory as with cte as (select * from numbers(10)) select * from cte; +drop table t;