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 6, 2021
1 parent 55bb592 commit f0b0834
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 @@ -1561,6 +1561,23 @@ func combination(items []string) func() []string {
}
}

func (s *testSuite10) 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:1292]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:1292]Incorrect date value: '2020-02-31' for column 'c1' at row 1`)
}

func (s *testSuite10) TestBinaryLiteralInsertToEnum(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test`)
Expand Down
3 changes: 2 additions & 1 deletion table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,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 f0b0834

Please sign in to comment.