Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: fix insert ignore invalid year #26097

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ func (s *testSuite3) TestInsertWrongValueForField(c *C) {
tk.MustExec(`create table t (a year);`)
_, err = tk.Exec(`insert into t values(2156);`)
c.Assert(err.Error(), Equals, `[types:8033]invalid year`)

_, err = tk.Exec(`insert ignore into t values(1900);`)
c.Assert(err, IsNil)
tk.MustQuery("show warnings;").Check(testutil.RowsWithSep("|", "Warning|8033|invalid year"))
tk.MustQuery(`select * from t;`).Check(testkit.Rows(`0`))
Copy link
Member

@mmyj mmyj Jul 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the doc

YEAR follows the following format rules:

Four-digit numeral ranges from 1901 to 2155
Four-digit string ranges from '1901' to '2155'
One-digit or two-digit numeral ranges from 1 to 99. Accordingly, 1-69 is converted to 2001-2069 and 70-99 is converted to 1970-1999
One-digit or two-digit string ranges from '0' to '99'
Value 0 is taken as 0000 whereas the string '0' or '00' is taken as 2000
Invalid YEAR value is automatically converted to 0000 (if users are not using the NO_ZERO_DATE SQL mode).

I think we need to add a new test unit with the both valid and invalid case.

By the way, what value the invalid YEAR value will be converted when using NO_ZERO_DATE SQL mode? In my local, whatever using or not, the invalid value is always converted to 0.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I will add some test cases and check NO_ZERO_DATE mode

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mmyj
NO_ZERO_DATE will not affect the insertion of year type data, we can ignore it.
According to MySQL doc https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#sql-mode-strict
If the input parameter is an invalid value, an error will be produced only in strict SQL mode and without ignore, otherwise it can be inserted successfully and results in a warning.
I have add some test case, PTAL

}

func (s *testSuite3) TestInsertValueForCastDecimalField(c *C) {
Expand Down
2 changes: 2 additions & 0 deletions table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ func CastValue(ctx sessionctx.Context, val types.Datum, col *model.ColumnInfo, r
if innCasted, exit, innErr := handleZeroDatetime(ctx, col, casted, val.GetString(), types.ErrWrongValue.Equal(err)); exit {
return innCasted, innErr
}
} else if (sc.InInsertStmt || sc.InUpdateStmt) && types.ErrInvalidYear.Equal(err) {
casted.SetInt64(0)
}

err = sc.HandleTruncate(err)
Expand Down