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

*: modify the printing of column default expression in SHOW CREATE TABLE and Restore #52940

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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