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

executor:adjust 'show create table' for mysql compatibility #9113

Merged
merged 27 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8762ddb
executor: adjust 'show create table'
xiekeyi98 Jan 18, 2019
64b381b
update:change the test
xiekeyi98 Jan 18, 2019
a32d6e9
update: add test to improve CodeCov
xiekeyi98 Jan 18, 2019
67f445a
Merge remote-tracking branch 'upstream/master' into TiDB-2866
xiekeyi98 Jan 21, 2019
4af0f42
style: update the variable
xiekeyi98 Jan 21, 2019
b333b65
Merge branch 'master' into TiDB-2866
xiekeyi98 Jan 31, 2019
98e0095
fix test
xiekeyi98 Jan 31, 2019
829b0e2
Merge remote-tracking branch 'upstream/master' into TiDB-2866
xiekeyi98 Jan 31, 2019
98c22dd
Wait for change parser
xiekeyi98 Feb 12, 2019
e057bca
just need to test
xiekeyi98 Feb 12, 2019
a2c1e18
change go.mod
xiekeyi98 Feb 12, 2019
55cda15
Merge remote-tracking branch 'upstream/master' into TiDB-2866
xiekeyi98 Feb 12, 2019
08b15b2
fix invalid pointer access
xiekeyi98 Feb 12, 2019
0de461d
Revert "update:change the test"
xiekeyi98 Feb 12, 2019
78df2be
Revert "fix test"
xiekeyi98 Feb 12, 2019
81e6e59
Revert "update: add test to improve CodeCov"
xiekeyi98 Feb 12, 2019
b673b49
passed test(need change test data)
xiekeyi98 Feb 12, 2019
2b88bd0
add judge when show create table
xiekeyi98 Feb 12, 2019
ae15f78
fix logic wrong
xiekeyi98 Feb 13, 2019
010a0e3
add test
xiekeyi98 Feb 13, 2019
43346e0
change variable name
xiekeyi98 Feb 13, 2019
906dfb7
Merge remote-tracking branch 'upstream/master' into TiDB-2866
xiekeyi98 Feb 13, 2019
41da45f
revert something because complex
xiekeyi98 Feb 14, 2019
8a3a0c0
update go.mod
xiekeyi98 Feb 14, 2019
e21c4be
Merge remote-tracking branch 'upstream/master' into TiDB-2866
xiekeyi98 Feb 14, 2019
8ecd0fb
Merge branch 'master' into TiDB-2866
xiekeyi98 Feb 14, 2019
dca2737
update go.mod
xiekeyi98 Feb 14, 2019
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
4 changes: 2 additions & 2 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ func (s *testDBSuite) TestGeneratedColumnDDL(c *C) {
result = s.tk.MustQuery(`show create table table_with_gen_col_blanks`)
result.Check(testkit.Rows("table_with_gen_col_blanks CREATE TABLE `table_with_gen_col_blanks` (\n" +
" `a` int(11) DEFAULT NULL,\n" +
" `b` char(20) CHARSET utf8mb4 COLLATE utf8mb4_bin GENERATED ALWAYS AS (CAST(`a` AS CHAR)) VIRTUAL DEFAULT NULL\n" +
" `b` char(20) GENERATED ALWAYS AS (CAST(`a` AS CHAR)) VIRTUAL DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))

genExprTests := []struct {
Expand Down Expand Up @@ -1925,7 +1925,7 @@ func (s *testDBSuite) TestCheckColumnDefaultValue(c *C) {
s.tk.MustExec("create table text_default_text(c1 text not null default '');")
s.tk.MustQuery(`show create table text_default_text`).Check(testutil.RowsWithSep("|",
"text_default_text CREATE TABLE `text_default_text` (\n"+
" `c1` text CHARSET utf8mb4 COLLATE utf8mb4_bin NOT NULL\n"+
" `c1` text NOT NULL\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))
ctx := s.tk.Se.(sessionctx.Context)
Expand Down
2 changes: 1 addition & 1 deletion executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func (s *testSuite3) TestAlterTableModifyColumn(c *C) {
tk.MustExec("alter table mc modify column c2 text")
result := tk.MustQuery("show create table mc")
createSQL := result.Rows()[0][1]
expected := "CREATE TABLE `mc` (\n `c1` bigint(20) DEFAULT NULL,\n `c2` text CHARSET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"
expected := "CREATE TABLE `mc` (\n `c1` bigint(20) DEFAULT NULL,\n `c2` text DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"
c.Assert(createSQL, Equals, expected)
tk.MustExec("create or replace view alter_view as select c1,c2 from mc")
_, err = tk.Exec("alter table alter_view modify column c2 text")
Expand Down
10 changes: 5 additions & 5 deletions executor/seqtest/seq_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,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 CHARSET utf8 COLLATE utf8_bin 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 @@ -203,7 +203,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) CHARSET utf8mb4 COLLATE utf8mb4_bin 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 Expand Up @@ -412,7 +412,7 @@ func (s *seqTestSuite) TestShow(c *C) {
c.Check(result.Rows(), HasLen, 1)
row = result.Rows()[0]
expectedRow = []interface{}{
"show_test", "CREATE TABLE `show_test` (\n `a` varchar(10) CHARSET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT 'a\\nb\\rc d\\0e'\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='a\\nb\\rc d\\0e'"}
"show_test", "CREATE TABLE `show_test` (\n `a` varchar(10) DEFAULT NULL COMMENT 'a\\nb\\rc d\\0e'\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='a\\nb\\rc d\\0e'"}
for i, r := range row {
c.Check(r, Equals, expectedRow[i])
}
Expand All @@ -428,7 +428,7 @@ func (s *seqTestSuite) TestShow(c *C) {
c.Check(result.Rows(), HasLen, 1)
row = result.Rows()[0]
expectedRow = []interface{}{
"show_test", "CREATE TABLE `show_test` (\n `a` varchar(10) CHARSET utf8mb4 COLLATE utf8mb4_bin DEFAULT 'a\\nb\\rc d\\0e'\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"}
"show_test", "CREATE TABLE `show_test` (\n `a` varchar(10) DEFAULT 'a\\nb\\rc d\\0e'\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"}
for i, r := range row {
c.Check(r, Equals, expectedRow[i])
}
Expand Down Expand Up @@ -520,7 +520,7 @@ func (s *seqTestSuite) TestShow(c *C) {
"t CREATE TABLE `t` (\n"+
" `a` int(11) DEFAULT NULL,\n"+
" `b` int(11) DEFAULT NULL,\n"+
" `c` char(1) CHARSET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,\n"+
" `c` char(1) DEFAULT NULL,\n"+
" `d` int(11) DEFAULT NULL\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"+"\nPARTITION BY RANGE COLUMNS(a,d,c) (\n PARTITION p0 VALUES LESS THAN (5,10,\"ggg\"),\n PARTITION p1 VALUES LESS THAN (10,20,\"mmm\"),\n PARTITION p2 VALUES LESS THAN (15,30,\"sss\"),\n PARTITION p3 VALUES LESS THAN (50,MAXVALUE,MAXVALUE)\n)",
))
Expand Down
29 changes: 16 additions & 13 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,13 +590,25 @@ func (e *ShowExec) fetchShowCreateTable() error {
return nil
}

tblCharset := tb.Meta().Charset
if len(tblCharset) == 0 {
tblCharset = mysql.DefaultCharset
}
tblCollate := tb.Meta().Collate
// Set default collate if collate is not specified.
if len(tblCollate) == 0 {
tblCollate = getDefaultCollate(tblCharset)
}

fmt.Fprintf(&buf, "CREATE TABLE %s (\n", escape(tb.Meta().Name, sqlMode))
var pkCol *table.Column
var hasAutoIncID bool
for i, col := range tb.Cols() {
fmt.Fprintf(&buf, " %s %s", escape(col.Name, sqlMode), col.GetTypeDesc())
if col.Charset != "binary" {
fmt.Fprintf(&buf, " CHARSET %s COLLATE %s", col.Charset, col.Collate)
if col.Charset != tblCharset || col.Collate != tblCollate {
fmt.Fprintf(&buf, " CHARSET %s COLLATE %s", col.Charset, col.Collate)
}
}
if col.IsGenerated() {
// It's a generated column.
Expand Down Expand Up @@ -694,23 +706,14 @@ func (e *ShowExec) fetchShowCreateTable() error {
buf.WriteString("\n")

buf.WriteString(") ENGINE=InnoDB")
charsetName := tb.Meta().Charset
if len(charsetName) == 0 {
charsetName = mysql.DefaultCharset
}
collate := tb.Meta().Collate
// Set default collate if collate is not specified.
if len(collate) == 0 {
collate = getDefaultCollate(charsetName)
}
// Because we only support case sensitive utf8_bin collate, we need to explicitly set the default charset and collation
// to make it work on MySQL server which has default collate utf8_general_ci.
if len(collate) == 0 {
if len(tblCollate) == 0 {
// If we can not find default collate for the given charset,
// do not show the collate part.
fmt.Fprintf(&buf, " DEFAULT CHARSET=%s", charsetName)
fmt.Fprintf(&buf, " DEFAULT CHARSET=%s", tblCharset)
} else {
fmt.Fprintf(&buf, " DEFAULT CHARSET=%s COLLATE=%s", charsetName, collate)
fmt.Fprintf(&buf, " DEFAULT CHARSET=%s COLLATE=%s", tblCharset, tblCollate)
}

// Displayed if the compression typed is set.
Expand Down
11 changes: 11 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,17 @@ func (s *testSuite2) TestShowCreateTable(c *C) {

tk.MustExec("drop view v1")
tk.MustExec("drop table t1")

tk.MustExec(`drop table if exists different_charset`)
tk.MustExec(`create table different_charset(ch1 varchar(10) charset utf8, ch2 varchar(10) charset binary);`)
tk.MustQuery(`show create table different_charset`).Check(testutil.RowsWithSep("|",
""+
"different_charset CREATE TABLE `different_charset` (\n"+
" `ch1` varchar(10) CHARSET utf8 COLLATE utf8_bin DEFAULT NULL,\n"+
" `ch2` varbinary(10) DEFAULT NULL\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))

}

func (s *testSuite2) TestShowEscape(c *C) {
Expand Down