Skip to content

Commit 29e9a41

Browse files
qw4990ngaut
authored andcommitted
expression: fix issue that monthname is not compatible with Mysql (#10109)
1 parent 4f20b5b commit 29e9a41

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

expression/builtin_time.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,9 @@ func (b *builtinStrToDateDateSig) evalTime(row chunk.Row) (types.Time, bool, err
18471847
if !succ {
18481848
return types.Time{}, true, handleInvalidTimeError(b.ctx, types.ErrIncorrectDatetimeValue.GenWithStackByArgs(t.String()))
18491849
}
1850+
if b.ctx.GetSessionVars().SQLMode.HasNoZeroDateMode() && (t.Time.Year() == 0 || t.Time.Month() == 0 || t.Time.Day() == 0) {
1851+
return types.Time{}, true, handleInvalidTimeError(b.ctx, types.ErrIncorrectDatetimeValue.GenWithStackByArgs(t.String()))
1852+
}
18501853
t.Type, t.Fsp = mysql.TypeDate, types.MinFsp
18511854
return t, false, nil
18521855
}
@@ -1876,6 +1879,9 @@ func (b *builtinStrToDateDatetimeSig) evalTime(row chunk.Row) (types.Time, bool,
18761879
if !succ {
18771880
return types.Time{}, true, handleInvalidTimeError(b.ctx, types.ErrIncorrectDatetimeValue.GenWithStackByArgs(t.String()))
18781881
}
1882+
if b.ctx.GetSessionVars().SQLMode.HasNoZeroDateMode() && (t.Time.Year() == 0 || t.Time.Month() == 0 || t.Time.Day() == 0) {
1883+
return types.Time{}, true, handleInvalidTimeError(b.ctx, types.ErrIncorrectDatetimeValue.GenWithStackByArgs(t.String()))
1884+
}
18791885
t.Type, t.Fsp = mysql.TypeDatetime, b.tp.Decimal
18801886
return t, false, nil
18811887
}
@@ -1908,6 +1914,9 @@ func (b *builtinStrToDateDurationSig) evalDuration(row chunk.Row) (types.Duratio
19081914
if !succ {
19091915
return types.Duration{}, true, handleInvalidTimeError(b.ctx, types.ErrIncorrectDatetimeValue.GenWithStackByArgs(t.String()))
19101916
}
1917+
if b.ctx.GetSessionVars().SQLMode.HasNoZeroDateMode() && (t.Time.Year() == 0 || t.Time.Month() == 0 || t.Time.Day() == 0) {
1918+
return types.Duration{}, true, handleInvalidTimeError(b.ctx, types.ErrIncorrectDatetimeValue.GenWithStackByArgs(t.String()))
1919+
}
19111920
t.Fsp = b.tp.Decimal
19121921
dur, err := t.ConvertToDuration()
19131922
return dur, err != nil, err

expression/integration_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4135,6 +4135,37 @@ func (s *testIntegrationSuite) TestDecimalConvertToTime(c *C) {
41354135
tk.MustQuery("select * from t").Check(testkit.Rows("2001-01-01 10:00:00.123456 2011-07-07 10:11:12"))
41364136
}
41374137

4138+
func (s *testIntegrationSuite) TestIssue9732(c *C) {
4139+
tk := testkit.NewTestKit(c, s.store)
4140+
defer s.cleanEnv(c)
4141+
4142+
tk.MustQuery(`select monthname(str_to_date(null, '%m')), monthname(str_to_date(null, '%m')),
4143+
monthname(str_to_date(1, '%m')), monthname(str_to_date(0, '%m'));`).Check(testkit.Rows("<nil> <nil> <nil> <nil>"))
4144+
4145+
nullCases := []struct {
4146+
sql string
4147+
ret string
4148+
}{
4149+
{"select str_to_date(1, '%m')", "0000-01-00"},
4150+
{"select str_to_date(01, '%d')", "0000-00-01"},
4151+
{"select str_to_date(2019, '%Y')", "2019-00-00"},
4152+
{"select str_to_date('5,2019','%m,%Y')", "2019-05-00"},
4153+
{"select str_to_date('01,2019','%d,%Y')", "2019-00-01"},
4154+
{"select str_to_date('01,5','%d,%m')", "0000-05-01"},
4155+
}
4156+
4157+
for _, nullCase := range nullCases {
4158+
tk.MustQuery(nullCase.sql).Check(testkit.Rows("<nil>"))
4159+
}
4160+
4161+
// remove NO_ZERO_DATE mode
4162+
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'")
4163+
4164+
for _, nullCase := range nullCases {
4165+
tk.MustQuery(nullCase.sql).Check(testkit.Rows(nullCase.ret))
4166+
}
4167+
}
4168+
41384169
func (s *testIntegrationSuite) TestDaynameArithmetic(c *C) {
41394170
tk := testkit.NewTestKit(c, s.store)
41404171
defer s.cleanEnv(c)

0 commit comments

Comments
 (0)