From 9376dcf205d1f4698c6d67e0684499eed7b87128 Mon Sep 17 00:00:00 2001 From: yisaer Date: Fri, 17 Dec 2021 17:59:23 +0800 Subject: [PATCH 1/4] support race Signed-off-by: yisaer --- planner/core/plan.go | 2 +- planner/core/rule_predicate_push_down.go | 124 +++++++++++++++++------ 2 files changed, 93 insertions(+), 33 deletions(-) diff --git a/planner/core/plan.go b/planner/core/plan.go index 3515f44e91750..ca539bc56d517 100644 --- a/planner/core/plan.go +++ b/planner/core/plan.go @@ -237,7 +237,7 @@ type LogicalPlan interface { // PredicatePushDown pushes down the predicates in the where/on/having clauses as deeply as possible. // It will accept a predicate that is an expression slice, and return the expressions that can't be pushed. // Because it might change the root if the having clause exists, we need to return a plan that represents a new root. - PredicatePushDown([]expression.Expression) ([]expression.Expression, LogicalPlan) + PredicatePushDown([]expression.Expression, *logicalOptimizeOp) ([]expression.Expression, LogicalPlan) // PruneColumns prunes the unused columns. PruneColumns([]*expression.Column) error diff --git a/planner/core/rule_predicate_push_down.go b/planner/core/rule_predicate_push_down.go index f59fedc25ba28..7616a4c22a0f2 100644 --- a/planner/core/rule_predicate_push_down.go +++ b/planner/core/rule_predicate_push_down.go @@ -15,7 +15,9 @@ package core import ( + "bytes" "context" + "fmt" "github.com/pingcap/tidb/expression" "github.com/pingcap/tidb/kv" @@ -28,11 +30,11 @@ import ( type ppdSolver struct{} func (s *ppdSolver) optimize(ctx context.Context, lp LogicalPlan, opt *logicalOptimizeOp) (LogicalPlan, error) { - _, p := lp.PredicatePushDown(nil) + _, p := lp.PredicatePushDown(nil, opt) return p, nil } -func addSelection(p LogicalPlan, child LogicalPlan, conditions []expression.Expression, chIdx int) { +func addSelection(p LogicalPlan, child LogicalPlan, conditions []expression.Expression, chIdx int, opt *logicalOptimizeOp) { if len(conditions) == 0 { p.Children()[chIdx] = child return @@ -42,6 +44,7 @@ func addSelection(p LogicalPlan, child LogicalPlan, conditions []expression.Expr dual := Conds2TableDual(child, conditions) if dual != nil { p.Children()[chIdx] = dual + appendTableDualTraceStep(child, dual, conditions, opt) return } @@ -53,16 +56,18 @@ func addSelection(p LogicalPlan, child LogicalPlan, conditions []expression.Expr selection := LogicalSelection{Conditions: conditions}.Init(p.SCtx(), p.SelectBlockOffset()) selection.SetChildren(child) p.Children()[chIdx] = selection + opt.appendStepToCurrent(selection.ID(), selection.TP(), "", + fmt.Sprintf("add %v[%v] to connect %v[%v] and %v[%v]", selection.TP(), selection.ID(), p.TP(), p.ID(), child.TP(), child.ID())) } // PredicatePushDown implements LogicalPlan interface. -func (p *baseLogicalPlan) PredicatePushDown(predicates []expression.Expression) ([]expression.Expression, LogicalPlan) { +func (p *baseLogicalPlan) PredicatePushDown(predicates []expression.Expression, opt *logicalOptimizeOp) ([]expression.Expression, LogicalPlan) { if len(p.children) == 0 { return predicates, p.self } child := p.children[0] - rest, newChild := child.PredicatePushDown(predicates) - addSelection(p.self, newChild, rest, 0) + rest, newChild := child.PredicatePushDown(predicates, opt) + addSelection(p.self, newChild, rest, 0, opt) return nil, p.self } @@ -80,17 +85,17 @@ func splitSetGetVarFunc(filters []expression.Expression) ([]expression.Expressio } // PredicatePushDown implements LogicalPlan PredicatePushDown interface. -func (p *LogicalSelection) PredicatePushDown(predicates []expression.Expression) ([]expression.Expression, LogicalPlan) { +func (p *LogicalSelection) PredicatePushDown(predicates []expression.Expression, opt *logicalOptimizeOp) ([]expression.Expression, LogicalPlan) { predicates = DeleteTrueExprs(p, predicates) p.Conditions = DeleteTrueExprs(p, p.Conditions) var child LogicalPlan var retConditions []expression.Expression if p.buildByHaving { - retConditions, child = p.children[0].PredicatePushDown(predicates) + retConditions, child = p.children[0].PredicatePushDown(predicates, opt) retConditions = append(retConditions, p.Conditions...) } else { canBePushDown, canNotBePushDown := splitSetGetVarFunc(p.Conditions) - retConditions, child = p.children[0].PredicatePushDown(append(canBePushDown, predicates...)) + retConditions, child = p.children[0].PredicatePushDown(append(canBePushDown, predicates...), opt) retConditions = append(retConditions, canNotBePushDown...) } if len(retConditions) > 0 { @@ -98,16 +103,18 @@ func (p *LogicalSelection) PredicatePushDown(predicates []expression.Expression) // Return table dual when filter is constant false or null. dual := Conds2TableDual(p, p.Conditions) if dual != nil { + appendTableDualTraceStep(p, dual, p.Conditions, opt) return nil, dual } return nil, p } + appendSelectionPredicatePushDownTraceStep(p, opt) return nil, child } // PredicatePushDown implements LogicalPlan PredicatePushDown interface. -func (p *LogicalUnionScan) PredicatePushDown(predicates []expression.Expression) ([]expression.Expression, LogicalPlan) { - retainedPredicates, _ := p.children[0].PredicatePushDown(predicates) +func (p *LogicalUnionScan) PredicatePushDown(predicates []expression.Expression, opt *logicalOptimizeOp) ([]expression.Expression, LogicalPlan) { + retainedPredicates, _ := p.children[0].PredicatePushDown(predicates, opt) p.conditions = make([]expression.Expression, 0, len(predicates)) p.conditions = append(p.conditions, predicates...) // The conditions in UnionScan is only used for added rows, so parent Selection should not be removed. @@ -115,21 +122,22 @@ func (p *LogicalUnionScan) PredicatePushDown(predicates []expression.Expression) } // PredicatePushDown implements LogicalPlan PredicatePushDown interface. -func (ds *DataSource) PredicatePushDown(predicates []expression.Expression) ([]expression.Expression, LogicalPlan) { +func (ds *DataSource) PredicatePushDown(predicates []expression.Expression, opt *logicalOptimizeOp) ([]expression.Expression, LogicalPlan) { predicates = expression.PropagateConstant(ds.ctx, predicates) predicates = DeleteTrueExprs(ds, predicates) ds.allConds = predicates ds.pushedDownConds, predicates = expression.PushDownExprs(ds.ctx.GetSessionVars().StmtCtx, predicates, ds.ctx.GetClient(), kv.UnSpecified) + appendDataSourcePredicatePushDownTraceStep(ds, opt) return predicates, ds } // PredicatePushDown implements LogicalPlan PredicatePushDown interface. -func (p *LogicalTableDual) PredicatePushDown(predicates []expression.Expression) ([]expression.Expression, LogicalPlan) { +func (p *LogicalTableDual) PredicatePushDown(predicates []expression.Expression, opt *logicalOptimizeOp) ([]expression.Expression, LogicalPlan) { return predicates, p } // PredicatePushDown implements LogicalPlan PredicatePushDown interface. -func (p *LogicalJoin) PredicatePushDown(predicates []expression.Expression) (ret []expression.Expression, retPlan LogicalPlan) { +func (p *LogicalJoin) PredicatePushDown(predicates []expression.Expression, opt *logicalOptimizeOp) (ret []expression.Expression, retPlan LogicalPlan) { simplifyOuterJoin(p, predicates) var equalCond []*expression.ScalarFunction var leftPushCond, rightPushCond, otherCond, leftCond, rightCond []expression.Expression @@ -138,6 +146,7 @@ func (p *LogicalJoin) PredicatePushDown(predicates []expression.Expression) (ret predicates = p.outerJoinPropConst(predicates) dual := Conds2TableDual(p, predicates) if dual != nil { + appendTableDualTraceStep(p, dual, predicates, opt) return ret, dual } // Handle where conditions @@ -156,6 +165,7 @@ func (p *LogicalJoin) PredicatePushDown(predicates []expression.Expression) (ret predicates = p.outerJoinPropConst(predicates) dual := Conds2TableDual(p, predicates) if dual != nil { + appendTableDualTraceStep(p, dual, predicates, opt) return ret, dual } // Handle where conditions @@ -182,6 +192,7 @@ func (p *LogicalJoin) PredicatePushDown(predicates []expression.Expression) (ret // Return table dual when filter is constant false or null. dual := Conds2TableDual(p, tempCond) if dual != nil { + appendTableDualTraceStep(p, dual, tempCond, opt) return ret, dual } equalCond, leftPushCond, rightPushCond, otherCond = p.extractOnCondition(tempCond, true, true) @@ -196,6 +207,7 @@ func (p *LogicalJoin) PredicatePushDown(predicates []expression.Expression) (ret // Return table dual when filter is constant false or null. dual := Conds2TableDual(p, predicates) if dual != nil { + appendTableDualTraceStep(p, dual, predicates, opt) return ret, dual } // `predicates` should only contain left conditions or constant filters. @@ -212,10 +224,10 @@ func (p *LogicalJoin) PredicatePushDown(predicates []expression.Expression) (ret } leftCond = expression.RemoveDupExprs(p.ctx, leftCond) rightCond = expression.RemoveDupExprs(p.ctx, rightCond) - leftRet, lCh := p.children[0].PredicatePushDown(leftCond) - rightRet, rCh := p.children[1].PredicatePushDown(rightCond) - addSelection(p, lCh, leftRet, 0) - addSelection(p, rCh, rightRet, 1) + leftRet, lCh := p.children[0].PredicatePushDown(leftCond, opt) + rightRet, rCh := p.children[1].PredicatePushDown(rightCond, opt) + addSelection(p, lCh, leftRet, 0, opt) + addSelection(p, rCh, rightRet, 1, opt) p.updateEQCond() buildKeyInfo(p) return ret, p.self @@ -380,12 +392,12 @@ func isNullRejected(ctx sessionctx.Context, schema *expression.Schema, expr expr } // PredicatePushDown implements LogicalPlan PredicatePushDown interface. -func (p *LogicalProjection) PredicatePushDown(predicates []expression.Expression) (ret []expression.Expression, retPlan LogicalPlan) { +func (p *LogicalProjection) PredicatePushDown(predicates []expression.Expression, opt *logicalOptimizeOp) (ret []expression.Expression, retPlan LogicalPlan) { canBePushed := make([]expression.Expression, 0, len(predicates)) canNotBePushed := make([]expression.Expression, 0, len(predicates)) for _, expr := range p.Exprs { if expression.HasAssignSetVarFunc(expr) { - _, child := p.baseLogicalPlan.PredicatePushDown(nil) + _, child := p.baseLogicalPlan.PredicatePushDown(nil, opt) return predicates, child } } @@ -397,23 +409,23 @@ func (p *LogicalProjection) PredicatePushDown(predicates []expression.Expression canNotBePushed = append(canNotBePushed, cond) } } - remained, child := p.baseLogicalPlan.PredicatePushDown(canBePushed) + remained, child := p.baseLogicalPlan.PredicatePushDown(canBePushed, opt) return append(remained, canNotBePushed...), child } // PredicatePushDown implements LogicalPlan PredicatePushDown interface. -func (p *LogicalUnionAll) PredicatePushDown(predicates []expression.Expression) (ret []expression.Expression, retPlan LogicalPlan) { +func (p *LogicalUnionAll) PredicatePushDown(predicates []expression.Expression, opt *logicalOptimizeOp) (ret []expression.Expression, retPlan LogicalPlan) { for i, proj := range p.children { newExprs := make([]expression.Expression, 0, len(predicates)) newExprs = append(newExprs, predicates...) - retCond, newChild := proj.PredicatePushDown(newExprs) - addSelection(p, newChild, retCond, i) + retCond, newChild := proj.PredicatePushDown(newExprs, opt) + addSelection(p, newChild, retCond, i, opt) } return nil, p } // PredicatePushDown implements LogicalPlan PredicatePushDown interface. -func (la *LogicalAggregation) PredicatePushDown(predicates []expression.Expression) (ret []expression.Expression, retPlan LogicalPlan) { +func (la *LogicalAggregation) PredicatePushDown(predicates []expression.Expression, opt *logicalOptimizeOp) (ret []expression.Expression, retPlan LogicalPlan) { var condsToPush []expression.Expression exprsOriginal := make([]expression.Expression, 0, len(la.AggFuncs)) for _, fun := range la.AggFuncs { @@ -447,21 +459,21 @@ func (la *LogicalAggregation) PredicatePushDown(predicates []expression.Expressi ret = append(ret, cond) } } - la.baseLogicalPlan.PredicatePushDown(condsToPush) + la.baseLogicalPlan.PredicatePushDown(condsToPush, opt) return ret, la } // PredicatePushDown implements LogicalPlan PredicatePushDown interface. -func (p *LogicalLimit) PredicatePushDown(predicates []expression.Expression) ([]expression.Expression, LogicalPlan) { +func (p *LogicalLimit) PredicatePushDown(predicates []expression.Expression, opt *logicalOptimizeOp) ([]expression.Expression, LogicalPlan) { // Limit forbids any condition to push down. - p.baseLogicalPlan.PredicatePushDown(nil) + p.baseLogicalPlan.PredicatePushDown(nil, opt) return predicates, p } // PredicatePushDown implements LogicalPlan PredicatePushDown interface. -func (p *LogicalMaxOneRow) PredicatePushDown(predicates []expression.Expression) ([]expression.Expression, LogicalPlan) { +func (p *LogicalMaxOneRow) PredicatePushDown(predicates []expression.Expression, opt *logicalOptimizeOp) ([]expression.Expression, LogicalPlan) { // MaxOneRow forbids any condition to push down. - p.baseLogicalPlan.PredicatePushDown(nil) + p.baseLogicalPlan.PredicatePushDown(nil, opt) return predicates, p } @@ -610,7 +622,7 @@ func (p *LogicalWindow) GetPartitionByCols() []*expression.Column { } // PredicatePushDown implements LogicalPlan PredicatePushDown interface. -func (p *LogicalWindow) PredicatePushDown(predicates []expression.Expression) ([]expression.Expression, LogicalPlan) { +func (p *LogicalWindow) PredicatePushDown(predicates []expression.Expression, opt *logicalOptimizeOp) ([]expression.Expression, LogicalPlan) { canBePushed := make([]expression.Expression, 0, len(predicates)) canNotBePushed := make([]expression.Expression, 0, len(predicates)) partitionCols := expression.NewSchema(p.GetPartitionByCols()...) @@ -623,12 +635,12 @@ func (p *LogicalWindow) PredicatePushDown(predicates []expression.Expression) ([ canNotBePushed = append(canNotBePushed, cond) } } - p.baseLogicalPlan.PredicatePushDown(canBePushed) + p.baseLogicalPlan.PredicatePushDown(canBePushed, opt) return canNotBePushed, p } // PredicatePushDown implements LogicalPlan PredicatePushDown interface. -func (p *LogicalMemTable) PredicatePushDown(predicates []expression.Expression) ([]expression.Expression, LogicalPlan) { +func (p *LogicalMemTable) PredicatePushDown(predicates []expression.Expression, opt *logicalOptimizeOp) ([]expression.Expression, LogicalPlan) { if p.Extractor != nil { predicates = p.Extractor.Extract(p.ctx, p.schema, p.names, predicates) } @@ -638,3 +650,51 @@ func (p *LogicalMemTable) PredicatePushDown(predicates []expression.Expression) func (*ppdSolver) name() string { return "predicate_push_down" } + +func appendTableDualTraceStep(replaced LogicalPlan, dual LogicalPlan, conditions []expression.Expression, opt *logicalOptimizeOp) { + action := fmt.Sprintf("%v[%v] is replaced by %v[%v]", replaced.TP(), replaced.ID(), dual.TP(), dual.ID()) + reason := func() string { + buffer := bytes.NewBufferString("The conditions[") + for i, cond := range conditions { + if i > 0 { + buffer.WriteString(",") + } + buffer.WriteString(cond.String()) + } + buffer.WriteString("] are constant false or null") + return buffer.String() + }() + opt.appendStepToCurrent(dual.ID(), dual.TP(), reason, action) +} + +func appendSelectionPredicatePushDownTraceStep(p *LogicalSelection, opt *logicalOptimizeOp) { + action := fmt.Sprintf("%v[%v] is removed", p.TP(), p.ID()) + reason := func() string { + buffer := bytes.NewBufferString("Conditions[") + for i, cond := range p.Conditions { + if i > 0 { + buffer.WriteString(cond.String()) + } + } + buffer.WriteString("] are pushed down") + return buffer.String() + }() + opt.appendStepToCurrent(p.ID(), p.TP(), reason, action) +} + +func appendDataSourcePredicatePushDownTraceStep(ds *DataSource, opt *logicalOptimizeOp) { + if len(ds.pushedDownConds) < 1 { + return + } + action := func() string { + buffer := bytes.NewBufferString("Conditions[") + for i, cond := range ds.pushedDownConds { + if i > 0 { + buffer.WriteString(cond.String()) + } + } + buffer.WriteString(fmt.Sprintf("] are pushed down across %v[%v]", ds.TP(), ds.ID())) + return buffer.String() + }() + opt.appendStepToCurrent(ds.ID(), ds.TP(), "", action) +} From e17efb6bcc244005aa93611c37be41516b19a67c Mon Sep 17 00:00:00 2001 From: yisaer Date: Mon, 20 Dec 2021 13:46:10 +0800 Subject: [PATCH 2/4] add testcase Signed-off-by: yisaer --- planner/core/logical_plan_trace_test.go | 38 +++++++++++++++++++++++ planner/core/rule_predicate_push_down.go | 39 ++++++++++++++---------- 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/planner/core/logical_plan_trace_test.go b/planner/core/logical_plan_trace_test.go index 58348bd7712de..8a71aaf43306b 100644 --- a/planner/core/logical_plan_trace_test.go +++ b/planner/core/logical_plan_trace_test.go @@ -86,6 +86,44 @@ func (s *testPlanSuite) TestSingleRuleTraceStep(c *C) { assertRuleName string assertRuleSteps []assertTraceStep }{ + { + sql: "select * from t as t1 join t as t2 on t1.a = t2.a where t1.a < 1;", + flags: []uint64{flagPredicatePushDown, flagBuildKeyInfo, flagPrunColumns}, + assertRuleName: "predicate_push_down", + assertRuleSteps: []assertTraceStep{ + { + assertReason: "", + assertAction: "The conditions[lt(test.t.a, 1)] are pushed down across DataSource_1", + }, + { + assertReason: "", + assertAction: "The conditions[lt(test.t.a, 1)] are pushed down across DataSource_1", + }, + { + assertAction: "Selection_4 is removed", + assertReason: "The conditions[eq(test.t.a, test.t.a)] in Selection_4 are pushed down", + }, + { + assertAction: "Selection_5 is removed", + assertReason: "The conditions[lt(test.t.a, 1)] in Selection_5 are pushed down", + }, + }, + }, + { + sql: "select * from t where a < 1;", + flags: []uint64{flagPredicatePushDown, flagBuildKeyInfo, flagPrunColumns}, + assertRuleName: "predicate_push_down", + assertRuleSteps: []assertTraceStep{ + { + assertReason: "", + assertAction: "The conditions[lt(test.t.a, 1)] are pushed down across DataSource_1", + }, + { + assertReason: "The conditions[lt(test.t.a, 1)] in Selection_2 are pushed down", + assertAction: "Selection_2 is removed", + }, + }, + }, { sql: "select * from pt3 where ptn > 3;", flags: []uint64{flagPartitionProcessor, flagPredicatePushDown, flagBuildKeyInfo, flagPrunColumns}, diff --git a/planner/core/rule_predicate_push_down.go b/planner/core/rule_predicate_push_down.go index 7616a4c22a0f2..a57cbe5547e56 100644 --- a/planner/core/rule_predicate_push_down.go +++ b/planner/core/rule_predicate_push_down.go @@ -57,7 +57,7 @@ func addSelection(p LogicalPlan, child LogicalPlan, conditions []expression.Expr selection.SetChildren(child) p.Children()[chIdx] = selection opt.appendStepToCurrent(selection.ID(), selection.TP(), "", - fmt.Sprintf("add %v[%v] to connect %v[%v] and %v[%v]", selection.TP(), selection.ID(), p.TP(), p.ID(), child.TP(), child.ID())) + fmt.Sprintf("add %v_%v to connect %v_%v and %v_%v", selection.TP(), selection.ID(), p.TP(), p.ID(), child.TP(), child.ID())) } // PredicatePushDown implements LogicalPlan interface. @@ -90,11 +90,13 @@ func (p *LogicalSelection) PredicatePushDown(predicates []expression.Expression, p.Conditions = DeleteTrueExprs(p, p.Conditions) var child LogicalPlan var retConditions []expression.Expression + var originConditions []expression.Expression if p.buildByHaving { retConditions, child = p.children[0].PredicatePushDown(predicates, opt) retConditions = append(retConditions, p.Conditions...) } else { canBePushDown, canNotBePushDown := splitSetGetVarFunc(p.Conditions) + originConditions = canBePushDown retConditions, child = p.children[0].PredicatePushDown(append(canBePushDown, predicates...), opt) retConditions = append(retConditions, canNotBePushDown...) } @@ -108,7 +110,7 @@ func (p *LogicalSelection) PredicatePushDown(predicates []expression.Expression, } return nil, p } - appendSelectionPredicatePushDownTraceStep(p, opt) + appendSelectionPredicatePushDownTraceStep(p, originConditions, opt) return nil, child } @@ -652,7 +654,7 @@ func (*ppdSolver) name() string { } func appendTableDualTraceStep(replaced LogicalPlan, dual LogicalPlan, conditions []expression.Expression, opt *logicalOptimizeOp) { - action := fmt.Sprintf("%v[%v] is replaced by %v[%v]", replaced.TP(), replaced.ID(), dual.TP(), dual.ID()) + action := fmt.Sprintf("%v_%v is replaced by %v_%v", replaced.TP(), replaced.ID(), dual.TP(), dual.ID()) reason := func() string { buffer := bytes.NewBufferString("The conditions[") for i, cond := range conditions { @@ -667,18 +669,22 @@ func appendTableDualTraceStep(replaced LogicalPlan, dual LogicalPlan, conditions opt.appendStepToCurrent(dual.ID(), dual.TP(), reason, action) } -func appendSelectionPredicatePushDownTraceStep(p *LogicalSelection, opt *logicalOptimizeOp) { - action := fmt.Sprintf("%v[%v] is removed", p.TP(), p.ID()) - reason := func() string { - buffer := bytes.NewBufferString("Conditions[") - for i, cond := range p.Conditions { - if i > 0 { +func appendSelectionPredicatePushDownTraceStep(p *LogicalSelection, conditions []expression.Expression, opt *logicalOptimizeOp) { + action := fmt.Sprintf("%v_%v is removed", p.TP(), p.ID()) + reason := "" + if len(conditions) > 0 && !p.buildByHaving { + reason = func() string { + buffer := bytes.NewBufferString("The conditions[") + for i, cond := range conditions { + if i > 0 { + buffer.WriteString(",") + } buffer.WriteString(cond.String()) } - } - buffer.WriteString("] are pushed down") - return buffer.String() - }() + buffer.WriteString(fmt.Sprintf("] in %v_%v are pushed down", p.TP(), p.ID())) + return buffer.String() + }() + } opt.appendStepToCurrent(p.ID(), p.TP(), reason, action) } @@ -687,13 +693,14 @@ func appendDataSourcePredicatePushDownTraceStep(ds *DataSource, opt *logicalOpti return } action := func() string { - buffer := bytes.NewBufferString("Conditions[") + buffer := bytes.NewBufferString("The conditions[") for i, cond := range ds.pushedDownConds { if i > 0 { - buffer.WriteString(cond.String()) + buffer.WriteString(",") } + buffer.WriteString(cond.String()) } - buffer.WriteString(fmt.Sprintf("] are pushed down across %v[%v]", ds.TP(), ds.ID())) + buffer.WriteString(fmt.Sprintf("] are pushed down across %v_%v", ds.TP(), ds.ID())) return buffer.String() }() opt.appendStepToCurrent(ds.ID(), ds.TP(), "", action) From 7d631cb3cd6b77b80b4fa41c189ebeda57973c33 Mon Sep 17 00:00:00 2001 From: yisaer Date: Wed, 22 Dec 2021 16:26:40 +0800 Subject: [PATCH 3/4] fix conflict Signed-off-by: yisaer --- planner/core/rule_predicate_push_down.go | 36 ++++++++++++++++++------ 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/planner/core/rule_predicate_push_down.go b/planner/core/rule_predicate_push_down.go index a57cbe5547e56..feed34d7ee567 100644 --- a/planner/core/rule_predicate_push_down.go +++ b/planner/core/rule_predicate_push_down.go @@ -56,8 +56,7 @@ func addSelection(p LogicalPlan, child LogicalPlan, conditions []expression.Expr selection := LogicalSelection{Conditions: conditions}.Init(p.SCtx(), p.SelectBlockOffset()) selection.SetChildren(child) p.Children()[chIdx] = selection - opt.appendStepToCurrent(selection.ID(), selection.TP(), "", - fmt.Sprintf("add %v_%v to connect %v_%v and %v_%v", selection.TP(), selection.ID(), p.TP(), p.ID(), child.TP(), child.ID())) + appendAddSelectionTraceStep(p, child, selection, opt) } // PredicatePushDown implements LogicalPlan interface. @@ -654,7 +653,9 @@ func (*ppdSolver) name() string { } func appendTableDualTraceStep(replaced LogicalPlan, dual LogicalPlan, conditions []expression.Expression, opt *logicalOptimizeOp) { - action := fmt.Sprintf("%v_%v is replaced by %v_%v", replaced.TP(), replaced.ID(), dual.TP(), dual.ID()) + action := func() string { + return fmt.Sprintf("%v_%v is replaced by %v_%v", replaced.TP(), replaced.ID(), dual.TP(), dual.ID()) + } reason := func() string { buffer := bytes.NewBufferString("The conditions[") for i, cond := range conditions { @@ -665,13 +666,17 @@ func appendTableDualTraceStep(replaced LogicalPlan, dual LogicalPlan, conditions } buffer.WriteString("] are constant false or null") return buffer.String() - }() + } opt.appendStepToCurrent(dual.ID(), dual.TP(), reason, action) } func appendSelectionPredicatePushDownTraceStep(p *LogicalSelection, conditions []expression.Expression, opt *logicalOptimizeOp) { - action := fmt.Sprintf("%v_%v is removed", p.TP(), p.ID()) - reason := "" + action := func() string { + return fmt.Sprintf("%v_%v is removed", p.TP(), p.ID()) + } + reason := func() string { + return "" + } if len(conditions) > 0 && !p.buildByHaving { reason = func() string { buffer := bytes.NewBufferString("The conditions[") @@ -683,7 +688,7 @@ func appendSelectionPredicatePushDownTraceStep(p *LogicalSelection, conditions [ } buffer.WriteString(fmt.Sprintf("] in %v_%v are pushed down", p.TP(), p.ID())) return buffer.String() - }() + } } opt.appendStepToCurrent(p.ID(), p.TP(), reason, action) } @@ -692,6 +697,9 @@ func appendDataSourcePredicatePushDownTraceStep(ds *DataSource, opt *logicalOpti if len(ds.pushedDownConds) < 1 { return } + reason := func() string { + return "" + } action := func() string { buffer := bytes.NewBufferString("The conditions[") for i, cond := range ds.pushedDownConds { @@ -702,6 +710,16 @@ func appendDataSourcePredicatePushDownTraceStep(ds *DataSource, opt *logicalOpti } buffer.WriteString(fmt.Sprintf("] are pushed down across %v_%v", ds.TP(), ds.ID())) return buffer.String() - }() - opt.appendStepToCurrent(ds.ID(), ds.TP(), "", action) + } + opt.appendStepToCurrent(ds.ID(), ds.TP(), reason, action) +} + +func appendAddSelectionTraceStep(p LogicalPlan, child LogicalPlan, sel *LogicalSelection, opt *logicalOptimizeOp) { + reason := func() string { + return "" + } + action := func() string { + return fmt.Sprintf("add %v_%v to connect %v_%v and %v_%v", sel.TP(), sel.ID(), p.TP(), p.ID(), child.TP(), child.ID()) + } + opt.appendStepToCurrent(sel.ID(), sel.TP(), reason, action) } From 0332162a0dfc438eea037f2ff2e3cea3a7a2d21b Mon Sep 17 00:00:00 2001 From: yisaer Date: Fri, 24 Dec 2021 12:54:18 +0800 Subject: [PATCH 4/4] fix test Signed-off-by: yisaer --- planner/core/logical_plan_trace_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planner/core/logical_plan_trace_test.go b/planner/core/logical_plan_trace_test.go index 43dd15c5179e8..a315e9a23ea3f 100644 --- a/planner/core/logical_plan_trace_test.go +++ b/planner/core/logical_plan_trace_test.go @@ -97,7 +97,7 @@ func (s *testPlanSuite) TestSingleRuleTraceStep(c *C) { }, { assertReason: "", - assertAction: "The conditions[lt(test.t.a, 1)] are pushed down across DataSource_1", + assertAction: "The conditions[lt(test.t.a, 1)] are pushed down across DataSource_2", }, { assertAction: "Selection_4 is removed",