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: fix issue that monthname is not compatible with Mysql (#10109) #10733

Merged
merged 2 commits into from
Jun 6, 2019
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
9 changes: 9 additions & 0 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,9 @@ func (b *builtinStrToDateDateSig) evalTime(row chunk.Row) (types.Time, bool, err
if !succ {
return types.Time{}, true, handleInvalidTimeError(b.ctx, types.ErrIncorrectDatetimeValue.GenWithStackByArgs(t.String()))
}
if b.ctx.GetSessionVars().SQLMode.HasNoZeroDateMode() && (t.Time.Year() == 0 || t.Time.Month() == 0 || t.Time.Day() == 0) {
return types.Time{}, true, handleInvalidTimeError(b.ctx, types.ErrIncorrectDatetimeValue.GenWithStackByArgs(t.String()))
}
t.Type, t.Fsp = mysql.TypeDate, types.MinFsp
return t, false, nil
}
Expand Down Expand Up @@ -1857,6 +1860,9 @@ func (b *builtinStrToDateDatetimeSig) evalTime(row chunk.Row) (types.Time, bool,
if !succ {
return types.Time{}, true, handleInvalidTimeError(b.ctx, types.ErrIncorrectDatetimeValue.GenWithStackByArgs(t.String()))
}
if b.ctx.GetSessionVars().SQLMode.HasNoZeroDateMode() && (t.Time.Year() == 0 || t.Time.Month() == 0 || t.Time.Day() == 0) {
return types.Time{}, true, handleInvalidTimeError(b.ctx, types.ErrIncorrectDatetimeValue.GenWithStackByArgs(t.String()))
}
t.Type, t.Fsp = mysql.TypeDatetime, b.tp.Decimal
return t, false, nil
}
Expand Down Expand Up @@ -1889,6 +1895,9 @@ func (b *builtinStrToDateDurationSig) evalDuration(row chunk.Row) (types.Duratio
if !succ {
return types.Duration{}, true, handleInvalidTimeError(b.ctx, types.ErrIncorrectDatetimeValue.GenWithStackByArgs(t.String()))
}
if b.ctx.GetSessionVars().SQLMode.HasNoZeroDateMode() && (t.Time.Year() == 0 || t.Time.Month() == 0 || t.Time.Day() == 0) {
return types.Duration{}, true, handleInvalidTimeError(b.ctx, types.ErrIncorrectDatetimeValue.GenWithStackByArgs(t.String()))
}
t.Fsp = b.tp.Decimal
dur, err := t.ConvertToDuration()
return dur, err != nil, errors.Trace(err)
Expand Down
38 changes: 38 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4069,3 +4069,41 @@ func (s *testIntegrationSuite) TestDecimalConvertToTime(c *C) {
tk.MustExec("insert t values (20010101100000.123456, 20110707101112.123456)")
tk.MustQuery("select * from t").Check(testkit.Rows("2001-01-01 10:00:00.123456 2011-07-07 10:11:12"))
}

func (s *testIntegrationSuite) TestIssue9732(c *C) {
tk := testkit.NewTestKit(c, s.store)
r := tk.MustQuery("select @@sql_mode")
m := fmt.Sprintf("%v", r.Rows()[0])
m = strings.Trim(m, "[]")
tk.MustExec(fmt.Sprintf("set sql_mode='%v'", m+",NO_ZERO_DATE"))
defer func() {
tk.MustExec(fmt.Sprintf("set sql_mode='%v'", m))
s.cleanEnv(c)
}()

tk.MustQuery(`select monthname(str_to_date(null, '%m')), monthname(str_to_date(null, '%m')),
monthname(str_to_date(1, '%m')), monthname(str_to_date(0, '%m'));`).Check(testkit.Rows("<nil> <nil> <nil> <nil>"))

nullCases := []struct {
sql string
ret string
}{
{"select str_to_date(1, '%m')", "0000-01-00"},
{"select str_to_date(01, '%d')", "0000-00-01"},
{"select str_to_date(2019, '%Y')", "2019-00-00"},
{"select str_to_date('5,2019','%m,%Y')", "2019-05-00"},
{"select str_to_date('01,2019','%d,%Y')", "2019-00-01"},
{"select str_to_date('01,5','%d,%m')", "0000-05-01"},
}

for _, nullCase := range nullCases {
tk.MustQuery(nullCase.sql).Check(testkit.Rows("<nil>"))
}

// remove NO_ZERO_DATE mode
tk.MustExec("set sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'")

for _, nullCase := range nullCases {
tk.MustQuery(nullCase.sql).Check(testkit.Rows(nullCase.ret))
}
}