From fbe82908de6f207de3756d1dcfaa7d764c7bdeb6 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Tue, 31 Mar 2020 21:16:37 +0800 Subject: [PATCH 1/5] fix issue 15743 --- expression/expression.go | 4 ++-- expression/integration_test.go | 9 +++++++++ types/datum.go | 6 +++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/expression/expression.go b/expression/expression.go index cddaf493c9a30..0e44a484edd4d 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 @@ -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 diff --git a/expression/integration_test.go b/expression/integration_test.go index eef59ae7b5164..53568e9f35df8 100755 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -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")) +} diff --git a/types/datum.go b/types/datum.go index 132ce47738566..5c714e06b6f63 100644 --- a/types/datum.go +++ b/types/datum.go @@ -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 @@ -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: From 182b5319549744fa333de2ead6f611586df6a0ce Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Wed, 1 Apr 2020 15:13:37 +0800 Subject: [PATCH 2/5] fix CI --- types/datum_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/types/datum_test.go b/types/datum_test.go index 831c8c20bc0f5..b72c1eddae207 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", 0) 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 From 378b7a75c3c83bdfce4c792adc2f2537bd8bb9a1 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Wed, 15 Apr 2020 20:06:20 +0800 Subject: [PATCH 3/5] fix CI --- expression/integration_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/expression/integration_test.go b/expression/integration_test.go index e332f03638c7b..5a64c8248e048 100755 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -6023,6 +6023,7 @@ func (s *testIntegrationSuite) TestIssue15725(c *C) { func (s *testIntegrationSuite) TestIssue15790(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(c0) VALUES (0);") tk.MustQuery("SELECT * FROM t0 WHERE -10000000000000000000 | t0.c0 UNION SELECT * FROM t0;").Check(testkit.Rows("0")) @@ -6033,6 +6034,8 @@ func (s *testIntegrationSuite) TestIssue15790(c *C) { func (s *testIntegrationSuite) TestIssue15992(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 i0") tk.MustExec("CREATE TABLE t0(c0 INT, c1 INT AS (c0));") tk.MustExec("CREATE INDEX i0 ON t0(c1);") tk.MustQuery("SELECT t0.c0 FROM t0 UNION ALL SELECT 0 FROM t0;").Check(testkit.Rows()) From 1f1c82b47d3be0c988b48e0984af137d42dc1b88 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Fri, 17 Apr 2020 16:28:57 +0800 Subject: [PATCH 4/5] fix issue 16426 --- expression/integration_test.go | 12 ++++++++++++ types/datum.go | 3 +-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index 34f845340b996..e4f35a13b69fb 100755 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -6090,3 +6090,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 5c714e06b6f63..3c7619d6572d6 100644 --- a/types/datum.go +++ b/types/datum.go @@ -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 = v == 0, err1 + isZero = d.GetMysqlDecimal().IsZero() case KindMysqlEnum: isZero = d.GetMysqlEnum().ToNumber() == 0 case KindMysqlSet: From f319ce9e6c6914a5199fb4a80c23f69eb4586368 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Mon, 20 Apr 2020 11:07:15 +0800 Subject: [PATCH 5/5] fix toBool --- expression/expression.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/expression/expression.go b/expression/expression.go index e35ae90c6b942..ba8551c7ab5a3 100644 --- a/expression/expression.go +++ b/expression/expression.go @@ -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 v == 0 { + if d64s[i].IsZero() { isZero[i] = 0 } else { isZero[i] = 1