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 message about Charset&&Collation in 'show create table` for compatibility #9306

Merged
merged 5 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
executor:adjust 'show create table' for mysql compatibility (#9113)
cherry-pick for release 2.1
  • Loading branch information
xiekeyi98 committed Feb 15, 2019
commit 47ce14cc59a359a0945491c57da10357e6ded881
4 changes: 2 additions & 2 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2238,7 +2238,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 @@ -2389,7 +2389,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 @@ -233,7 +233,7 @@ func (s *testSuite) 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)
}

Expand Down
37 changes: 23 additions & 14 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,13 +492,31 @@ func (e *ShowExec) fetchShowCreateTable() error {

// TODO: let the result more like MySQL.
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("CREATE TABLE %s (\n", escape(tb.Meta().Name, sqlMode)))
if tb.Meta().IsView() {
e.fetchShowCreateTable4View(tb.Meta(), &buf)
e.appendRow([]interface{}{tb.Meta().Name.O, buf.String(), tb.Meta().Charset, tb.Meta().Collate})
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() {
buf.WriteString(fmt.Sprintf(" %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 @@ -596,23 +614,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.
buf.WriteString(fmt.Sprintf(" DEFAULT CHARSET=%s", charsetName))
fmt.Fprintf(&buf, " DEFAULT CHARSET=%s", tblCharset)
} else {
buf.WriteString(fmt.Sprintf(" 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
27 changes: 26 additions & 1 deletion executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,32 @@ func (s *testSuite) TestShowSlow(c *C) {
tk.MustQuery(`admin show slow top all 3`)
}

func (s *testSuite) TestShowEscape(c *C) {
func (s *testSuite2) TestShowCreateTable(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1(a int,b int)")
tk.MustExec("drop view if exists v1")
tk.MustExec("create or replace definer=`root`@`127.0.0.1` view v1 as select * from t1")
tk.MustQuery("show create table v1").Check(testutil.RowsWithSep("|", "v1|CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`127.0.0.1` SQL SECURITY DEFINER VIEW `v1` (`a`, `b`) AS select * from t1 "))

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) {
tk := testkit.NewTestKit(c, s.store)

tk.MustExec("use test")
Expand Down