Skip to content

Commit

Permalink
planner: quick fix the panic issue when restoring hints from a plan (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
AmoebaProtozoa authored Jan 18, 2024
1 parent 821c8b8 commit 2abc622
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 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
12 changes: 9 additions & 3 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,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 !isTableName && b.ctx.GetSessionVars().PlannerSelectBlockAsName.Load() != nil {
plannerSelectBlockAsName := *(b.ctx.GetSessionVars().PlannerSelectBlockAsName.Load())
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 @@ -572,7 +575,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 2abc622

Please sign in to comment.