Skip to content

Commit

Permalink
expression: fix getIntervalFromDecimal in DATE_ADD() (#11297)
Browse files Browse the repository at this point in the history
  • Loading branch information
wshwsh12 authored and SunRunAway committed Jul 18, 2019
1 parent 3217bff commit 5aef053
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 21 deletions.
52 changes: 32 additions & 20 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2662,26 +2662,38 @@ func (du *baseDateArithmitical) getIntervalFromDecimal(ctx sessionctx.Context, a
}

switch strings.ToUpper(unit) {
case "HOUR_MINUTE", "MINUTE_SECOND":
interval = strings.Replace(interval, ".", ":", -1)
case "YEAR_MONTH":
interval = strings.Replace(interval, ".", "-", -1)
case "DAY_HOUR":
interval = strings.Replace(interval, ".", " ", -1)
case "DAY_MINUTE":
interval = "0 " + strings.Replace(interval, ".", ":", -1)
case "DAY_SECOND":
interval = "0 00:" + strings.Replace(interval, ".", ":", -1)
case "DAY_MICROSECOND":
interval = "0 00:00:" + interval
case "HOUR_MICROSECOND":
interval = "00:00:" + interval
case "HOUR_SECOND":
interval = "00:" + strings.Replace(interval, ".", ":", -1)
case "MINUTE_MICROSECOND":
interval = "00:" + interval
case "SECOND_MICROSECOND":
/* keep interval as original decimal */
case "HOUR_MINUTE", "MINUTE_SECOND", "YEAR_MONTH", "DAY_HOUR", "DAY_MINUTE",
"DAY_SECOND", "DAY_MICROSECOND", "HOUR_MICROSECOND", "HOUR_SECOND", "MINUTE_MICROSECOND", "SECOND_MICROSECOND":
neg := false
if interval != "" && interval[0] == '-' {
neg = true
interval = interval[1:]
}
switch strings.ToUpper(unit) {
case "HOUR_MINUTE", "MINUTE_SECOND":
interval = strings.Replace(interval, ".", ":", -1)
case "YEAR_MONTH":
interval = strings.Replace(interval, ".", "-", -1)
case "DAY_HOUR":
interval = strings.Replace(interval, ".", " ", -1)
case "DAY_MINUTE":
interval = "0 " + strings.Replace(interval, ".", ":", -1)
case "DAY_SECOND":
interval = "0 00:" + strings.Replace(interval, ".", ":", -1)
case "DAY_MICROSECOND":
interval = "0 00:00:" + interval
case "HOUR_MICROSECOND":
interval = "00:00:" + interval
case "HOUR_SECOND":
interval = "00:" + strings.Replace(interval, ".", ":", -1)
case "MINUTE_MICROSECOND":
interval = "00:" + interval
case "SECOND_MICROSECOND":
/* keep interval as original decimal */
}
if neg {
interval = "-" + interval
}
case "SECOND":
// Decimal's EvalString is like %f format.
interval, isNull, err = args[1].EvalString(ctx, row)
Expand Down
134 changes: 133 additions & 1 deletion expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4465,8 +4465,9 @@ func (s *testIntegrationSuite) TestIssue10675(c *C) {
tk.MustQuery(`select * from t where a > 184467440737095516167.1;`).Check(testkit.Rows())
}

func (s *testIntegrationSuite) TestIssue11257(c *C) {
func (s *testIntegrationSuite) TestDatetimeMicrosecond(c *C) {
tk := testkit.NewTestKit(c, s.store)
// For int
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 SECOND_MICROSECOND);`).Check(
testkit.Rows("2007-03-28 22:08:27.800000"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 MINUTE_MICROSECOND);`).Check(
Expand All @@ -4475,4 +4476,135 @@ func (s *testIntegrationSuite) TestIssue11257(c *C) {
testkit.Rows("2007-03-28 22:08:27.800000"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 DAY_MICROSECOND);`).Check(
testkit.Rows("2007-03-28 22:08:27.800000"))

// For Decimal
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR_MINUTE);`).Check(
testkit.Rows("2007-03-29 00:10:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MINUTE_SECOND);`).Check(
testkit.Rows("2007-03-28 22:10:30"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 YEAR_MONTH);`).Check(
testkit.Rows("2009-05-28 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_HOUR);`).Check(
testkit.Rows("2007-03-31 00:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_MINUTE);`).Check(
testkit.Rows("2007-03-29 00:10:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_SECOND);`).Check(
testkit.Rows("2007-03-28 22:10:30"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR_SECOND);`).Check(
testkit.Rows("2007-03-28 22:10:30"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 SECOND);`).Check(
testkit.Rows("2007-03-28 22:08:30.200000"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 YEAR);`).Check(
testkit.Rows("2009-03-28 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 QUARTER);`).Check(
testkit.Rows("2007-09-28 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MONTH);`).Check(
testkit.Rows("2007-05-28 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 WEEK);`).Check(
testkit.Rows("2007-04-11 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY);`).Check(
testkit.Rows("2007-03-30 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR);`).Check(
testkit.Rows("2007-03-29 00:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MINUTE);`).Check(
testkit.Rows("2007-03-28 22:10:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MICROSECOND);`).Check(
testkit.Rows("2007-03-28 22:08:28.000002"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_MINUTE);`).Check(
testkit.Rows("2007-03-28 20:06:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE_SECOND);`).Check(
testkit.Rows("2007-03-28 22:06:26"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 YEAR_MONTH);`).Check(
testkit.Rows("2005-01-28 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_HOUR);`).Check(
testkit.Rows("2007-03-26 20:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_MINUTE);`).Check(
testkit.Rows("2007-03-28 20:06:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_SECOND);`).Check(
testkit.Rows("2007-03-28 22:06:26"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_SECOND);`).Check(
testkit.Rows("2007-03-28 22:06:26"))
// tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 SECOND);`).Check(
// testkit.Rows("2007-03-28 22:08:25.800000"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 YEAR);`).Check(
testkit.Rows("2005-03-28 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 QUARTER);`).Check(
testkit.Rows("2006-09-28 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MONTH);`).Check(
testkit.Rows("2007-01-28 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 WEEK);`).Check(
testkit.Rows("2007-03-14 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY);`).Check(
testkit.Rows("2007-03-26 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR);`).Check(
testkit.Rows("2007-03-28 20:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE);`).Check(
testkit.Rows("2007-03-28 22:06:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MICROSECOND);`).Check(
testkit.Rows("2007-03-28 22:08:27.999998"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR_MINUTE);`).Check(
testkit.Rows("2007-03-28 20:06:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MINUTE_SECOND);`).Check(
testkit.Rows("2007-03-28 22:06:26"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" YEAR_MONTH);`).Check(
testkit.Rows("2005-01-28 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_HOUR);`).Check(
testkit.Rows("2007-03-26 20:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_MINUTE);`).Check(
testkit.Rows("2007-03-28 20:06:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_SECOND);`).Check(
testkit.Rows("2007-03-28 22:06:26"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR_SECOND);`).Check(
testkit.Rows("2007-03-28 22:06:26"))
// tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" SECOND);`).Check(
// testkit.Rows("2007-03-28 22:08:25.800000"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" YEAR);`).Check(
testkit.Rows("2005-03-28 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" QUARTER);`).Check(
testkit.Rows("2006-09-28 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MONTH);`).Check(
testkit.Rows("2007-01-28 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" WEEK);`).Check(
testkit.Rows("2007-03-14 22:08:28"))
// tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY);`).Check(
// testkit.Rows("2007-03-26 22:08:28"))
// tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR);`).Check(
// testkit.Rows("2007-03-28 20:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MINUTE);`).Check(
testkit.Rows("2007-03-28 22:06:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MICROSECOND);`).Check(
testkit.Rows("2007-03-28 22:08:27.999998"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR_MINUTE);`).Check(
testkit.Rows("2007-03-28 20:06:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MINUTE_SECOND);`).Check(
testkit.Rows("2007-03-28 22:06:26"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" YEAR_MONTH);`).Check(
testkit.Rows("2005-01-28 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_HOUR);`).Check(
testkit.Rows("2007-03-26 20:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_MINUTE);`).Check(
testkit.Rows("2007-03-28 20:06:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_SECOND);`).Check(
testkit.Rows("2007-03-28 22:06:26"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR_SECOND);`).Check(
testkit.Rows("2007-03-28 22:06:26"))
// tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" SECOND);`).Check(
// testkit.Rows("2007-03-28 22:08:26"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" YEAR);`).Check(
testkit.Rows("2005-03-28 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" QUARTER);`).Check(
testkit.Rows("2006-09-28 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MONTH);`).Check(
testkit.Rows("2007-01-28 22:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" WEEK);`).Check(
testkit.Rows("2007-03-14 22:08:28"))
// tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY);`).Check(
// testkit.Rows("2007-03-26 22:08:28"))
// tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR);`).Check(
// testkit.Rows("2007-03-28 20:08:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MINUTE);`).Check(
testkit.Rows("2007-03-28 22:06:28"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MICROSECOND);`).Check(
testkit.Rows("2007-03-28 22:08:27.999998"))

}

0 comments on commit 5aef053

Please sign in to comment.