Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: Add trace for proj elimination rule #30275

Merged
merged 3 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fmt.Sprintf("Proj[%v] is eliminated,Proj[%v]'s expressions changed into[", child.ID(), parent.ID()))
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)
}