Skip to content

Commit

Permalink
executor: Add support for placement in SHOW CREATE TABLE (#27792)
Browse files Browse the repository at this point in the history
  • Loading branch information
sylzd authored Sep 9, 2021
1 parent dcd3bdb commit ef0098a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
28 changes: 28 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
gjson "encoding/json"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -1065,6 +1066,12 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T
fmt.Fprintf(buf, " ON COMMIT DELETE ROWS")
}

if tableInfo.PlacementPolicyRef != nil {
fmt.Fprintf(buf, " /*T![placement] PLACEMENT POLICY=`%s` */", tableInfo.PlacementPolicyRef.Name.String())
}

// add direct placement info here
appendDirectPlacementInfo(tableInfo.DirectPlacementOpts, buf)
// add partition info here.
appendPartitionInfo(tableInfo.Partition, buf)
return nil
Expand Down Expand Up @@ -1196,6 +1203,27 @@ func fetchShowCreateTable4View(ctx sessionctx.Context, tb *model.TableInfo, buf
fmt.Fprintf(buf, ") AS %s", tb.View.SelectStmt)
}

func appendDirectPlacementInfo(directPlacementOpts *model.PlacementSettings, buf *bytes.Buffer) {
if directPlacementOpts == nil {
return
}
opts := reflect.ValueOf(*directPlacementOpts)
typeOpts := opts.Type()
fmt.Fprintf(buf, " /*T![placement]")
for i := 0; i < opts.NumField(); i++ {
if !opts.Field(i).IsZero() {
v := opts.Field(i).Interface()
switch v.(type) {
case string:
fmt.Fprintf(buf, ` %s="%s"`, strings.ToUpper(typeOpts.Field(i).Tag.Get("json")), v)
case uint64:
fmt.Fprintf(buf, " %s=%d", strings.ToUpper(typeOpts.Field(i).Tag.Get("json")), v)
}
}
}
fmt.Fprintf(buf, " */")
}

func appendPartitionInfo(partitionInfo *model.PartitionInfo, buf *bytes.Buffer) {
if partitionInfo == nil {
return
Expand Down
43 changes: 43 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,49 @@ func (s *testSuite5) TestShowCreateTable(c *C) {
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
}

func (s *testAutoRandomSuite) TestShowCreateTablePlacement(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

// case for direct opts
tk.MustExec(`DROP TABLE IF EXISTS t`)
tk.MustExec("create table t(a int) " +
"PRIMARY_REGION=\"cn-east-1\" " +
"REGIONS=\"cn-east-1, cn-east-2\" " +
"FOLLOWERS=2 " +
"FOLLOWER_CONSTRAINTS=\"[+zone=cn-east-1]\" " +
"CONSTRAINTS=\"[+disk=ssd]\"")
tk.MustQuery(`show create table t`).Check(testutil.RowsWithSep("|",
"t CREATE TABLE `t` (\n"+
" `a` int(11) DEFAULT NULL\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+
"/*T![placement] PRIMARY_REGION=\"cn-east-1\" "+
"REGIONS=\"cn-east-1, cn-east-2\" "+
"FOLLOWERS=2 "+
"CONSTRAINTS=\"[+disk=ssd]\" "+
"FOLLOWER_CONSTRAINTS=\"[+zone=cn-east-1]\" */",
))

// case for policy
tk.MustExec(`DROP TABLE IF EXISTS t`)
tk.MustExec("create placement policy x " +
"PRIMARY_REGION=\"cn-east-1\" " +
"REGIONS=\"cn-east-1, cn-east-2\" " +
"FOLLOWERS=2 " +
"FOLLOWER_CONSTRAINTS=\"[+zone=cn-east-1]\" " +
"CONSTRAINTS=\"[+disk=ssd]\" ")
tk.MustExec("create table t(a int)" +
"PLACEMENT POLICY=\"x\"")
tk.MustQuery(`show create table t`).Check(testutil.RowsWithSep("|",
"t CREATE TABLE `t` (\n"+
" `a` int(11) DEFAULT NULL\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+
"/*T![placement] PLACEMENT POLICY=`x` */",
))

tk.MustExec(`DROP TABLE IF EXISTS t`)
}

func (s *testAutoRandomSuite) TestShowCreateTableAutoRandom(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down

0 comments on commit ef0098a

Please sign in to comment.