diff --git a/be/src/olap/memtable.cpp b/be/src/olap/memtable.cpp index e5c05c3a1d630d..18e85503c33a0b 100644 --- a/be/src/olap/memtable.cpp +++ b/be/src/olap/memtable.cpp @@ -481,7 +481,7 @@ void MemTable::_clear_row_agg(RowInBlock* row) { row->remove_init_agg(); } } - +// only in `to_block` the `is_final` flag will be true, in other cases, it will be false template void MemTable::_aggregate() { SCOPED_RAW_TIMER(&_stat.agg_ns); @@ -506,10 +506,15 @@ void MemTable::_aggregate() { } _stat.merged_rows++; _aggregate_two_row_in_block(mutable_block, cur_row, prev_row); + // Clean up aggregation state of the merged row to avoid memory leak + if (cur_row) { + _clear_row_agg(cur_row); + } } else { prev_row = cur_row; if (!temp_row_in_blocks.empty()) { - // no more rows to merge for prev row, finalize it + // The rows from the previous batch of _row_in_blocks have been merged into temp_row_in_blocks, + // now call finalize to write the aggregation results into _output_mutable_block. _finalize_one_row(temp_row_in_blocks.back().get(), block_data, row_pos); }