Skip to content

Commit

Permalink
expression, types: use time.Add() to calculate subtime (#32903)
Browse files Browse the repository at this point in the history
close #31868
  • Loading branch information
Defined2014 authored Mar 16, 2022
1 parent 2dd0074 commit 0f343d3
Show file tree
Hide file tree
Showing 5 changed files with 19 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 @@ -5391,21 +5391,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 @@ -6476,12 +6471,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 @@ -6521,12 +6511,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 @@ -1036,6 +1036,8 @@ func TestSubTimeSig(t *testing.T) {
{"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 @@ -175,12 +175,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 @@ -205,12 +200,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
7 changes: 7 additions & 0 deletions expression/integration_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2115,6 +2115,13 @@ func TestTimeBuiltin(t *testing.T) {
tk.MustQuery("select subtime(cast('10:10:10' as time), cast('9:10:10' as time))").Check(testkit.Rows("01:00:00"))
tk.MustQuery("select subtime('10:10:10', cast('9:10:10' as time))").Check(testkit.Rows("01:00:00"))

// SUBTIME issue #31868
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a DATETIME(6))")
tk.MustExec(`insert into t values ("1000-01-01 01:00:00.000000"), ("1000-01-01 01:00:00.000001")`)
tk.MustQuery(`SELECT SUBTIME(a, '00:00:00.000001') FROM t ORDER BY a;`).Check(testkit.Rows("1000-01-01 00:59:59.999999", "1000-01-01 01:00:00.000000"))
tk.MustQuery(`SELECT SUBTIME(a, '10:00:00.000001') FROM t ORDER BY a;`).Check(testkit.Rows("0999-12-31 14:59:59.999999", "0999-12-31 15:00:00.000000"))

// ADDTIME & SUBTIME issue #5966
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a datetime, b timestamp, c time, d date, e bit(1))")
Expand Down

0 comments on commit 0f343d3

Please sign in to comment.