Skip to content

Commit

Permalink
types: invalid year should be 0 (#12715) (#12745)
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored Oct 30, 2019
1 parent 5cb5162 commit 399e03f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
18 changes: 16 additions & 2 deletions types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,12 +654,26 @@ func (s *testTypeConvertSuite) TestConvert(c *C) {
signedAccept(c, mysql.TypeDouble, "1e+1", "10")

// year
signedDeny(c, mysql.TypeYear, 123, "<nil>")
signedDeny(c, mysql.TypeYear, 3000, "<nil>")
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")
Expand Down
9 changes: 6 additions & 3 deletions types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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) {
Expand Down

0 comments on commit 399e03f

Please sign in to comment.