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

types,execute: fix errcode return like mysql when inserting incorrect int value #22432

Closed
wants to merge 4 commits into from
Closed
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
13 changes: 12 additions & 1 deletion executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"math"
"time"
"unicode"

"github.com/opentracing/opentracing-go"
"github.com/pingcap/errors"
Expand Down Expand Up @@ -293,7 +294,17 @@ func (e *InsertValues) handleErr(col *table.Column, val *types.Datum, rowIdx int
err = types.ErrWarnDataOutOfRange.GenWithStackByArgs(colName, rowIdx+1)
} else if types.ErrTruncated.Equal(err) {
err = types.ErrTruncated.GenWithStackByArgs(colName, rowIdx+1)
} else if types.ErrTruncatedWrongVal.Equal(err) && (colTp == mysql.TypeDuration || colTp == mysql.TypeDatetime || colTp == mysql.TypeDate || colTp == mysql.TypeTimestamp) {
} else if types.ErrTruncatedWrongVal.Equal(err) && types.IsTypeInteger(colTp) {
valStr, err1 := val.ToString()
if err1 != nil {
// do nothing
}
if (len(valStr) >= 1 && unicode.IsDigit(rune(valStr[0]))) || (len(valStr) >= 2 && valStr[0] == '.' && unicode.IsDigit(rune(valStr[1]))) {
err = types.ErrTruncated.GenWithStackByArgs(colName, rowIdx+1)
} else {
err = table.ErrTruncatedWrongValueForField.GenWithStackByArgs(types.TypeStr(colTp), valStr, colName, rowIdx+1)
}
} else if types.ErrTruncatedWrongVal.Equal(err) && types.IsTypeTemporal(colTp) {
valStr, err1 := val.ToString()
if err1 != nil {
// do nothing
Expand Down
26 changes: 26 additions & 0 deletions executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,32 @@ func (s *testSuite3) TestInsertWrongValueForField(c *C) {
tk.MustExec(`create table t (a year);`)
_, err = tk.Exec(`insert into t values(2156);`)
c.Assert(err.Error(), Equals, `[types:8033]invalid year`)

tk.MustExec(`drop table if exists t;`)
tk.MustExec(`
CREATE TABLE t (
id1 tinyint DEFAULT NULL,
id2 smallint DEFAULT NULL,
id3 mediumint DEFAULT NULL,
id4 int DEFAULT NULL,
id5 bigint DEFAULT NULL
)`)
tk.MustGetErrCode(`insert into t(id1) values('1asdf')`, errno.WarnDataTruncated)
tk.MustGetErrCode(`insert into t(id1) values('.1asdf')`, errno.WarnDataTruncated)
tk.MustGetErrCode(`insert into t(id2) values('1asdf')`, errno.WarnDataTruncated)
tk.MustGetErrCode(`insert into t(id2) values('.1asdf')`, errno.WarnDataTruncated)
tk.MustGetErrCode(`insert into t(id3) values('1asdf')`, errno.WarnDataTruncated)
tk.MustGetErrCode(`insert into t(id3) values('.1asdf')`, errno.WarnDataTruncated)
tk.MustGetErrCode(`insert into t(id4) values('1asdf')`, errno.WarnDataTruncated)
tk.MustGetErrCode(`insert into t(id4) values('.1asdf')`, errno.WarnDataTruncated)
tk.MustGetErrCode(`insert into t(id5) values('1asdf')`, errno.WarnDataTruncated)
tk.MustGetErrCode(`insert into t(id5) values('.1asdf')`, errno.WarnDataTruncated)
tk.MustGetErrCode(`insert into t(id1) values('asdf')`, errno.ErrTruncatedWrongValueForField)
tk.MustGetErrCode(`insert into t(id2) values('asdf')`, errno.ErrTruncatedWrongValueForField)
tk.MustGetErrCode(`insert into t(id3) values('asdf')`, errno.ErrTruncatedWrongValueForField)
tk.MustGetErrCode(`insert into t(id4) values('asdf')`, errno.ErrTruncatedWrongValueForField)
tk.MustGetErrCode(`insert into t(id5) values('asdf')`, errno.ErrTruncatedWrongValueForField)
tk.MustExec(`drop table if exists t;`)
}

func (s *testSuite3) TestInsertDateTimeWithTimeZone(c *C) {
Expand Down
3 changes: 3 additions & 0 deletions types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ func StrToInt(sc *stmtctx.StatementContext, str string, isFuncCast bool) (int64,
validPrefix, err := getValidIntPrefix(sc, str, isFuncCast)
iVal, err1 := strconv.ParseInt(validPrefix, 10, 64)
if err1 != nil {
if err != nil {
return iVal, ErrTruncatedWrongVal
}
return iVal, ErrOverflow.GenWithStackByArgs("BIGINT", validPrefix)
}
return iVal, errors.Trace(err)
Expand Down