Skip to content

Commit

Permalink
fix the STR_TO_DATE incompatible between MySQL and TiDB (#12623) (#12725
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sre-bot authored and ngaut committed Oct 16, 2019
1 parent 4956268 commit c39b38e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
3 changes: 3 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3796,6 +3796,9 @@ func (s *testSuite4) TearDownTest(c *C) {

func (s *testSuite) TestStrToDateBuiltin(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustQuery(`select str_to_date('20190101','%Y%m%d%!') from dual`).Check(testkit.Rows("2019-01-01"))
tk.MustQuery(`select str_to_date('20190101','%Y%m%d%f') from dual`).Check(testkit.Rows("2019-01-01 00:00:00.000000"))
tk.MustQuery(`select str_to_date('20190101','%Y%m%d%H%i%s') from dual`).Check(testkit.Rows("2019-01-01 00:00:00"))
tk.MustQuery(`select str_to_date('18/10/22','%y/%m/%d') from dual`).Check(testkit.Rows("2018-10-22"))
tk.MustQuery(`select str_to_date('a18/10/22','%y/%m/%d') from dual`).Check(testkit.Rows("<nil>"))
tk.MustQuery(`select str_to_date('69/10/22','%y/%m/%d') from dual`).Check(testkit.Rows("2069-10-22"))
Expand Down
1 change: 1 addition & 0 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,7 @@ func (c *strToDateFunctionClass) getRetTp(ctx sessionctx.Context, arg Expression
if err != nil || isNull {
return
}

isDuration, isDate := types.GetFormatType(format)
if isDuration && !isDate {
tp = mysql.TypeDuration
Expand Down
18 changes: 8 additions & 10 deletions types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2179,6 +2179,11 @@ func strToDate(t *MysqlTime, date string, format string, ctx map[string]int) boo
return true
}

if len(date) == 0 {
ctx[token] = 0
return true
}

dateRemain, succ := matchDateWithToken(t, date, token, ctx)
if !succ {
return false
Expand Down Expand Up @@ -2296,21 +2301,14 @@ func GetFormatType(format string) (isDuration, isDate bool) {
isDuration, isDate = false, false
break
}
var durationTokens bool
var dateTokens bool
if len(token) >= 2 && token[0] == '%' {
switch token[1] {
case 'h', 'H', 'i', 'I', 's', 'S', 'k', 'l':
durationTokens = true
case 'h', 'H', 'i', 'I', 's', 'S', 'k', 'l', 'f':
isDuration = true
case 'y', 'Y', 'm', 'M', 'c', 'b', 'D', 'd', 'e':
dateTokens = true
isDate = true
}
}
if durationTokens {
isDuration = true
} else if dateTokens {
isDate = true
}
if isDuration && isDate {
break
}
Expand Down

0 comments on commit c39b38e

Please sign in to comment.