diff --git a/dbms/src/Processors/Merges/AggregatingSortedTransform.cpp b/dbms/src/Processors/Merges/AggregatingSortedTransform.cpp index b3a1be9f253..c490d6a7762 100644 --- a/dbms/src/Processors/Merges/AggregatingSortedTransform.cpp +++ b/dbms/src/Processors/Merges/AggregatingSortedTransform.cpp @@ -184,26 +184,24 @@ void AggregatingSortedTransform::merge() if (key_differs) { + /// Write the simple aggregation result for the previous group. + if (merged_data.mergedRows() > 0) + insertSimpleAggregationResult(); + + merged_data.insertRow(); + /// if there are enough rows accumulated and the last one is calculated completely if (merged_data.hasEnoughRows()) - { - /// Write the simple aggregation result for the previous group. - insertSimpleAggregationResult(); return; - } /// We will write the data for the group. We copy the values of ordinary columns. - merged_data.insertRow(current->all_columns, current->pos, - columns_definition.column_numbers_not_to_aggregate); + merged_data.initializeRow(current->all_columns, current->pos, + columns_definition.column_numbers_not_to_aggregate); /// Add the empty aggregation state to the aggregate columns. The state will be updated in the `addRow` function. for (auto & column_to_aggregate : columns_definition.columns_to_aggregate) column_to_aggregate.column->insertDefault(); - /// Write the simple aggregation result for the previous group. - if (merged_data.mergedRows() > 0) - insertSimpleAggregationResult(); - /// Reset simple aggregation states for next row for (auto & desc : columns_definition.columns_to_simple_aggregate) desc.createState(); @@ -229,7 +227,10 @@ void AggregatingSortedTransform::merge() /// Write the simple aggregation result for the previous group. if (merged_data.mergedRows() > 0) + { insertSimpleAggregationResult(); + merged_data.insertRow(); + } last_chunk_sort_columns.clear(); is_finished = true; diff --git a/dbms/src/Processors/Merges/AggregatingSortedTransform.h b/dbms/src/Processors/Merges/AggregatingSortedTransform.h index 5ebc41d6ccf..7f32ed4eade 100644 --- a/dbms/src/Processors/Merges/AggregatingSortedTransform.h +++ b/dbms/src/Processors/Merges/AggregatingSortedTransform.h @@ -59,11 +59,14 @@ private: public: using MergedData::MergedData; - void insertRow(const ColumnRawPtrs & raw_columns, size_t row, const ColumnNumbers & column_numbers) + void initializeRow(const ColumnRawPtrs & raw_columns, size_t row, const ColumnNumbers & column_numbers) { - for (auto column_number :column_numbers) + for (auto column_number : column_numbers) columns[column_number]->insertFrom(*raw_columns[column_number], row); + } + void insertRow() + { ++total_merged_rows; ++merged_rows; /// TODO: sum_blocks_granularity += block_size;