From 85e57700aa271590c80542482c8dac8b503055fb Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Tue, 9 Jul 2024 17:40:53 +0200 Subject: [PATCH] WIP on resolve from parent scopes --- src/Analyzer/Resolve/QueryAnalyzer.cpp | 28 ++++++++++++++++---------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Analyzer/Resolve/QueryAnalyzer.cpp b/src/Analyzer/Resolve/QueryAnalyzer.cpp index c9f70da1597..8be555b365b 100644 --- a/src/Analyzer/Resolve/QueryAnalyzer.cpp +++ b/src/Analyzer/Resolve/QueryAnalyzer.cpp @@ -472,6 +472,7 @@ void QueryAnalyzer::evaluateScalarSubqueryIfNeeded(QueryTreeNodePtr & node, Iden "Node must have query or union type. Actual {} {}", node->getNodeTypeName(), node->formatASTForErrorMessage()); + LOG_DEBUG(&Poco::Logger::get("evaluateScalarSubqueryIfNeeded"), "QT:\n{}", node->dumpTree()); auto & context = scope.context; @@ -1251,24 +1252,29 @@ IdentifierResolveResult QueryAnalyzer::tryResolveIdentifierInParentScopes(const if (resolved_identifier) { - auto * subquery_node = resolved_identifier->as(); - auto * union_node = resolved_identifier->as(); - - bool is_cte = (subquery_node && subquery_node->isCTE()) || (union_node && union_node->isCTE()); - bool is_table_from_expression_arguments = lookup_result.resolve_place == IdentifierResolvePlace::EXPRESSION_ARGUMENTS && - resolved_identifier->getNodeType() == QueryTreeNodeType::TABLE; - bool is_valid_table_expression = is_cte || is_table_from_expression_arguments; - /** From parent scopes we can resolve table identifiers only as CTE. * Example: SELECT (SELECT 1 FROM a) FROM test_table AS a; * * During child scope table identifier resolve a, table node test_table with alias a from parent scope * is invalid. */ - if (identifier_lookup.isTableExpressionLookup() && !is_valid_table_expression) - continue; + if (identifier_lookup.isTableExpressionLookup()) + { + auto * subquery_node = resolved_identifier->as(); + auto * union_node = resolved_identifier->as(); - if (lookup_result.isResolvedFromCTEs()) + bool is_cte = (subquery_node && subquery_node->isCTE()) || (union_node && union_node->isCTE()); + bool is_table_from_expression_arguments = lookup_result.resolve_place == IdentifierResolvePlace::EXPRESSION_ARGUMENTS && + resolved_identifier->getNodeType() == QueryTreeNodeType::TABLE; + bool is_valid_table_expression = is_cte || is_table_from_expression_arguments; + + + if (!is_valid_table_expression) + continue; + return lookup_result; + } + + if (identifier_lookup.isExpressionLookup() && lookup_result.isResolvedFromCTEs()) resolveExpressionNode(resolved_identifier, scope, false, false); bool dependent_column = false;