Skip to content

Commit

Permalink
executor, planner: add SHOW CREATE PLACEMENT POLICY (pingcap#28797)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgo authored Oct 14, 2021
1 parent fdcf122 commit e30392c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ func (e *ShowExec) fetchAll(ctx context.Context) error {
return e.fetchShowCreateView()
case ast.ShowCreateDatabase:
return e.fetchShowCreateDatabase()
case ast.ShowCreatePlacementPolicy:
return e.fetchShowCreatePlacementPolicy()
case ast.ShowDatabases:
return e.fetchShowDatabases()
case ast.ShowDrainerStatus:
Expand Down Expand Up @@ -1380,6 +1382,17 @@ func (e *ShowExec) fetchShowCreateDatabase() error {
return nil
}

// fetchShowCreatePlacementPolicy composes show create policy result.
func (e *ShowExec) fetchShowCreatePlacementPolicy() error {
policy, found := e.is.PolicyByName(e.DBName)
if !found {
return infoschema.ErrPlacementPolicyNotExists.GenWithStackByArgs(e.DBName.O)
}
showCreate := fmt.Sprintf("CREATE PLACEMENT POLICY `%s` %s", e.DBName.O, policy.PlacementSettings.String())
e.appendRow([]interface{}{e.DBName.O, showCreate})
return nil
}

func (e *ShowExec) fetchShowCollation() error {
collations := collate.GetSupportedCollations()
for _, v := range collations {
Expand Down
13 changes: 13 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,19 @@ func (s *testSuite5) TestShowPerformanceSchema(c *C) {
"events_statements_summary_by_digest 0 SCHEMA_NAME 2 DIGEST A 0 <nil> <nil> YES BTREE YES <nil> NO"))
}

func (s *testSuite5) TestShowCreatePlacementPolicy(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("CREATE PLACEMENT POLICY xyz PRIMARY_REGION='us-east-1' REGIONS='us-east-1,us-east-2' FOLLOWERS=4")
tk.MustQuery("SHOW CREATE PLACEMENT POLICY xyz").Check(testkit.Rows("xyz CREATE PLACEMENT POLICY `xyz` PRIMARY_REGION=\"us-east-1\" REGIONS=\"us-east-1,us-east-2\" FOLLOWERS=4"))
// non existent policy
err := tk.QueryToErr("SHOW CREATE PLACEMENT POLICY doesnotexist")
c.Assert(err.Error(), Equals, infoschema.ErrPlacementPolicyNotExists.GenWithStackByArgs("doesnotexist").Error())
// alter and try second example
tk.MustExec("ALTER PLACEMENT POLICY xyz FOLLOWERS=4")
tk.MustQuery("SHOW CREATE PLACEMENT POLICY xyz").Check(testkit.Rows("xyz CREATE PLACEMENT POLICY `xyz` FOLLOWERS=4"))
tk.MustExec("DROP PLACEMENT POLICY xyz")
}

func (s *testSuite5) TestShowTemporaryTable(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
2 changes: 2 additions & 0 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4047,6 +4047,8 @@ func buildShowSchema(s *ast.ShowStmt, isView bool, isSequence bool) (schema *exp
} else {
names = []string{"Table", "Create Table"}
}
case ast.ShowCreatePlacementPolicy:
names = []string{"Policy", "Create Policy"}
case ast.ShowCreateUser:
if s.User != nil {
names = []string{fmt.Sprintf("CREATE USER for %s", s.User)}
Expand Down

0 comments on commit e30392c

Please sign in to comment.