@@ -104,7 +104,7 @@ static NameSet findIdentifiersOfNode(const ActionsDAG::Node * node)
104104 return res;
105105}
106106
107- static std::optional<ActionsDAG> splitFilter (QueryPlan::Node * parent_node, const Names & available_inputs, size_t child_idx = 0 )
107+ static std::optional<ActionsDAG> splitFilter (QueryPlan::Node * parent_node, bool step_changes_the_number_of_rows, const Names & available_inputs, size_t child_idx = 0 )
108108{
109109 QueryPlan::Node * child_node = parent_node->children .front ();
110110 checkChildrenSize (child_node, child_idx + 1 );
@@ -118,7 +118,8 @@ static std::optional<ActionsDAG> splitFilter(QueryPlan::Node * parent_node, cons
118118 bool removes_filter = filter->removesFilterColumn ();
119119
120120 const auto & all_inputs = child->getInputHeaders ()[child_idx].getColumnsWithTypeAndName ();
121- return expression.splitActionsForFilterPushDown (filter_column_name, removes_filter, available_inputs, all_inputs);
121+ bool allow_deterministic_functions = !step_changes_the_number_of_rows;
122+ return expression.splitActionsForFilterPushDown (filter_column_name, removes_filter, available_inputs, all_inputs, allow_deterministic_functions);
122123}
123124
124125static size_t
@@ -186,23 +187,23 @@ addNewFilterStepOrThrow(QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes,
186187}
187188
188189static size_t
189- tryAddNewFilterStep (QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes, const Names & allowed_inputs,
190+ tryAddNewFilterStep (QueryPlan::Node * parent_node, bool step_changes_the_number_of_rows, QueryPlan::Nodes & nodes, const Names & allowed_inputs,
190191 bool can_remove_filter = true , size_t child_idx = 0 )
191192{
192- if (auto split_filter = splitFilter (parent_node, allowed_inputs, child_idx))
193+ if (auto split_filter = splitFilter (parent_node, step_changes_the_number_of_rows, allowed_inputs, child_idx))
193194 return addNewFilterStepOrThrow (parent_node, nodes, std::move (*split_filter), can_remove_filter, child_idx);
194195 return 0 ;
195196}
196197
197198
198199// / Push down filter through specified type of step
199200template <typename Step>
200- static size_t simplePushDownOverStep (QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes, QueryPlanStepPtr & child)
201+ static size_t simplePushDownOverStep (QueryPlan::Node * parent_node, bool step_changes_the_number_of_rows, QueryPlan::Nodes & nodes, QueryPlanStepPtr & child)
201202{
202203 if (typeid_cast<Step *>(child.get ()))
203204 {
204205 Names allowed_inputs = child->getOutputHeader ().getNames ();
205- if (auto updated_steps = tryAddNewFilterStep (parent_node, nodes, allowed_inputs))
206+ if (auto updated_steps = tryAddNewFilterStep (parent_node, step_changes_the_number_of_rows, nodes, allowed_inputs))
206207 return updated_steps;
207208 }
208209 return 0 ;
@@ -507,7 +508,7 @@ size_t tryPushDownFilter(QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes
507508 bool filter_is_not_among_aggregates_arguments = merging_aggregated || filterColumnIsNotAmongAggregatesArguments (params.aggregates , filter->getFilterColumnName ());
508509 const bool can_remove_filter = filter_column_is_not_among_aggregation_keys && filter_is_not_among_aggregates_arguments;
509510
510- if (auto updated_steps = tryAddNewFilterStep (parent_node, nodes, keys, can_remove_filter))
511+ if (auto updated_steps = tryAddNewFilterStep (parent_node, true , nodes, keys, can_remove_filter))
511512 return updated_steps;
512513 }
513514
@@ -568,7 +569,7 @@ size_t tryPushDownFilter(QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes
568569 // / ) group by y with totals) where y != 2`
569570 // / Optimization will replace totals row `y, sum(x)` from `(0, 45)` to `(0, 37)`.
570571 // / It is expected to ok, cause AST optimization `enable_optimize_predicate_expression = 1` also brakes it.
571- if (auto updated_steps = tryAddNewFilterStep (parent_node, nodes, keys))
572+ if (auto updated_steps = tryAddNewFilterStep (parent_node, false , nodes, keys))
572573 return updated_steps;
573574 }
574575
@@ -584,11 +585,11 @@ size_t tryPushDownFilter(QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes
584585 if (!keys_set.contains (column.name ))
585586 allowed_inputs.push_back (column.name );
586587
587- if (auto updated_steps = tryAddNewFilterStep (parent_node, nodes, allowed_inputs))
588+ if (auto updated_steps = tryAddNewFilterStep (parent_node, true , nodes, allowed_inputs))
588589 return updated_steps;
589590 }
590591
591- if (auto updated_steps = simplePushDownOverStep<DistinctStep>(parent_node, nodes, child))
592+ if (auto updated_steps = simplePushDownOverStep<DistinctStep>(parent_node, true , nodes, child))
592593 return updated_steps;
593594
594595 if (auto updated_steps = tryPushDownOverJoinStep (parent_node, nodes, child))
@@ -619,14 +620,14 @@ size_t tryPushDownFilter(QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes
619620 bool can_remove_filter = sort_description_it == sort_description.end ();
620621
621622 Names allowed_inputs = child->getOutputHeader ().getNames ();
622- if (auto updated_steps = tryAddNewFilterStep (parent_node, nodes, allowed_inputs, can_remove_filter))
623+ if (auto updated_steps = tryAddNewFilterStep (parent_node, false , nodes, allowed_inputs, can_remove_filter))
623624 return updated_steps;
624625 }
625626
626627 if (typeid_cast<CustomMetricLogViewStep *>(child.get ()))
627628 {
628629 Names allowed_inputs = {" event_date" , " event_time" , " hostname" };
629- if (auto updated_steps = tryAddNewFilterStep (parent_node, nodes, allowed_inputs, true ))
630+ if (auto updated_steps = tryAddNewFilterStep (parent_node, true , nodes, allowed_inputs, true ))
630631 return updated_steps;
631632 }
632633
@@ -636,7 +637,7 @@ size_t tryPushDownFilter(QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes
636637 bool can_remove_filter = !join_filter_set_step->isColumnPartOfSetKey (filter_column_name);
637638
638639 Names allowed_inputs = child->getOutputHeader ().getNames ();
639- if (auto updated_steps = tryAddNewFilterStep (parent_node, nodes, allowed_inputs, can_remove_filter))
640+ if (auto updated_steps = tryAddNewFilterStep (parent_node, false , nodes, allowed_inputs, can_remove_filter))
640641 return updated_steps;
641642 }
642643
0 commit comments