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 getIntervalFromDecimal in DATE_ADD() #11297

Merged
merged 6 commits into from
Jul 18, 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
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"))

}