diff --git a/src/Analyzer/Passes/QueryAnalysisPass.cpp b/src/Analyzer/Passes/QueryAnalysisPass.cpp index 6d9208428d8..15ef30568f0 100644 --- a/src/Analyzer/Passes/QueryAnalysisPass.cpp +++ b/src/Analyzer/Passes/QueryAnalysisPass.cpp @@ -6209,6 +6209,9 @@ public: query_tree_node_type == QueryTreeNodeType::INTERPOLATE) return; + if (nodeIsAggregateFunctionOrInGroupByKeys(node)) + return; + auto * function_node = node->as(); if (function_node && function_node->getFunctionName() == "grouping") { @@ -6244,12 +6247,6 @@ public: if (column_node_source->getNodeType() == QueryTreeNodeType::LAMBDA) return; - for (const auto & group_by_key_node : group_by_keys_nodes) - { - if (node->isEqual(*group_by_key_node)) - return; - } - std::string column_name; if (column_node_source->hasAlias()) @@ -6268,24 +6265,32 @@ public: scope.scope_node->formatASTForErrorMessage()); } - bool needChildVisit(const QueryTreeNodePtr &, const QueryTreeNodePtr & child_node) + bool needChildVisit(const QueryTreeNodePtr & parent_node, const QueryTreeNodePtr & child_node) { - if (auto * child_function_node = child_node->as()) - { - if (child_function_node->isAggregateFunction()) - return false; + if (nodeIsAggregateFunctionOrInGroupByKeys(parent_node)) + return false; - for (const auto & group_by_key_node : group_by_keys_nodes) - { - if (child_node->isEqual(*group_by_key_node, {.compare_aliases = false})) - return false; - } - } + if (parent_node->getNodeType() == QueryTreeNodeType::CONSTANT) + return false; - return !(child_node->getNodeType() == QueryTreeNodeType::QUERY || child_node->getNodeType() == QueryTreeNodeType::UNION); + auto child_node_type = child_node->getNodeType(); + return !(child_node_type == QueryTreeNodeType::QUERY || child_node_type == QueryTreeNodeType::UNION); } private: + bool nodeIsAggregateFunctionOrInGroupByKeys(const QueryTreeNodePtr & node) const + { + if (auto * function_node = node->as()) + if (function_node->isAggregateFunction()) + return true; + + for (const auto & group_by_key_node : group_by_keys_nodes) + if (node->isEqual(*group_by_key_node, {.compare_aliases = false})) + return true; + + return false; + } + const QueryTreeNodes & group_by_keys_nodes; const IdentifierResolveScope & scope; }; diff --git a/src/Planner/Planner.cpp b/src/Planner/Planner.cpp index 84fb861c9c3..789a010c737 100644 --- a/src/Planner/Planner.cpp +++ b/src/Planner/Planner.cpp @@ -1206,10 +1206,6 @@ void Planner::buildPlanForQueryNode() return; PlannerQueryProcessingInfo query_processing_info(from_stage, select_query_options.to_stage); - - if (!query_processing_info.isFirstStage() && !query_processing_info.isSecondStage() && !query_processing_info.isIntermediateStage()) - return; - QueryAnalysisResult query_analysis_result(query_tree, query_processing_info, planner_context); auto expression_analysis_result = buildExpressionAnalysisResult(query_tree, query_plan.getCurrentDataStream().header.getColumnsWithTypeAndName(),