From eb9c52499d31baf3f6247b5414540076e870c1b4 Mon Sep 17 00:00:00 2001 From: xiongjiwei Date: Tue, 24 Aug 2021 13:00:04 +0800 Subject: [PATCH] cherry pick #27490 to release-5.1 Signed-off-by: ti-srebot --- executor/aggfuncs/func_group_concat.go | 22 +++++++++++++++++----- executor/analyze_test.go | 13 +++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/executor/aggfuncs/func_group_concat.go b/executor/aggfuncs/func_group_concat.go index f2ca28061a866..59ab77625205d 100644 --- a/executor/aggfuncs/func_group_concat.go +++ b/executor/aggfuncs/func_group_concat.go @@ -27,8 +27,8 @@ import ( "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/chunk" "github.com/pingcap/tidb/util/codec" + "github.com/pingcap/tidb/util/collate" "github.com/pingcap/tidb/util/dbterror" - "github.com/pingcap/tidb/util/hack" "github.com/pingcap/tidb/util/set" ) @@ -220,10 +220,16 @@ func (e *groupConcatDistinct) UpdatePartialResult(sctx sessionctx.Context, rowsI memDelta += int64(p.buffer.Cap()) } }() + + collators := make([]collate.Collator, 0, len(e.args)) + for _, arg := range e.args { + collators = append(collators, collate.GetCollator(arg.GetType().Collate)) + } + for _, row := range rowsInGroup { p.valsBuf.Reset() p.encodeBytesBuffer = p.encodeBytesBuffer[:0] - for _, arg := range e.args { + for i, arg := range e.args { v, isNull, err = arg.EvalString(sctx, row) if err != nil { return memDelta, err @@ -231,7 +237,7 @@ func (e *groupConcatDistinct) UpdatePartialResult(sctx sessionctx.Context, rowsI if isNull { break } - p.encodeBytesBuffer = codec.EncodeBytes(p.encodeBytesBuffer, hack.Slice(v)) + p.encodeBytesBuffer = codec.EncodeBytes(p.encodeBytesBuffer, collators[i].Key(v)) p.valsBuf.WriteString(v) } if isNull { @@ -536,10 +542,16 @@ func (e *groupConcatDistinctOrder) UpdatePartialResult(sctx sessionctx.Context, v, isNull := "", false memDelta -= int64(cap(p.encodeBytesBuffer)) defer func() { memDelta += int64(cap(p.encodeBytesBuffer)) }() + + collators := make([]collate.Collator, 0, len(e.args)) + for _, arg := range e.args { + collators = append(collators, collate.GetCollator(arg.GetType().Collate)) + } + for _, row := range rowsInGroup { buffer := new(bytes.Buffer) p.encodeBytesBuffer = p.encodeBytesBuffer[:0] - for _, arg := range e.args { + for i, arg := range e.args { v, isNull, err = arg.EvalString(sctx, row) if err != nil { return memDelta, err @@ -547,7 +559,7 @@ func (e *groupConcatDistinctOrder) UpdatePartialResult(sctx sessionctx.Context, if isNull { break } - p.encodeBytesBuffer = codec.EncodeBytes(p.encodeBytesBuffer, hack.Slice(v)) + p.encodeBytesBuffer = codec.EncodeBytes(p.encodeBytesBuffer, collators[i].Key(v)) buffer.WriteString(v) } if isNull { diff --git a/executor/analyze_test.go b/executor/analyze_test.go index 02d575c195ded..5c35c573a3144 100644 --- a/executor/analyze_test.go +++ b/executor/analyze_test.go @@ -931,6 +931,19 @@ func (s *testSuite1) TestDefaultValForAnalyze(c *C) { "└─IndexRangeScan_5 1.00 cop[tikv] table:t, index:a(a) range:[1,1], keep order:false")) } +func (s *testSerialSuite2) TestIssue27429(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table test.t(id int, value varchar(20) charset utf8mb4 collate utf8mb4_general_ci, value1 varchar(20) charset utf8mb4 collate utf8mb4_bin)") + tk.MustExec("insert into test.t values (1, 'abc', 'abc '),(4, 'Abc', 'abc'),(3,'def', 'def ');") + + tk.MustQuery("select upper(group_concat(distinct value order by 1)) from test.t;").Check(testkit.Rows("ABC,DEF")) + tk.MustQuery("select upper(group_concat(distinct value)) from test.t;").Check(testkit.Rows("ABC,DEF")) +} + func (s *testSerialSuite2) TestIssue20874(c *C) { collate.SetNewCollationEnabledForTest(true) defer collate.SetNewCollationEnabledForTest(false)