From aff057afbb1bfa548d0f60072efa21265c721291 Mon Sep 17 00:00:00 2001 From: Yu Shuaipeng Date: Tue, 16 Apr 2019 10:36:57 +0800 Subject: [PATCH 1/4] fix bad null insert error handling when disabling strict sql mode --- executor/executor.go | 5 ++++- executor/executor_test.go | 3 +++ executor/write_test.go | 5 +++++ table/column.go | 7 ++++++- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/executor/executor.go b/executor/executor.go index 60d8b40666edb..619d16a370b83 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -1333,8 +1333,11 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) { sc.Priority = stmt.Priority case *ast.InsertStmt: sc.InInsertStmt = true + // For insert statement (not for update statement), disabling the StrictSQLMode + // should make TruncateAsWarning and DividedByZeroAsWarning, + // but should not make DupKeyAsWarning or BadNullAsWarning, sc.DupKeyAsWarning = stmt.IgnoreErr - sc.BadNullAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr + sc.BadNullAsWarning = stmt.IgnoreErr sc.TruncateAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr sc.DividedByZeroAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr sc.AllowInvalidDate = vars.SQLMode.HasAllowInvalidDatesMode() diff --git a/executor/executor_test.go b/executor/executor_test.go index f8b80e7a36818..6bd89a0db403f 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -1767,6 +1767,9 @@ func (s *testSuite) TestSQLMode(c *C) { tk.MustExec("set sql_mode = ''") tk.MustExec("insert t values ()") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1364 Field 'a' doesn't have a default value")) + _, err = tk.Exec("insert t values (null)") + c.Check(err, NotNil) tk.MustExec("insert t values (1000)") tk.MustQuery("select * from t").Check(testkit.Rows("0", "127")) diff --git a/executor/write_test.go b/executor/write_test.go index d6cf321a1447a..b553bde6ba788 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -719,6 +719,11 @@ commit;` tk.CheckLastMessage("") tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1048 Column 'f2' cannot be null")) tk.MustQuery(`SELECT * FROM t1 order by f1;`).Check(testkit.Rows("1 0", "2 2")) + + tk.MustExec(`SET sql_mode='';`) + _, err = tk.Exec(`INSERT t1 VALUES (1, 1) ON DUPLICATE KEY UPDATE f2 = null;`) + c.Assert(err, NotNil) + tk.MustQuery(`SELECT * FROM t1 order by f1;`).Check(testkit.Rows("1 0", "2 2")) } func (s *testSuite2) TestInsertIgnoreOnDup(c *C) { diff --git a/table/column.go b/table/column.go index f4fc28d360b78..48ec41eff751a 100644 --- a/table/column.go +++ b/table/column.go @@ -426,11 +426,16 @@ func getColDefaultValueFromNil(ctx sessionctx.Context, col *model.ColumnInfo) (t if col.IsGenerated() { return types.Datum{}, nil } - sc := ctx.GetSessionVars().StmtCtx + vars := ctx.GetSessionVars() + sc := vars.StmtCtx if sc.BadNullAsWarning { sc.AppendWarning(ErrColumnCantNull.GenWithStackByArgs(col.Name)) return GetZeroValue(col), nil } + if !vars.StrictSQLMode { + sc.AppendWarning(ErrNoDefaultValue.GenWithStackByArgs(col.Name)) + return GetZeroValue(col), nil + } return types.Datum{}, ErrNoDefaultValue.GenWithStackByArgs(col.Name) } From 978369eed47e6d56167d5b7c86ff74c4bdbb8e65 Mon Sep 17 00:00:00 2001 From: Yu Shuaipeng Date: Tue, 16 Apr 2019 12:58:41 +0800 Subject: [PATCH 2/4] fix CI --- executor/executor_test.go | 3 ++- executor/insert_common.go | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/executor/executor_test.go b/executor/executor_test.go index 6bd89a0db403f..ca378a6e5d794 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -1770,8 +1770,9 @@ func (s *testSuite) TestSQLMode(c *C) { tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1364 Field 'a' doesn't have a default value")) _, err = tk.Exec("insert t values (null)") c.Check(err, NotNil) + tk.MustExec("insert t select null") tk.MustExec("insert t values (1000)") - tk.MustQuery("select * from t").Check(testkit.Rows("0", "127")) + tk.MustQuery("select * from t order by a").Check(testkit.Rows("0", "0", "127")) tk.MustExec("insert tdouble values (10.23)") tk.MustQuery("select * from tdouble").Check(testkit.Rows("9.99")) diff --git a/executor/insert_common.go b/executor/insert_common.go index 6df2e1ab9e0bc..265215550f63b 100644 --- a/executor/insert_common.go +++ b/executor/insert_common.go @@ -301,6 +301,10 @@ func (e *InsertValues) insertRowsFromSelect(ctx context.Context, exec func(ctx c rows := make([][]types.Datum, 0, chk.Capacity()) sessVars := e.ctx.GetSessionVars() + if !sessVars.StrictSQLMode { + // If StrictSQLMode is disabled and it is a insert-select statement, it also handle BadNullAsWarning. + sessVars.StmtCtx.BadNullAsWarning = true + } batchInsert := sessVars.BatchInsert && !sessVars.InTxn() batchSize := sessVars.DMLBatchSize From 4af294c524b72e995f7c3d38ebec3cd739b96691 Mon Sep 17 00:00:00 2001 From: Yu Shuaipeng Date: Tue, 16 Apr 2019 12:59:53 +0800 Subject: [PATCH 3/4] add warning check --- executor/executor_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/executor/executor_test.go b/executor/executor_test.go index ca378a6e5d794..fbfbf78980bcd 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -1771,6 +1771,7 @@ func (s *testSuite) TestSQLMode(c *C) { _, err = tk.Exec("insert t values (null)") c.Check(err, NotNil) tk.MustExec("insert t select null") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1048 Column 'a' cannot be null")) tk.MustExec("insert t values (1000)") tk.MustQuery("select * from t order by a").Check(testkit.Rows("0", "0", "127")) From 61ba3a64a46512d1b5e7426753515b8b13acc7a6 Mon Sep 17 00:00:00 2001 From: Yu Shuaipeng Date: Wed, 17 Apr 2019 11:19:38 +0800 Subject: [PATCH 4/4] address comments --- executor/executor_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/executor/executor_test.go b/executor/executor_test.go index fbfbf78980bcd..60707de717b93 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -1770,10 +1770,12 @@ func (s *testSuite) TestSQLMode(c *C) { tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1364 Field 'a' doesn't have a default value")) _, err = tk.Exec("insert t values (null)") c.Check(err, NotNil) + tk.MustExec("insert ignore t values (null)") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1048 Column 'a' cannot be null")) tk.MustExec("insert t select null") tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1048 Column 'a' cannot be null")) tk.MustExec("insert t values (1000)") - tk.MustQuery("select * from t order by a").Check(testkit.Rows("0", "0", "127")) + tk.MustQuery("select * from t order by a").Check(testkit.Rows("0", "0", "0", "127")) tk.MustExec("insert tdouble values (10.23)") tk.MustQuery("select * from tdouble").Check(testkit.Rows("9.99"))