From 0fc0084e56b81b49b341bbb04c8b8ddfb55a8602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E6=89=8B=E6=8E=89=E5=8C=85=E5=B7=A5=E7=A8=8B?= =?UTF-8?q?=E5=B8=88?= Date: Mon, 22 Jan 2024 13:39:19 +0800 Subject: [PATCH] optimizer: move adjust optimization flags code out (#50622) --- pkg/planner/core/optimizer.go | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/pkg/planner/core/optimizer.go b/pkg/planner/core/optimizer.go index 552e1181b6..54f9e5f95a 100644 --- a/pkg/planner/core/optimizer.go +++ b/pkg/planner/core/optimizer.go @@ -62,6 +62,8 @@ var AllowCartesianProduct = atomic.NewBool(true) // IsReadOnly check whether the ast.Node is a read only statement. var IsReadOnly func(node ast.Node, vars *variable.SessionVars) bool +// Note: The order of flags is same as the order of optRule in the list. +// Do not mess up the order. const ( flagGcSubstitute uint64 = 1 << iota flagPrunColumns @@ -312,19 +314,7 @@ func doOptimize( logic LogicalPlan, ) (LogicalPlan, PhysicalPlan, float64, error) { sessVars := sctx.GetSessionVars() - // if there is something after flagPrunColumns, do flagPrunColumnsAgain - if flag&flagPrunColumns > 0 && flag-flagPrunColumns > flagPrunColumns { - flag |= flagPrunColumnsAgain - } - if checkStableResultMode(logic.SCtx()) { - flag |= flagStabilizeResults - } - if logic.SCtx().GetSessionVars().StmtCtx.StraightJoinOrder { - // When we use the straight Join Order hint, we should disable the join reorder optimization. - flag &= ^flagJoinReOrder - } - flag |= flagCollectPredicateColumnsPoint - flag |= flagSyncWaitStatsLoadPoint + flag = adjustOptimizationFlags(flag, logic) logic, err := logicalOptimize(ctx, flag, logic) if err != nil { return nil, nil, 0, err @@ -355,6 +345,24 @@ func doOptimize( return logic, finalPlan, cost, nil } +func adjustOptimizationFlags(flag uint64, logic LogicalPlan) uint64 { + // If there is something after flagPrunColumns, do flagPrunColumnsAgain. + if flag&flagPrunColumns > 0 && flag-flagPrunColumns > flagPrunColumns { + flag |= flagPrunColumnsAgain + } + if checkStableResultMode(logic.SCtx()) { + flag |= flagStabilizeResults + } + if logic.SCtx().GetSessionVars().StmtCtx.StraightJoinOrder { + // When we use the straight Join Order hint, we should disable the join reorder optimization. + flag &= ^flagJoinReOrder + } + flag |= flagCollectPredicateColumnsPoint + flag |= flagSyncWaitStatsLoadPoint + + return flag +} + // DoOptimize optimizes a logical plan to a physical plan. func DoOptimize( ctx context.Context,