Skip to content

Commit

Permalink
cherry pick pingcap#32896 to release-5.2
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
Defined2014 authored and ti-srebot committed Mar 8, 2022
1 parent ae76eac commit 0fd5481
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0fd5481

Please sign in to comment.