diff --git a/expression/builtin_time_test.go b/expression/builtin_time_test.go index b8ef7f9d4a36e..e10c8fd27dae0 100644 --- a/expression/builtin_time_test.go +++ b/expression/builtin_time_test.go @@ -1584,6 +1584,7 @@ func (s *testEvaluatorSuite) TestTimeDiff(c *C) { fsp int8 getWarning bool }{ +<<<<<<< HEAD {[]interface{}{"2000:01:01 00:00:00", "2000:01:01 00:00:00.000001"}, "-00:00:00.000001", false, 6, false}, {[]interface{}{"2008-12-31 23:59:59.000001", "2008-12-30 01:01:01.000002"}, "46:58:57.999999", false, 6, false}, {[]interface{}{"2016-12-00 12:00:00", "2016-12-01 12:00:00"}, "-24:00:00", false, 0, false}, @@ -1596,6 +1597,21 @@ func (s *testEvaluatorSuite) TestTimeDiff(c *C) { preWarningCnt := s.ctx.GetSessionVars().StmtCtx.WarningCount() f, err := newFunctionForTest(s.ctx, ast.TimeDiff, s.primitiveValsToConstants(t.args)...) c.Assert(err, IsNil) +======= + {[]interface{}{"2000:01:01 00:00:00", "2000:01:01 00:00:00.000001"}, "-00:00:00.000001", false, 6, 17, false}, + {[]interface{}{"2008-12-31 23:59:59.000001", "2008-12-30 01:01:01.000002"}, "46:58:57.999999", false, 6, 17, false}, + {[]interface{}{"2016-12-00 12:00:00", "2016-12-01 12:00:00"}, "-24:00:00", false, 0, 10, false}, + {[]interface{}{"10:10:10", "10:9:0"}, "00:01:10", false, 0, 10, false}, + {[]interface{}{"2016-12-00 12:00:00", "10:9:0"}, "", true, 0, 10, false}, + {[]interface{}{"2016-12-00 12:00:00", ""}, "", true, 0, 10, true}, + {[]interface{}{"00:00:00.000000", "00:00:00.000001"}, "-00:00:00.000001", false, 6, 17, false}, + } + + for _, c := range tests { + preWarningCnt := ctx.GetSessionVars().StmtCtx.WarningCount() + f, err := newFunctionForTest(ctx, ast.TimeDiff, primitiveValsToConstants(ctx, c.args)...) + require.NoError(t, err) +>>>>>>> d8fbad38a... expression, types: fix StrToDuration result (#32896) tp := f.GetType() c.Assert(tp.Tp, Equals, mysql.TypeDuration) c.Assert(tp.Charset, Equals, charset.CharsetBin) diff --git a/types/convert.go b/types/convert.go index f3ab3c69ad807..c38de116d0624 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)