From 38b77d39976f22cdb144a8c7bfa1143913ce7342 Mon Sep 17 00:00:00 2001
From: wshwsh12 <793703860@qq.com>
Date: Tue, 23 Feb 2021 13:22:28 +0800
Subject: [PATCH] fix

---
 executor/aggregate.go | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/executor/aggregate.go b/executor/aggregate.go
index bcba95859299d..f9c40afb8f7eb 100644
--- a/executor/aggregate.go
+++ b/executor/aggregate.go
@@ -523,6 +523,7 @@ func getGroupKey(ctx sessionctx.Context, input *chunk.Chunk, groupKey [][]byte,
 func (w *baseHashAggWorker) getPartialResult(sc *stmtctx.StatementContext, groupKey [][]byte, mapper aggPartialResultMapper) [][]aggfuncs.PartialResult {
 	n := len(groupKey)
 	partialResults := make([][]aggfuncs.PartialResult, n)
+	allMemDelta := int64(0)
 	for i := 0; i < n; i++ {
 		var ok bool
 		if partialResults[i], ok = mapper[string(groupKey[i])]; ok {
@@ -531,16 +532,17 @@ func (w *baseHashAggWorker) getPartialResult(sc *stmtctx.StatementContext, group
 		for _, af := range w.aggFuncs {
 			partialResult, memDelta := af.AllocPartialResult()
 			partialResults[i] = append(partialResults[i], partialResult)
-			w.memTracker.Consume(memDelta)
+			allMemDelta += memDelta
 		}
 		mapper[string(groupKey[i])] = partialResults[i]
-		w.memTracker.Consume(int64(len(groupKey[i])))
+		allMemDelta += int64(len(groupKey[i]))
 		// Map will expand when count > bucketNum * loadFactor. The memory usage will doubled.
 		if len(mapper) > (1<<w.BInMap)*loadFactorNum/loadFactorDen {
 			w.memTracker.Consume(defBucketMemoryUsage * (1 << w.BInMap))
 			w.BInMap++
 		}
 	}
+	w.memTracker.Consume(allMemDelta)
 	return partialResults
 }