Skip to content

Commit

Permalink
expression: fix incorrect date arithmitical with negative interval (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nanne007 authored and zz-jason committed Dec 5, 2018
1 parent 08f0168 commit b4b9f21
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
44 changes: 27 additions & 17 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1615,27 +1615,37 @@ func (s *testEvaluatorSuite) TestDateArithFuncs(c *C) {
fcAdd := funcs[ast.DateAdd]
fcSub := funcs[ast.DateSub]

args := types.MakeDatums(date[0], 1, "DAY")
f, err := fcAdd.getFunction(s.ctx, s.datumsToConstants(args))
c.Assert(err, IsNil)
c.Assert(f, NotNil)
v, err := evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(v.GetMysqlTime().String(), Equals, date[1])
tests := []struct {
inputDate string
fc functionClass
inputDecimal float64
expect string
}{
{date[0], fcAdd, 1, date[1]},
{date[1], fcAdd, -1, date[0]},
{date[1], fcAdd, -0.5, date[0]},
{date[1], fcAdd, -1.4, date[0]},

args = types.MakeDatums(date[1], 1, "DAY")
f, err = fcSub.getFunction(s.ctx, s.datumsToConstants(args))
c.Assert(err, IsNil)
c.Assert(f, NotNil)
v, err = evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(v.GetMysqlTime().String(), Equals, date[0])
{date[1], fcSub, 1, date[0]},
{date[0], fcSub, -1, date[1]},
{date[0], fcSub, -0.5, date[1]},
{date[0], fcSub, -1.4, date[1]},
}
for _, test := range tests {
args := types.MakeDatums(test.inputDate, test.inputDecimal, "DAY")
f, err := test.fc.getFunction(s.ctx, s.datumsToConstants(args))
c.Assert(err, IsNil)
c.Assert(f, NotNil)
v, err := evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(v.GetMysqlTime().String(), Equals, test.expect)
}

args = types.MakeDatums(date[0], nil, "DAY")
f, err = fcAdd.getFunction(s.ctx, s.datumsToConstants(args))
args := types.MakeDatums(date[0], nil, "DAY")
f, err := fcAdd.getFunction(s.ctx, s.datumsToConstants(args))
c.Assert(err, IsNil)
c.Assert(f, NotNil)
v, err = evalBuiltinFunc(f, chunk.Row{})
v, err := evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(v.IsNull(), IsTrue)

Expand Down
2 changes: 1 addition & 1 deletion types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,7 @@ func extractSingleTimeValue(unit string, format string) (int64, int64, int64, fl
if err != nil {
return 0, 0, 0, 0, ErrIncorrectDatetimeValue.GenWithStackByArgs(format)
}
iv := int64(fv + 0.5)
iv := int64(math.Round(fv))

switch strings.ToUpper(unit) {
case "MICROSECOND":
Expand Down

0 comments on commit b4b9f21

Please sign in to comment.