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 data too long when convert to bit returns null value (#20241) #20362

Merged
merged 3 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ func (s *testIntegrationSuite) TestConvertToBit(c *C) {
tk.MustExec(`insert t1 value ('09-01-01')`)
tk.MustExec(`insert t select a from t1`)
tk.MustQuery("select a+0 from t").Check(testkit.Rows("20090101000000"))

// For issue 20118
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a tinyint, b bit(63));")
tk.MustExec("insert ignore into t values(599999999, -1);")
tk.MustQuery("show warnings;").Check(testkit.Rows(
"Warning 1690 constant 599999999 overflows tinyint",
"Warning 1406 Data Too Long, field len 63"))
tk.MustQuery("select * from t;").Check(testkit.Rows("127 \u007f\xff\xff\xff\xff\xff\xff\xff"))
}

func (s *testIntegrationSuite) TestMathBuiltin(c *C) {
Expand Down
7 changes: 6 additions & 1 deletion types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -1287,9 +1287,14 @@ func (d *Datum) convertToMysqlBit(sc *stmtctx.StatementContext, target *FieldTyp
uintDatum, err1 := d.convertToUint(sc, target)
uintValue, err = uintDatum.GetUint64(), err1
}
if target.Flen < 64 && uintValue >= 1<<(uint64(target.Flen)) {
// Avoid byte size panic, never goto this branch.
if target.Flen <= 0 || target.Flen >= 128 {
return Datum{}, errors.Trace(ErrDataTooLong.GenWithStack("Data Too Long, field len %d", target.Flen))
}
if target.Flen < 64 && uintValue >= 1<<(uint64(target.Flen)) {
uintValue &= (1 << (uint64(target.Flen))) - 1
err = ErrDataTooLong.GenWithStack("Data Too Long, field len %d", target.Flen)
}
byteSize := (target.Flen + 7) >> 3
ret.SetMysqlBit(NewBinaryLiteralFromUint(uintValue, byteSize))
return ret, errors.Trace(err)
Expand Down