Merge pull request #65267 from wudidapaopao/fix_unexpected_cte_projection_name

Fix unexpected projection name when query with CTE
This commit is contained in:
Yakov Olkhovskiy 2024-07-15 02:14:42 +00:00 committed by GitHub
commit 77396d00d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 28 additions and 21 deletions

View File

@ -1490,6 +1490,10 @@ void QueryAnalyzer::qualifyColumnNodesWithProjectionNames(const QueryTreeNodes &
additional_column_qualification_parts = {table_expression_node->getAlias()};
else if (auto * table_node = table_expression_node->as<TableNode>())
additional_column_qualification_parts = {table_node->getStorageID().getDatabaseName(), table_node->getStorageID().getTableName()};
else if (auto * query_node = table_expression_node->as<QueryNode>(); query_node && query_node->isCTE())
additional_column_qualification_parts = {query_node->getCTEName()};
else if (auto * union_node = table_expression_node->as<UnionNode>(); union_node && union_node->isCTE())
additional_column_qualification_parts = {union_node->getCTEName()};
size_t additional_column_qualification_parts_size = additional_column_qualification_parts.size();
const auto & table_expression_data = scope.getTableExpressionDataOrThrow(table_expression_node);
@ -4455,9 +4459,8 @@ void QueryAnalyzer::initializeTableExpressionData(const QueryTreeNodePtr & table
{
auto left_table_expression = extractLeftTableExpression(scope_query_node->getJoinTree());
if (table_expression_node.get() == left_table_expression.get() &&
scope.joins_count == 1 &&
scope.context->getSettingsRef().single_join_prefer_left_table)
table_expression_data.should_qualify_columns = false;
scope.joins_count == 1 && scope.context->getSettingsRef().single_join_prefer_left_table)
table_expression_data.should_qualify_columns = false;
}
scope.table_expression_node_to_data.emplace(table_expression_node, std::move(table_expression_data));

View File

@ -443,6 +443,18 @@ SELECT '--';
--
DESCRIBE (WITH test_table_in_cte AS (SELECT id FROM test_table) SELECT id IN test_table_in_cte FROM test_table);
in(id, test_table_in_cte) UInt8
SELECT '--';
--
DESCRIBE (WITH test_table_in_cte_1 AS (SELECT 1 AS c1), test_table_in_cte_2 AS (SELECT 1 AS c1) SELECT *
FROM test_table_in_cte_1 INNER JOIN test_table_in_cte_2 as test_table_in_cte_2 ON test_table_in_cte_1.c1 = test_table_in_cte_2.c1);
test_table_in_cte_1.c1 UInt8
test_table_in_cte_2.c1 UInt8
SELECT '--';
--
DESCRIBE (WITH test_table_in_cte_1 AS (SELECT 1 AS c1), test_table_in_cte_2 AS (SELECT 1 AS c1 UNION ALL SELECT 1 AS c1) SELECT *
FROM test_table_in_cte_1 INNER JOIN test_table_in_cte_2 as test_table_in_cte_2 ON test_table_in_cte_1.c1 = test_table_in_cte_2.c1);
test_table_in_cte_1.c1 UInt8
test_table_in_cte_2.c1 UInt8
SELECT 'Joins';
Joins
DESCRIBE (SELECT * FROM test_table_join_1, test_table_join_2);

View File

@ -408,6 +408,16 @@ SELECT '--';
DESCRIBE (WITH test_table_in_cte AS (SELECT id FROM test_table) SELECT id IN test_table_in_cte FROM test_table);
SELECT '--';
DESCRIBE (WITH test_table_in_cte_1 AS (SELECT 1 AS c1), test_table_in_cte_2 AS (SELECT 1 AS c1) SELECT *
FROM test_table_in_cte_1 INNER JOIN test_table_in_cte_2 as test_table_in_cte_2 ON test_table_in_cte_1.c1 = test_table_in_cte_2.c1);
SELECT '--';
DESCRIBE (WITH test_table_in_cte_1 AS (SELECT 1 AS c1), test_table_in_cte_2 AS (SELECT 1 AS c1 UNION ALL SELECT 1 AS c1) SELECT *
FROM test_table_in_cte_1 INNER JOIN test_table_in_cte_2 as test_table_in_cte_2 ON test_table_in_cte_1.c1 = test_table_in_cte_2.c1);
SELECT 'Joins';
DESCRIBE (SELECT * FROM test_table_join_1, test_table_join_2);

View File

@ -1,17 +0,0 @@
-- https://github.com/ClickHouse/ClickHouse/issues/22627
SET allow_experimental_analyzer=1;
WITH
x AS
(
SELECT 1 AS a
),
xx AS
(
SELECT *
FROM x
, x AS x1
, x AS x2
)
SELECT *
FROM xx
WHERE a = 1;