diff --git a/expression/builtin_compare.go b/expression/builtin_compare.go index 19101135ef825..2d2bf42db6f41 100644 --- a/expression/builtin_compare.go +++ b/expression/builtin_compare.go @@ -1097,6 +1097,9 @@ func RefineComparedConstant(ctx sessionctx.Context, targetFieldType types.FieldT } sc := ctx.GetSessionVars().StmtCtx + if targetFieldType.Tp == mysql.TypeBit { + targetFieldType = *types.NewFieldType(mysql.TypeLonglong) + } var intDatum types.Datum intDatum, err = dt.ConvertTo(sc, &targetFieldType) if err != nil { diff --git a/expression/integration_test.go b/expression/integration_test.go index 44bfe2286464d..2e43cf6333cba 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -4212,6 +4212,22 @@ func (s *testIntegrationSuite) TestIssue10675(c *C) { tk.MustQuery(`select * from t where a < 184467440737095516167.1;`).Check( testkit.Rows("1")) tk.MustQuery(`select * from t where a > 184467440737095516167.1;`).Check(testkit.Rows()) + + // issue 11647 + tk.MustExec(`drop table if exists t;`) + tk.MustExec(`create table t(b bit(1));`) + tk.MustExec(`insert into t values(b'1');`) + tk.MustQuery(`select count(*) from t where b = 1;`).Check(testkit.Rows("1")) + tk.MustQuery(`select count(*) from t where b = '1';`).Check(testkit.Rows("1")) + tk.MustQuery(`select count(*) from t where b = b'1';`).Check(testkit.Rows("1")) + + tk.MustExec(`drop table if exists t;`) + tk.MustExec(`create table t(b bit(63));`) + // Not 64, because the behavior of mysql is amazing. I have no idea to fix it. + tk.MustExec(`insert into t values(b'111111111111111111111111111111111111111111111111111111111111111');`) + tk.MustQuery(`select count(*) from t where b = 9223372036854775807;`).Check(testkit.Rows("1")) + tk.MustQuery(`select count(*) from t where b = '9223372036854775807';`).Check(testkit.Rows("1")) + tk.MustQuery(`select count(*) from t where b = b'111111111111111111111111111111111111111111111111111111111111111';`).Check(testkit.Rows("1")) } func (s *testIntegrationSuite) TestDatetimeMicrosecond(c *C) {