Skip to content

Commit

Permalink
handle unsigned hour overflow (#10074)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewDi authored and qw4990 committed Apr 9, 2019
1 parent 7acbe52 commit 826c299
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -4682,6 +4682,10 @@ func (b *builtinMakeTimeSig) evalDuration(row chunk.Row) (types.Duration, bool,
var overflow bool
// MySQL TIME datatype: https://dev.mysql.com/doc/refman/5.7/en/time.html
// ranges from '-838:59:59.000000' to '838:59:59.000000'
if hour < 0 && mysql.HasUnsignedFlag(b.args[0].GetType().Flag) {
hour = 838
overflow = true
}
if hour < -838 {
hour = -838
overflow = true
Expand Down
17 changes: 17 additions & 0 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,23 @@ func (s *testEvaluatorSuite) TestMakeTime(c *C) {
}
}

// MAKETIME(CAST(-1 AS UNSIGNED),0,0);
tp1 := &types.FieldType{
Tp: mysql.TypeLonglong,
Flag: mysql.UnsignedFlag,
Charset: charset.CharsetBin,
Collate: charset.CollationBin,
Flen: mysql.MaxIntWidth,
}
f := BuildCastFunction(s.ctx, &Constant{Value: types.NewDatum("-1"), RetType: types.NewFieldType(mysql.TypeString)}, tp1)
res, err := f.Eval(chunk.Row{})
c.Assert(err, IsNil)
f1, err := maketime.getFunction(s.ctx, s.datumsToConstants([]types.Datum{res, makeDatums(0)[0], makeDatums(0)[0]}))
c.Assert(err, IsNil)
got, err := evalBuiltinFunc(f1, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(got.GetMysqlDuration().String(), Equals, "838:59:59")

tbl = []struct {
Args []interface{}
Want interface{}
Expand Down

0 comments on commit 826c299

Please sign in to comment.