diff --git a/src/Storages/MergeTree/MergeTreeData.cpp b/src/Storages/MergeTree/MergeTreeData.cpp index a268468ea59..081444c68b5 100644 --- a/src/Storages/MergeTree/MergeTreeData.cpp +++ b/src/Storages/MergeTree/MergeTreeData.cpp @@ -4122,7 +4122,8 @@ bool MergeTreeData::getQueryProcessingStageWithAggregateProjection( candidate.before_aggregation = analysis_result.before_aggregation->clone(); auto required_columns = candidate.before_aggregation->foldActionsByProjection(keys, projection.sample_block_for_keys); - if (required_columns.empty() && !keys.empty()) + // TODO Let's find out the exact required_columns for keys. + if (required_columns.empty() && (!keys.empty() && !candidate.before_aggregation->getRequiredColumns().empty())) continue; if (analysis_result.optimize_aggregation_in_order)