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 @@ -5949,3 +5949,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
8 changes: 4 additions & 4 deletions types/datum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", 0)
testDatumToBool(c, []byte{}, 0)
Expand All @@ -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
Expand Down