From 33abea88965e92b24f978d77db5562bffc970f96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E5=86=B0?= Date: Wed, 4 Nov 2020 23:20:50 +0800 Subject: [PATCH 1/5] fixed bug for #20207 --- executor/insert_common.go | 13 +++++++++++++ executor/write_test.go | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/executor/insert_common.go b/executor/insert_common.go index 6e0c944172ec5..82c1d87bc9428 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,6 +36,7 @@ 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" @@ -291,6 +293,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) { + valStr, err1 := val.ToString() + if err1 != nil { + logutil.BgLogger().Warn("truncate value failed", zap.Error(err1)) + } + 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 { diff --git a/executor/write_test.go b/executor/write_test.go index b36f44eae91af..2569f19aba3af 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -262,7 +262,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')") @@ -271,7 +271,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=''") From c945b7b38a398b0a74c89e4bf6efd248ad89758d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E5=86=B0?= Date: Thu, 5 Nov 2020 11:31:20 +0800 Subject: [PATCH 2/5] ignore err --- executor/insert_common.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/executor/insert_common.go b/executor/insert_common.go index 82c1d87bc9428..0e27a715c0fe0 100644 --- a/executor/insert_common.go +++ b/executor/insert_common.go @@ -294,10 +294,7 @@ func (e *InsertValues) handleErr(col *table.Column, val *types.Datum, rowIdx int } 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) { - valStr, err1 := val.ToString() - if err1 != nil { - logutil.BgLogger().Warn("truncate value failed", zap.Error(err1)) - } + valStr, _ := val.ToString() err = dbterror.ClassTable.NewStdErr( errno.ErrTruncatedWrongValue, mysql.Message("Incorrect %-.32s value: '%-.128s' for column '%.192s' at row %d", nil), From 740385e7ddcd12e37f6a8b43ddccd7960f4fa11a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E5=86=B0?= Date: Thu, 5 Nov 2020 11:43:53 +0800 Subject: [PATCH 3/5] do nothing in err branch --- executor/insert_common.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/executor/insert_common.go b/executor/insert_common.go index 285c931298edb..b43655279ca60 100644 --- a/executor/insert_common.go +++ b/executor/insert_common.go @@ -38,9 +38,7 @@ import ( "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. @@ -294,7 +292,10 @@ func (e *InsertValues) handleErr(col *table.Column, val *types.Datum, rowIdx int } 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) { - valStr, _ := val.ToString() + 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), @@ -304,7 +305,7 @@ func (e *InsertValues) handleErr(col *table.Column, val *types.Datum, rowIdx int } 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) } From db7bd3dc6bb548a0c7d9f59691bf22a725575154 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E5=86=B0?= Date: Fri, 6 Nov 2020 14:05:20 +0800 Subject: [PATCH 4/5] fixed date value --- executor/insert_common.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/executor/insert_common.go b/executor/insert_common.go index b43655279ca60..57112bcf5d5c3 100644 --- a/executor/insert_common.go +++ b/executor/insert_common.go @@ -291,7 +291,7 @@ 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) { + } else if types.ErrTruncatedWrongVal.Equal(err) && (colTp == mysql.TypeDuration || colTp == mysql.TypeDatetime || colTp == mysql.TypeDate) { valStr, err1 := val.ToString() if err1 != nil { // do nothing From 51da642cd017a36db71c87e107039dd933fd091f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A7=E5=86=B0?= Date: Fri, 6 Nov 2020 14:47:47 +0800 Subject: [PATCH 5/5] fixed timestamp value --- executor/insert_common.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/executor/insert_common.go b/executor/insert_common.go index 57112bcf5d5c3..d742beefd17c4 100644 --- a/executor/insert_common.go +++ b/executor/insert_common.go @@ -291,7 +291,7 @@ 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) { + } 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