diff --git a/dbms/src/Processors/Merges/AggregatingSortedTransform.cpp b/dbms/src/Processors/Merges/AggregatingSortedTransform.cpp index 239d8b95cdf..3de4e7773f0 100644 --- a/dbms/src/Processors/Merges/AggregatingSortedTransform.cpp +++ b/dbms/src/Processors/Merges/AggregatingSortedTransform.cpp @@ -168,13 +168,19 @@ void AggregatingSortedTransform::merge() bool has_previous_group = !last_key.empty(); SortCursor current = queue.current(); - detail::RowRef current_key; - current_key.set(current); - if (!has_previous_group) /// The first key encountered. - key_differs = true; - else - key_differs = !last_key.hasEqualSortColumnsWith(current_key); + { + detail::RowRef current_key; + current_key.set(current); + + if (!has_previous_group) /// The first key encountered. + key_differs = true; + else + key_differs = !last_key.hasEqualSortColumnsWith(current_key); + + last_key = current_key; + last_chunk_sort_columns.clear(); + } if (key_differs) { @@ -183,7 +189,6 @@ void AggregatingSortedTransform::merge() { /// Write the simple aggregation result for the previous group. insertSimpleAggregationResult(); - last_key.reset(); return; } @@ -211,8 +216,6 @@ void AggregatingSortedTransform::merge() if (!current->isLast()) { - last_key = current_key; - last_chunk_sort_columns.clear(); queue.next(); } else diff --git a/dbms/src/Processors/Merges/SummingSortedTransform.cpp b/dbms/src/Processors/Merges/SummingSortedTransform.cpp index efdd7f85472..af97999a4b9 100644 --- a/dbms/src/Processors/Merges/SummingSortedTransform.cpp +++ b/dbms/src/Processors/Merges/SummingSortedTransform.cpp @@ -540,16 +540,22 @@ void SummingSortedTransform::merge() bool has_previous_group = !last_key.empty(); SortCursor current = queue.current(); - detail::RowRef current_key; - current_key.set(current); - if (!has_previous_group) /// The first key encountered. { - key_differs = true; - current_row_is_zero = true; + detail::RowRef current_key; + current_key.set(current); + + if (!has_previous_group) /// The first key encountered. + { + key_differs = true; + current_row_is_zero = true; + } + else + key_differs = !last_key.hasEqualSortColumnsWith(current_key); + + last_key = current_key; + last_chunk_sort_columns.clear(); } - else - key_differs = !last_key.hasEqualSortColumnsWith(current_key); if (key_differs) { @@ -599,8 +605,6 @@ void SummingSortedTransform::merge() if (!current->isLast()) { - last_key = current_key; - last_chunk_sort_columns.clear(); queue.next(); } else