Skip to content

Commit

Permalink
*: modify the printing of column default expression in `SHOW CREATE T…
Browse files Browse the repository at this point in the history
…ABLE` and `Restore` (#52940)

close #52939
  • Loading branch information
CbcWestwolf authored Apr 28, 2024
1 parent 0412aa1 commit 66ba419
Show file tree
Hide file tree
Showing 10 changed files with 6,284 additions and 6,245 deletions.
6 changes: 3 additions & 3 deletions pkg/ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1593,17 +1593,17 @@ func TestDefaultColumnWithRand(t *testing.T) {
tk.MustQuery("show create table t").Check(testkit.Rows(
"t CREATE TABLE `t` (\n" +
" `c` int(10) DEFAULT NULL,\n" +
" `c1` int(11) DEFAULT rand()\n" +
" `c1` int(11) DEFAULT (rand())\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
tk.MustQuery("show create table t1").Check(testkit.Rows(
"t1 CREATE TABLE `t1` (\n" +
" `c` int(11) DEFAULT NULL,\n" +
" `c1` double DEFAULT rand()\n" +
" `c1` double DEFAULT (rand())\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
tk.MustQuery("show create table t2").Check(testkit.Rows(
"t2 CREATE TABLE `t2` (\n" +
" `c` int(11) DEFAULT NULL,\n" +
" `c1` double DEFAULT rand(1)\n" +
" `c1` double DEFAULT (rand(1))\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))

// use a non-existent function name
Expand Down
11 changes: 9 additions & 2 deletions pkg/executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -1032,12 +1032,19 @@ func constructResultOfShowCreateTable(ctx sessionctx.Context, dbName *model.CISt
}
buf.WriteString(" DEFAULT NULL")
}
case "CURRENT_TIMESTAMP", "CURRENT_DATE":
case "CURRENT_TIMESTAMP":
buf.WriteString(" DEFAULT ")
buf.WriteString(defaultValue.(string))
if col.GetDecimal() > 0 {
fmt.Fprintf(buf, "(%d)", col.GetDecimal())
}
case "CURRENT_DATE":
buf.WriteString(" DEFAULT (")
buf.WriteString(defaultValue.(string))
if col.GetDecimal() > 0 {
fmt.Fprintf(buf, "(%d)", col.GetDecimal())
}
buf.WriteString(")")
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 All @@ -1050,7 +1057,7 @@ func constructResultOfShowCreateTable(ctx sessionctx.Context, dbName *model.CISt
}

if col.DefaultIsExpr {
fmt.Fprintf(buf, " DEFAULT %s", defaultValStr)
fmt.Fprintf(buf, " DEFAULT (%s)", defaultValStr)
} else {
if col.GetType() == mysql.TypeBit {
defaultValBinaryLiteral := types.BinaryLiteral(defaultValStr)
Expand Down
12 changes: 12 additions & 0 deletions pkg/parser/ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,9 +579,21 @@ func (n *ColumnOption) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("AUTO_INCREMENT")
case ColumnOptionDefaultValue:
ctx.WriteKeyWord("DEFAULT ")
printOuterParentheses := false
if funcCallExpr, ok := n.Expr.(*FuncCallExpr); ok {
if name := funcCallExpr.FnName.L; name != CurrentTimestamp {
printOuterParentheses = true
}
}
if printOuterParentheses {
ctx.WritePlain("(")
}
if err := n.Expr.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while splicing ColumnOption DefaultValue Expr")
}
if printOuterParentheses {
ctx.WritePlain(")")
}
case ColumnOptionUniqKey:
ctx.WriteKeyWord("UNIQUE KEY")
case ColumnOptionNull:
Expand Down
Loading

0 comments on commit 66ba419

Please sign in to comment.