Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: fix enum and set type expression in where clause #22785

Merged
merged 5 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reference in the mysql manual?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mysql> create table t(a enum("a", "b", "c"));
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t values('a'),('b');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> set @@sql_mode='';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> insert into t values("");
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from t;
+------+
| a    |
+------+
| a    |
| b    |
|      |
+------+
3 rows in set (0.00 sec)

mysql> select * from t where a;
+------+
| a    |
+------+
| a    |
| b    |
+------+
2 rows in set (0.00 sec)

case mysql.TypeBit:
var bl types.BinaryLiteral = buf.GetBytes(i)
iVal, err := bl.ToInt(sc)
Expand Down
19 changes: 19 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("<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.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"))
}