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) #52948

Merged
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
8 changes: 4 additions & 4 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2053,17 +2053,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 Expand Up @@ -2093,7 +2093,7 @@ func TestDefaultColumnWithUUID(t *testing.T) {
tk.MustQuery("show create table t").Check(testkit.Rows(
"t CREATE TABLE `t` (\n" +
" `c` int(10) DEFAULT NULL,\n" +
" `c1` varchar(256) DEFAULT uuid()\n" +
" `c1` varchar(256) DEFAULT (uuid())\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
}

Expand Down
9 changes: 8 additions & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,13 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T
if col.GetDecimal() > 0 {
buf.WriteString(fmt.Sprintf("(%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 @@ -1004,7 +1011,7 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T
defaultValBinaryLiteral := types.BinaryLiteral(defaultValStr)
fmt.Fprintf(buf, " DEFAULT %s", defaultValBinaryLiteral.ToBitLiteralString(true))
} else if col.DefaultIsExpr {
fmt.Fprintf(buf, " DEFAULT %s", format.OutputFormat(defaultValStr))
fmt.Fprintf(buf, " DEFAULT (%s)", format.OutputFormat(defaultValStr))
} else {
fmt.Fprintf(buf, " DEFAULT '%s'", format.OutputFormat(defaultValStr))
}
Expand Down
2 changes: 1 addition & 1 deletion executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,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
12 changes: 12 additions & 0 deletions parser/ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,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