From 5025d582a7a69ac9c2f6f8ed744aa23ce687d433 Mon Sep 17 00:00:00 2001 From: b41sh Date: Mon, 25 Mar 2019 08:49:26 -0500 Subject: [PATCH] fix invalid `YEAR` string is not compatible with Mysql (#9856) --- types/convert_test.go | 3 +++ types/datum.go | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/types/convert_test.go b/types/convert_test.go index e8e4d69df945b..f6487f980bfe7 100644 --- a/types/convert_test.go +++ b/types/convert_test.go @@ -621,6 +621,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 5c04ee1910939..06a7a8c9dc3bf 100644 --- a/types/datum.go +++ b/types/datum.go @@ -1186,7 +1186,9 @@ func (d *Datum) convertToMysqlYear(sc *stmtctx.StatementContext, target *FieldTy if err != nil { return ret, errors.Trace(err) } - adjust = len(s) < 4 + if len(s) != 4 && len(s) > 0 && s[0:1] == "0" { + adjust = true + } case KindMysqlTime: y = int64(d.GetMysqlTime().Time.Year()) case KindMysqlDuration: