Skip to content

Commit

Permalink
expression: make group_concat handle character correct (pingcap#2821) (
Browse files Browse the repository at this point in the history
  • Loading branch information
bobotu authored and coocood committed Apr 19, 2017
1 parent 78d4520 commit 12f07da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions executor/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ func (s *testSuite) TestAggregation(c *C) {
result = tk.MustQuery("select 1-d as d from t having d + 1 < 0 order by d + 1")
result.Check(testkit.Rows("-2", "-2"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (keywords varchar(20), type int)")
tk.MustExec("insert into t values('测试', 1), ('test', 2)")
result = tk.MustQuery("select group_concat(keywords) from t group by type order by type")
result.Check(testkit.Rows("测试", "test"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (c int, d int)")
tk.MustExec("insert t values (1, -1)")
tk.MustExec("insert t values (1, 0)")
Expand Down
12 changes: 10 additions & 2 deletions expression/aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,14 @@ func (cf *concatFunction) GetType() *types.FieldType {
return types.NewFieldType(mysql.TypeVarString)
}

func (cf *concatFunction) writeValue(ctx *aggEvaluateContext, val types.Datum) {
if val.Kind() == types.KindBytes {
ctx.Buffer.Write(val.GetBytes())
} else {
ctx.Buffer.WriteString(fmt.Sprintf("%v", val.GetValue()))
}
}

// Update implements AggregationFunction interface.
func (cf *concatFunction) Update(row []types.Datum, groupKey []byte, sc *variable.StatementContext) error {
ctx := cf.getContext(groupKey)
Expand Down Expand Up @@ -694,7 +702,7 @@ func (cf *concatFunction) Update(row []types.Datum, groupKey []byte, sc *variabl
ctx.Buffer.WriteString(",")
}
for _, val := range cf.datumBuf {
ctx.Buffer.WriteString(fmt.Sprintf("%v", val.GetValue()))
cf.writeValue(ctx, val)
}
// TODO: if total length is greater than global var group_concat_max_len, truncate it.
return nil
Expand Down Expand Up @@ -730,7 +738,7 @@ func (cf *concatFunction) StreamUpdate(row []types.Datum, sc *variable.Statement
ctx.Buffer.WriteString(",")
}
for _, val := range cf.datumBuf {
ctx.Buffer.WriteString(fmt.Sprintf("%v", val.GetValue()))
cf.writeValue(ctx, val)
}
// TODO: if total length is greater than global var group_concat_max_len, truncate it.
return nil
Expand Down

0 comments on commit 12f07da

Please sign in to comment.