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: support "show create table" for View #8865

Merged
merged 18 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from 11 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
23 changes: 22 additions & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,12 @@ func (e *ShowExec) fetchShowCreateTable() error {

// TODO: let the result more like MySQL.
var buf bytes.Buffer
if tb.Meta().IsView() {
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
e.fetchShowCreateTable4View(tb.Meta(), &buf)
e.appendRow([]interface{}{tb.Meta().Name.O, buf.String()})
return nil
}

fmt.Fprintf(&buf, "CREATE TABLE %s (\n", escape(tb.Meta().Name, sqlMode))
var pkCol *table.Column
var hasAutoIncID bool
Expand Down Expand Up @@ -729,11 +735,26 @@ func (e *ShowExec) fetchShowCreateTable() error {
if len(tb.Meta().Comment) > 0 {
fmt.Fprintf(&buf, " COMMENT='%s'", format.OutputFormat(tb.Meta().Comment))
}

e.appendRow([]interface{}{tb.Meta().Name.O, buf.String()})
return nil
}

func (e *ShowExec) fetchShowCreateTable4View(tb *model.TableInfo, buf *bytes.Buffer) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @eurekaka mentioned in the earlier code reviews. MySQL behaves different between normal table and view on show create table. For a normal table, the output has two columns, namely Table and Create Table:

MySQL(root@localhost:test) > create table t(a bigint, b bigint);
Query OK, 0 rows affected (0.11 sec)

MySQL(root@localhost:test) > show create table t;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                       |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------+
| t     | CREATE TABLE `t` (
  `a` bigint(20) DEFAULT NULL,
  `b` bigint(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

But for View, the output contains 4 columns, namely View, Create View, character_set_client and collation_connection:

MySQL(root@localhost:test) > create view v as select * from t;
Query OK, 0 rows affected (0.11 sec)

MySQL(root@localhost:test) > show create table v;
+------+---------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| View | Create View                                                                                                                                             | character_set_client | collation_connection |
+------+---------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| v    | CREATE ALGORITHM=UNDEFINED DEFINER=`skip-grants user`@`skip-grants host` SQL SECURITY DEFINER VIEW `v` AS select `t`.`a` AS `a`,`t`.`b` AS `b` from `t` | utf8mb4              | utf8mb4_0900_ai_ci   |
+------+---------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
1 row in set (0.00 sec)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

sqlMode := e.ctx.GetSessionVars().SQLMode

fmt.Fprintf(buf, "CREATE ALGORITHM=%s ", tb.View.Algorithm.String())
fmt.Fprintf(buf, "DEFINER=%s@%s ", escape(model.NewCIStr(tb.View.Definer.Username), sqlMode), escape(model.NewCIStr(tb.View.Definer.Hostname), sqlMode))
fmt.Fprintf(buf, "SQL SECURITY %s ", tb.View.Security.String())
fmt.Fprintf(buf, "VIEW %s (", escape(tb.Name, sqlMode))
for i, col := range tb.Columns {
fmt.Fprintf(buf, "%s", escape(col.Name, sqlMode))
if i < len(tb.Columns)-1 {
fmt.Fprintf(buf, ", ")
}
}
fmt.Fprintf(buf, ") AS %s", tb.View.SelectStmt)
}

func appendPartitionInfo(partitionInfo *model.PartitionInfo, buf *bytes.Buffer) {
if partitionInfo == nil {
return
Expand Down
14 changes: 14 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,20 @@ func (s *testSuite2) TestShowSlow(c *C) {
tk.MustQuery(`admin show slow top all 3`)
}

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")
}

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

Expand Down