(ctx context.Context, sctx planctx.PlanContext, node *resolve.NodeW, is infoschema.InfoSchema)
| 470 | var optimizeCnt int |
| 471 | |
| 472 | func 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 | } |
no test coverage detected