Analyzer fix GROUP BY columns validation

This commit is contained in:
Maksim Kita 2023-02-08 19:30:06 +01:00
parent 84065fb13f
commit c29f3c0f99
2 changed files with 23 additions and 22 deletions

View File

@ -6209,6 +6209,9 @@ public:
query_tree_node_type == QueryTreeNodeType::INTERPOLATE) query_tree_node_type == QueryTreeNodeType::INTERPOLATE)
return; return;
if (nodeIsAggregateFunctionOrInGroupByKeys(node))
return;
auto * function_node = node->as<FunctionNode>(); auto * function_node = node->as<FunctionNode>();
if (function_node && function_node->getFunctionName() == "grouping") if (function_node && function_node->getFunctionName() == "grouping")
{ {
@ -6244,12 +6247,6 @@ public:
if (column_node_source->getNodeType() == QueryTreeNodeType::LAMBDA) if (column_node_source->getNodeType() == QueryTreeNodeType::LAMBDA)
return; return;
for (const auto & group_by_key_node : group_by_keys_nodes)
{
if (node->isEqual(*group_by_key_node))
return;
}
std::string column_name; std::string column_name;
if (column_node_source->hasAlias()) if (column_node_source->hasAlias())
@ -6268,24 +6265,32 @@ public:
scope.scope_node->formatASTForErrorMessage()); 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<FunctionNode>()) if (nodeIsAggregateFunctionOrInGroupByKeys(parent_node))
{ return false;
if (child_function_node->isAggregateFunction())
return false;
for (const auto & group_by_key_node : group_by_keys_nodes) if (parent_node->getNodeType() == QueryTreeNodeType::CONSTANT)
{ return false;
if (child_node->isEqual(*group_by_key_node, {.compare_aliases = false}))
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: private:
bool nodeIsAggregateFunctionOrInGroupByKeys(const QueryTreeNodePtr & node) const
{
if (auto * function_node = node->as<FunctionNode>())
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 QueryTreeNodes & group_by_keys_nodes;
const IdentifierResolveScope & scope; const IdentifierResolveScope & scope;
}; };

View File

@ -1206,10 +1206,6 @@ void Planner::buildPlanForQueryNode()
return; return;
PlannerQueryProcessingInfo query_processing_info(from_stage, select_query_options.to_stage); 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); QueryAnalysisResult query_analysis_result(query_tree, query_processing_info, planner_context);
auto expression_analysis_result = buildExpressionAnalysisResult(query_tree, auto expression_analysis_result = buildExpressionAnalysisResult(query_tree,
query_plan.getCurrentDataStream().header.getColumnsWithTypeAndName(), query_plan.getCurrentDataStream().header.getColumnsWithTypeAndName(),