Skip to content

Commit

Permalink
executor: fix show default num and sequence (#16450)
Browse files Browse the repository at this point in the history
  • Loading branch information
AilinKid authored Apr 17, 2020
1 parent d8e6cf8 commit 4e44a74
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
14 changes: 7 additions & 7 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,13 @@ func (s *testIntegrationSuite1) TestIssue5092(c *C) {
tk.MustExec("alter table t_issue_5092 add column d int default 4 after c1, add column aa int default 0 first")
tk.MustQuery("select * from t_issue_5092").Check(testkit.Rows("0 1 2 22 3 33 4"))
tk.MustQuery("show create table t_issue_5092").Check(testkit.Rows("t_issue_5092 CREATE TABLE `t_issue_5092` (\n" +
" `aa` int(11) DEFAULT '0',\n" +
" `a` int(11) DEFAULT '1',\n" +
" `b` int(11) DEFAULT '2',\n" +
" `b1` int(11) DEFAULT '22',\n" +
" `c` int(11) DEFAULT '3',\n" +
" `c1` int(11) DEFAULT '33',\n" +
" `d` int(11) DEFAULT '4'\n" +
" `aa` int(11) DEFAULT 0,\n" +
" `a` int(11) DEFAULT 1,\n" +
" `b` int(11) DEFAULT 2,\n" +
" `b1` int(11) DEFAULT 22,\n" +
" `c` int(11) DEFAULT 3,\n" +
" `c1` int(11) DEFAULT 33,\n" +
" `d` int(11) DEFAULT 4\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
tk.MustExec("drop table t_issue_5092")

Expand Down
4 changes: 2 additions & 2 deletions executor/seqtest/seq_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (s *seqTestSuite) TestShow(c *C) {
row := result.Rows()[0]
// For issue https://github.com/pingcap/tidb/issues/1061
expectedRow := []interface{}{
"SHOW_test", "CREATE TABLE `SHOW_test` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `c1` int(11) DEFAULT NULL COMMENT 'c1_comment',\n `c2` int(11) DEFAULT NULL,\n `c3` int(11) DEFAULT '1',\n `c4` text DEFAULT NULL,\n `c5` tinyint(1) DEFAULT NULL,\n PRIMARY KEY (`id`),\n KEY `idx_wide_c4` (`c3`,`c4`(10))\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=28934 COMMENT='table_comment'"}
"SHOW_test", "CREATE TABLE `SHOW_test` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `c1` int(11) DEFAULT NULL COMMENT 'c1_comment',\n `c2` int(11) DEFAULT NULL,\n `c3` int(11) DEFAULT 1,\n `c4` text DEFAULT NULL,\n `c5` tinyint(1) DEFAULT NULL,\n PRIMARY KEY (`id`),\n KEY `idx_wide_c4` (`c3`,`c4`(10))\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=28934 COMMENT='table_comment'"}
for i, r := range row {
c.Check(r, Equals, expectedRow[i])
}
Expand All @@ -219,7 +219,7 @@ func (s *seqTestSuite) TestShow(c *C) {
c.Check(result.Rows(), HasLen, 1)
row = result.Rows()[0]
expectedRow = []interface{}{
"ptest", "CREATE TABLE `ptest` (\n `a` int(11) NOT NULL,\n `b` double NOT NULL DEFAULT '2.0',\n `c` varchar(10) NOT NULL,\n `d` time DEFAULT NULL,\n `e` timestamp NULL DEFAULT NULL,\n `f` timestamp NULL DEFAULT NULL,\n PRIMARY KEY (`a`),\n UNIQUE KEY `d` (`d`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"}
"ptest", "CREATE TABLE `ptest` (\n `a` int(11) NOT NULL,\n `b` double NOT NULL DEFAULT 2.0,\n `c` varchar(10) NOT NULL,\n `d` time DEFAULT NULL,\n `e` timestamp NULL DEFAULT NULL,\n `f` timestamp NULL DEFAULT NULL,\n PRIMARY KEY (`a`),\n UNIQUE KEY `d` (`d`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"}
for i, r := range row {
c.Check(r, Equals, expectedRow[i])
}
Expand Down
2 changes: 2 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,8 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T
if col.Tp == mysql.TypeBit {
defaultValBinaryLiteral := types.BinaryLiteral(defaultValStr)
fmt.Fprintf(buf, " DEFAULT %s", defaultValBinaryLiteral.ToBitLiteralString(true))
} else if types.IsTypeNumeric(col.Tp) || col.DefaultIsExpr {
fmt.Fprintf(buf, " DEFAULT %s", format.OutputFormat(defaultValStr))
} else {
fmt.Fprintf(buf, " DEFAULT '%s'", format.OutputFormat(defaultValStr))
}
Expand Down
25 changes: 25 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,31 @@ func (s *testSuite5) TestShowCreateTable(c *C) {
" `a` varchar(10) DEFAULT NULL\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin", // non-binary collate is kept.
))
// Test for issue #17 in bug competition, default num and sequence should be shown without quote.
tk.MustExec(`drop table if exists default_num`)
tk.MustExec("create table default_num(a int default 11)")
tk.MustQuery("show create table default_num").Check(testutil.RowsWithSep("|",
""+
"default_num CREATE TABLE `default_num` (\n"+
" `a` int(11) DEFAULT 11\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))
tk.MustExec(`drop table if exists default_varchar`)
tk.MustExec("create table default_varchar(a varchar(10) default \"haha\")")
tk.MustQuery("show create table default_varchar").Check(testutil.RowsWithSep("|",
""+
"default_varchar CREATE TABLE `default_varchar` (\n"+
" `a` varchar(10) DEFAULT 'haha'\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))
tk.MustExec(`drop table if exists default_sequence`)
tk.MustExec("create table default_sequence(a int default nextval(seq))")
tk.MustQuery("show create table default_sequence").Check(testutil.RowsWithSep("|",
""+
"default_sequence CREATE TABLE `default_sequence` (\n"+
" `a` int(11) DEFAULT nextval(`test`.`seq`)\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))
}

func (s *testAutoRandomSuite) TestShowCreateTableAutoRandom(c *C) {
Expand Down
4 changes: 2 additions & 2 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,12 @@ func (s *testTableSuite) TestInfoschemaFieldValue(c *C) {
tk.MustQuery("show create table information_schema.PROCESSLIST").Check(
testkit.Rows("" +
"PROCESSLIST CREATE TABLE `PROCESSLIST` (\n" +
" `ID` bigint(21) unsigned NOT NULL DEFAULT '0',\n" +
" `ID` bigint(21) unsigned NOT NULL DEFAULT 0,\n" +
" `USER` varchar(16) NOT NULL DEFAULT '',\n" +
" `HOST` varchar(64) NOT NULL DEFAULT '',\n" +
" `DB` varchar(64) DEFAULT NULL,\n" +
" `COMMAND` varchar(16) NOT NULL DEFAULT '',\n" +
" `TIME` int(7) NOT NULL DEFAULT '0',\n" +
" `TIME` int(7) NOT NULL DEFAULT 0,\n" +
" `STATE` varchar(7) DEFAULT NULL,\n" +
" `INFO` binary(512) DEFAULT NULL,\n" +
" `MEM` bigint(21) unsigned DEFAULT NULL,\n" +
Expand Down

0 comments on commit 4e44a74

Please sign in to comment.