diff --git a/executor/aggfuncs/func_group_concat.go b/executor/aggfuncs/func_group_concat.go index 40ed32bce0093..68d4584f3d543 100644 --- a/executor/aggfuncs/func_group_concat.go +++ b/executor/aggfuncs/func_group_concat.go @@ -64,7 +64,8 @@ func (e *baseGroupConcat4String) truncatePartialResultIfNeed(sctx sessionctx.Con } type basePartialResult4GroupConcat struct { - buffer *bytes.Buffer + valsBuf *bytes.Buffer + buffer *bytes.Buffer } type partialResult4GroupConcat struct { @@ -76,7 +77,9 @@ type groupConcat struct { } func (e *groupConcat) AllocPartialResult() PartialResult { - return PartialResult(new(partialResult4GroupConcat)) + p := new(partialResult4GroupConcat) + p.valsBuf = &bytes.Buffer{} + return PartialResult(p) } func (e *groupConcat) ResetPartialResult(pr PartialResult) { @@ -86,12 +89,9 @@ func (e *groupConcat) ResetPartialResult(pr PartialResult) { func (e *groupConcat) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []chunk.Row, pr PartialResult) (err error) { p := (*partialResult4GroupConcat)(pr) - v, isNull, preLen := "", false, 0 + v, isNull := "", false for _, row := range rowsInGroup { - if p.buffer != nil && p.buffer.Len() != 0 { - preLen = p.buffer.Len() - p.buffer.WriteString(e.sep) - } + p.valsBuf.Reset() for _, arg := range e.args { v, isNull, err = arg.EvalString(sctx, row) if err != nil { @@ -100,17 +100,17 @@ func (e *groupConcat) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup [ if isNull { break } - if p.buffer == nil { - p.buffer = &bytes.Buffer{} - } - p.buffer.WriteString(v) + p.valsBuf.WriteString(v) } if isNull { - if p.buffer != nil { - p.buffer.Truncate(preLen) - } continue } + if p.buffer == nil { + p.buffer = &bytes.Buffer{} + } else { + p.buffer.WriteString(e.sep) + } + p.buffer.WriteString(p.valsBuf.String()) } if p.buffer != nil { return e.truncatePartialResultIfNeed(sctx, p.buffer) @@ -144,8 +144,7 @@ func (e *groupConcat) GetTruncated() *int32 { type partialResult4GroupConcatDistinct struct { basePartialResult4GroupConcat - valsBuf *bytes.Buffer - valSet set.StringSet + valSet set.StringSet } type groupConcatDistinct struct { diff --git a/executor/aggregate_test.go b/executor/aggregate_test.go index ee4d1c5b2285c..36bc7cba42371 100644 --- a/executor/aggregate_test.go +++ b/executor/aggregate_test.go @@ -407,6 +407,9 @@ func (s *testSuite) TestGroupConcatAggr(c *C) { result = tk.MustQuery("select id, group_concat(name SEPARATOR '123') from test group by id order by id") result.Check(testkit.Rows("1 101232012330", "2 20", "3 200123500")) + + // issue #9920 + tk.MustQuery("select group_concat(123, null)").Check(testkit.Rows("")) } func (s *testSuite) TestSelectDistinct(c *C) {