Skip to content

Commit

Permalink
fix invalid YEAR string is not compatible with Mysql (#10493)
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 authored and zz-jason committed May 17, 2019
1 parent 08c4559 commit 6a4e14c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (s *testSuite) TestInsertZeroYear(c *C) {
tk.MustQuery(`select * from t1;`).Check(testkit.Rows(
`0`,
`0`,
`2000`,
`0`,
`2000`,
))
}
Expand Down
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 6a4e14c

Please sign in to comment.