From e56d16dd9c3c5f558a9d0906ef8e41fd67475cf8 Mon Sep 17 00:00:00 2001 From: b41sh Date: Mon, 25 Mar 2019 08:49:26 -0500 Subject: [PATCH 1/2] fix invalid `YEAR` string is not compatible with Mysql (#9856) --- types/convert_test.go | 3 +++ types/datum.go | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/types/convert_test.go b/types/convert_test.go index 3d58fc19bb56d..b5f3f737be0d5 100644 --- a/types/convert_test.go +++ b/types/convert_test.go @@ -619,6 +619,9 @@ func (s *testTypeConvertSuite) TestConvert(c *C) { signedDeny(c, mysql.TypeYear, 123, "") signedDeny(c, mysql.TypeYear, 3000, "") signedAccept(c, mysql.TypeYear, "2000", "2000") + signedAccept(c, mysql.TypeYear, "abc", "0") + signedAccept(c, mysql.TypeYear, "00abc", "2000") + signedAccept(c, mysql.TypeYear, "0019", "2019") // time from string signedAccept(c, mysql.TypeDate, "2012-08-23", "2012-08-23") diff --git a/types/datum.go b/types/datum.go index a16563f5b07d5..880e6312bcf97 100644 --- a/types/datum.go +++ b/types/datum.go @@ -1172,11 +1172,14 @@ func (d *Datum) convertToMysqlYear(sc *stmtctx.StatementContext, target *FieldTy ) switch d.k { case KindString, KindBytes: - y, err = StrToInt(sc, d.GetString()) + s := d.GetString() + y, err = StrToInt(sc, s) if err != nil { return ret, errors.Trace(err) } - fromStr = true + if len(s) != 4 && len(s) > 0 && s[0:1] == "0" { + fromStr = true + } case KindMysqlTime: y = int64(d.GetMysqlTime().Time.Year()) case KindMysqlDuration: From 37c41df86434163e0eb56b0bebd09a02c9dc59f1 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Wed, 15 May 2019 20:18:22 +0800 Subject: [PATCH 2/2] update UT --- executor/insert_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/executor/insert_test.go b/executor/insert_test.go index b8f92f0e4a55a..6350ae78237b0 100644 --- a/executor/insert_test.go +++ b/executor/insert_test.go @@ -172,7 +172,7 @@ func (s *testSuite) TestInsertZeroYear(c *C) { tk.MustQuery(`select * from t1;`).Check(testkit.Rows( `0`, `0`, - `2000`, + `0`, `2000`, )) }