Skip to content

Commit

Permalink
executor: fix bad null error handling for insert statement when disab…
Browse files Browse the repository at this point in the history
…ling the strict SQL mode (#10161)
  • Loading branch information
jackysp authored and tiancaiamao committed Apr 21, 2019
1 parent 7cc7323 commit 70c3718
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
5 changes: 4 additions & 1 deletion executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 8 additions & 1 deletion executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1767,8 +1767,15 @@ 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 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").Check(testkit.Rows("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"))
Expand Down
4 changes: 4 additions & 0 deletions executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 70c3718

Please sign in to comment.