Skip to content

Commit

Permalink
expression: fix the issue that incorrect result for a predicate that …
Browse files Browse the repository at this point in the history
…uses the CHAR() function (#16014) (#16557)
  • Loading branch information
sre-bot authored Apr 30, 2020
1 parent 0be031a commit 04baec7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
31 changes: 31 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5014,3 +5014,34 @@ func (s *testIntegrationSuite) TestCacheRefineArgs(c *C) {
tk.MustExec("set @p0='-184467440737095516167.1'")
tk.MustQuery("execute stmt using @p0").Check(testkit.Rows("0"))
}

func (s *testIntegrationSuite) TestIssue15986(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 (0)")
tk.MustQuery("SELECT t0.c0 FROM t0 WHERE CHAR(204355900);").Check(testkit.Rows("0"))
tk.MustQuery("SELECT t0.c0 FROM t0 WHERE not CHAR(204355900);").Check(testkit.Rows())
tk.MustQuery("SELECT t0.c0 FROM t0 WHERE '.0';").Check(testkit.Rows())
tk.MustQuery("SELECT t0.c0 FROM t0 WHERE not '.0';").Check(testkit.Rows("0"))
// If the number does not exceed the range of float64 and its value is not 0, it will be converted to true.
tk.MustQuery("select * from t0 where '.000000000000000000000000000000000000000000000000000000" +
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"0000000000000000000000000000000000000000000000000000000000000000009';").Check(testkit.Rows("0"))
tk.MustQuery("select * from t0 where not '.000000000000000000000000000000000000000000000000000000" +
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"0000000000000000000000000000000000000000000000000000000000000000009';").Check(testkit.Rows())

// If the number is truncated beyond the range of float64, it will be converted to true when the truncated result is 0.
tk.MustQuery("select * from t0 where '.0000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000009';").Check(testkit.Rows())
tk.MustQuery("select * from t0 where not '.0000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000009';").Check(testkit.Rows("0"))
}
2 changes: 1 addition & 1 deletion types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ func (d *Datum) ToBool(sc *stmtctx.StatementContext) (int64, error) {
case KindFloat64:
isZero = RoundFloat(d.GetFloat64()) == 0
case KindString, KindBytes:
iVal, err1 := StrToInt(sc, d.GetString())
iVal, err1 := StrToFloat(sc, d.GetString())
isZero, err = iVal == 0, err1
case KindMysqlTime:
isZero = d.GetMysqlTime().IsZero()
Expand Down
4 changes: 2 additions & 2 deletions types/datum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func (ts *testDatumSuite) TestToBool(c *C) {
testDatumToBool(c, float32(0.1), 0)
testDatumToBool(c, float64(0.1), 0)
testDatumToBool(c, "", 0)
testDatumToBool(c, "0.1", 0)
testDatumToBool(c, "0.1", 1)
testDatumToBool(c, []byte{}, 0)
testDatumToBool(c, []byte("0.1"), 0)
testDatumToBool(c, []byte("0.1"), 1)
testDatumToBool(c, NewBinaryLiteralFromUint(0, -1), 0)
testDatumToBool(c, Enum{Name: "a", Value: 1}, 1)
testDatumToBool(c, Set{Name: "a", Value: 1}, 1)
Expand Down

0 comments on commit 04baec7

Please sign in to comment.