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

ddl: fix create exists resource group #40837

Merged
merged 4 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 13 additions & 4 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7617,10 +7617,14 @@ func (d *ddl) CreateResourceGroup(ctx sessionctx.Context, stmt *ast.CreateResour
return err
}
}
if !stmt.IfNotExists {
if _, ok := d.GetInfoSchemaWithInterceptor(ctx).ResourceGroupByName(groupName); ok {
return infoschema.ErrResourceGroupExists.GenWithStackByArgs(groupName)

if _, ok := d.GetInfoSchemaWithInterceptor(ctx).ResourceGroupByName(groupName); ok {
if stmt.IfNotExists {
err = infoschema.ErrResourceGroupExists.GenWithStackByArgs(groupName)
ctx.GetSessionVars().StmtCtx.AppendNote(err)
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return nil
err = infoschema.ErrResourceGroupExists.GenWithStackByArgs(groupName)
ctx.GetSessionVars().StmtCtx.AppendNote(err)
return nil

Copy link
Contributor

Choose a reason for hiding this comment

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

and we also needs to handle ifNotExsits clause for AlterResourceGroup().

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

}
return infoschema.ErrResourceGroupExists.GenWithStackByArgs(groupName)
}

if groupName.L == defaultResourceGroupName {
Expand Down Expand Up @@ -7710,7 +7714,12 @@ func (d *ddl) AlterResourceGroup(ctx sessionctx.Context, stmt *ast.AlterResource
// Check group existence.
group, ok := is.ResourceGroupByName(groupName)
if !ok {
return infoschema.ErrResourceGroupNotExists.GenWithStackByArgs(groupName)
err := infoschema.ErrResourceGroupNotExists.GenWithStackByArgs(groupName)
if stmt.IfExists {
ctx.GetSessionVars().StmtCtx.AppendNote(err)
return nil
}
return err
}
newGroupInfo, err := buildResourceGroup(group, stmt.ResourceGroupOptionList)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions ddl/resource_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ func TestResourceGroupBasic(t *testing.T) {
g := testResourceGroupNameFromIS(t, tk.Session(), "x")
checkFunc(g)

// test create if not exists
tk.MustExec("create resource group if not exists x " +
"RRU_PER_SEC=10000 " +
"WRU_PER_SEC=20000")
// Check the resource group is not changed
g = testResourceGroupNameFromIS(t, tk.Session(), "x")
checkFunc(g)
// Check warning message
res := tk.MustQuery("show warnings")
res.Check(testkit.Rows("Note 8248 Resource group 'x' already exists"))

tk.MustExec("set global tidb_enable_resource_control = DEFAULT")
tk.MustGetErrCode("alter resource group x "+
"RRU_PER_SEC=2000 "+
Expand All @@ -91,6 +102,13 @@ func TestResourceGroupBasic(t *testing.T) {
re.Equal(uint64(2000), g.RRURate)
re.Equal(uint64(3000), g.WRURate)

tk.MustExec("alter resource group if exists not_exists " +
"RRU_PER_SEC=2000 " +
"WRU_PER_SEC=3000")
// Check warning message
res = tk.MustQuery("show warnings")
res.Check(testkit.Rows("Note 8249 Unknown resource group 'not_exists'"))

tk.MustQuery("select * from information_schema.resource_groups where group_name = 'x'").Check(testkit.Rows(strconv.FormatInt(g.ID, 10) + " x 2000 3000"))

tk.MustExec("drop resource group x")
Expand Down