Skip to content

Commit

Permalink
expression: fix the issue that incorrect result for query that uses a…
Browse files Browse the repository at this point in the history
…n AND operator on floats (#15927)
  • Loading branch information
qw4990 authored Apr 21, 2020
1 parent 0d5a831 commit 94011e6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
8 changes: 2 additions & 6 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 @@ -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
Expand Down
21 changes: 21 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6152,6 +6152,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;")
Expand Down Expand Up @@ -6219,6 +6228,18 @@ func (s *testIntegrationSuite) TestIssue16029(c *C) {
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"))
}

func (s *testIntegrationSuite) TestIssue16505(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test;")
Expand Down
7 changes: 3 additions & 4 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 := StrToFloat(sc, d.GetString())
isZero, err = iVal == 0, err1
Expand All @@ -1444,8 +1444,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:
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, 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)
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

0 comments on commit 94011e6

Please sign in to comment.