Skip to content

Commit

Permalink
*: support curdate() as column's default value | tidb-test=pr/2057 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
CbcWestwolf committed Jan 5, 2023
1 parent 5eea731 commit affe126
Show file tree
Hide file tree
Showing 12 changed files with 9,829 additions and 9,757 deletions.
4 changes: 2 additions & 2 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ func columnDefToCol(ctx sessionctx.Context, offset int, colDef *ast.ColumnDef, o
// getFuncCallDefaultValue gets the default column value of function-call expression.
func getFuncCallDefaultValue(col *table.Column, option *ast.ColumnOption, expr *ast.FuncCallExpr) (interface{}, bool, error) {
switch expr.FnName.L {
case ast.CurrentTimestamp:
case ast.CurrentTimestamp, ast.CurrentDate:
tp, fsp := col.FieldType.GetType(), col.FieldType.GetDecimal()
if tp == mysql.TypeTimestamp || tp == mysql.TypeDatetime {
defaultFsp := 0
Expand Down Expand Up @@ -1221,7 +1221,7 @@ func getDefaultValue(ctx sessionctx.Context, col *table.Column, option *ast.Colu
// If the function call is ast.CurrentTimestamp, it needs to be continuously processed.
}

if tp == mysql.TypeTimestamp || tp == mysql.TypeDatetime {
if tp == mysql.TypeTimestamp || tp == mysql.TypeDatetime || tp == mysql.TypeDate {
vd, err := expression.GetTimeValue(ctx, option.Expr, tp, fsp)
value := vd.GetValue()
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,17 @@ func TestAlterTableAddColumn(t *testing.T) {
tk.MustExec("create sequence alter_seq")
err = tk.ExecToErr("alter table alter_seq add column c int")
require.Equal(t, dbterror.ErrWrongObject.GenWithStackByArgs("test", "alter_seq", "BASE TABLE").Error(), err.Error())
tk.MustExec("alter table alter_test add column c4 date default current_date")
now = time.Now().Format(types.DateFormat)
r, err = tk.Exec("select c4 from alter_test")
require.NoError(t, err)
req = r.NewChunk(nil)
err = r.Next(context.Background(), req)
require.NoError(t, err)
row = req.GetRow(0)
require.Equal(t, 1, row.Len())
require.GreaterOrEqual(t, now, row.GetTime(0).String())
require.Nil(t, r.Close())
tk.MustExec("drop sequence alter_seq")
}

Expand Down
5 changes: 3 additions & 2 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,8 +1010,9 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T
}
buf.WriteString(" DEFAULT NULL")
}
case "CURRENT_TIMESTAMP":
buf.WriteString(" DEFAULT CURRENT_TIMESTAMP")
case "CURRENT_TIMESTAMP", "CURRENT_DATE":
buf.WriteString(" DEFAULT ")
buf.WriteString(defaultValue.(string))
if col.GetDecimal() > 0 {
buf.WriteString(fmt.Sprintf("(%d)", col.GetDecimal()))
}
Expand Down
10 changes: 8 additions & 2 deletions executor/showtest/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ func TestShowCreateTable(t *testing.T) {
"`d` datetime(4) default current_timestamp(4),\n" +
"`e` varchar(20) default 'cUrrent_tImestamp',\n" +
"`f` datetime(2) default current_timestamp(2) on update current_timestamp(2),\n" +
"`g` timestamp(2) default current_timestamp(2) on update current_timestamp(2))")
"`g` timestamp(2) default current_timestamp(2) on update current_timestamp(2),\n" +
"`h` date default current_date )")
tk.MustQuery("show create table `t`").Check(testkit.RowsWithSep("|",
""+
"t CREATE TABLE `t` (\n"+
Expand All @@ -153,7 +154,8 @@ func TestShowCreateTable(t *testing.T) {
" `d` datetime(4) DEFAULT CURRENT_TIMESTAMP(4),\n"+
" `e` varchar(20) DEFAULT 'cUrrent_tImestamp',\n"+
" `f` datetime(2) DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP(2),\n"+
" `g` timestamp(2) DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP(2)\n"+
" `g` timestamp(2) DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP(2),\n"+
" `h` date DEFAULT CURRENT_DATE\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))
tk.MustExec("drop table t")
Expand Down Expand Up @@ -966,6 +968,8 @@ func TestShow2(t *testing.T) {
c_timestamp_default timestamp default current_timestamp,
c_timestamp_default_3 timestamp(3) default current_timestamp(3),
c_timestamp_default_4 timestamp(3) default current_timestamp(3) on update current_timestamp(3),
c_date_default date default current_date,
c_date_default_2 date default (curdate()),
c_blob blob,
c_tinyblob tinyblob,
c_mediumblob mediumblob,
Expand Down Expand Up @@ -1002,6 +1006,8 @@ func TestShow2(t *testing.T) {
"[c_timestamp_default timestamp <nil> YES CURRENT_TIMESTAMP select,insert,update,references ]\n" +
"[c_timestamp_default_3 timestamp(3) <nil> YES CURRENT_TIMESTAMP(3) select,insert,update,references ]\n" +
"[c_timestamp_default_4 timestamp(3) <nil> YES CURRENT_TIMESTAMP(3) DEFAULT_GENERATED on update CURRENT_TIMESTAMP(3) select,insert,update,references ]\n" +
"[c_date_default date <nil> YES CURRENT_DATE select,insert,update,references ]\n" +
"[c_date_default_2 date <nil> YES CURRENT_DATE select,insert,update,references ]\n" +
"[c_blob blob <nil> YES <nil> select,insert,update,references ]\n" +
"[c_tinyblob tinyblob <nil> YES <nil> select,insert,update,references ]\n" +
"[c_mediumblob mediumblob <nil> YES <nil> select,insert,update,references ]\n" +
Expand Down
12 changes: 6 additions & 6 deletions expression/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func getTimeCurrentTimeStamp(ctx sessionctx.Context, tp byte, fsp int) (t types.
return value, err
}
value.SetCoreTime(types.FromGoTime(defaultTime.Truncate(time.Duration(math.Pow10(9-fsp)) * time.Nanosecond)))
if tp == mysql.TypeTimestamp || tp == mysql.TypeDatetime {
if tp == mysql.TypeTimestamp || tp == mysql.TypeDatetime || tp == mysql.TypeDate {
err = value.ConvertTimeZone(time.Local, ctx.GetSessionVars().Location())
if err != nil {
return value, err
Expand All @@ -90,12 +90,12 @@ func GetTimeValue(ctx sessionctx.Context, v interface{}, tp byte, fsp int) (d ty
sc := ctx.GetSessionVars().StmtCtx
switch x := v.(type) {
case string:
upperX := strings.ToUpper(x)
if upperX == strings.ToUpper(ast.CurrentTimestamp) {
lowerX := strings.ToLower(x)
if lowerX == ast.CurrentTimestamp || lowerX == ast.CurrentDate {
if value, err = getTimeCurrentTimeStamp(ctx, tp, fsp); err != nil {
return d, err
}
} else if upperX == types.ZeroDatetimeStr {
} else if lowerX == types.ZeroDatetimeStr {
value, err = types.ParseTimeFromNum(sc, 0, tp, fsp)
terror.Log(err)
} else {
Expand All @@ -122,8 +122,8 @@ func GetTimeValue(ctx sessionctx.Context, v interface{}, tp byte, fsp int) (d ty
return d, errDefaultValue
}
case *ast.FuncCallExpr:
if x.FnName.L == ast.CurrentTimestamp {
d.SetString(strings.ToUpper(ast.CurrentTimestamp), mysql.DefaultCollationName)
if x.FnName.L == ast.CurrentTimestamp || x.FnName.L == ast.CurrentDate {
d.SetString(strings.ToUpper(x.FnName.L), mysql.DefaultCollationName)
return d, nil
}
return d, errDefaultValue
Expand Down
4 changes: 4 additions & 0 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ func TestCurrentTimestampAsDefault(t *testing.T) {
c_timestamp timestamp,
c_timestamp_default timestamp default current_timestamp,
c_timestamp_default_3 timestamp(3) default current_timestamp(3),
c_date_default date default current_date,
c_date_default_2 date default curdate(),
c_varchar_default varchar(20) default "current_timestamp",
c_varchar_default_3 varchar(20) default "current_timestamp(3)",
c_varchar_default_on_update datetime default current_timestamp on update current_timestamp,
Expand All @@ -276,6 +278,8 @@ func TestCurrentTimestampAsDefault(t *testing.T) {
WHERE table_schema = "default_time_test" AND table_name = "default_time_table"
ORDER BY column_name`,
).Check(testkit.Rows(
"c_date_default CURRENT_DATE ",
"c_date_default_2 CURRENT_DATE ",
"c_datetime <nil> ",
"c_datetime_default CURRENT_TIMESTAMP ",
"c_datetime_default_2 CURRENT_TIMESTAMP(2) ",
Expand Down
1 change: 1 addition & 0 deletions parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ var tokenMap = map[string]int{
"CSV_NULL": csvNull,
"CSV_SEPARATOR": csvSeparator,
"CSV_TRIM_LAST_SEPARATORS": csvTrimLastSeparators,
"CURDATE": curDate,
"CURRENT_DATE": currentDate,
"CURRENT_ROLE": currentRole,
"CURRENT_TIME": currentTime,
Expand Down
Loading

0 comments on commit affe126

Please sign in to comment.