
**The creation of table becomes too slow when I use CTEs within the query.**

***ClickHouse server version 22.4.3***

**How to reproduce**
Trying to create a table based on the result of a query, I take very bad performance when I use a CTE:

```
--creation of table t and insertion of 10000000000 records
--exec time: ~260 second
drop table if exists t;
create table t(a Int64) engine = MergeTree() order by a;
insert into t SELECT * FROM system.numbers LIMIT 10000000000;


--scenario 1 
--exec time: ~200 seconds
drop table if exists t2;
create table t2 engine = MergeTree() order by c
as (

with

cte1 as (
  SELECT * FROM t WHERE modulo(a,2)=1
)

SELECT count(*) as c FROM cte1 

);
--end of scenario 1


--scenario 2 
--exec time: ~10 seconds
drop table if exists t3;
create table t3 engine = MergeTree() order by c
as (

  SELECT count(*) as c FROM t WHERE modulo(a,2)=1

);
--end of scenario 2
```

Although the two scenarios do exactly the same thing, with the difference that in the first scenario we use an intermediate CTE, there is a huge difference in the execution time.

The issue becomes even stranger if you execute the following queries and see that the execution time is the same, that in generally makes sense.

```
--query from scenario 1 
--exec time: ~9 seconds
with

cte1 as (
  SELECT * FROM t WHERE modulo(a,2)=1
)

SELECT count(*) as c FROM t WHERE modulo(a,2)=1


--query from scenario 2
--exec time: ~9 seconds
SELECT count(*) as c FROM t WHERE modulo(a,2)=1
```

Obviously, there is a performance issue when someone wants to `CREATE TABLE AS` and uses CTEs in the query.

Does anyone know why clickhouse behaves in that strange way? 


*/
                Settings new_settings = getContext()->getSettings();
                new_settings.max_threads = std::max<UInt64>(1, settings.max_insert_threads);
                if (table->prefersLargeBlocks())
                {
                    if (settings.min_insert_block_size_rows)
                        new_settings.max_block_size = settings.min_insert_block_size_rows;
                    if (settings.min_insert_block_size_bytes)
                        new_settings.preferred_block_size_bytes = settings.min_insert_block_size_bytes;
                }
                auto new_context = Context::createCopy(context);
                new_context->setSettings(new_settings);
                new_context->setInsertionTable(getContext()->getInsertionTable());
                InterpreterSelectWithUnionQuery interpreter_select{
                    query.select, new_context, SelectQueryOptions(QueryProcessingStage::Complete, 1)};
                pipeline = interpreter_select.buildQueryPipeline();
            }
            else
            {
                
                InterpreterSelectWithUnionQuery interpreter_select{
                    query.select, getContext(), SelectQueryOptions(QueryProcessingStage::Complete, 1)};
                pipeline = interpreter_select.buildQueryPipeline();
            }
            pipeline.dropTotalsAndExtremes();
            if (table->supportsParallelInsert() && settings.max_insert_threads > 1)
                out_streams_size = std::min(static_cast<size_t>(settings.max_insert_threads), pipeline.getNumStreams());
            pipeline.resize(out_streams_size);
            
            if (getContext()->getSettingsRef().insert_null_as_default)