Skip to content

Commit

Permalink
fix the STR_TO_DATE incompatible between MySQL and TiDB (#12623) (#12757
Browse files Browse the repository at this point in the history
)
  • Loading branch information
lonng authored and ngaut committed Oct 16, 2019
1 parent 56d9d25 commit 3ba8b41
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,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
2 changes: 2 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,8 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) {
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Incorrect datetime value: '0000-00-00 00:00:00'"))
result = tk.MustQuery("select str_to_date('2018-6-1', '%Y-%m-%d'), str_to_date('2018-6-1', '%Y-%c-%d'), str_to_date('59:20:1', '%s:%i:%k'), str_to_date('59:20:1', '%s:%i:%l')")
result.Check(testkit.Rows("2018-06-01 2018-06-01 01:20:59 01:20:59"))
result = tk.MustQuery("select str_to_date('20190101','%Y%m%d%!'), str_to_date('20190101','%Y%m%d%f'), str_to_date('20190101','%Y%m%d%H%i%s')")
result.Check(testkit.Rows("2019-01-01 2019-01-01 00:00:00.000000 2019-01-01 00:00:00"))

// for maketime
tk.MustExec(`drop table if exists t`)
Expand Down
6 changes: 6 additions & 0 deletions types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2175,6 +2175,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 @@ -2289,6 +2294,7 @@ func GetFormatType(format string) (isDuration, isDate bool) {
"%S": {},
"%k": {},
"%l": {},
"%f": {},
}
dateTokens := map[string]struct{}{
"%y": {},
Expand Down

0 comments on commit 3ba8b41

Please sign in to comment.