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 a predicate that uses the CHAR() function (#16014) #16557

Merged
merged 4 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
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