Skip to content

Commit

Permalink
planner: quick fix the panic issue when restoring hints from a plan (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Dec 4, 2023
1 parent 8b7057a commit 92cd5c1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
5 changes: 4 additions & 1 deletion planner/core/hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ func getJoinHints(sctx sessionctx.Context, joinType string, parentOffset int, no
}
var dbName, tableName *model.CIStr
if blockOffset != parentOffset {
blockAsNames := *(sctx.GetSessionVars().PlannerSelectBlockAsName.Load())
var blockAsNames []ast.HintTable
if p := sctx.GetSessionVars().PlannerSelectBlockAsName.Load(); p != nil {
blockAsNames = *p
}
if blockOffset >= len(blockAsNames) {
continue
}
Expand Down
11 changes: 9 additions & 2 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,11 @@ func (b *PlanBuilder) buildResultSetNode(ctx context.Context, node ast.ResultSet
}
}
// `TableName` is not a select block, so we do not need to handle it.
if plannerSelectBlockAsName := *(b.ctx.GetSessionVars().PlannerSelectBlockAsName.Load()); len(plannerSelectBlockAsName) > 0 && !isTableName {
var plannerSelectBlockAsName []ast.HintTable
if p := b.ctx.GetSessionVars().PlannerSelectBlockAsName.Load(); p != nil {
plannerSelectBlockAsName = *p
}
if len(plannerSelectBlockAsName) > 0 && !isTableName {
plannerSelectBlockAsName[p.SelectBlockOffset()] = ast.HintTable{DBName: p.OutputNames()[0].DBName, TableName: p.OutputNames()[0].TblName}
}
// Duplicate column name in one table is not allowed.
Expand Down Expand Up @@ -589,7 +593,10 @@ func extractTableAlias(p Plan, parentOffset int) *hintTableInfo {
}
}
blockOffset := p.SelectBlockOffset()
blockAsNames := *(p.SCtx().GetSessionVars().PlannerSelectBlockAsName.Load())
var blockAsNames []ast.HintTable
if p := p.SCtx().GetSessionVars().PlannerSelectBlockAsName.Load(); p != nil {
blockAsNames = *p
}
// For sub-queries like `(select * from t) t1`, t1 should belong to its surrounding select block.
if blockOffset != parentOffset && blockAsNames != nil && blockAsNames[blockOffset].TableName.L != "" {
blockOffset = parentOffset
Expand Down
5 changes: 4 additions & 1 deletion planner/core/rule_join_reorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,10 @@ func (s *baseSingleGroupJoinOrderSolver) generateLeadingJoinGroup(curJoinGroup [
var leadingJoinGroup []LogicalPlan
leftJoinGroup := make([]LogicalPlan, len(curJoinGroup))
copy(leftJoinGroup, curJoinGroup)
queryBlockNames := *(s.ctx.GetSessionVars().PlannerSelectBlockAsName.Load())
var queryBlockNames []ast.HintTable
if p := s.ctx.GetSessionVars().PlannerSelectBlockAsName.Load(); p != nil {
queryBlockNames = *p
}
for _, hintTbl := range hintInfo.leadingJoinOrder {
match := false
for i, joinGroup := range leftJoinGroup {
Expand Down

0 comments on commit 92cd5c1

Please sign in to comment.