From 60a69d4689938818ce28b93f8f0e1270c7480bad Mon Sep 17 00:00:00 2001 From: Igor Nikonov Date: Thu, 26 Jan 2023 21:53:08 +0000 Subject: [PATCH] Fix distinct after JOIN or UNION --- .../Optimizations/removeRedundantDistinct.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Processors/QueryPlan/Optimizations/removeRedundantDistinct.cpp b/src/Processors/QueryPlan/Optimizations/removeRedundantDistinct.cpp index 45491052734..bed30c0fc21 100644 --- a/src/Processors/QueryPlan/Optimizations/removeRedundantDistinct.cpp +++ b/src/Processors/QueryPlan/Optimizations/removeRedundantDistinct.cpp @@ -1,8 +1,10 @@ #include #include +#include #include -#include +#include #include +#include namespace DB::QueryPlanOptimizations { @@ -46,10 +48,16 @@ size_t tryRemoveRedundantDistinct(QueryPlan::Node * parent_node, QueryPlan::Node distinct_node = parent_node->children.front(); - DistinctStep * inner_distinct_step = nullptr; + const DistinctStep * inner_distinct_step = nullptr; QueryPlan::Node * node = distinct_node; while (!node->children.empty()) { + const IQueryPlanStep* current_step = node->step.get(); + + /// don't try to remove DISTINCT after union or join + if (typeid_cast(current_step) || typeid_cast(current_step)) + break; + node = node->children.front(); inner_distinct_step = typeid_cast(node->step.get()); if (inner_distinct_step)