diff --git a/dbms/src/Interpreters/InterpreterSelectQuery.cpp b/dbms/src/Interpreters/InterpreterSelectQuery.cpp index 14e4d15f113..6c1ef1aa5f2 100644 --- a/dbms/src/Interpreters/InterpreterSelectQuery.cpp +++ b/dbms/src/Interpreters/InterpreterSelectQuery.cpp @@ -1510,7 +1510,7 @@ void InterpreterSelectQuery::executeOrder(Pipeline & pipeline, SelectQueryInfo & settings.max_bytes_before_external_sort, context.getTemporaryPath()); }; - if (settings.optimize_pk_order) + if (settings.optimize_pk_order && !query.groupBy()) { if (const auto * merge_tree = dynamic_cast(storage.get())) {