Skip to content

Commit 3425871

Browse files
committed
do not rewrite count(if()) to countIf
1 parent b2fcc5a commit 3425871

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Analyzer/Passes/RewriteAggregateFunctionWithIfPass.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ class RewriteAggregateFunctionWithIfVisitor : public InDepthQueryTreeVisitorWith
7474

7575
/// We need to preserve the output type from if()
7676
if (if_arguments_nodes[1]->getResultType()->getName() != if_node->getResultType()->getName())
77+
{
78+
/// For count, adding an extra cast is too heavy for the benefit.
79+
if (lower_name == "count")
80+
return;
81+
7782
new_arguments[0] = createCastFunction(std::move(if_arguments_nodes[1]), if_node->getResultType(), getContext());
83+
}
7884
else
7985
new_arguments[0] = std::move(if_arguments_nodes[1]);
8086

@@ -100,7 +106,13 @@ class RewriteAggregateFunctionWithIfVisitor : public InDepthQueryTreeVisitorWith
100106
QueryTreeNodes new_arguments{2};
101107

102108
if (if_arguments_nodes[2]->getResultType()->getName() != if_node->getResultType()->getName())
109+
{
110+
/// For count, adding an extra cast is too heavy for the benefit.
111+
if (lower_name == "count")
112+
return;
113+
103114
new_arguments[0] = createCastFunction(std::move(if_arguments_nodes[2]), if_node->getResultType(), getContext());
115+
}
104116
else
105117
new_arguments[0] = std::move(if_arguments_nodes[2]);
106118

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<test>
2+
<query>select count(if(rand() % 2 = 0, 1, null)) from numbers(100000000) settings optimize_rewrite_aggregate_function_with_if = true</query>
3+
<query>select count(if(rand() % 2 = 0, null, 1)) from numbers(100000000) settings optimize_rewrite_aggregate_function_with_if = true</query>
4+
</test>

0 commit comments

Comments
 (0)