Repository: pingcap/tidb (Go)

Issue:
Table definition for date filed is showing date and time in Tidb and mysql is only showing date
## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)
```sql
drop table if exists t1;
create table t1 (da date default '1962-03-03 23:33:34', dt datetime default '1962-03-03');
show create table t1;
```

### 2. What did you expect to see? (Required)
```sql
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                           |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `da` date DEFAULT '1962-03-03',
  `dt` datetime DEFAULT '1962-03-03 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
```
### 3. What did you see instead (Required)
```sql
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                             |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `da` date DEFAULT '1962-03-03 23:33:34',
  `dt` datetime DEFAULT '1962-03-03 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin |
```
### 4. What is your TiDB version? (Required)
```sql
| Release Version: v5.4.0-alpha-264-g6efa36df6
Edition: Community
Git Commit Hash: 6efa36df6cff325106f67ecfe3d79816ba37ca6a
Git Branch: master
UTC Build Time: 2021-11-29 16:57:51
GoVersion: go1.17.2
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false |
```




Discussion:

Patch:
diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go
index 5ffcbac725957..40d3e9fdf0ce5 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)
 		}


Test patch:
diff --git a/ddl/column_test.go b/ddl/column_test.go
index ae743d36ba7bb..0f142967b9cfe 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"))
+}