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

executor: fix cast function will ignore tht error for point-get key construction (#22869) #23211

Merged
merged 6 commits into from
Mar 15, 2021
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
5 changes: 4 additions & 1 deletion executor/point_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,11 @@ func encodeIndexKey(e *baseExecutor, tblInfo *model.TableInfo, idxInfo *model.In
str, err = idxVals[i].ToString()
idxVals[i].SetString(str, colInfo.FieldType.Collate)
} else {
// If a truncated error or an overflow error is thrown when converting the type of `idxVal[i]` to
// the type of `colInfo`, the `idxVal` does not exist in the `idxInfo` for sure.
idxVals[i], err = table.CastValue(e.ctx, idxVals[i], colInfo, true, false)
if types.ErrOverflow.Equal(err) {
if types.ErrOverflow.Equal(err) || types.ErrDataTooLong.Equal(err) ||
types.ErrTruncated.Equal(err) || types.ErrTruncatedWrongVal.Equal(err) {
return nil, false, kv.ErrNotExist
}
}
Expand Down
19 changes: 19 additions & 0 deletions executor/point_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ func (s *testPointGetSuite) TestPointGetOverflow(c *C) {
tk.MustQuery("SELECT t0.c1 FROM t0 WHERE t0.c1=127").Check(testkit.Rows("127"))
}

// Close issue #22839
func (s *testPointGetSuite) TestPointGetDataTooLong(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists PK_1389;")
tk.MustExec("CREATE TABLE `PK_1389` ( " +
" `COL1` bit(1) NOT NULL," +
" `COL2` varchar(20) DEFAULT NULL," +
" `COL3` datetime DEFAULT NULL," +
" `COL4` bigint(20) DEFAULT NULL," +
" `COL5` float DEFAULT NULL," +
" PRIMARY KEY (`COL1`)" +
");")
tk.MustExec("insert into PK_1389 values(0, \"皟钹糁泅埞礰喾皑杏灚暋蛨歜檈瓗跾咸滐梀揉\", \"7701-12-27 23:58:43\", 4806951672419474695, -1.55652e38);")
tk.MustQuery("select count(1) from PK_1389 where col1 = 0x30;").Check(testkit.Rows("0"))
tk.MustQuery("select count(1) from PK_1389 where col1 in ( 0x30);").Check(testkit.Rows("0"))
tk.MustExec("drop table if exists PK_1389;")
}

func (s *testPointGetSuite) TestPointGetCharPK(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test;`)
Expand Down
4 changes: 2 additions & 2 deletions table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ func handleZeroDatetime(ctx sessionctx.Context, col *model.ColumnInfo, casted ty
// Set it to true only in FillVirtualColumnValue and UnionScanExec.Next()
// If the handle of err is changed latter, the behavior of forceIgnoreTruncate also need to change.
// TODO: change the third arg to TypeField. Not pass ColumnInfo.
func CastValue(ctx sessionctx.Context, val types.Datum, col *model.ColumnInfo, returnOverflow, forceIgnoreTruncate bool) (casted types.Datum, err error) {
func CastValue(ctx sessionctx.Context, val types.Datum, col *model.ColumnInfo, returnErr, forceIgnoreTruncate bool) (casted types.Datum, err error) {
sc := ctx.GetSessionVars().StmtCtx
casted, err = val.ConvertTo(sc, &col.FieldType)
// TODO: make sure all truncate errors are handled by ConvertTo.
if types.ErrOverflow.Equal(err) && returnOverflow {
if returnErr && err != nil {
return casted, err
}
if err != nil && types.ErrTruncated.Equal(err) && col.Tp != mysql.TypeSet && col.Tp != mysql.TypeEnum {
Expand Down