Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not reanalyze expressions from aggregation in projection #46738

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Planner/PlannerActionsVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ class ActionsScopeNode
return node_name_to_node.find(node_name) != node_name_to_node.end();
}

[[maybe_unused]] bool containsInputNode(const std::string & node_name)
{
const auto * node = tryGetNode(node_name);
if (node && node->type == ActionsDAG::ActionType::INPUT)
return true;

return false;
}

[[maybe_unused]] const ActionsDAG::Node * tryGetNode(const std::string & node_name)
{
auto it = node_name_to_node.find(node_name);
Expand Down Expand Up @@ -421,7 +430,16 @@ PlannerActionsVisitorImpl::NodeNameAndNodeMinLevel PlannerActionsVisitorImpl::vi

auto function_node_name = calculateActionNodeName(node, *planner_context, node_to_node_name);

if (function_node.isAggregateFunction() || function_node.isWindowFunction())
/* Aggregate functions, window functions, and GROUP BY expressions were already analyzed in the previous steps.
* If we have already visited some expression, we don't need to revisit it or its arguments again.
* For example, the expression from the aggregation step is also present in the projection:
* SELECT foo(a, b, c) as x FROM table GROUP BY foo(a, b, c)
* In this case we should not analyze `a`, `b`, `c` again.
* Moreover, it can lead to an error if we have arrayJoin in the arguments because it will be calculated twice.
*/
bool is_input_node = function_node.isAggregateFunction() || function_node.isWindowFunction()
|| actions_stack.front().containsInputNode(function_node_name);
if (is_input_node)
{
size_t actions_stack_size = actions_stack.size();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
2
3
4
2
3
4
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SET allow_experimental_analyzer = 1;

SELECT 1 + arrayJoin(a) AS m FROM (SELECT [1, 2, 3] AS a) GROUP BY m;

SELECT 1 + arrayJoin(a) AS m FROM (SELECT [1, 2, 3] AS a) GROUP BY 1 + arrayJoin(a);