diff --git a/src/Interpreters/ActionsDAG.cpp b/src/Interpreters/ActionsDAG.cpp index 8bc430ee825..5f1398fed39 100644 --- a/src/Interpreters/ActionsDAG.cpp +++ b/src/Interpreters/ActionsDAG.cpp @@ -2260,32 +2260,4 @@ ActionsDAGPtr ActionsDAG::buildFilterActionsDAG( return result_dag; } -const ActionsDAG::Node * ActionsDAG::getOriginalNodeForOutputAlias(const String & output_name) -{ - /// find alias in output - const Node * output_alias = nullptr; - for (const auto * node : outputs) - { - if (node->result_name == output_name && node->type == ActionsDAG::ActionType::ALIAS) - { - output_alias = node; - break; - } - } - if (!output_alias) - return nullptr; - - /// find original(non alias) node it refers to - const Node * node = output_alias; - while (node && node->type == ActionsDAG::ActionType::ALIAS) - { - chassert(!node->children.empty()); - node = node->children.front(); - } - if (node->type != ActionsDAG::ActionType::INPUT) - return nullptr; - - return node; -} - } diff --git a/src/Interpreters/ActionsDAG.h b/src/Interpreters/ActionsDAG.h index a1d57db71d5..40bc76fe057 100644 --- a/src/Interpreters/ActionsDAG.h +++ b/src/Interpreters/ActionsDAG.h @@ -347,9 +347,6 @@ public: const std::unordered_map & node_name_to_input_node_column, const ContextPtr & context); - /// Return original node (input) for alias in output if exists - const Node * getOriginalNodeForOutputAlias(const String & output_name); - private: NodeRawConstPtrs getParents(const Node * target) const; diff --git a/src/Processors/QueryPlan/Optimizations/removeRedundantDistinct.cpp b/src/Processors/QueryPlan/Optimizations/removeRedundantDistinct.cpp index aa235e9e8af..1b012d81f82 100644 --- a/src/Processors/QueryPlan/Optimizations/removeRedundantDistinct.cpp +++ b/src/Processors/QueryPlan/Optimizations/removeRedundantDistinct.cpp @@ -37,6 +37,34 @@ namespace return non_const_columns; } + const ActionsDAG::Node * getOriginalNodeForOutputAlias(const ActionsDAGPtr & actions, const String & output_name) + { + /// find alias in output + const ActionsDAG::Node * output_alias = nullptr; + for (const auto * node : actions->getOutputs()) + { + if (node->result_name == output_name && node->type == ActionsDAG::ActionType::ALIAS) + { + output_alias = node; + break; + } + } + if (!output_alias) + return nullptr; + + /// find original(non alias) node it refers to + const ActionsDAG::Node * node = output_alias; + while (node && node->type == ActionsDAG::ActionType::ALIAS) + { + chassert(!node->children.empty()); + node = node->children.front(); + } + if (node->type != ActionsDAG::ActionType::INPUT) + return nullptr; + + return node; + } + bool canRemoveDistinct(const QueryPlan::Node * distinct_node) { const DistinctStep * distinct_step = typeid_cast(distinct_node->step.get()); @@ -98,7 +126,7 @@ namespace /// compare columns of two DISTINCTs for (const auto & column : distinct_columns) { - const auto * alias_node = path_actions->getOriginalNodeForOutputAlias(String(column)); + const auto * alias_node = getOriginalNodeForOutputAlias(path_actions, String(column)); if (!alias_node) return false; @@ -129,7 +157,7 @@ size_t tryRemoveRedundantDistinct(QueryPlan::Node * parent_node, QueryPlan::Node for (const auto * node : parent_node->children) { /// check if it is distinct node - if (typeid_cast(node->step.get())) + if (typeid_cast(node->step.get()) == nullptr) continue; if (canRemoveDistinct(node))