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) (#52951)

close #52939
  • Loading branch information
ti-chi-bot authored Apr 29, 2024
1 parent 0359bbc commit bf84e23
Show file tree
Hide file tree
Showing 8 changed files with 6,579 additions and 6,540 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 @@ -1592,17 +1592,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 @@ -1059,12 +1059,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 @@ -1077,7 +1084,7 @@ func constructResultOfShowCreateTable(ctx sessionctx.Context, dbName *model.CISt
}

if col.DefaultIsExpr {
fmt.Fprintf(buf, " DEFAULT %s", format.OutputFormat(defaultValStr))
fmt.Fprintf(buf, " DEFAULT (%s)", format.OutputFormat(defaultValStr))
} else {
if col.GetType() == mysql.TypeBit {
defaultValBinaryLiteral := types.BinaryLiteral(defaultValStr)
Expand Down
6 changes: 3 additions & 3 deletions pkg/executor/test/showtest/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestShowCreateTable(t *testing.T) {
" `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"+
" `h` date DEFAULT CURRENT_DATE\n"+
" `h` date DEFAULT (CURRENT_DATE)\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))
tk.MustExec("drop table t")
Expand Down Expand Up @@ -318,7 +318,7 @@ func TestShowCreateTable(t *testing.T) {
tk.MustQuery("show create table default_sequence").Check(testkit.RowsWithSep("|",
""+
"default_sequence CREATE TABLE `default_sequence` (\n"+
" `a` int(11) DEFAULT nextval(`test`.`seq`)\n"+
" `a` int(11) DEFAULT (nextval(`test`.`seq`))\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))

Expand Down Expand Up @@ -489,7 +489,7 @@ func TestShowCreateTable(t *testing.T) {
tk.MustExec(`create table t(a bit default (rand()))`)
tk.MustQuery(`show create table t`).Check(testkit.RowsWithSep("|", ""+
"t CREATE TABLE `t` (\n"+
" `a` bit(1) DEFAULT rand()\n"+
" `a` bit(1) DEFAULT (rand())\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))

tk.MustExec(`drop table if exists t`)
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 @@ -578,9 +578,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 bf84e23

Please sign in to comment.