Skip to content

Commit

Permalink
expression, types: fix StrToDuration result (#32896)
Browse files Browse the repository at this point in the history
close #31680
  • Loading branch information
Defined2014 committed Mar 8, 2022
1 parent 7c69e74 commit d8fbad3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,7 @@ func TestTimeDiff(t *testing.T) {
{[]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 {
Expand Down
3 changes: 3 additions & 0 deletions types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ func StrToDuration(sc *stmtctx.StatementContext, str string, fsp int) (d Duratio
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 @@ -1212,6 +1212,8 @@ func TestStrToDuration(t *testing.T) {
{"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 d8fbad3

Please sign in to comment.