Skip to content

Commit

Permalink
fix for INSERT INTO t SET timestamp_col = DEFAULT (pingcap#29926)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjonss committed Nov 22, 2021
1 parent 7aa86cc commit aee4fd2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
5 changes: 4 additions & 1 deletion parser/ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,15 @@ type DefaultExpr struct {
exprNode
// Name is the column name.
Name *ColumnName
// True - DEFAULT(ColumnName), False - just DEFAULT, but name filled in afterwards
// like for: INSERT INTO t1 SET t = DEFAULT
NameIsGiven bool
}

// Restore implements Node interface.
func (n *DefaultExpr) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("DEFAULT")
if n.Name != nil {
if n.NameIsGiven && n.Name != nil {
ctx.WritePlain("(")
if err := n.Name.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore DefaultExpr.Name")
Expand Down
4 changes: 1 addition & 3 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -6207,7 +6207,6 @@ ProcedureCall:
*
* Insert Statements
*
* TODO: support PARTITION
**********************************************************************************/
InsertIntoStmt:
"INSERT" TableOptimizerHintsOpt PriorityOpt IgnoreOptional IntoOpt TableName PartitionNameListOpt InsertValues OnDuplicateKeyUpdate
Expand Down Expand Up @@ -6381,7 +6380,6 @@ OnDuplicateKeyUpdate:
* Replace Statements
* See https://dev.mysql.com/doc/refman/5.7/en/replace.html
*
* TODO: support PARTITION
**********************************************************************************/
ReplaceIntoStmt:
"REPLACE" PriorityOpt IntoOpt TableName PartitionNameListOpt InsertValues
Expand Down Expand Up @@ -6848,7 +6846,7 @@ SimpleExpr:
}
| "DEFAULT" '(' SimpleIdent ')'
{
$$ = &ast.DefaultExpr{Name: $3.(*ast.ColumnNameExpr).Name}
$$ = &ast.DefaultExpr{Name: $3.(*ast.ColumnNameExpr).Name NameIsGiven: true}
}
| "VALUES" '(' SimpleIdent ')' %prec lowerThanInsertValues
{
Expand Down
16 changes: 14 additions & 2 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1941,11 +1941,23 @@ func (er *expressionRewriter) evalDefaultExpr(v *ast.DefaultExpr) {
isCurrentTimestamp := hasCurrentDatetimeDefault(col)
var val *expression.Constant
switch {
case isCurrentTimestamp && col.Tp == mysql.TypeDatetime:
case isCurrentTimestamp && !v.NameIsGiven && (col.Tp == mysql.TypeDatetime || col.Tp == mysql.TypeTimestamp):
// SET col = DEFAULT
t, err := expression.GetTimeValue(er.sctx, ast.CurrentTimestamp, col.Tp, int8(col.Decimal))
if err != nil {
return
}
val = &expression.Constant{
Value: t,
RetType: types.NewFieldType(col.Tp),
}
case isCurrentTimestamp && v.NameIsGiven && col.Tp == mysql.TypeDatetime:
// for DATETIME column with current_timestamp, use NULL to be compatible with MySQL 5.7
// DEFAULT(colname)
val = expression.NewNull()
case isCurrentTimestamp && col.Tp == mysql.TypeTimestamp:
case isCurrentTimestamp && v.NameIsGiven && col.Tp == mysql.TypeTimestamp:
// for TIMESTAMP column with current_timestamp, use 0 to be compatible with MySQL 5.7
// DEFAULT(colname)
zero := types.NewTime(types.ZeroCoreTime, mysql.TypeTimestamp, int8(col.Decimal))
val = &expression.Constant{
Value: types.NewDatum(zero),
Expand Down
3 changes: 2 additions & 1 deletion planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3215,7 +3215,8 @@ func (b *PlanBuilder) buildSetValuesOfInsert(ctx context.Context, insert *ast.In
insertPlan.AllAssignmentsAreConstant = true
for i, assign := range insert.Setlist {
defaultExpr := extractDefaultExpr(assign.Expr)
if defaultExpr != nil {
if defaultExpr != nil && !defaultExpr.NameIsGiven {
// Set the name of the column in the assignment
defaultExpr.Name = assign.Column
}
// Note: For INSERT, REPLACE, and UPDATE, if a generated column is inserted into, replaced, or updated explicitly, the only permitted value is DEFAULT.
Expand Down

0 comments on commit aee4fd2

Please sign in to comment.