Skip to content

Commit

Permalink
add switch and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wshwsh12 committed Jul 18, 2019
1 parent 727efab commit ff0eb51
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 31 deletions.
64 changes: 33 additions & 31 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2660,53 +2660,55 @@ func (du *baseDateArithmitical) getIntervalFromDecimal(ctx sessionctx.Context, a
if isNull || err != nil {
return "", true, err
}
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 */
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.
neg = false
interval, isNull, err = args[1].EvalString(ctx, row)
if isNull || err != nil {
return "", true, err
}
default:
// YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE, MICROSECOND
neg = false
args[1] = WrapWithCastAsInt(ctx, args[1])
interval, isNull, err = args[1].EvalString(ctx, row)
if isNull || err != nil {
return "", true, err
}
}

if neg {
interval = "-" + interval
}

return interval, false, nil
}

Expand Down
32 changes: 32 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4467,6 +4467,7 @@ func (s *testIntegrationSuite) TestIssue10675(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 @@ -4476,6 +4477,7 @@ func (s *testIntegrationSuite) TestDatetimeMicrosecond(c *C) {
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 SECOND_MICROSECOND);`).Check(
testkit.Rows("2007-03-28 22:08:25.800000"))
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE_MICROSECOND);`).Check(
Expand All @@ -4485,4 +4487,34 @@ func (s *testIntegrationSuite) TestDatetimeMicrosecond(c *C) {
tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_MICROSECOND);`).Check(
testkit.Rows("2007-03-28 22:08:25.800000"))

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 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 ff0eb51

Please sign in to comment.