From 8028834185374ae54e8f383ed7d64c6071e8efd5 Mon Sep 17 00:00:00 2001 From: tangenta Date: Thu, 5 Aug 2021 03:05:07 +0800 Subject: [PATCH 1/2] cherry pick #26828 to release-5.0 Signed-off-by: ti-srebot --- executor/executor.go | 5 ++++ executor/executor_test.go | 52 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/executor/executor.go b/executor/executor.go index 4b8bae365586f..9ed2ef67918da 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -1659,6 +1659,11 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) { case *ast.CreateTableStmt, *ast.AlterTableStmt: // Make sure the sql_mode is strict when checking column default value. sc.InCreateOrAlterStmt = true +<<<<<<< HEAD +======= + sc.AllowInvalidDate = vars.SQLMode.HasAllowInvalidDatesMode() + sc.IgnoreZeroInDate = !vars.SQLMode.HasNoZeroInDateMode() || !vars.StrictSQLMode || sc.AllowInvalidDate +>>>>>>> 0c7283418... executor: make NO_ZERO_IN_DATE affect the default values (#26828) case *ast.LoadDataStmt: sc.DupKeyAsWarning = true sc.BadNullAsWarning = true diff --git a/executor/executor_test.go b/executor/executor_test.go index a47dc09be8e72..6d5d6dba3fda8 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -7637,6 +7637,58 @@ func (s *testSuite) TestZeroDateTimeCompatibility(c *C) { } } +<<<<<<< HEAD +======= +// https://github.com/pingcap/tidb/issues/24165. +func (s *testSuite) TestInvalidDateValueInCreateTable(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test;") + tk.MustExec("drop table if exists t;") + + // Test for sql mode 'NO_ZERO_IN_DATE'. + tk.MustExec("set @@sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE';") + tk.MustGetErrCode("create table t (a datetime default '2999-00-00 00:00:00');", errno.ErrInvalidDefault) + tk.MustExec("create table t (a datetime);") + tk.MustGetErrCode("alter table t modify column a datetime default '2999-00-00 00:00:00';", errno.ErrInvalidDefault) + tk.MustExec("drop table if exists t;") + + // Test for sql mode 'NO_ZERO_DATE'. + tk.MustExec("set @@sql_mode='STRICT_TRANS_TABLES,NO_ZERO_DATE';") + tk.MustGetErrCode("create table t (a datetime default '0000-00-00 00:00:00');", errno.ErrInvalidDefault) + tk.MustExec("create table t (a datetime);") + tk.MustGetErrCode("alter table t modify column a datetime default '0000-00-00 00:00:00';", errno.ErrInvalidDefault) + tk.MustExec("drop table if exists t;") + + // Remove NO_ZERO_DATE and NO_ZERO_IN_DATE. + tk.MustExec("set @@sql_mode='STRICT_TRANS_TABLES';") + // Test create table with zero datetime as a default value. + tk.MustExec("create table t (a datetime default '2999-00-00 00:00:00');") + tk.MustExec("drop table if exists t;") + tk.MustExec("create table t (a datetime default '0000-00-00 00:00:00');") + tk.MustExec("drop table if exists t;") + tk.MustExec("create table t (a datetime);") + tk.MustExec("alter table t modify column a datetime default '2999-00-00 00:00:00';") + tk.MustExec("alter table t modify column a datetime default '0000-00-00 00:00:00';") + tk.MustExec("drop table if exists t;") + + // Test create table with invalid datetime(02-30) as a default value. + tk.MustExec("set @@sql_mode='STRICT_TRANS_TABLES';") + tk.MustGetErrCode("create table t (a datetime default '2999-02-30 00:00:00');", errno.ErrInvalidDefault) + tk.MustExec("drop table if exists t;") + // NO_ZERO_IN_DATE and NO_ZERO_DATE have nothing to do with invalid datetime(02-30). + tk.MustExec("set @@sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE';") + tk.MustGetErrCode("create table t (a datetime default '2999-02-30 00:00:00');", errno.ErrInvalidDefault) + tk.MustExec("drop table if exists t;") + // ALLOW_INVALID_DATES allows invalid datetime(02-30). + tk.MustExec("set @@sql_mode='STRICT_TRANS_TABLES,ALLOW_INVALID_DATES';") + tk.MustExec("create table t (a datetime default '2999-02-30 00:00:00');") + tk.MustExec("drop table if exists t;") + tk.MustExec("create table t (a datetime);") + tk.MustExec("alter table t modify column a datetime default '2999-02-30 00:00:00';") + tk.MustExec("drop table if exists t;") +} + +>>>>>>> 0c7283418... executor: make NO_ZERO_IN_DATE affect the default values (#26828) func (s *testSuite) TestOOMActionPriority(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") From 21465fb21297f7fd36d4832b8296757582d7c002 Mon Sep 17 00:00:00 2001 From: tangenta Date: Thu, 2 Sep 2021 18:03:23 +0800 Subject: [PATCH 2/2] resolve conflicts --- executor/executor.go | 3 --- executor/executor_test.go | 4 +--- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/executor/executor.go b/executor/executor.go index 9ed2ef67918da..53873253af720 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -1659,11 +1659,8 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) { case *ast.CreateTableStmt, *ast.AlterTableStmt: // Make sure the sql_mode is strict when checking column default value. sc.InCreateOrAlterStmt = true -<<<<<<< HEAD -======= sc.AllowInvalidDate = vars.SQLMode.HasAllowInvalidDatesMode() sc.IgnoreZeroInDate = !vars.SQLMode.HasNoZeroInDateMode() || !vars.StrictSQLMode || sc.AllowInvalidDate ->>>>>>> 0c7283418... executor: make NO_ZERO_IN_DATE affect the default values (#26828) case *ast.LoadDataStmt: sc.DupKeyAsWarning = true sc.BadNullAsWarning = true diff --git a/executor/executor_test.go b/executor/executor_test.go index 6d5d6dba3fda8..97fde5aad6c8e 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -42,6 +42,7 @@ import ( "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/domain/infosync" + "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/executor" "github.com/pingcap/tidb/expression" "github.com/pingcap/tidb/infoschema" @@ -7637,8 +7638,6 @@ func (s *testSuite) TestZeroDateTimeCompatibility(c *C) { } } -<<<<<<< HEAD -======= // https://github.com/pingcap/tidb/issues/24165. func (s *testSuite) TestInvalidDateValueInCreateTable(c *C) { tk := testkit.NewTestKit(c, s.store) @@ -7688,7 +7687,6 @@ func (s *testSuite) TestInvalidDateValueInCreateTable(c *C) { tk.MustExec("drop table if exists t;") } ->>>>>>> 0c7283418... executor: make NO_ZERO_IN_DATE affect the default values (#26828) func (s *testSuite) TestOOMActionPriority(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test")