From e5a2bdfa04cf17d796aa622cca5bfc5c3fae5f69 Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Fri, 17 May 2024 04:06:22 +0000 Subject: [PATCH] Backport #63930 to 24.3: Fix analyzer: there's turtles all the way down... --- src/Analyzer/Passes/QueryAnalysisPass.cpp | 8 ++++++-- .../03155_in_nested_subselects.reference | 4 ++++ .../03155_in_nested_subselects.sql | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 tests/queries/0_stateless/03155_in_nested_subselects.reference create mode 100644 tests/queries/0_stateless/03155_in_nested_subselects.sql diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index 83d2b6d8480..313d9d7d43f 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -5604,9 +5604,13 @@ ProjectionNames QueryAnalyzer::resolveFunction(QueryTreeNodePtr & node, Identifi /// Replace storage with values storage of insertion block if (StoragePtr storage = scope.context->getViewSource()) { - if (auto * query_node = in_second_argument->as()) + QueryTreeNodePtr table_expression; + /// Process possibly nested sub-selects + for (auto * query_node = in_second_argument->as(); query_node; query_node = table_expression->as()) + table_expression = extractLeftTableExpression(query_node->getJoinTree()); + + if (table_expression) { - auto table_expression = extractLeftTableExpression(query_node->getJoinTree()); if (auto * query_table_node = table_expression->as()) { if (query_table_node->getStorageID().getFullNameNotQuoted() == storage->getStorageID().getFullNameNotQuoted()) diff --git a/tests/queries/0_stateless/03155_in_nested_subselects.reference b/tests/queries/0_stateless/03155_in_nested_subselects.reference new file mode 100644 index 00000000000..5565ed6787f --- /dev/null +++ b/tests/queries/0_stateless/03155_in_nested_subselects.reference @@ -0,0 +1,4 @@ +0 +1 +0 +1 diff --git a/tests/queries/0_stateless/03155_in_nested_subselects.sql b/tests/queries/0_stateless/03155_in_nested_subselects.sql new file mode 100644 index 00000000000..4f5ccd30aa3 --- /dev/null +++ b/tests/queries/0_stateless/03155_in_nested_subselects.sql @@ -0,0 +1,19 @@ +-- https://github.com/ClickHouse/ClickHouse/issues/63833 +SET allow_experimental_analyzer = 1; + +create table Example (id Int32) engine = MergeTree ORDER BY id; +INSERT INTO Example SELECT number AS id FROM numbers(2); + +create table Null engine=Null as Example ; +--create table Null engine=MergeTree order by id as Example ; + +create materialized view Transform to Example as +select * from Null +join ( select * FROM Example + WHERE id IN (SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT * FROM Null))))) + ) as old +using id; + +INSERT INTO Null SELECT number AS id FROM numbers(2); + +select * from Example; -- should return 4 rows