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 join eliminate rule #30343

Merged
merged 9 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
22 changes: 22 additions & 0 deletions planner/core/logical_plan_trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,28 @@ func (s *testPlanSuite) TestSingleRuleTraceStep(c *C) {
},
},
},
{
sql: "select t1.b,t1.c from t as t1 left join t as t2 on t1.a = t2.a;",
flags: []uint64{flagBuildKeyInfo, flagEliminateOuterJoin},
assertRuleName: "outer_join_eliminate",
assertRuleSteps: []assertTraceStep{
{
assertAction: "Outer join[3] is eliminated and become DataSource[1]",
assertReason: "The columns[test.t.b,test.t.c] are from outer table, and the inner join keys[test.t.a] are unique",
},
},
},
{
sql: "select count(distinct t1.a, t1.b) from t t1 left join t t2 on t1.b = t2.b",
flags: []uint64{flagPrunColumns, flagBuildKeyInfo, flagEliminateOuterJoin},
assertRuleName: "outer_join_eliminate",
assertRuleSteps: []assertTraceStep{
{
assertAction: "Outer join[3] is eliminated and become DataSource[1]",
assertReason: "The columns[test.t.a,test.t.b] in agg are from outer table, and the agg functions are duplicate agnostic",
},
},
},
}

for i, tc := range tt {
Expand Down
55 changes: 50 additions & 5 deletions planner/core/rule_join_elimination.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/parser/ast"
Expand All @@ -32,7 +34,7 @@ type outerJoinEliminator struct {
// 2. outer join elimination with duplicate agnostic aggregate functions: For example left outer join.
// If the parent only use the columns from left table with 'distinct' label. The left outer join can
// be eliminated.
func (o *outerJoinEliminator) tryToEliminateOuterJoin(p *LogicalJoin, aggCols []*expression.Column, parentCols []*expression.Column) (LogicalPlan, bool, error) {
func (o *outerJoinEliminator) tryToEliminateOuterJoin(p *LogicalJoin, aggCols []*expression.Column, parentCols []*expression.Column, opt *logicalOptimizeOp) (LogicalPlan, bool, error) {
var innerChildIdx int
switch p.JoinType {
case LeftOuterJoin:
Expand All @@ -56,6 +58,7 @@ func (o *outerJoinEliminator) tryToEliminateOuterJoin(p *LogicalJoin, aggCols []
// outer join elimination with duplicate agnostic aggregate functions
matched = IsColsAllFromOuterTable(aggCols, outerUniqueIDs)
if matched {
appendOuterJoinEliminateAggregationTraceStep(p, outerPlan, aggCols, opt)
return outerPlan, true, nil
}
// outer join elimination without duplicate agnostic aggregate functions
Expand All @@ -65,13 +68,15 @@ func (o *outerJoinEliminator) tryToEliminateOuterJoin(p *LogicalJoin, aggCols []
return p, false, err
}
if contain {
appendOuterJoinEliminateTraceStep(p, outerPlan, parentCols, innerJoinKeys, opt)
return outerPlan, true, nil
}
contain, err = o.isInnerJoinKeysContainIndex(innerPlan, innerJoinKeys)
if err != nil {
return p, false, err
}
if contain {
appendOuterJoinEliminateTraceStep(p, outerPlan, parentCols, innerJoinKeys, opt)
return outerPlan, true, nil
}

Expand Down Expand Up @@ -178,11 +183,11 @@ func GetDupAgnosticAggCols(
return true, newAggCols
}

func (o *outerJoinEliminator) doOptimize(p LogicalPlan, aggCols []*expression.Column, parentCols []*expression.Column) (LogicalPlan, error) {
func (o *outerJoinEliminator) doOptimize(p LogicalPlan, aggCols []*expression.Column, parentCols []*expression.Column, opt *logicalOptimizeOp) (LogicalPlan, error) {
var err error
var isEliminated bool
for join, isJoin := p.(*LogicalJoin); isJoin; join, isJoin = p.(*LogicalJoin) {
p, isEliminated, err = o.tryToEliminateOuterJoin(join, aggCols, parentCols)
p, isEliminated, err = o.tryToEliminateOuterJoin(join, aggCols, parentCols, opt)
if err != nil {
return p, err
}
Expand Down Expand Up @@ -216,7 +221,7 @@ func (o *outerJoinEliminator) doOptimize(p LogicalPlan, aggCols []*expression.Co
}

for i, child := range p.Children() {
newChild, err := o.doOptimize(child, aggCols, parentCols)
newChild, err := o.doOptimize(child, aggCols, parentCols, opt)
if err != nil {
return nil, err
}
Expand All @@ -226,10 +231,50 @@ func (o *outerJoinEliminator) doOptimize(p LogicalPlan, aggCols []*expression.Co
}

func (o *outerJoinEliminator) optimize(ctx context.Context, p LogicalPlan, opt *logicalOptimizeOp) (LogicalPlan, error) {
p, err := o.doOptimize(p, nil, nil)
p, err := o.doOptimize(p, nil, nil, opt)
return p, err
}

func (*outerJoinEliminator) name() string {
return "outer_join_eliminate"
}

func appendOuterJoinEliminateTraceStep(join *LogicalJoin, outerPlan LogicalPlan, parentCols []*expression.Column,
innerJoinKeys *expression.Schema, opt *logicalOptimizeOp) {
reason := func() string {
buffer := bytes.NewBufferString("The columns[")
for i, col := range parentCols {
if i > 0 {
buffer.WriteString(",")
}
buffer.WriteString(col.String())
}
buffer.WriteString("] are from outer table, and the inner join keys[")
for i, key := range innerJoinKeys.Columns {
if i > 0 {
buffer.WriteString(",")
}
buffer.WriteString(key.String())
}
buffer.WriteString("] are unique")
return buffer.String()
}()
action := fmt.Sprintf("Outer join[%v] is eliminated and become %v[%v]", join.ID(), outerPlan.TP(), outerPlan.ID())
opt.appendStepToCurrent(join.ID(), join.TP(), reason, action)
}

func appendOuterJoinEliminateAggregationTraceStep(join *LogicalJoin, outerPlan LogicalPlan, aggCols []*expression.Column, opt *logicalOptimizeOp) {
reason := func() string {
buffer := bytes.NewBufferString("The columns[")
for i, col := range aggCols {
if i > 0 {
buffer.WriteString(",")
}
buffer.WriteString(col.String())
}
buffer.WriteString("] in agg are from outer table, and the agg functions are duplicate agnostic")
return buffer.String()
}()
action := fmt.Sprintf("Outer join[%v] is eliminated and become %v[%v]", join.ID(), outerPlan.TP(), outerPlan.ID())
opt.appendStepToCurrent(join.ID(), join.TP(), reason, action)
}