Skip to content

Commit

Permalink
executor: fix unexpected behavior when casting invalid string to date (
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Sep 13, 2021
1 parent 67def4d commit 842cb0c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
17 changes: 17 additions & 0 deletions executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1309,3 +1309,20 @@ func (s *testSuite9) TestBinaryLiteralInsertToSet(c *C) {
tk.MustExec("insert into bintest(h) values(0x61)")
tk.MustQuery("select * from bintest").Check(testkit.Rows("a"))
}

func (s *testSuite9) TestIssue26762(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test`)
tk.MustExec("drop table if exists t1;")
tk.MustExec("create table t1(c1 date);")
_, err := tk.Exec("insert into t1 values('2020-02-31');")
c.Assert(err.Error(), Equals, `[table:1366]Incorrect date value: '2020-02-31' for column 'c1' at row 1`)

tk.MustExec("set @@sql_mode='ALLOW_INVALID_DATES';")
tk.MustExec("insert into t1 values('2020-02-31');")
tk.MustQuery("select * from t1").Check(testkit.Rows("2020-02-31"))

tk.MustExec("set @@sql_mode='STRICT_TRANS_TABLES';")
_, err = tk.Exec("insert into t1 values('2020-02-31');")
c.Assert(err.Error(), Equals, `[table:1366]Incorrect date value: '2020-02-31' for column 'c1' at row 1`)
}
3 changes: 2 additions & 1 deletion table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ func handleZeroDatetime(ctx sessionctx.Context, col *model.ColumnInfo, casted ty
return types.NewDatum(zeroV), true, nil
} else if tm.IsZero() || tm.InvalidZero() {
if tm.IsZero() {
if !mode.HasNoZeroDateMode() {
// Don't care NoZeroDate mode if time val is invalid.
if !tmIsInvalid && !mode.HasNoZeroDateMode() {
return types.NewDatum(zeroV), true, nil
}
} else if tm.InvalidZero() {
Expand Down

0 comments on commit 842cb0c

Please sign in to comment.