From a7abcf53ad4261942b68372d58d7a738dfbe74e5 Mon Sep 17 00:00:00 2001 From: lzmhhh123 Date: Thu, 18 Feb 2021 15:21:09 +0800 Subject: [PATCH 1/4] expression: fix enum and set type expression in where clause Signed-off-by: lzmhhh123 --- expression/expression.go | 3 ++- expression/integration_test.go | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/expression/expression.go b/expression/expression.go index 19d4211f00d2c..7ef275b1fa9c6 100644 --- a/expression/expression.go +++ b/expression/expression.go @@ -459,7 +459,8 @@ func toBool(sc *stmtctx.StatementContext, tp *types.FieldType, eType types.EvalT if tp.Hybrid() { switch tp.Tp { case mysql.TypeEnum, mysql.TypeSet: - fVal = float64(len(sVal)) + // Enum and Set type value to bool is always true. + fVal = 1 case mysql.TypeBit: var bl types.BinaryLiteral = buf.GetBytes(i) iVal, err := bl.ToInt(sc) diff --git a/expression/integration_test.go b/expression/integration_test.go index c45db8537f19d..0bcfadff0eb7b 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -8690,3 +8690,22 @@ func (s *testIntegrationSerialSuite) TestCollationUnion2(c *C) { tk.MustQuery("select * from (select a from t) aaa union all select null as a order by a").Check(testkit.Rows("", "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','b','c'), + b enum('0','1','2'), + c set('a','b','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.MustQuery("select * from t").Check(testkit.Rows("a 0 a 0", "b 1 b 1", "c 2 a,b 0,1")) + tk.MustQuery("select a from t where a").Check(testkit.Rows("a", "b", "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", "b", "a,b")) + tk.MustQuery("select d from t where d").Check(testkit.Rows("0", "1", "0,1")) +} From fb897e6fd142ce13f98a028a006f5d5ac905e7b3 Mon Sep 17 00:00:00 2001 From: lzmhhh123 Date: Thu, 18 Feb 2021 16:24:13 +0800 Subject: [PATCH 2/4] update test case Signed-off-by: lzmhhh123 --- expression/integration_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index 0bcfadff0eb7b..833105397c2d1 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -8697,15 +8697,15 @@ func (s *testIntegrationSuite) Test22717(c *C) { tk.MustExec("use test") tk.MustExec("drop table if exists t") tk.MustExec(`create table t( - a enum('a','b','c'), + a enum('a','','c'), b enum('0','1','2'), - c set('a','b','c'), + 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.MustQuery("select * from t").Check(testkit.Rows("a 0 a 0", "b 1 b 1", "c 2 a,b 0,1")) - tk.MustQuery("select a from t where a").Check(testkit.Rows("a", "b", "c")) + 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", "b", "a,b")) + 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")) } From 4c8d7290d96aba2a1026c20550bcc6cadc0d788f Mon Sep 17 00:00:00 2001 From: lzmhhh123 Date: Fri, 19 Feb 2021 11:54:59 +0800 Subject: [PATCH 3/4] fix Signed-off-by: lzmhhh123 --- expression/expression.go | 14 +++++++++++--- expression/integration_test.go | 8 +++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/expression/expression.go b/expression/expression.go index 7ef275b1fa9c6..4fe98928e313d 100644 --- a/expression/expression.go +++ b/expression/expression.go @@ -458,9 +458,17 @@ 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: - // Enum and Set type value to bool is always true. - fVal = 1 + case mysql.TypeSet, mysql.TypeEnum: + fVal = float64(len(sVal)) + if fVal == 0 { + // Check whether "" in set type. + 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) diff --git a/expression/integration_test.go b/expression/integration_test.go index 833105397c2d1..961080684ea61 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -8703,9 +8703,11 @@ func (s *testIntegrationSuite) Test22717(c *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.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.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 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")) } From 14b32697a2593f435fcb3c3ad07cddb0516a51eb Mon Sep 17 00:00:00 2001 From: lzmhhh123 Date: Fri, 19 Feb 2021 14:23:43 +0800 Subject: [PATCH 4/4] change comment Signed-off-by: lzmhhh123 --- expression/expression.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/expression/expression.go b/expression/expression.go index 4fe98928e313d..8eefc711530c7 100644 --- a/expression/expression.go +++ b/expression/expression.go @@ -461,7 +461,10 @@ func toBool(sc *stmtctx.StatementContext, tp *types.FieldType, eType types.EvalT case mysql.TypeSet, mysql.TypeEnum: fVal = float64(len(sVal)) if fVal == 0 { - // Check whether "" in set type. + // 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)