Skip to content

Commit 3f1c698

Browse files
authored
planner: quick fix the panic issue when restoring hints from a plan (#48640)
ref #46791
1 parent 15f7b6e commit 3f1c698

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

pkg/planner/core/hints.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ func getJoinHints(sctx sessionctx.Context, joinType string, parentOffset int, no
108108
}
109109
var dbName, tableName *model.CIStr
110110
if blockOffset != parentOffset {
111-
blockAsNames := *(sctx.GetSessionVars().PlannerSelectBlockAsName.Load())
111+
var blockAsNames []ast.HintTable
112+
if p := sctx.GetSessionVars().PlannerSelectBlockAsName.Load(); p != nil {
113+
blockAsNames = *p
114+
}
112115
if blockOffset >= len(blockAsNames) {
113116
continue
114117
}

pkg/planner/core/logical_plan_builder.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,11 @@ func (b *PlanBuilder) buildResultSetNode(ctx context.Context, node ast.ResultSet
549549
}
550550
}
551551
// `TableName` is not a select block, so we do not need to handle it.
552-
if plannerSelectBlockAsName := *(b.ctx.GetSessionVars().PlannerSelectBlockAsName.Load()); len(plannerSelectBlockAsName) > 0 && !isTableName {
552+
var plannerSelectBlockAsName []ast.HintTable
553+
if p := b.ctx.GetSessionVars().PlannerSelectBlockAsName.Load(); p != nil {
554+
plannerSelectBlockAsName = *p
555+
}
556+
if len(plannerSelectBlockAsName) > 0 && !isTableName {
553557
plannerSelectBlockAsName[p.SelectBlockOffset()] = ast.HintTable{DBName: p.OutputNames()[0].DBName, TableName: p.OutputNames()[0].TblName}
554558
}
555559
// Duplicate column name in one table is not allowed.
@@ -721,7 +725,10 @@ func extractTableAlias(p Plan, parentOffset int) *hintTableInfo {
721725
}
722726
}
723727
blockOffset := p.SelectBlockOffset()
724-
blockAsNames := *(p.SCtx().GetSessionVars().PlannerSelectBlockAsName.Load())
728+
var blockAsNames []ast.HintTable
729+
if p := p.SCtx().GetSessionVars().PlannerSelectBlockAsName.Load(); p != nil {
730+
blockAsNames = *p
731+
}
725732
// For sub-queries like `(select * from t) t1`, t1 should belong to its surrounding select block.
726733
if blockOffset != parentOffset && blockAsNames != nil && blockAsNames[blockOffset].TableName.L != "" {
727734
blockOffset = parentOffset

pkg/planner/core/rule_join_reorder.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,10 @@ func (s *baseSingleGroupJoinOrderSolver) generateLeadingJoinGroup(curJoinGroup [
399399
var leadingJoinGroup []LogicalPlan
400400
leftJoinGroup := make([]LogicalPlan, len(curJoinGroup))
401401
copy(leftJoinGroup, curJoinGroup)
402-
queryBlockNames := *(s.ctx.GetSessionVars().PlannerSelectBlockAsName.Load())
402+
var queryBlockNames []ast.HintTable
403+
if p := s.ctx.GetSessionVars().PlannerSelectBlockAsName.Load(); p != nil {
404+
queryBlockNames = *p
405+
}
403406
for _, hintTbl := range hintInfo.leadingJoinOrder {
404407
match := false
405408
for i, joinGroup := range leftJoinGroup {

0 commit comments

Comments
 (0)