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... (#15927) #16666

Merged
merged 2 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -447,11 +447,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
60 changes: 60 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6074,6 +6074,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 @@ -6102,3 +6111,54 @@ func (s *testIntegrationSuite) TestIssue15992(c *C) {
tk.MustQuery("SELECT t0.c0 FROM t0 UNION ALL SELECT 0 FROM t0;").Check(testkit.Rows())
tk.MustExec("drop table t0;")
}
<<<<<<< HEAD
=======

func (s *testIntegrationSuite) TestIssue16419(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("use test;")
tk.MustExec("drop table if exists t0")
tk.MustExec("drop table if exists t1")
tk.MustExec("CREATE TABLE t0(c0 INT);")
tk.MustExec("CREATE TABLE t1(c0 INT);")
tk.MustQuery("SELECT * FROM t1 NATURAL LEFT JOIN t0 WHERE NOT t1.c0;").Check(testkit.Rows())
tk.MustExec("drop table t0, t1;")
}

func (s *testIntegrationSuite) TestIssue16029(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test;")
tk.MustExec("drop table if exists t0,t1;")
tk.MustExec("CREATE TABLE t0(c0 INT);")
tk.MustExec("CREATE TABLE t1(c0 INT);")
tk.MustExec("INSERT INTO t0 VALUES (NULL), (1);")
tk.MustExec("INSERT INTO t1 VALUES (0);")
tk.MustQuery("SELECT t0.c0 FROM t0 JOIN t1 ON (t0.c0 REGEXP 1) | t1.c0 WHERE BINARY STRCMP(t1.c0, t0.c0);").Check(testkit.Rows("1"))
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"))
}

func (s *testIntegrationSuite) TestIssue16505(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test;")
tk.MustExec("drop table if exists t;")
tk.MustExec("CREATE TABLE t(c varchar(100), index idx(c(100)));")
tk.MustExec("INSERT INTO t VALUES (NULL),('1'),('0'),(''),('aaabbb'),('0abc'),('123e456'),('0.0001deadsfeww');")
tk.MustQuery("select * from t where c;").Sort().Check(testkit.Rows("0.0001deadsfeww", "1", "123e456"))
tk.MustQuery("select /*+ USE_INDEX(t, idx) */ * from t where c;").Sort().Check(testkit.Rows("0.0001deadsfeww", "1", "123e456"))
tk.MustQuery("select /*+ IGNORE_INDEX(t, idx) */* from t where c;").Sort().Check(testkit.Rows("0.0001deadsfeww", "1", "123e456"))
tk.MustExec("drop table t;")
}
>>>>>>> 94011e6... expression: fix the issue that incorrect result for query that uses an AND operator on floats (#15927)
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 := StrToInt(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, 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