Skip to content

Commit

Permalink
planner: Improve the DoOptimize function names to minimize unnecessar…
Browse files Browse the repository at this point in the history
…y confusion (#48540)
  • Loading branch information
Rustin170506 authored Nov 13, 2023
1 parent 6d00d10 commit ebb7568
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 16 additions & 4 deletions pkg/planner/core/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,15 @@ func checkStableResultMode(sctx sessionctx.Context) bool {
return s.EnableStableResultMode && (!st.InInsertStmt && !st.InUpdateStmt && !st.InDeleteStmt && !st.InLoadDataStmt)
}

// DoOptimizeAndLogicAsRet optimizes a logical plan to a physical plan and return the optimized logical plan.
func DoOptimizeAndLogicAsRet(ctx context.Context, sctx sessionctx.Context, flag uint64, logic LogicalPlan) (LogicalPlan, PhysicalPlan, float64, error) {
// doOptimize optimizes a logical plan into a physical plan,
// while also returning the optimized logical plan, the final physical plan, and the cost of the final plan.
// The returned logical plan is necessary for generating plans for Common Table Expressions (CTEs).
func doOptimize(
ctx context.Context,
sctx sessionctx.Context,
flag uint64,
logic LogicalPlan,
) (LogicalPlan, PhysicalPlan, float64, error) {
sessVars := sctx.GetSessionVars()
// if there is something after flagPrunColumns, do flagPrunColumnsAgain
if flag&flagPrunColumns > 0 && flag-flagPrunColumns > flagPrunColumns {
Expand Down Expand Up @@ -349,13 +356,18 @@ func DoOptimizeAndLogicAsRet(ctx context.Context, sctx sessionctx.Context, flag
}

// DoOptimize optimizes a logical plan to a physical plan.
func DoOptimize(ctx context.Context, sctx sessionctx.Context, flag uint64, logic LogicalPlan) (PhysicalPlan, float64, error) {
func DoOptimize(
ctx context.Context,
sctx sessionctx.Context,
flag uint64,
logic LogicalPlan,
) (PhysicalPlan, float64, error) {
sessVars := sctx.GetSessionVars()
if sessVars.StmtCtx.EnableOptimizerDebugTrace {
debugtrace.EnterContextCommon(sctx)
defer debugtrace.LeaveContextCommon(sctx)
}
_, finalPlan, cost, err := DoOptimizeAndLogicAsRet(ctx, sctx, flag, logic)
_, finalPlan, cost, err := doOptimize(ctx, sctx, flag, logic)
return finalPlan, cost, err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/planner/core/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ func (p *LogicalCTE) DeriveStats(_ []*property.StatsInfo, selfSchema *expression
p.cte.seedPartLogicalPlan = newSel
p.cte.optFlag |= flagPredicatePushDown
}
p.cte.seedPartLogicalPlan, p.cte.seedPartPhysicalPlan, _, err = DoOptimizeAndLogicAsRet(context.TODO(), p.SCtx(), p.cte.optFlag, p.cte.seedPartLogicalPlan)
p.cte.seedPartLogicalPlan, p.cte.seedPartPhysicalPlan, _, err = doOptimize(context.TODO(), p.SCtx(), p.cte.optFlag, p.cte.seedPartLogicalPlan)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit ebb7568

Please sign in to comment.