From 5fa3da7e354d0ccb484dcd9f44d080f58e092ff4 Mon Sep 17 00:00:00 2001 From: Benjamin2037 Date: Wed, 30 Mar 2022 15:46:29 +0800 Subject: [PATCH] DDL: Wrong Date type colunm's default value (#33571) ref pingcap/tidb#30358 --- ddl/column_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++ ddl/ddl_api.go | 2 +- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/ddl/column_test.go b/ddl/column_test.go index ae743d36ba..0f142967b9 100644 --- a/ddl/column_test.go +++ b/ddl/column_test.go @@ -907,3 +907,53 @@ func testGetTable(t *testing.T, dom *domain.Domain, tableID int64) table.Table { require.True(t, exist) return tbl } + +func TestGetDefaultValueOfColumn(t *testing.T) { + store, _, clean := testkit.CreateMockStoreAndDomain(t) + defer clean() + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create table t1 (da date default '1962-03-03 23:33:34', dt datetime default '1962-03-03'," + + " ti time default '2020-10-11 12:23:23', ts timestamp default '2020-10-13 12:23:23')") + + tk.MustQuery("show create table t1").Check(testkit.RowsWithSep("|", ""+ + "t1 CREATE TABLE `t1` (\n"+ + " `da` date DEFAULT '1962-03-03',\n"+ + " `dt` datetime DEFAULT '1962-03-03 00:00:00',\n"+ + " `ti` time DEFAULT '12:23:23',\n"+ + " `ts` timestamp DEFAULT '2020-10-13 12:23:23'\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) + + tk.MustExec("insert into t1 values()") + + tk.MustQuery("select * from t1").Check(testkit.RowsWithSep("|", ""+ + "1962-03-03 1962-03-03 00:00:00 12:23:23 2020-10-13 12:23:23")) + + tk.MustExec("alter table t1 add column da1 date default '2020-03-27 20:20:20 123456'") + + tk.MustQuery("show create table t1").Check(testkit.RowsWithSep("|", ""+ + "t1 CREATE TABLE `t1` (\n"+ + " `da` date DEFAULT '1962-03-03',\n"+ + " `dt` datetime DEFAULT '1962-03-03 00:00:00',\n"+ + " `ti` time DEFAULT '12:23:23',\n"+ + " `ts` timestamp DEFAULT '2020-10-13 12:23:23',\n"+ + " `da1` date DEFAULT '2020-03-27'\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) + + tk.MustQuery("select * from t1").Check(testkit.RowsWithSep("|", ""+ + "1962-03-03 1962-03-03 00:00:00 12:23:23 2020-10-13 12:23:23 2020-03-27")) + + tk.MustExec("alter table t1 change ts da2 date default '2020-10-10 20:20:20'") + + tk.MustQuery("show create table t1").Check(testkit.RowsWithSep("|", ""+ + "t1 CREATE TABLE `t1` (\n"+ + " `da` date DEFAULT '1962-03-03',\n"+ + " `dt` datetime DEFAULT '1962-03-03 00:00:00',\n"+ + " `ti` time DEFAULT '12:23:23',\n"+ + " `da2` date DEFAULT '2020-10-10',\n"+ + " `da1` date DEFAULT '2020-03-27'\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) + + tk.MustQuery("select * from t1").Check(testkit.RowsWithSep("|", ""+ + "1962-03-03 1962-03-03 00:00:00 12:23:23 2020-10-13 2020-03-27")) +} diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 5ffcbac725..40d3e9fdf0 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1123,7 +1123,7 @@ func getDefaultValue(ctx sessionctx.Context, col *table.Column, c *ast.ColumnOpt case mysql.TypeEnum: val, err := getEnumDefaultValue(v, col) return val, false, err - case mysql.TypeDuration: + case mysql.TypeDuration, mysql.TypeDate: if v, err = v.ConvertTo(ctx.GetSessionVars().StmtCtx, &col.FieldType); err != nil { return "", false, errors.Trace(err) }