diff --git a/types/convert_test.go b/types/convert_test.go index 3411423942d2d..845e5fc4dfde4 100644 --- a/types/convert_test.go +++ b/types/convert_test.go @@ -654,12 +654,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 7118531652c40..d6421cce35730 100644 --- a/types/datum.go +++ b/types/datum.go @@ -1184,6 +1184,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" { @@ -1196,16 +1197,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, adjust) 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) {