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 the issue that incorrect result for query that uses an AND operator on floats #15927

Merged
merged 11 commits into from
Apr 21, 2020
4 changes: 2 additions & 2 deletions expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -451,7 +451,7 @@ func toBool(sc *stmtctx.StatementContext, eType types.EvalType, buf *chunk.Colum
if err != nil {
return err
}
if types.RoundFloat(v) == 0 {
if v == 0 {
isZero[i] = 0
} else {
isZero[i] = 1
Expand Down
9 changes: 9 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5951,3 +5951,12 @@ func (s *testIntegrationSuite) TestNegativeZeroForHashJoin(c *C) {
tk.MustExec("drop TABLE t0;")
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"))
}
6 changes: 3 additions & 3 deletions types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -1433,9 +1433,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 := StrToInt(sc, d.GetString())
isZero, err = iVal == 0, err1
Expand All @@ -1445,7 +1445,7 @@ func (d *Datum) ToBool(sc *stmtctx.StatementContext) (int64, error) {
isZero = d.GetMysqlDuration().Duration == 0
case KindMysqlDecimal:
v, err1 := d.GetMysqlDecimal().ToFloat64()
isZero, err = RoundFloat(v) == 0, err1
isZero, err = v == 0, err1
case KindMysqlEnum:
isZero = d.GetMysqlEnum().ToNumber() == 0
case KindMysqlSet:
Expand Down