Skip to content

Commit 9dc7370

Browse files
Merge pull request ClickHouse#88843 from ClickHouse/backport/25.8/87789
Backport ClickHouse#87789 to 25.8: Fix Insert Select with CTE
2 parents 0da2a01 + 5c7cc01 commit 9dc7370

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/Interpreters/InterpreterInsertQuery.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <Core/ServerSettings.h>
1010
#include <DataTypes/DataTypeNullable.h>
1111
#include <IO/ReadBuffer.h>
12+
#include <Interpreters/ApplyWithAliasVisitor.h>
13+
#include <Interpreters/ApplyWithSubqueryVisitor.h>
1214
#include <Interpreters/DatabaseCatalog.h>
1315
#include <Interpreters/InterpreterSelectWithUnionQuery.h>
1416
#include <Interpreters/InterpreterWatchQuery.h>
@@ -80,6 +82,7 @@ namespace Setting
8082
extern const SettingsBool async_query_sending_for_remote;
8183
extern const SettingsBool async_socket_for_remote;
8284
extern const SettingsUInt64 max_distributed_depth;
85+
extern const SettingsBool enable_global_with_statement;
8386
}
8487

8588
namespace MergeTreeSetting
@@ -760,6 +763,10 @@ InterpreterInsertQuery::distributedWriteIntoReplicatedMergeTreeFromClusterStorag
760763
{
761764
if (auto * select_query = select.list_of_selects->children.at(0)->as<ASTSelectQuery>())
762765
{
766+
if (local_context->getSettingsRef()[Setting::enable_global_with_statement])
767+
ApplyWithAliasVisitor::visit(select.list_of_selects->children.at(0));
768+
ApplyWithSubqueryVisitor(local_context).visit(select.list_of_selects->children.at(0));
769+
763770
JoinedTables joined_tables(Context::createCopy(local_context), *select_query);
764771
if (joined_tables.tablesCount() == 1)
765772
src_storage = joined_tables.getLeftTableStorage();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
SET enable_analyzer=1; -- parallel distributed insert select for replicated tables works only with analyzer
2+
SET parallel_distributed_insert_select=2;
3+
SET enable_global_with_statement=1;
4+
5+
DROP TABLE IF EXISTS test_insert SYNC;
6+
7+
CREATE TABLE test_insert (c1 String, c2 UInt8)
8+
ENGINE = ReplicatedMergeTree('/clickhouse/tables/{database}/test_03632/tables/test_insert', '{replica}')
9+
ORDER BY ();
10+
11+
INSERT INTO test_insert
12+
WITH cte_test AS (SELECT '1234', 1)
13+
SELECT * FROM cte_test;
14+
15+
SELECT count() FROM test_insert;
16+
17+
DROP TABLE test_insert;

0 commit comments

Comments
 (0)