From ca237ee3c45cadfafa24230c30b8255d61149f3a Mon Sep 17 00:00:00 2001 From: Nikolai Kochetov Date: Wed, 16 Oct 2024 19:33:23 +0000 Subject: [PATCH] Fix constant result for prewhere optimization. --- .../QueryPlan/Optimizations/filterPushDown.cpp | 5 +---- .../QueryPlan/Optimizations/optimizePrewhere.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Processors/QueryPlan/Optimizations/filterPushDown.cpp b/src/Processors/QueryPlan/Optimizations/filterPushDown.cpp index ba5ff89a653..63359d039e8 100644 --- a/src/Processors/QueryPlan/Optimizations/filterPushDown.cpp +++ b/src/Processors/QueryPlan/Optimizations/filterPushDown.cpp @@ -152,10 +152,7 @@ addNewFilterStepOrThrow(QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes, node.step = std::make_unique( node.children.at(0)->step->getOutputHeader(), std::move(split_filter), std::move(split_filter_column_name), can_remove_filter); - if (auto * join = typeid_cast(child.get())) - join->updateInputHeader(node.step->getOutputHeader(), child_idx); - else - child->updateInputHeader(node.step->getOutputHeader()); + child->updateInputHeader(node.step->getOutputHeader(), child_idx); if (update_parent_filter) { diff --git a/src/Processors/QueryPlan/Optimizations/optimizePrewhere.cpp b/src/Processors/QueryPlan/Optimizations/optimizePrewhere.cpp index 4933026377f..d8771c268ed 100644 --- a/src/Processors/QueryPlan/Optimizations/optimizePrewhere.cpp +++ b/src/Processors/QueryPlan/Optimizations/optimizePrewhere.cpp @@ -194,6 +194,22 @@ void optimizePrewhere(Stack & stack, QueryPlan::Nodes &) } else { + if (!filter_step->removesFilterColumn()) + { + const auto * node = split_result.second.tryFindInOutputs(filter_step->getFilterColumnName()); + if (node && !node->column) + { + ColumnWithTypeAndName column; + column.type = node->result_type; + column.name = node->result_name; + column.column = column.type->createColumnConst(0, 1u); + const auto * replaced_const = &split_result.second.addColumn(column); + + for (auto & output : split_result.second.getOutputs()) + if (output == node) + output = replaced_const; + } + } /// Have to keep this expression to change column names to column identifiers filter_node->step = std::make_unique( source_step_with_filter->getOutputHeader(),