diff --git a/be/src/olap/memtable.cpp b/be/src/olap/memtable.cpp index 15d33a5b73c0a1..e4d3ca553a3874 100644 --- a/be/src/olap/memtable.cpp +++ b/be/src/olap/memtable.cpp @@ -469,7 +469,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); @@ -494,10 +494,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); }