From 2f4ff9bd6b820dfddf23fa7eee8e908c2cb1a017 Mon Sep 17 00:00:00 2001 From: xhe Date: Fri, 4 Sep 2020 09:42:49 +0800 Subject: [PATCH] types: fix year conversion --- types/convert_test.go | 16 ++++++++++++++++ types/datum.go | 7 +++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/types/convert_test.go b/types/convert_test.go index 4b898f82bf007..7d5147f0f7de0 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 106568a9f8b7a..87ca8558a79b9 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: