diff --git a/expression/builtin_time_test.go b/expression/builtin_time_test.go index c9b03ba0618e6..7d4f44301c06a 100644 --- a/expression/builtin_time_test.go +++ b/expression/builtin_time_test.go @@ -1591,6 +1591,7 @@ func (s *testEvaluatorSuite) TestTimeDiff(c *C) { {[]interface{}{"10:10:10", "10:9:0"}, "00:01:10", false, 0, false}, {[]interface{}{"2016-12-00 12:00:00", "10:9:0"}, "", true, 0, false}, {[]interface{}{"2016-12-00 12:00:00", ""}, "", true, 0, true}, + {[]interface{}{"00:00:00.000000", "00:00:00.000001"}, "-00:00:00.000001", false, 6, false}, } for _, t := range tests { diff --git a/types/convert.go b/types/convert.go index ee32a73a30f1d..0239517431525 100644 --- a/types/convert.go +++ b/types/convert.go @@ -314,6 +314,9 @@ func StrToDuration(sc *stmtctx.StatementContext, str string, fsp int8) (d Durati if length > 0 && str[0] == '-' { length-- } + if n := strings.IndexByte(str, '.'); n >= 0 { + length = length - len(str[n:]) + } // Timestamp format is 'YYYYMMDDHHMMSS' or 'YYMMDDHHMMSS', which length is 12. // See #3923, it explains what we do here. if length >= 12 { diff --git a/types/convert_test.go b/types/convert_test.go index 34a5f5aea5435..cab7670b64104 100644 --- a/types/convert_test.go +++ b/types/convert_test.go @@ -1124,6 +1124,8 @@ func (s *testTypeConvertSuite) TestStrToDuration(c *C) { {"20190101180000", 6, false}, {"20190101180000", 1, false}, {"20190101181234", 3, false}, + {"00:00:00.000000", 6, true}, + {"00:00:00", 0, true}, } for _, tt := range tests { _, _, isDuration, err := StrToDuration(sc, tt.str, tt.fsp)