Skip to content

Commit

Permalink
session: add check resource group hint existence (#45296) (#45347)
Browse files Browse the repository at this point in the history
close #45178
  • Loading branch information
ti-chi-bot authored Jul 13, 2023
1 parent 399dd0d commit 503a6c5
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2184,18 +2184,28 @@ func (s *session) ExecuteStmt(ctx context.Context, stmtNode ast.StmtNode) (sqlex
s.setRequestSource(ctx, stmtLabel, stmtNode)

// Backup the original resource group name since sql hint might change it during optimization
originalResourceGroup := s.GetSessionVars().ResourceGroupName
originalResourceGroup := sessVars.ResourceGroupName

// Transform abstract syntax tree to a physical plan(stored in executor.ExecStmt).
compiler := executor.Compiler{Ctx: s}
stmt, err := compiler.Compile(ctx, stmtNode)
// session resource-group might be changed by query hint, ensure restore it back when
// the execution finished.
if s.GetSessionVars().ResourceGroupName != originalResourceGroup {
defer func() {
// Restore the resource group for the session
s.GetSessionVars().ResourceGroupName = originalResourceGroup
}()
if sessVars.ResourceGroupName != originalResourceGroup {
// if target resource group doesn't exist, fallback to the origin resource group.
if _, ok := domain.GetDomain(s).InfoSchema().ResourceGroupByName(model.NewCIStr(sessVars.ResourceGroupName)); !ok {
logutil.Logger(ctx).Warn("Unknown resource group from hint", zap.String("name", sessVars.ResourceGroupName))
sessVars.ResourceGroupName = originalResourceGroup
// if we are in a txn, should also reset the txn resource group.
if txn, err := s.Txn(false); err == nil && txn != nil && txn.Valid() {
txn.SetOption(kv.ResourceGroupName, originalResourceGroup)
}
} else {
defer func() {
// Restore the resource group for the session
sessVars.ResourceGroupName = originalResourceGroup
}()
}
}
if err != nil {
s.rollbackOnError(ctx)
Expand Down

0 comments on commit 503a6c5

Please sign in to comment.