From b7077ed53221164a53ed3c3525c91bf269242ed4 Mon Sep 17 00:00:00 2001 From: Chengyou Liu Date: Tue, 15 Oct 2019 15:13:56 +0800 Subject: [PATCH] types: invalid year should be 0 --- types/convert_test.go | 18 ++++++++++++++++-- types/datum.go | 9 ++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/types/convert_test.go b/types/convert_test.go index d8bca3083293f..5d6e9b383e8b7 100644 --- a/types/convert_test.go +++ b/types/convert_test.go @@ -652,12 +652,26 @@ func (s *testTypeConvertSuite) TestConvert(c *C) { signedAccept(c, mysql.TypeDouble, "1e+1", "10") // year - signedDeny(c, mysql.TypeYear, 123, "") - signedDeny(c, mysql.TypeYear, 3000, "") + signedDeny(c, mysql.TypeYear, 123, "0") + signedDeny(c, mysql.TypeYear, 3000, "0") signedAccept(c, mysql.TypeYear, "2000", "2000") signedAccept(c, mysql.TypeYear, "abc", "0") signedAccept(c, mysql.TypeYear, "00abc", "2000") signedAccept(c, mysql.TypeYear, "0019", "2019") + signedAccept(c, mysql.TypeYear, 2155, "2155") + signedAccept(c, mysql.TypeYear, 2155.123, "2155") + signedDeny(c, mysql.TypeYear, 2156, "0") + signedDeny(c, mysql.TypeYear, 123.123, "0") + signedDeny(c, mysql.TypeYear, 1900, "0") + signedAccept(c, mysql.TypeYear, 1901, "1901") + signedAccept(c, mysql.TypeYear, 1900.567, "1901") + signedDeny(c, mysql.TypeYear, 1900.456, "0") + signedAccept(c, mysql.TypeYear, 1, "2001") + signedAccept(c, mysql.TypeYear, 69, "2069") + signedAccept(c, mysql.TypeYear, 70, "1970") + signedAccept(c, mysql.TypeYear, 99, "1999") + signedDeny(c, mysql.TypeYear, 100, "0") + signedDeny(c, mysql.TypeYear, "99999999999999999999999999999999999", "0") // time from string signedAccept(c, mysql.TypeDate, "2012-08-23", "2012-08-23") diff --git a/types/datum.go b/types/datum.go index 9a520bcebbc4f..3211fa08c3c6d 100644 --- a/types/datum.go +++ b/types/datum.go @@ -1175,6 +1175,7 @@ func (d *Datum) convertToMysqlYear(sc *stmtctx.StatementContext, target *FieldTy s := d.GetString() y, err = StrToInt(sc, s) if err != nil { + ret.SetInt64(0) return ret, errors.Trace(err) } if len(s) != 4 && len(s) > 0 && s[0:1] == "0" { @@ -1187,16 +1188,18 @@ func (d *Datum) convertToMysqlYear(sc *stmtctx.StatementContext, target *FieldTy default: ret, err = d.convertToInt(sc, NewFieldType(mysql.TypeLonglong)) if err != nil { - return invalidConv(d, target.Tp) + _, err = invalidConv(d, target.Tp) + ret.SetInt64(0) + return ret, err } y = ret.GetInt64() } y, err = AdjustYear(y, fromStr) if err != nil { - return invalidConv(d, target.Tp) + _, err = invalidConv(d, target.Tp) } ret.SetInt64(y) - return ret, nil + return ret, err } func (d *Datum) convertToMysqlBit(sc *stmtctx.StatementContext, target *FieldType) (Datum, error) {