Skip to content

Commit

Permalink
ddl: Allow create timestamp default 0 without sql_mode NO_ZERO_DATE (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mjonss authored Dec 28, 2021
1 parent 4ca3b02 commit e983d32
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions ddl/backfilling.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ func (w *worker) writePhysicalTableRecord(t table.PhysicalTable, bfWorkerType ba
sessCtx.GetSessionVars().StmtCtx.AllowInvalidDate = sqlMode.HasAllowInvalidDatesMode()
sessCtx.GetSessionVars().StmtCtx.DividedByZeroAsWarning = !sqlMode.HasStrictMode()
sessCtx.GetSessionVars().StmtCtx.IgnoreZeroInDate = !sqlMode.HasStrictMode() || sqlMode.HasAllowInvalidDatesMode()
sessCtx.GetSessionVars().StmtCtx.NoZeroDate = sqlMode.HasStrictMode()

switch bfWorkerType {
case typeAddIndexWorker:
Expand Down
42 changes: 42 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,48 @@ func (s *testIntegrationSuite5) TestNoZeroDateMode(c *C) {
tk.MustGetErrCode("create table test_zero_date(agent_start_time timestamp NOT NULL DEFAULT '0000-00-00 00:00:00')", errno.ErrInvalidDefault)
tk.MustGetErrCode("create table test_zero_date(a timestamp default '0000-00-00 00');", errno.ErrInvalidDefault)
tk.MustGetErrCode("create table test_zero_date(a timestamp default 0);", errno.ErrInvalidDefault)
defer tk.MustExec(`drop table if exists test_zero_date`)
tk.MustExec("set session 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';")
tk.MustExec("create table test_zero_date (a timestamp default 0)")
tk.MustExec(`insert into test_zero_date values (0)`)
tk.MustQuery(`select a, unix_timestamp(a) from test_zero_date`).Check(testkit.Rows("0000-00-00 00:00:00 0"))
tk.MustExec(`update test_zero_date set a = '2001-01-01 11:11:11' where a = 0`)
tk.MustExec(`replace into test_zero_date values (0)`)
tk.MustExec(`delete from test_zero_date where a = 0`)
tk.MustExec(`update test_zero_date set a = 0 where a = '2001-01-01 11:11:11'`)
tk.MustExec("set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';")
tk.MustGetErrCode(`insert into test_zero_date values (0)`, errno.ErrTruncatedWrongValue)
tk.MustGetErrCode(`replace into test_zero_date values (0)`, errno.ErrTruncatedWrongValue)
tk.MustGetErrCode(`update test_zero_date set a = 0 where a = 0`, errno.ErrTruncatedWrongValue)
tk.MustExec(`delete from test_zero_date where a = 0`)
tk.MustQuery(`select a, unix_timestamp(a) from test_zero_date`).Check(testkit.Rows())
tk.MustExec(`drop table test_zero_date`)
tk.MustExec("set session sql_mode=''")
tk.MustExec("create table test_zero_date (a timestamp default 0)")
tk.MustExec(`drop table test_zero_date`)
tk.MustExec(`create table test_zero_date (a int)`)
tk.MustExec(`insert into test_zero_date values (0)`)
tk.MustExec(`alter table test_zero_date modify a date`)
tk.MustExec("set session sql_mode='NO_ZERO_DATE'")
tk.MustExec(`drop table test_zero_date`)
tk.MustExec("create table test_zero_date (a timestamp default 0)")
tk.MustExec(`drop table test_zero_date`)
tk.MustExec(`create table test_zero_date (a int)`)
tk.MustExec(`insert into test_zero_date values (0)`)
tk.MustExec(`alter table test_zero_date modify a date`)
tk.MustExec("set session sql_mode='STRICT_TRANS_TABLES'")
tk.MustExec(`drop table test_zero_date`)
tk.MustExec("create table test_zero_date (a timestamp default 0)")
tk.MustExec(`drop table test_zero_date`)
tk.MustExec(`create table test_zero_date (a int)`)
tk.MustExec(`insert into test_zero_date values (0)`)
tk.MustGetErrCode(`alter table test_zero_date modify a date`, errno.ErrTruncatedWrongValue)
tk.MustExec("set session sql_mode='NO_ZERO_DATE,STRICT_TRANS_TABLES'")
tk.MustExec(`drop table test_zero_date`)
tk.MustGetErrCode("create table test_zero_date (a timestamp default 0)", errno.ErrInvalidDefault)
tk.MustExec(`create table test_zero_date (a int)`)
tk.MustExec(`insert into test_zero_date values (0)`)
tk.MustGetErrCode(`alter table test_zero_date modify a date`, errno.ErrTruncatedWrongValue)
}

func (s *testIntegrationSuite2) TestInvalidDefault(c *C) {
Expand Down
2 changes: 2 additions & 0 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,8 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
sc.InCreateOrAlterStmt = true
sc.AllowInvalidDate = vars.SQLMode.HasAllowInvalidDatesMode()
sc.IgnoreZeroInDate = !vars.SQLMode.HasNoZeroInDateMode() || !vars.StrictSQLMode || sc.AllowInvalidDate
sc.NoZeroDate = vars.SQLMode.HasNoZeroDateMode()
sc.TruncateAsWarning = !vars.StrictSQLMode
case *ast.LoadDataStmt:
sc.DupKeyAsWarning = true
sc.BadNullAsWarning = true
Expand Down
1 change: 1 addition & 0 deletions sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type StatementContext struct {
InCreateOrAlterStmt bool
IgnoreTruncate bool
IgnoreZeroInDate bool
NoZeroDate bool
DupKeyAsWarning bool
BadNullAsWarning bool
DividedByZeroAsWarning bool
Expand Down
2 changes: 1 addition & 1 deletion types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -1988,7 +1988,7 @@ func ParseTimeFromNum(sc *stmtctx.StatementContext, num int64, tp byte, fsp int8
// MySQL compatibility: 0 should not be converted to null, see #11203
if num == 0 {
zt := NewTime(ZeroCoreTime, tp, DefaultFsp)
if sc != nil && sc.InCreateOrAlterStmt && !sc.TruncateAsWarning {
if sc != nil && sc.InCreateOrAlterStmt && !sc.TruncateAsWarning && sc.NoZeroDate {
switch tp {
case mysql.TypeTimestamp:
return zt, ErrTruncatedWrongVal.GenWithStackByArgs(TimestampStr, "0")
Expand Down

0 comments on commit e983d32

Please sign in to comment.