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

*: support curdate() as column's default value | tidb-test=pr/2057 #40326

Merged
merged 8 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
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
5 changes: 5 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,11 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T
if col.GetDecimal() > 0 {
buf.WriteString(fmt.Sprintf("(%d)", col.GetDecimal()))
}
case "CURRENT_DATE":
buf.WriteString(" DEFAULT CURRENT_DATE")
if col.GetDecimal() > 0 {
buf.WriteString(fmt.Sprintf("(%d)", col.GetDecimal()))
}
xhebox marked this conversation as resolved.
Show resolved Hide resolved
default:
defaultValStr := fmt.Sprintf("%v", defaultValue)
// If column is timestamp, and default value is not current_timestamp, should convert the default value to the current session time zone.
Expand Down
8 changes: 4 additions & 4 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 @@ -91,7 +91,7 @@ func GetTimeValue(ctx sessionctx.Context, v interface{}, tp byte, fsp int) (d ty
switch x := v.(type) {
case string:
upperX := strings.ToUpper(x)
if upperX == strings.ToUpper(ast.CurrentTimestamp) {
if upperX == strings.ToUpper(ast.CurrentTimestamp) || upperX == strings.ToUpper(ast.CurrentDate) {
xhebox marked this conversation as resolved.
Show resolved Hide resolved
if value, err = getTimeCurrentTimeStamp(ctx, tp, fsp); err != nil {
return d, err
}
Expand Down Expand Up @@ -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
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