Skip to content

Commit

Permalink
cherry pick pingcap#32903 to release-5.2
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <[email protected]>
  • Loading branch information
Defined2014 authored and ti-srebot committed Mar 16, 2022
1 parent ae76eac commit 868641d
Show file tree
Hide file tree
Showing 5 changed files with 4,524 additions and 45 deletions.
27 changes: 6 additions & 21 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -5231,21 +5231,16 @@ func strDatetimeSubDuration(sc *stmtctx.StatementContext, d string, arg1 types.D
sc.AppendWarning(err)
return "", true, nil
}
arg1time, err := arg1.ConvertToTime(sc, uint8(types.GetFsp(arg1.String())))
resultTime, err := arg0.Add(sc, arg1.Neg())
if err != nil {
return "", false, err
}
tmpDuration := arg0.Sub(sc, &arg1time)
fsp := types.MaxFsp
if tmpDuration.MicroSecond() == 0 {
if resultTime.Microsecond() == 0 {
fsp = types.MinFsp
}
resultDuration, err := tmpDuration.ConvertToTime(sc, mysql.TypeDatetime)
if err != nil {
return "", false, err
}
resultDuration.SetFsp(fsp)
return resultDuration.String(), false, nil
resultTime.SetFsp(fsp)
return resultTime.String(), false, nil
}

// strDurationSubDuration subtracts duration from duration string, returns a string value.
Expand Down Expand Up @@ -6312,12 +6307,7 @@ func (b *builtinSubDatetimeAndDurationSig) evalTime(row chunk.Row) (types.Time,
return types.ZeroDatetime, isNull, err
}
sc := b.ctx.GetSessionVars().StmtCtx
arg1time, err := arg1.ConvertToTime(sc, mysql.TypeDatetime)
if err != nil {
return arg1time, true, err
}
tmpDuration := arg0.Sub(sc, &arg1time)
result, err := tmpDuration.ConvertToTime(sc, arg0.Type())
result, err := arg0.Add(sc, arg1.Neg())
return result, err != nil, err
}

Expand Down Expand Up @@ -6357,12 +6347,7 @@ func (b *builtinSubDatetimeAndStringSig) evalTime(row chunk.Row) (types.Time, bo
}
return types.ZeroDatetime, true, err
}
arg1time, err := arg1.ConvertToTime(sc, mysql.TypeDatetime)
if err != nil {
return types.ZeroDatetime, true, err
}
tmpDuration := arg0.Sub(sc, &arg1time)
result, err := tmpDuration.ConvertToTime(sc, mysql.TypeDatetime)
result, err := arg0.Add(sc, arg1.Neg())
return result, err != nil, err
}

Expand Down
2 changes: 2 additions & 0 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,8 @@ func (s *testEvaluatorSuite) TestSubTimeSig(c *C) {
{"110:00:00", "1 02:00:00", "84:00:00"},
{"2017-01-01 01:01:01.11", "01:01:01.11111", "2016-12-31 23:59:59.998890"},
{"2007-12-31 23:59:59.999999", "1 1:1:1.000002", "2007-12-30 22:58:58.999997"},
{"1000-01-01 01:00:00.000000", "00:00:00.000001", "1000-01-01 00:59:59.999999"},
{"1000-01-01 01:00:00.000001", "00:00:00.000001", "1000-01-01 01:00:00"},
{"1", "xxcvadfgasd", ""},
{"xxcvadfgasd", "1", ""},
}
Expand Down
14 changes: 2 additions & 12 deletions expression/builtin_time_vec_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 2 additions & 12 deletions expression/generator/time_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,7 @@ func (b *{{.SigName}}) vecEval{{ .Output.TypeName }}(input *chunk.Chunk, result
{{ else }}
sc := b.ctx.GetSessionVars().StmtCtx
arg1Duration := types.Duration{Duration: arg1, Fsp: -1}
arg1time, err := arg1Duration.ConvertToTime(sc, mysql.TypeDatetime)
if err != nil {
return err
}
tmpDuration := arg0.Sub(sc, &arg1time)
output, err := tmpDuration.ConvertToTime(sc, arg0.Type())
output, err := arg0.Add(sc, arg1Duration.Neg())
{{ end }}
if err != nil {
return err
Expand All @@ -202,12 +197,7 @@ func (b *{{.SigName}}) vecEval{{ .Output.TypeName }}(input *chunk.Chunk, result
}
return err
}
arg1time, err := arg1Duration.ConvertToTime(sc, mysql.TypeDatetime)
if err != nil {
return err
}
tmpDuration := arg0.Sub(sc, &arg1time)
output, err := tmpDuration.ConvertToTime(sc, mysql.TypeDatetime)
output, err := arg0.Add(sc, arg1Duration.Neg())
{{ end }}
if err != nil {
return err
Expand Down
Loading

0 comments on commit 868641d

Please sign in to comment.