diff --git a/expression/expression.go b/expression/expression.go index 34ad4d60b1906..429ab2939dbbd 100644 --- a/expression/expression.go +++ b/expression/expression.go @@ -389,7 +389,7 @@ func toBool(sc *stmtctx.StatementContext, eType types.EvalType, buf *chunk.Colum if buf.IsNull(i) { isZero[i] = -1 } else { - if types.RoundFloat(f64s[i]) == 0 { + if f64s[i] == 0 { isZero[i] = 0 } else { isZero[i] = 1 @@ -444,11 +444,7 @@ func toBool(sc *stmtctx.StatementContext, eType types.EvalType, buf *chunk.Colum if buf.IsNull(i) { isZero[i] = -1 } else { - v, err := d64s[i].ToFloat64() - if err != nil { - return err - } - if types.RoundFloat(v) == 0 { + if d64s[i].IsZero() { isZero[i] = 0 } else { isZero[i] = 1 diff --git a/expression/integration_test.go b/expression/integration_test.go index 8f7da00fe14d5..04e49235c9c49 100755 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -6187,6 +6187,15 @@ func (s *testIntegrationSuite) TestNegativeZeroForHashJoin(c *C) { tk.MustExec("drop table t1;") } +func (s *testIntegrationSuite) TestIssue15743(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t0") + tk.MustExec("CREATE TABLE t0(c0 int)") + tk.MustExec("INSERT INTO t0 VALUES (1)") + tk.MustQuery("SELECT * FROM t0 WHERE 1 AND 0.4").Check(testkit.Rows("1")) +} + func (s *testIntegrationSuite) TestIssue15725(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test;") @@ -6241,3 +6250,15 @@ func (s *testIntegrationSuite) TestIssue16029(c *C) { tk.MustExec("drop table t0;") tk.MustExec("drop table t1;") } + +func (s *testIntegrationSuite) TestIssue16426(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (a int)") + tk.MustExec("insert into t values (42)") + tk.MustQuery("select a from t where a/10000").Check(testkit.Rows("42")) + tk.MustQuery("select a from t where a/100000").Check(testkit.Rows("42")) + tk.MustQuery("select a from t where a/1000000").Check(testkit.Rows("42")) + tk.MustQuery("select a from t where a/10000000").Check(testkit.Rows("42")) +} diff --git a/types/datum.go b/types/datum.go index 8b3167f5fc763..f4bf7f9e316d4 100644 --- a/types/datum.go +++ b/types/datum.go @@ -1434,9 +1434,9 @@ func (d *Datum) ToBool(sc *stmtctx.StatementContext) (int64, error) { case KindUint64: isZero = d.GetUint64() == 0 case KindFloat32: - isZero = RoundFloat(d.GetFloat64()) == 0 + isZero = d.GetFloat64() == 0 case KindFloat64: - isZero = RoundFloat(d.GetFloat64()) == 0 + isZero = d.GetFloat64() == 0 case KindString, KindBytes: iVal, err1 := StrToFloat(sc, d.GetString()) isZero, err = iVal == 0, err1 @@ -1445,8 +1445,7 @@ func (d *Datum) ToBool(sc *stmtctx.StatementContext) (int64, error) { case KindMysqlDuration: isZero = d.GetMysqlDuration().Duration == 0 case KindMysqlDecimal: - v, err1 := d.GetMysqlDecimal().ToFloat64() - isZero, err = RoundFloat(v) == 0, err1 + isZero = d.GetMysqlDecimal().IsZero() case KindMysqlEnum: isZero = d.GetMysqlEnum().ToNumber() == 0 case KindMysqlSet: diff --git a/types/datum_test.go b/types/datum_test.go index bba8e1900a9f0..2d7e3810739cb 100644 --- a/types/datum_test.go +++ b/types/datum_test.go @@ -67,10 +67,10 @@ func (ts *testDatumSuite) TestToBool(c *C) { testDatumToBool(c, int(0), 0) testDatumToBool(c, int64(0), 0) testDatumToBool(c, uint64(0), 0) - testDatumToBool(c, float32(0.1), 0) - testDatumToBool(c, float64(0.1), 0) + testDatumToBool(c, float32(0.1), 1) + testDatumToBool(c, float64(0.1), 1) testDatumToBool(c, float64(0.5), 1) - testDatumToBool(c, float64(0.499), 0) + testDatumToBool(c, float64(0.499), 1) testDatumToBool(c, "", 0) testDatumToBool(c, "0.1", 1) testDatumToBool(c, []byte{}, 0) @@ -91,7 +91,7 @@ func (ts *testDatumSuite) TestToBool(c *C) { ft.Decimal = 5 v, err := Convert(0.1415926, ft) c.Assert(err, IsNil) - testDatumToBool(c, v, 0) + testDatumToBool(c, v, 1) d := NewDatum(&invalidMockType{}) sc := new(stmtctx.StatementContext) sc.IgnoreTruncate = true