Skip to content

Commit

Permalink
fix invalid YEAR string is not compatible with Mysql (pingcap#9856)
Browse files Browse the repository at this point in the history
  • Loading branch information
b41sh authored and qw4990 committed May 15, 2019
1 parent 205418a commit e56d16d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 3 additions & 0 deletions types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,9 @@ func (s *testTypeConvertSuite) TestConvert(c *C) {
signedDeny(c, mysql.TypeYear, 123, "<nil>")
signedDeny(c, mysql.TypeYear, 3000, "<nil>")
signedAccept(c, mysql.TypeYear, "2000", "2000")
signedAccept(c, mysql.TypeYear, "abc", "0")
signedAccept(c, mysql.TypeYear, "00abc", "2000")
signedAccept(c, mysql.TypeYear, "0019", "2019")

// time from string
signedAccept(c, mysql.TypeDate, "2012-08-23", "2012-08-23")
Expand Down
7 changes: 5 additions & 2 deletions types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -1172,11 +1172,14 @@ func (d *Datum) convertToMysqlYear(sc *stmtctx.StatementContext, target *FieldTy
)
switch d.k {
case KindString, KindBytes:
y, err = StrToInt(sc, d.GetString())
s := d.GetString()
y, err = StrToInt(sc, s)
if err != nil {
return ret, errors.Trace(err)
}
fromStr = true
if len(s) != 4 && len(s) > 0 && s[0:1] == "0" {
fromStr = true
}
case KindMysqlTime:
y = int64(d.GetMysqlTime().Time.Year())
case KindMysqlDuration:
Expand Down

0 comments on commit e56d16d

Please sign in to comment.