From 821488fc173c8ba2c2bb52af2d9b4d917a6765d1 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Wed, 10 Apr 2019 20:12:02 +0800 Subject: [PATCH 1/4] fixup --- expression/builtin_time.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/expression/builtin_time.go b/expression/builtin_time.go index 44842541a41ff..ede5f34bb7df0 100644 --- a/expression/builtin_time.go +++ b/expression/builtin_time.go @@ -1847,6 +1847,10 @@ 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 } @@ -1876,6 +1880,10 @@ 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 } @@ -1908,6 +1916,10 @@ 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, err From d04512aaa29574a6badd31f65db891bc1d87660f Mon Sep 17 00:00:00 2001 From: qw4990 Date: Wed, 10 Apr 2019 20:21:35 +0800 Subject: [PATCH 2/4] add UT --- expression/integration_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/expression/integration_test.go b/expression/integration_test.go index 73f27629ee844..6c041e988f7aa 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -4135,6 +4135,26 @@ func (s *testIntegrationSuite) TestDecimalConvertToTime(c *C) { 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(" ")) + + nullSQLs := []string { + "select str_to_date(1, '%m')", + "select str_to_date(01, '%d')", + "select str_to_date(2019, '%Y')", + "select str_to_date('5,2019','%m,%Y')", + "select str_to_date('01,2019','%d,%Y')", + "select str_to_date('01,5','%d,%m')", + } + for _, sql := range nullSQLs { + tk.MustQuery(sql).Check(testkit.Rows("")) + } +} + func (s *testIntegrationSuite) TestDaynameArithmetic(c *C) { tk := testkit.NewTestKit(c, s.store) defer s.cleanEnv(c) From ba56f15353194dd3ba86e3222f42cb26d3ecd669 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Wed, 10 Apr 2019 20:22:23 +0800 Subject: [PATCH 3/4] fmt --- expression/builtin_time.go | 9 +++------ expression/integration_test.go | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/expression/builtin_time.go b/expression/builtin_time.go index ede5f34bb7df0..ba08265945dc6 100644 --- a/expression/builtin_time.go +++ b/expression/builtin_time.go @@ -1847,8 +1847,7 @@ 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) { + 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 @@ -1880,8 +1879,7 @@ 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) { + 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 @@ -1916,8 +1914,7 @@ 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) { + 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 diff --git a/expression/integration_test.go b/expression/integration_test.go index 6c041e988f7aa..b3d69d08d5df6 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -4142,7 +4142,7 @@ func (s *testIntegrationSuite) TestIssue9732(c *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(" ")) - nullSQLs := []string { + nullSQLs := []string{ "select str_to_date(1, '%m')", "select str_to_date(01, '%d')", "select str_to_date(2019, '%Y')", From acb2fa8fc7e561d45c1d781842f8c268d3081886 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Wed, 10 Apr 2019 20:49:43 +0800 Subject: [PATCH 4/4] add more cases --- expression/integration_test.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index b3d69d08d5df6..6bd91b57691bf 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -4142,16 +4142,27 @@ func (s *testIntegrationSuite) TestIssue9732(c *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(" ")) - nullSQLs := []string{ - "select str_to_date(1, '%m')", - "select str_to_date(01, '%d')", - "select str_to_date(2019, '%Y')", - "select str_to_date('5,2019','%m,%Y')", - "select str_to_date('01,2019','%d,%Y')", - "select str_to_date('01,5','%d,%m')", + 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("")) } - for _, sql := range nullSQLs { - tk.MustQuery(sql).Check(testkit.Rows("")) + + // 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)) } }