MCPcopy
hub / github.com/pingcap/tidb / optimize

Function optimize

pkg/planner/optimize.go:472–545  ·  view source on GitHub ↗
(ctx context.Context, sctx planctx.PlanContext, node *resolve.NodeW, is infoschema.InfoSchema)

Source from the content-addressed store, hash-verified

470var optimizeCnt int
471
472func optimize(ctx context.Context, sctx planctx.PlanContext, node *resolve.NodeW, is infoschema.InfoSchema) (base.Plan, types.NameSlice, float64, error) {
473 failpoint.Inject("checkOptimizeCountOne", func(val failpoint.Value) {
474 // only count the optimization for SQL with specified text
475 if testSQL, ok := val.(string); ok && testSQL == node.Node.OriginalText() {
476 optimizeCnt++
477 if optimizeCnt > 1 {
478 failpoint.Return(nil, nil, 0, errors.New("gofail wrong optimizerCnt error"))
479 }
480 }
481 })
482 failpoint.Inject("mockHighLoadForOptimize", func() {
483 sqlPrefixes := []string{"select"}
484 topsql.MockHighCPULoad(sctx.GetSessionVars().StmtCtx.OriginalSQL, sqlPrefixes, 10)
485 })
486 sessVars := sctx.GetSessionVars()
487 if sessVars.StmtCtx.EnableOptimizerDebugTrace {
488 debugtrace.EnterContextCommon(sctx)
489 defer debugtrace.LeaveContextCommon(sctx)
490 }
491
492 // build logical plan
493 hintProcessor := hint.NewQBHintHandler(sctx.GetSessionVars().StmtCtx)
494 node.Node.Accept(hintProcessor)
495 defer hintProcessor.HandleUnusedViewHints()
496 builder := planBuilderPool.Get().(*core.PlanBuilder)
497 defer planBuilderPool.Put(builder.ResetForReuse())
498 builder.Init(sctx, is, hintProcessor)
499 p, err := buildLogicalPlan(ctx, sctx, node, builder)
500 if err != nil {
501 return nil, nil, 0, err
502 }
503
504 activeRoles := sessVars.ActiveRoles
505 // Check privilege. Maybe it's better to move this to the Preprocess, but
506 // we need the table information to check privilege, which is collected
507 // into the visitInfo in the logical plan builder.
508 if pm := privilege.GetPrivilegeManager(sctx); pm != nil {
509 visitInfo := core.VisitInfo4PrivCheck(ctx, is, node.Node, builder.GetVisitInfo())
510 if err := core.CheckPrivilege(activeRoles, pm, visitInfo); err != nil {
511 return nil, nil, 0, err
512 }
513 }
514
515 if err := core.CheckTableLock(sctx, is, builder.GetVisitInfo()); err != nil {
516 return nil, nil, 0, err
517 }
518
519 if err := core.CheckTableMode(node); err != nil {
520 return nil, nil, 0, err
521 }
522
523 names := p.OutputNames()
524
525 // Handle the non-logical plan statement.
526 logic, isLogicalPlan := p.(base.LogicalPlan)
527 if !isLogicalPlan {
528 return p, names, 0, nil
529 }

Callers 1

OptimizeFunction · 0.70

Calls 15

HandleUnusedViewHintsMethod · 0.95
MockHighCPULoadFunction · 0.92
EnterContextCommonFunction · 0.92
LeaveContextCommonFunction · 0.92
NewQBHintHandlerFunction · 0.92
GetPrivilegeManagerFunction · 0.92
VisitInfo4PrivCheckFunction · 0.92
CheckPrivilegeFunction · 0.92
CheckTableLockFunction · 0.92
CheckTableModeFunction · 0.92
RecheckCTEFunction · 0.92
DoOptimizeFunction · 0.92

Tested by

no test coverage detected