Skip to content

Commit

Permalink
planner: Add trace for proj elimination rule (#30275)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yisaer committed Dec 1, 2021
1 parent a90344c commit a046014
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
11 changes: 11 additions & 0 deletions planner/core/logical_plan_trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ func (s *testPlanSuite) TestSingleRuleTraceStep(c *C) {
},
},
},
{
sql: "select 1+num from (select 1+a as num from t) t1;",
flags: []uint64{flagEliminateProjection},
assertRuleName: "projection_eliminate",
assertRuleSteps: []assertTraceStep{
{
assertAction: "Proj[2] is eliminated, Proj[3]'s expressions changed into[plus(1, plus(1, test.t.a))]",
assertReason: "Proj[3]'s child proj[2] is redundant",
},
},
},
}

for i, tc := range tt {
Expand Down
26 changes: 23 additions & 3 deletions planner/core/rule_eliminate_projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package core

import (
"bytes"
"context"
"fmt"

"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -146,12 +148,12 @@ type projectionEliminator struct {

// optimize implements the logicalOptRule interface.
func (pe *projectionEliminator) optimize(ctx context.Context, lp LogicalPlan, opt *logicalOptimizeOp) (LogicalPlan, error) {
root := pe.eliminate(lp, make(map[string]*expression.Column), false)
root := pe.eliminate(lp, make(map[string]*expression.Column), false, opt)
return root, nil
}

// eliminate eliminates the redundant projection in a logical plan.
func (pe *projectionEliminator) eliminate(p LogicalPlan, replace map[string]*expression.Column, canEliminate bool) LogicalPlan {
func (pe *projectionEliminator) eliminate(p LogicalPlan, replace map[string]*expression.Column, canEliminate bool, opt *logicalOptimizeOp) LogicalPlan {
proj, isProj := p.(*LogicalProjection)
childFlag := canEliminate
if _, isUnion := p.(*LogicalUnionAll); isUnion {
Expand All @@ -162,7 +164,7 @@ func (pe *projectionEliminator) eliminate(p LogicalPlan, replace map[string]*exp
childFlag = true
}
for i, child := range p.Children() {
p.Children()[i] = pe.eliminate(child, replace, childFlag)
p.Children()[i] = pe.eliminate(child, replace, childFlag, opt)
}

switch x := p.(type) {
Expand All @@ -186,6 +188,7 @@ func (pe *projectionEliminator) eliminate(p LogicalPlan, replace map[string]*exp
proj.Exprs[i] = foldedExpr
}
p.Children()[0] = child.Children()[0]
appendProjEliminateTraceStep(proj, child, opt)
}
}

Expand Down Expand Up @@ -292,3 +295,20 @@ func (p *LogicalWindow) replaceExprColumns(replace map[string]*expression.Column
func (*projectionEliminator) name() string {
return "projection_eliminate"
}

func appendProjEliminateTraceStep(parent, child *LogicalProjection, opt *logicalOptimizeOp) {
action := func() string {
buffer := bytes.NewBufferString(
fmt.Sprintf("Proj[%v] is eliminated, Proj[%v]'s expressions changed into[", child.ID(), parent.ID()))
for i, expr := range parent.Exprs {
if i > 0 {
buffer.WriteString(",")
}
buffer.WriteString(expr.String())
}
buffer.WriteString("]")
return buffer.String()
}()
reason := fmt.Sprintf("Proj[%v]'s child proj[%v] is redundant", parent.ID(), child.ID())
opt.appendStepToCurrent(child.ID(), child.TP(), reason, action)
}

0 comments on commit a046014

Please sign in to comment.