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

ddl: disallow change columns from zero int to time #25728

Merged
merged 10 commits into from
Nov 30, 2021
1 change: 1 addition & 0 deletions ddl/backfilling.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ func (w *worker) writePhysicalTableRecord(t table.PhysicalTable, bfWorkerType ba
backfillWorkers = append(backfillWorkers, idxWorker.backfillWorker)
go idxWorker.backfillWorker.run(reorgInfo.d, idxWorker)
case typeUpdateColumnWorker:
sessCtx.GetSessionVars().StmtCtx.InCreateOrAlterStmt = true
Copy link
Contributor

Choose a reason for hiding this comment

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

might we add a brief comment here (cast diff in select & alter)

Copy link
Member

Choose a reason for hiding this comment

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

@tangenta Please update the comment according to the address, thanks.

updateWorker := newUpdateColumnWorker(sessCtx, w, i, t, oldColInfo, colInfo, decodeColMap, reorgInfo.ReorgMeta.SQLMode)
updateWorker.priority = job.Priority
backfillWorkers = append(backfillWorkers, updateWorker.backfillWorker)
Expand Down
14 changes: 14 additions & 0 deletions ddl/column_type_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2129,3 +2129,17 @@ func (s *testColumnTypeChangeSuite) TestCastToTimeStampDecodeError(c *C) {
// Normal cast datetime to timestamp can succeed.
tk.MustQuery("select timestamp(cast('1000-11-11 12-3-1' as date));").Check(testkit.Rows("1000-11-11 00:00:00"))
}

// https://github.com/pingcap/tidb/issues/25285.
func (s *testColumnTypeChangeSuite) TestCastFromZeroIntToTimeError(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test;")

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int);")
tk.MustExec("insert into t values (0);")
tk.MustGetErrCode("alter table t modify column a date;", mysql.ErrTruncatedWrongValue)
tangenta marked this conversation as resolved.
Show resolved Hide resolved
tk.MustGetErrCode("alter table t modify column a datetime;", mysql.ErrTruncatedWrongValue)
tk.MustGetErrCode("alter table t modify column a timestamp;", mysql.ErrTruncatedWrongValue)
tk.MustExec("drop table if exists t;")
}
9 changes: 3 additions & 6 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5144,8 +5144,7 @@ func (s *testDBSuite1) TestModifyColumnTime_YearToDate(c *C) {
{"year", `"69"`, "date", "", errno.ErrTruncatedWrongValue},
{"year", `"70"`, "date", "", errno.ErrTruncatedWrongValue},
{"year", `"99"`, "date", "", errno.ErrTruncatedWrongValue},
// MySQL will get "Data truncation: Incorrect date value: '0000'", but TiDB treat 00 as valid datetime.
{"year", `00`, "date", "0000-00-00", 0},
{"year", `00`, "date", "", errno.ErrTruncatedWrongValue},
{"year", `69`, "date", "", errno.ErrTruncatedWrongValue},
{"year", `70`, "date", "", errno.ErrTruncatedWrongValue},
{"year", `99`, "date", "", errno.ErrTruncatedWrongValue},
Expand All @@ -5162,8 +5161,7 @@ func (s *testDBSuite1) TestModifyColumnTime_YearToDatetime(c *C) {
{"year", `"69"`, "datetime", "", errno.ErrTruncatedWrongValue},
{"year", `"70"`, "datetime", "", errno.ErrTruncatedWrongValue},
{"year", `"99"`, "datetime", "", errno.ErrTruncatedWrongValue},
// MySQL will get "Data truncation: Incorrect date value: '0000'", but TiDB treat 00 as valid datetime.
{"year", `00`, "datetime", "0000-00-00 00:00:00", 0},
{"year", `00`, "datetime", "", errno.ErrTruncatedWrongValue},
{"year", `69`, "datetime", "", errno.ErrTruncatedWrongValue},
{"year", `70`, "datetime", "", errno.ErrTruncatedWrongValue},
{"year", `99`, "datetime", "", errno.ErrTruncatedWrongValue},
Expand All @@ -5180,8 +5178,7 @@ func (s *testDBSuite1) TestModifyColumnTime_YearToTimestamp(c *C) {
{"year", `"69"`, "timestamp", "", errno.ErrTruncatedWrongValue},
{"year", `"70"`, "timestamp", "", errno.ErrTruncatedWrongValue},
{"year", `"99"`, "timestamp", "", errno.ErrTruncatedWrongValue},
// MySQL will get "Data truncation: Incorrect date value: '0000'", but TiDB treat 00 as valid datetime.
{"year", `00`, "timestamp", "0000-00-00 00:00:00", 0},
{"year", `00`, "timestamp", "", errno.ErrTruncatedWrongValue},
{"year", `69`, "timestamp", "", errno.ErrTruncatedWrongValue},
{"year", `70`, "timestamp", "", errno.ErrTruncatedWrongValue},
{"year", `99`, "timestamp", "", errno.ErrTruncatedWrongValue},
Expand Down
13 changes: 12 additions & 1 deletion types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -1974,7 +1974,18 @@ func ParseTimeFromYear(sc *stmtctx.StatementContext, year int64) (Time, error) {
func ParseTimeFromNum(sc *stmtctx.StatementContext, num int64, tp byte, fsp int8) (Time, error) {
// MySQL compatibility: 0 should not be converted to null, see #11203
if num == 0 {
return NewTime(ZeroCoreTime, tp, DefaultFsp), nil
zt := NewTime(ZeroCoreTime, tp, DefaultFsp)
if sc != nil && sc.InCreateOrAlterStmt {
switch tp {
case mysql.TypeTimestamp:
return zt, ErrTruncatedWrongVal.GenWithStackByArgs("timestamp", "0")
case mysql.TypeDate:
return zt, ErrTruncatedWrongVal.GenWithStackByArgs("date", "0")
tangenta marked this conversation as resolved.
Show resolved Hide resolved
case mysql.TypeDatetime:
return zt, ErrTruncatedWrongVal.GenWithStackByArgs("datetime", "0")
}
}
return zt, nil
}
fsp, err := CheckFsp(int(fsp))
if err != nil {
Expand Down