Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: invalid year should be 0 (#12715) #12745

Merged
merged 2 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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