From bacd3485606ed174a091d498032cf81389cf9ad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E5=86=B0?= <32380374+ou-bing@users.noreply.github.com> Date: Fri, 6 Nov 2020 16:24:40 +0800 Subject: [PATCH] executor: modify the error message of insert time value (#20847) --- executor/insert_common.go | 17 ++++++++++++++--- executor/write_test.go | 4 ++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/executor/insert_common.go b/executor/insert_common.go index 60504a90a7c3f..d742beefd17c4 100644 --- a/executor/insert_common.go +++ b/executor/insert_common.go @@ -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" @@ -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. @@ -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) } diff --git a/executor/write_test.go b/executor/write_test.go index 603f8f70b053a..e240925d3b5b2 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -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')") @@ -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=''")