Skip to content

Commit

Permalink
cherry pick pingcap#36208 to release-5.1
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <[email protected]>
  • Loading branch information
ywqzzy authored and ti-srebot committed Jul 22, 2022
1 parent 1c57ffd commit 106d171
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
14 changes: 14 additions & 0 deletions executor/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,24 @@ func getGroupKey(ctx sessionctx.Context, input *chunk.Chunk, groupKey [][]byte,

for _, item := range groupByItems {
tp := item.GetType()

buf, err := expression.GetColumn(tp.EvalType(), numRows)
if err != nil {
return nil, err
}

// In strict sql mode like ‘STRICT_TRANS_TABLES’,can not insert an invalid enum value like 0.
// While in sql mode like '', can insert an invalid enum value like 0,
// then the enum value 0 will have the enum name '', which maybe conflict with user defined enum ''.
// Ref to issue #26885.
// This check is used to handle invalid enum name same with user defined enum name.
// Use enum value as groupKey instead of enum name.
if item.GetType().GetType() == mysql.TypeEnum {
newTp := *tp
newTp.AddFlag(mysql.EnumSetAsIntFlag)
tp = &newTp
}

if err := expression.EvalExpr(ctx, item, tp.EvalType(), input, buf); err != nil {
expression.PutColumn(buf)
return nil, err
Expand All @@ -532,6 +545,7 @@ func getGroupKey(ctx sessionctx.Context, input *chunk.Chunk, groupKey [][]byte,
newTp.Flen = 0
tp = &newTp
}

groupKey, err = codec.HashGroupKey(ctx.GetSessionVars().StmtCtx, input.NumRows(), buf, groupKey, tp)
if err != nil {
expression.PutColumn(buf)
Expand Down
33 changes: 33 additions & 0 deletions executor/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1490,3 +1490,36 @@ func (s *testSuiteAgg) TestIssue23314(c *C) {
res := tk.MustQuery("select col1 from t1 group by col1")
res.Check(testkit.Rows("16:40:20.01"))
}

func TestIssue26885(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`SET sql_mode = 'NO_ENGINE_SUBSTITUTION';`)
tk.MustExec(`DROP TABLE IF EXISTS t1;`)

tk.MustExec("CREATE TABLE t1 (c1 ENUM('a', '', 'b'));")
tk.MustExec("INSERT INTO t1 (c1) VALUES ('b');")
tk.MustExec("INSERT INTO t1 (c1) VALUES ('');")
tk.MustExec("INSERT INTO t1 (c1) VALUES ('a');")
tk.MustExec("INSERT INTO t1 (c1) VALUES ('');")
tk.MustExec("INSERT INTO t1 (c1) VALUES (0);")
tk.MustQuery("select * from t1").Check(testkit.Rows("b", "", "a", "", ""))
tk.MustQuery("select c1 + 0 from t1").Check(testkit.Rows("3", "2", "1", "2", "0"))
tk.MustQuery("SELECT c1 + 0, COUNT(c1) FROM t1 GROUP BY c1 order by c1;").Check(testkit.Rows("0 1", "1 1", "2 2", "3 1"))

tk.MustExec("alter table t1 add index idx(c1); ")
tk.MustQuery("select c1 + 0 from t1").Check(testkit.Rows("3", "2", "1", "2", "0"))
tk.MustQuery("SELECT c1 + 0, COUNT(c1) FROM t1 GROUP BY c1 order by c1;").Check(testkit.Rows("0 1", "1 1", "2 2", "3 1"))

tk.MustExec(`DROP TABLE IF EXISTS t1;`)
tk.MustExec("CREATE TABLE t1 (c1 ENUM('a', 'b', 'c'));")
tk.MustExec("INSERT INTO t1 (c1) VALUES ('b');")
tk.MustExec("INSERT INTO t1 (c1) VALUES ('a');")
tk.MustExec("INSERT INTO t1 (c1) VALUES ('b');")
tk.MustExec("INSERT INTO t1 (c1) VALUES ('c');")
tk.MustExec("INSERT INTO t1 (c1) VALUES (0);")
tk.MustQuery("select * from t1").Check(testkit.Rows("b", "a", "b", "c", ""))
tk.MustQuery("SELECT c1 + 0, COUNT(c1) FROM t1 GROUP BY c1 order by c1;").Check(testkit.Rows("0 1", "1 1", "2 2", "3 1"))
}

0 comments on commit 106d171

Please sign in to comment.