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: incompatibility with MySQL for ADDTIME() #21401

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
10 changes: 5 additions & 5 deletions expression/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ func (g *randHexStrGener) gen() interface{} {
return string(buf)
}

// dateTimeGener is used to generate a dataTime
// dateTimeGener is used to generate a dateTime
type dateTimeGener struct {
Fsp int
Year int
Expand Down Expand Up @@ -740,7 +740,7 @@ func (g *dateTimeGener) gen() interface{} {
return t
}

// dateTimeStrGener is used to generate strings which are dataTime format
// dateTimeStrGener is used to generate strings which are dateTime format
type dateTimeStrGener struct {
Fsp int
Year int
Expand All @@ -762,14 +762,14 @@ func (g *dateTimeStrGener) gen() interface{} {
hour := g.randGen.Intn(12)
minute := g.randGen.Intn(60)
second := g.randGen.Intn(60)
dataTimeStr := fmt.Sprintf("%d-%d-%d %d:%d:%d",
dateTimeStr := fmt.Sprintf("%d-%d-%d %d:%d:%d",
g.Year, g.Month, g.Day, hour, minute, second)
if g.Fsp > 0 && g.Fsp <= 6 {
microFmt := fmt.Sprintf(".%%0%dd", g.Fsp)
return dataTimeStr + fmt.Sprintf(microFmt, g.randGen.Int()%(10^g.Fsp))
return dateTimeStr + fmt.Sprintf(microFmt, g.randGen.Int()%(10^g.Fsp))
}

return dataTimeStr
return dateTimeStr
}

// dateStrGener is used to generate strings which are date format
Expand Down
8 changes: 8 additions & 0 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -5559,7 +5559,15 @@ func (b *builtinAddStringAndStringSig) evalString(row chunk.Row) (result string,
if isNull || err != nil {
return "", isNull, err
}
if !isDuration(arg1Str) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Test case fails

[2021-08-12T09:34:14.503Z] FAIL: builtin_time_test.go:903: testEvaluatorSuite.TestAddTimeSig
[2021-08-12T09:34:14.503Z] 
[2021-08-12T09:34:14.503Z] builtin_time_test.go:1012:
[2021-08-12T09:34:14.503Z]     c.Assert(len(warnings), Equals, i+1+beforeWarnCnt)
[2021-08-12T09:34:14.503Z] ... obtained int = 8
[2021-08-12T09:34:14.503Z] ... expected int = 9
[2021-08-12T09:34:14.503Z] 

Here is the reason, if arg1Str is not a MySQL Time, it just return without error.

return "", true, nil
}
sc := b.ctx.GetSessionVars().StmtCtx
arg1Time, _ := types.ParseDatetime(sc, arg1Str)
hidehalo marked this conversation as resolved.
Show resolved Hide resolved
if arg1Time.Month() > 0 {
return "", true, err
}

arg1, err = types.ParseDuration(sc, arg1Str, getFsp4TimeAddSub(arg1Str))
Copy link
Contributor Author

@hidehalo hidehalo Aug 13, 2021

Choose a reason for hiding this comment

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

I'm more expect here throw a ErrTruncatedWrongVal error, IMO, it should be (in fact no). @wshwsh12 what do you think? more details I was said in Slack. please check it out.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think so. MySQL also doesn't throw a error/warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's make sense. Should we remove those two test cases(below)? Or we should throw a error/warning inside func (b *builtinAddStringAndStringSig) evalString(row chunk.Row)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wshwsh12 PTAL PR #11262, I really feel confuse, would you like to give me some advices? thx!

if err != nil {
if terror.ErrorEqual(err, types.ErrTruncatedWrongVal) {
Expand Down
4 changes: 4 additions & 0 deletions expression/builtin_time_vec_generated.go

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

8 changes: 4 additions & 4 deletions expression/generator/time_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import (
{{ end }}
{{ define "SetNull" }}{{if .Output.Fixed}}result.SetNull(i, true){{else}}result.AppendNull(){{end}} // fixed: {{.Output.Fixed }}{{ end }}
{{ define "ConvertStringToDuration" }}
{{ if and (ne .SigName "builtinAddStringAndStringSig") (ne .SigName "builtinSubStringAndStringSig") }}
{{ if (ne .SigName "builtinSubStringAndStringSig") }}
if !isDuration(arg1) {
{{ template "SetNull" . }}
continue
Expand Down Expand Up @@ -852,7 +852,7 @@ var timeDiffSigsTmpl = []sig{
{SigName: "builtinTimeTimeTimeDiffSig", TypeA: TypeDatetime, TypeB: TypeDatetime, Output: TypeDuration},
}

var addDataSigsTmpl = []sig{
var addDateSigsTmpl = []sig{
{SigName: "builtinAddDateStringStringSig", TypeA: TypeString, TypeB: TypeString, Output: TypeDatetime},
{SigName: "builtinAddDateStringIntSig", TypeA: TypeString, TypeB: TypeInt, Output: TypeDatetime},
{SigName: "builtinAddDateStringRealSig", TypeA: TypeString, TypeB: TypeReal, Output: TypeDatetime},
Expand Down Expand Up @@ -912,7 +912,7 @@ var tmplVal = struct {
{FuncName: "AddTime", Sigs: addTimeSigsTmpl},
{FuncName: "SubTime", Sigs: subTimeSigsTmpl},
{FuncName: "TimeDiff", Sigs: timeDiffSigsTmpl},
{FuncName: "AddDate", Sigs: addDataSigsTmpl},
{FuncName: "AddDate", Sigs: addDateSigsTmpl},
{FuncName: "SubDate", Sigs: subDataSigsTmpl},
},
}
Expand All @@ -931,7 +931,7 @@ func generateDotGo(fileName string) error {
if err != nil {
return err
}
err = addOrSubDate.Execute(w, function{FuncName: "AddDate", Sigs: addDataSigsTmpl})
err = addOrSubDate.Execute(w, function{FuncName: "AddDate", Sigs: addDateSigsTmpl})
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1668,6 +1668,8 @@ func (s *testIntegrationSuite2) TestTimeBuiltin(c *C) {
tk.MustQuery(`select addtime(cast(1 as time), cast(1 as time))`).Check(testkit.Rows("00:00:02"))
tk.MustQuery(`select addtime(cast(null as time), cast(1 as time))`).Check(testkit.Rows("<nil>"))
tk.MustQuery(`select addtime(cast(1 as time), cast(null as time))`).Check(testkit.Rows("<nil>"))
tk.MustQuery(`select addtime("2020-05-13 14:01:24", "2020-04-29 05:11:19")`).Check(testkit.Rows("<nil>"))
tk.MustQuery(`select addtime("2020-05-13 14:01:24", "-2020-04-29 05:11:19")`).Check(testkit.Rows("<nil>"))

// for SUBTIME
result = tk.MustQuery("select subtime('01:01:11', '00:00:01.013'), subtime('01:01:11.00', '00:00:01'), subtime" +
Expand Down