Skip to content

Commit

Permalink
executor: fix group_concat for cases like group_concat(123,null) (#9921
Browse files Browse the repository at this point in the history
…) (#9930)
  • Loading branch information
XuHuaiyu authored and zimulala committed Mar 28, 2019
1 parent f5b52cb commit 0f69856
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
31 changes: 15 additions & 16 deletions executor/aggfuncs/func_group_concat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions executor/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("<nil>"))
}

func (s *testSuite) TestSelectDistinct(c *C) {
Expand Down

0 comments on commit 0f69856

Please sign in to comment.