Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: handle invalid argument for addtime and subtime function #21600

Merged
merged 6 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -5101,21 +5101,23 @@ func isDuration(str string) bool {
}

// strDatetimeAddDuration adds duration to datetime string, returns a string value.
func strDatetimeAddDuration(sc *stmtctx.StatementContext, d string, arg1 types.Duration) (string, error) {
func strDatetimeAddDuration(sc *stmtctx.StatementContext, d string, arg1 types.Duration) (result string, isNull bool, err error) {
arg0, err := types.ParseTime(sc, d, mysql.TypeDatetime, types.MaxFsp)
if err != nil {
return "", err
// Return a warning regardless of the sql_mode, this is compatible with MySQL.
sc.AppendWarning(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we check the sql_mode to decide whether to return an error or a warning?

e.g. sc.HandleTruncate

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even in the strict mode, MySQL returns a warning.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment for it?

return "", true, nil
}
ret, err := arg0.Add(sc, arg1)
if err != nil {
return "", err
return "", false, err
}
fsp := types.MaxFsp
if ret.Microsecond() == 0 {
fsp = types.MinFsp
}
ret.SetFsp(fsp)
return ret.String(), nil
return ret.String(), false, nil
}

// strDurationAddDuration adds duration to duration string, returns a string value.
Expand All @@ -5136,14 +5138,16 @@ func strDurationAddDuration(sc *stmtctx.StatementContext, d string, arg1 types.D
}

// strDatetimeSubDuration subtracts duration from datetime string, returns a string value.
func strDatetimeSubDuration(sc *stmtctx.StatementContext, d string, arg1 types.Duration) (string, error) {
func strDatetimeSubDuration(sc *stmtctx.StatementContext, d string, arg1 types.Duration) (result string, isNull bool, err error) {
arg0, err := types.ParseTime(sc, d, mysql.TypeDatetime, types.MaxFsp)
if err != nil {
return "", err
// Return a warning regardless of the sql_mode, this is compatible with MySQL.
sc.AppendWarning(err)
return "", true, nil
}
arg1time, err := arg1.ConvertToTime(sc, uint8(types.GetFsp(arg1.String())))
if err != nil {
return "", err
return "", false, err
}
tmpDuration := arg0.Sub(sc, &arg1time)
fsp := types.MaxFsp
Expand All @@ -5152,10 +5156,10 @@ func strDatetimeSubDuration(sc *stmtctx.StatementContext, d string, arg1 types.D
}
resultDuration, err := tmpDuration.ConvertToTime(sc, mysql.TypeDatetime)
if err != nil {
return "", err
return "", false, err
}
resultDuration.SetFsp(fsp)
return resultDuration.String(), nil
return resultDuration.String(), false, nil
}

// strDurationSubDuration subtracts duration from duration string, returns a string value.
Expand Down Expand Up @@ -5456,8 +5460,8 @@ func (b *builtinAddStringAndDurationSig) evalString(row chunk.Row) (result strin
}
return result, false, nil
}
result, err = strDatetimeAddDuration(sc, arg0, arg1)
return result, err != nil, err
result, isNull, err = strDatetimeAddDuration(sc, arg0, arg1)
return result, isNull, err
}

type builtinAddStringAndStringSig struct {
Expand Down Expand Up @@ -5509,8 +5513,8 @@ func (b *builtinAddStringAndStringSig) evalString(row chunk.Row) (result string,
}
return result, false, nil
}
result, err = strDatetimeAddDuration(sc, arg0, arg1)
return result, err != nil, err
result, isNull, err = strDatetimeAddDuration(sc, arg0, arg1)
return result, isNull, err
}

type builtinAddDateAndDurationSig struct {
Expand Down Expand Up @@ -6331,8 +6335,8 @@ func (b *builtinSubStringAndDurationSig) evalString(row chunk.Row) (result strin
}
return result, false, nil
}
result, err = strDatetimeSubDuration(sc, arg0, arg1)
return result, err != nil, err
result, isNull, err = strDatetimeSubDuration(sc, arg0, arg1)
return result, isNull, err
}

type builtinSubStringAndStringSig struct {
Expand Down Expand Up @@ -6384,8 +6388,8 @@ func (b *builtinSubStringAndStringSig) evalString(row chunk.Row) (result string,
}
return result, false, nil
}
result, err = strDatetimeSubDuration(sc, arg0, arg1)
return result, err != nil, err
result, isNull, err = strDatetimeSubDuration(sc, arg0, arg1)
return result, isNull, err
}

type builtinSubTimeStringNullSig struct {
Expand Down
14 changes: 12 additions & 2 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,8 @@ func (s *testEvaluatorSuite) TestAddTimeSig(c *C) {
{"2017-12-31 23:59:59", "1", "2018-01-01 00:00:00"},
{"2007-12-31 23:59:59.999999", "2 1:1:1.000002", "2008-01-03 01:01:01.000001"},
{"2018-08-16 20:21:01", "00:00:00.000001", "2018-08-16 20:21:01.000001"},
{"1", "xxcvadfgasd", ""},
{"xxcvadfgasd", "1", ""},
}
fc := funcs[ast.AddTime]
for _, t := range tbl {
Expand Down Expand Up @@ -992,7 +994,10 @@ func (s *testEvaluatorSuite) TestAddTimeSig(c *C) {
{types.CurrentTime(mysql.TypeTimestamp), "-32073", types.ErrTruncatedWrongVal},
{types.CurrentTime(mysql.TypeDate), "-32073", types.ErrTruncatedWrongVal},
{types.CurrentTime(mysql.TypeDatetime), "-32073", types.ErrTruncatedWrongVal},
{"1", "xxcvadfgasd", types.ErrTruncatedWrongVal},
{"xxcvadfgasd", "1", types.ErrTruncatedWrongVal},
}
beforeWarnCnt := int(s.ctx.GetSessionVars().StmtCtx.WarningCount())
for i, t := range tblWarning {
tmpInput := types.NewDatum(t.Input)
tmpInputDuration := types.NewDatum(t.InputDuration)
Expand All @@ -1004,7 +1009,7 @@ func (s *testEvaluatorSuite) TestAddTimeSig(c *C) {
c.Assert(result, Equals, "")
c.Assert(d.IsNull(), Equals, true)
warnings := s.ctx.GetSessionVars().StmtCtx.GetWarnings()
c.Assert(len(warnings), Equals, i+1)
c.Assert(len(warnings), Equals, i+1+beforeWarnCnt)
c.Assert(terror.ErrorEqual(t.warning, warnings[i].Err), IsTrue, Commentf("err %v", warnings[i].Err))
}
}
Expand All @@ -1019,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"},
{"1", "xxcvadfgasd", ""},
{"xxcvadfgasd", "1", ""},
}
fc := funcs[ast.SubTime]
for _, t := range tbl {
Expand Down Expand Up @@ -1084,7 +1091,10 @@ func (s *testEvaluatorSuite) TestSubTimeSig(c *C) {
{types.CurrentTime(mysql.TypeTimestamp), "-32073", types.ErrTruncatedWrongVal},
{types.CurrentTime(mysql.TypeDate), "-32073", types.ErrTruncatedWrongVal},
{types.CurrentTime(mysql.TypeDatetime), "-32073", types.ErrTruncatedWrongVal},
{"1", "xxcvadfgasd", types.ErrTruncatedWrongVal},
{"xxcvadfgasd", "1", types.ErrTruncatedWrongVal},
}
beforeWarnCnt := int(s.ctx.GetSessionVars().StmtCtx.WarningCount())
for i, t := range tblWarning {
tmpInput := types.NewDatum(t.Input)
tmpInputDuration := types.NewDatum(t.InputDuration)
Expand All @@ -1096,7 +1106,7 @@ func (s *testEvaluatorSuite) TestSubTimeSig(c *C) {
c.Assert(result, Equals, "")
c.Assert(d.IsNull(), Equals, true)
warnings := s.ctx.GetSessionVars().StmtCtx.GetWarnings()
c.Assert(len(warnings), Equals, i+1)
c.Assert(len(warnings), Equals, i+1+beforeWarnCnt)
c.Assert(terror.ErrorEqual(t.warning, warnings[i].Err), IsTrue, Commentf("err %v", warnings[i].Err))
}
}
Expand Down
32 changes: 28 additions & 4 deletions expression/builtin_time_vec_generated.go

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

22 changes: 17 additions & 5 deletions expression/generator/time_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (b *{{.SigName}}) vecEval{{ .Output.TypeName }}(input *chunk.Chunk, result
if err != nil {
return err
}

{{ else if or (eq .SigName "builtinAddDatetimeAndStringSig") (eq .SigName "builtinSubDatetimeAndStringSig") }}
{{ if eq $.FuncName "AddTime" }}
{{ template "ConvertStringToDuration" . }}
Expand Down Expand Up @@ -242,6 +242,7 @@ func (b *{{.SigName}}) vecEval{{ .Output.TypeName }}(input *chunk.Chunk, result
fsp1 := int8(b.args[1].GetType().Decimal)
arg1Duration := types.Duration{Duration: arg1, Fsp: fsp1}
var output string
var isNull bool
if isDuration(arg0) {
{{ if eq $.FuncName "AddTime" }}
output, err = strDurationAddDuration(sc, arg0, arg1Duration)
Expand All @@ -258,17 +259,23 @@ func (b *{{.SigName}}) vecEval{{ .Output.TypeName }}(input *chunk.Chunk, result
}
} else {
{{ if eq $.FuncName "AddTime" }}
output, err = strDatetimeAddDuration(sc, arg0, arg1Duration)
output, isNull, err = strDatetimeAddDuration(sc, arg0, arg1Duration)
{{ else }}
output, err = strDatetimeSubDuration(sc, arg0, arg1Duration)
output, isNull, err = strDatetimeSubDuration(sc, arg0, arg1Duration)
{{ end }}
if err != nil {
return err
}
if isNull {
sc.AppendWarning(err)
{{ template "SetNull" . }}
continue
}
}
{{ else if or (eq .SigName "builtinAddStringAndStringSig") (eq .SigName "builtinSubStringAndStringSig") }}
{{ template "ConvertStringToDuration" . }}
var output string
var isNull bool
if isDuration(arg0) {
{{ if eq $.FuncName "AddTime" }}
output, err = strDurationAddDuration(sc, arg0, arg1Duration)
Expand All @@ -285,13 +292,18 @@ func (b *{{.SigName}}) vecEval{{ .Output.TypeName }}(input *chunk.Chunk, result
}
} else {
{{ if eq $.FuncName "AddTime" }}
output, err = strDatetimeAddDuration(sc, arg0, arg1Duration)
output, isNull, err = strDatetimeAddDuration(sc, arg0, arg1Duration)
{{ else }}
output, err = strDatetimeSubDuration(sc, arg0, arg1Duration)
output, isNull, err = strDatetimeSubDuration(sc, arg0, arg1Duration)
{{ end }}
if err != nil {
return err
}
if isNull {
sc.AppendWarning(err)
{{ template "SetNull" . }}
continue
}
}
{{ else if or (eq .SigName "builtinAddDateAndDurationSig") (eq .SigName "builtinSubDateAndDurationSig") }}
fsp0 := int8(b.args[0].GetType().Decimal)
Expand Down