diff --git a/expression/builtin_compare.go b/expression/builtin_compare.go index d9b8b45a86ec4..6e7cc3a2e4d0b 100644 --- a/expression/builtin_compare.go +++ b/expression/builtin_compare.go @@ -1117,6 +1117,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 5383834799e75..32bde832f3b39 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -4507,6 +4507,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) {