Skip to content

Commit

Permalink
expression: fix issue that monthname is not compatible with Mysql (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 committed Jun 6, 2019
1 parent 011caea commit a11badb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
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
31 changes: 31 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4069,3 +4069,34 @@ 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)
defer 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))
}
}

0 comments on commit a11badb

Please sign in to comment.