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

expression: fix incorrect date arithmitical with negative interval (#8523) #8574

Merged
merged 1 commit into from
Dec 5, 2018
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
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