diff --git a/types/convert_test.go b/types/convert_test.go index a3f6f67ff8a69..3d28731a7211e 100644 --- a/types/convert_test.go +++ b/types/convert_test.go @@ -687,10 +687,26 @@ func (s *testTypeConvertSuite) TestConvert(c *C) { signedAccept(c, mysql.TypeYear, 1901, "1901") signedAccept(c, mysql.TypeYear, 1900.567, "1901") signedDeny(c, mysql.TypeYear, 1900.456, "0") + signedAccept(c, mysql.TypeYear, 0, "0") + signedAccept(c, mysql.TypeYear, "0", "2000") + signedAccept(c, mysql.TypeYear, "00", "2000") + signedAccept(c, mysql.TypeYear, " 0", "2000") + signedAccept(c, mysql.TypeYear, " 00", "2000") + signedAccept(c, mysql.TypeYear, " 000", "0") + signedAccept(c, mysql.TypeYear, " 0000 ", "2000") + signedAccept(c, mysql.TypeYear, " 0ab", "0") + signedAccept(c, mysql.TypeYear, "00bc", "0") + signedAccept(c, mysql.TypeYear, "000a", "0") + signedAccept(c, mysql.TypeYear, " 000a ", "2000") signedAccept(c, mysql.TypeYear, 1, "2001") + signedAccept(c, mysql.TypeYear, "1", "2001") + signedAccept(c, mysql.TypeYear, "01", "2001") signedAccept(c, mysql.TypeYear, 69, "2069") + signedAccept(c, mysql.TypeYear, "69", "2069") signedAccept(c, mysql.TypeYear, 70, "1970") + signedAccept(c, mysql.TypeYear, "70", "1970") signedAccept(c, mysql.TypeYear, 99, "1999") + signedAccept(c, mysql.TypeYear, "99", "1999") signedDeny(c, mysql.TypeYear, 100, "0") signedDeny(c, mysql.TypeYear, "99999999999999999999999999999999999", "0") diff --git a/types/datum.go b/types/datum.go index e9a14008d7fd9..4e91056bbef57 100644 --- a/types/datum.go +++ b/types/datum.go @@ -1333,12 +1333,15 @@ func (d *Datum) convertToMysqlYear(sc *stmtctx.StatementContext, target *FieldTy switch d.k { case KindString, KindBytes: s := d.GetString() - y, err = StrToInt(sc, s, false) + trimS := strings.TrimSpace(s) + y, err = StrToInt(sc, trimS, false) if err != nil { ret.SetInt64(0) return ret, errors.Trace(err) } - if len(s) != 4 && len(s) > 0 && s[0:1] == "0" { + // condition: + // parsed to 0, not a string of length 4, the first valid char is a 0 digit + if len(s) != 4 && y == 0 && strings.HasPrefix(trimS, "0") { adjust = true } case KindMysqlTime: