Skip to content

Commit

Permalink
cherry pick pingcap#22785 to release-5.0-rc
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
lzmhhh123 authored and ti-srebot committed Feb 19, 2021
1 parent d3a4e00 commit 48a1f78
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
14 changes: 13 additions & 1 deletion expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,20 @@ func toBool(sc *stmtctx.StatementContext, tp *types.FieldType, eType types.EvalT
sVal := buf.GetString(i)
if tp.Hybrid() {
switch tp.Tp {
case mysql.TypeEnum, mysql.TypeSet:
case mysql.TypeSet, mysql.TypeEnum:
fVal = float64(len(sVal))
if fVal == 0 {
// The elements listed in the column specification are assigned index numbers, beginning
// with 1. The index value of the empty string error value (distinguish from a "normal"
// empty string) is 0. Thus we need to check whether it's an empty string error value when
// `fVal==0`.
for idx, elem := range tp.Elems {
if elem == sVal {
fVal = float64(idx + 1)
break
}
}
}
case mysql.TypeBit:
var bl types.BinaryLiteral = buf.GetBytes(i)
iVal, err := bl.ToInt(sc)
Expand Down
44 changes: 44 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8432,3 +8432,47 @@ func (s *testIntegrationSuite) TestIssue22098(c *C) {
tk.MustExec("set @a=3;set @b=20200414;set @c='a';set @d=20200414;set @e=3;set @f='a';")
tk.MustQuery("execute stmt using @a,@b,@c,@d,@e,@f").Check(testkit.Rows())
}
<<<<<<< HEAD
=======

func (s *testIntegrationSerialSuite) TestCollationUnion2(c *C) {
// For issue 22179
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a varchar(10))")
tk.MustExec("insert into t values('aaaaaaaaa'),('天王盖地虎宝塔镇河妖')")
tk.MustQuery("select * from t").Check(testkit.Rows("aaaaaaaaa", "天王盖地虎宝塔镇河妖"))

// check the collation of sub query of union statement.
tk.MustQuery("select collation(a) from (select null as a) aaa").Check(testkit.Rows("binary"))
tk.MustQuery("select collation(a) from (select a from t limit 1) aaa").Check(testkit.Rows("utf8mb4_bin"))

// Reverse sub query of union statement.
tk.MustQuery("select * from (select null as a union all select a from t) aaa order by a").Check(testkit.Rows("<nil>", "aaaaaaaaa", "天王盖地虎宝塔镇河妖"))
tk.MustQuery("select * from (select a from t) aaa union all select null as a order by a").Check(testkit.Rows("<nil>", "aaaaaaaaa", "天王盖地虎宝塔镇河妖"))
tk.MustExec("drop table if exists t")
}

func (s *testIntegrationSuite) Test22717(c *C) {
// For issue 22717
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec(`create table t(
a enum('a','','c'),
b enum('0','1','2'),
c set('a','','c'),
d set('0','1','2')
)`)
tk.MustExec("insert into t values(1,1,1,1),(2,2,2,2),(3,3,3,3)")
tk.MustExec("set @@sql_mode = ''")
tk.MustExec("insert into t values('','','','')")
tk.MustQuery("select * from t").Check(testkit.Rows("a 0 a 0", " 1 1", "c 2 a, 0,1", " "))
tk.MustQuery("select a from t where a").Check(testkit.Rows("a", "", "c", ""))
tk.MustQuery("select b from t where b").Check(testkit.Rows("0", "1", "2"))
tk.MustQuery("select c from t where c").Check(testkit.Rows("a", "", "a,", ""))
tk.MustQuery("select d from t where d").Check(testkit.Rows("0", "1", "0,1"))
}
>>>>>>> 8c2db1b93... expression: fix enum and set type expression in where clause (#22785)

0 comments on commit 48a1f78

Please sign in to comment.