diff --git a/src/Processors/QueryPlan/Optimizations/removeRedundantOrderBy.cpp b/src/Processors/QueryPlan/Optimizations/removeRedundantOrderBy.cpp index 10a8c2e5143..e4ad11bc529 100644 --- a/src/Processors/QueryPlan/Optimizations/removeRedundantOrderBy.cpp +++ b/src/Processors/QueryPlan/Optimizations/removeRedundantOrderBy.cpp @@ -219,6 +219,9 @@ private: /// (1) aggregation if (const AggregatingStep * parent_aggr = typeid_cast(step_affect_order); parent_aggr) { + if (parent_aggr->inOrder()) + return false; + auto const & aggregates = parent_aggr->getParams().aggregates; for (const auto & aggregate : aggregates) {