Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#52940
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
CbcWestwolf authored and ti-chi-bot committed Apr 28, 2024
1 parent 0359bbc commit 76b6ab1
Show file tree
Hide file tree
Showing 10 changed files with 18,918 additions and 2,401 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
13 changes: 12 additions & 1 deletion 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,11 @@ func constructResultOfShowCreateTable(ctx sessionctx.Context, dbName *model.CISt
}

if col.DefaultIsExpr {
<<<<<<< HEAD
fmt.Fprintf(buf, " DEFAULT %s", format.OutputFormat(defaultValStr))
=======
fmt.Fprintf(buf, " DEFAULT (%s)", defaultValStr)
>>>>>>> 66ba419636c (*: modify the printing of column default expression in `SHOW CREATE TABLE` and `Restore` (#52940))
} 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 @@ -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 76b6ab1

Please sign in to comment.