Fixing 02535_analyzer_group_by_use_nulls

This commit is contained in:
Nikolai Kochetov 2024-03-26 19:10:34 +00:00
parent 2dee605d52
commit 3b6ea659df

View File

@ -6678,45 +6678,48 @@ void QueryAnalyzer::resolveGroupByNode(QueryNode & query_node_typed, IdentifierR
if (query_node_typed.isGroupByWithGroupingSets()) if (query_node_typed.isGroupByWithGroupingSets())
{ {
QueryTreeNodes nullable_group_by_keys;
for (auto & grouping_sets_keys_list_node : query_node_typed.getGroupBy().getNodes()) for (auto & grouping_sets_keys_list_node : query_node_typed.getGroupBy().getNodes())
{ {
if (settings.enable_positional_arguments) if (settings.enable_positional_arguments)
replaceNodesWithPositionalArguments(grouping_sets_keys_list_node, query_node_typed.getProjection().getNodes(), scope); replaceNodesWithPositionalArguments(grouping_sets_keys_list_node, query_node_typed.getProjection().getNodes(), scope);
resolveExpressionNodeList(grouping_sets_keys_list_node, scope, false /*allow_lambda_expression*/, false /*allow_table_expression*/);
// Remove redundant calls to `tuple` function. It simplifies checking if expression is an aggregation key. // Remove redundant calls to `tuple` function. It simplifies checking if expression is an aggregation key.
// It's required to support queries like: SELECT number FROM numbers(3) GROUP BY (number, number % 2) // It's required to support queries like: SELECT number FROM numbers(3) GROUP BY (number, number % 2)
auto & group_by_list = grouping_sets_keys_list_node->as<ListNode &>().getNodes(); auto & group_by_list = grouping_sets_keys_list_node->as<ListNode &>().getNodes();
expandTuplesInList(group_by_list); expandTuplesInList(group_by_list);
if (scope.group_by_use_nulls)
for (const auto & group_by_elem : group_by_list)
nullable_group_by_keys.push_back(group_by_elem->clone());
resolveExpressionNodeList(grouping_sets_keys_list_node, scope, false /*allow_lambda_expression*/, false /*allow_table_expression*/);
} }
if (scope.group_by_use_nulls) for (auto & nullable_group_by_key : nullable_group_by_keys)
{ scope.nullable_group_by_keys.insert(std::move(nullable_group_by_key));
for (const auto & grouping_set : query_node_typed.getGroupBy().getNodes())
{
for (const auto & group_by_elem : grouping_set->as<ListNode>()->getNodes())
scope.nullable_group_by_keys.insert(group_by_elem);
}
}
} }
else else
{ {
if (settings.enable_positional_arguments) if (settings.enable_positional_arguments)
replaceNodesWithPositionalArguments(query_node_typed.getGroupByNode(), query_node_typed.getProjection().getNodes(), scope); replaceNodesWithPositionalArguments(query_node_typed.getGroupByNode(), query_node_typed.getProjection().getNodes(), scope);
resolveExpressionNodeList(query_node_typed.getGroupByNode(), scope, false /*allow_lambda_expression*/, false /*allow_table_expression*/);
// Remove redundant calls to `tuple` function. It simplifies checking if expression is an aggregation key. // Remove redundant calls to `tuple` function. It simplifies checking if expression is an aggregation key.
// It's required to support queries like: SELECT number FROM numbers(3) GROUP BY (number, number % 2) // It's required to support queries like: SELECT number FROM numbers(3) GROUP BY (number, number % 2)
auto & group_by_list = query_node_typed.getGroupBy().getNodes(); auto & group_by_list = query_node_typed.getGroupBy().getNodes();
expandTuplesInList(group_by_list); expandTuplesInList(group_by_list);
QueryTreeNodes nullable_group_by_keys;
if (scope.group_by_use_nulls) if (scope.group_by_use_nulls)
{ {
for (const auto & group_by_elem : query_node_typed.getGroupBy().getNodes()) for (const auto & group_by_elem : query_node_typed.getGroupBy().getNodes())
scope.nullable_group_by_keys.insert(group_by_elem); nullable_group_by_keys.push_back(group_by_elem->clone());
} }
resolveExpressionNodeList(query_node_typed.getGroupByNode(), scope, false /*allow_lambda_expression*/, false /*allow_table_expression*/);
for (auto & nullable_group_by_key : nullable_group_by_keys)
scope.nullable_group_by_keys.insert(std::move(nullable_group_by_key));
} }
} }