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

Merged
merged 10 commits into from
Mar 9, 2021
7 changes: 6 additions & 1 deletion executor/point_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,13 @@ func EncodeUniqueIndexValuesForKey(ctx sessionctx.Context, tblInfo *model.TableI
str, err = idxVals[i].ToString()
idxVals[i].SetString(str, colInfo.FieldType.Collate)
} else {
// Since this func is only used batch point get, the idxVals should be exactly equals to value in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

// the index record. Errors like truncate/data-too-long will trim the idxVals value to the short
// another one, which will leading unexpected results. Here we force the cast func to return all
// the errors.
idxVals[i], err = table.CastValue(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, 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 @@ -133,6 +133,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 @@ -239,11 +239,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 returnOverflow && types.ErrOverflow.Equal(err) {
if returnErr && err != nil {
return casted, err
}
if err != nil && types.ErrTruncated.Equal(err) && col.Tp != mysql.TypeSet && col.Tp != mysql.TypeEnum {
Expand Down