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: modify the error message of insert time value #20847

Merged
merged 15 commits into from
Nov 6, 2020
Merged
17 changes: 14 additions & 3 deletions executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta/autoid"
Expand All @@ -35,10 +36,9 @@ import (
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/dbterror"
"github.com/pingcap/tidb/util/execdetails"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/memory"
"go.uber.org/zap"
)

// InsertValues is the data to insert.
Expand Down Expand Up @@ -291,10 +291,21 @@ 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) {
valStr, err1 := val.ToString()
if err1 != nil {
// do nothing
}
err = dbterror.ClassTable.NewStdErr(
errno.ErrTruncatedWrongValue,
mysql.Message("Incorrect %-.32s value: '%-.128s' for column '%.192s' at row %d", nil),
"",
"",
).GenWithStackByArgs(types.TypeStr(colTp), valStr, colName, rowIdx+1)
} else if types.ErrTruncatedWrongVal.Equal(err) || types.ErrWrongValue.Equal(err) {
valStr, err1 := val.ToString()
if err1 != nil {
logutil.BgLogger().Warn("truncate value failed", zap.Error(err1))
// do nothing
}
err = table.ErrTruncatedWrongValueForField.GenWithStackByArgs(types.TypeStr(colTp), valStr, colName, rowIdx+1)
}
Expand Down
4 changes: 2 additions & 2 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (s *testSuite) TestInsert(c *C) {
r.Check(testkit.Rows("0", "0", "18446744073709551615", "0", "0"))
tk.MustExec("set @@sql_mode = @orig_sql_mode;")

// issue 6424
// issue 6424 & issue 20207
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a time(6))")
tk.MustExec("insert into t value('20070219173709.055870'), ('20070219173709.055'), ('20070219173709.055870123')")
Expand All @@ -272,7 +272,7 @@ func (s *testSuite) TestInsert(c *C) {
tk.MustExec("insert into t value(20070219173709.055870), (20070219173709.055), (20070219173709.055870123)")
tk.MustQuery("select * from t").Check(testkit.Rows("17:37:09.055870", "17:37:09.055000", "17:37:09.055870"))
_, err = tk.Exec("insert into t value(-20070219173709.055870)")
c.Assert(err.Error(), Equals, "[table:1366]Incorrect time value: '-20070219173709.055870' for column 'a' at row 1")
c.Assert(err.Error(), Equals, "[table:1292]Incorrect time value: '-20070219173709.055870' for column 'a' at row 1")

tk.MustExec("drop table if exists t")
tk.MustExec("set @@sql_mode=''")
Expand Down