-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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: support join elimination rule #8021
Merged
Merged
Changes from 7 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
6820811
initialization
lzmhhh123 fa10389
debug
lzmhhh123 3394288
add tests
lzmhhh123 64996b5
Merge branch 'master' into dev/join_elimination
lzmhhh123 48df0c1
Update explain_easy.result
lzmhhh123 11fe6a7
Update explain_easy.result
lzmhhh123 05bc4eb
debug
lzmhhh123 438fafb
address comment
lzmhhh123 27f2b06
address comments
lzmhhh123 80b9125
Merge branch 'master' into dev/join_elimination
lzmhhh123 1b4903e
fix
lzmhhh123 9842cad
Merge branch 'dev/join_elimination' of https://github.com/lzmhhh123/t…
lzmhhh123 53d18e4
address comments
lzmhhh123 2cb071f
address comment
lzmhhh123 01649a1
change join eliminating rule order
lzmhhh123 4b4936d
Merge branch 'master' into dev/join_elimination
zz-jason 83b589c
change rule order
lzmhhh123 89a6bb7
Merge branch 'master' into dev/join_elimination
lzmhhh123 155445e
address
lzmhhh123 129de34
Merge branch 'dev/join_elimination' of https://github.com/lzmhhh123/t…
lzmhhh123 91a5cdb
fix count args
lzmhhh123 03ad39a
add comments
lzmhhh123 d5a4164
improve
lzmhhh123 122f438
address comments
lzmhhh123 3c93712
ci
lzmhhh123 8fff6e8
address comments
lzmhhh123 684fd29
fix
lzmhhh123 f1de131
Merge branch 'master' into dev/join_elimination
zz-jason 71e757d
Merge branch 'master' into dev/join_elimination
zz-jason File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Copyright 2018 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package core | ||
|
||
import ( | ||
"github.com/pingcap/tidb/ast" | ||
"github.com/pingcap/tidb/expression" | ||
) | ||
|
||
type outerJoinEliminator struct { | ||
hasDistinct int | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cols [][]*expression.Column | ||
schemas []*expression.Schema | ||
} | ||
|
||
// tryToEliminateOuterJoin will eliminate outer join plan base on the following rules | ||
// 1. outer join elimination: For example left outer join, if the parent only use the | ||
// columns from left table and the join key of right table(the inner table) is a unique | ||
// key of the right table. the left outer join can be eliminated. | ||
// 2. outer join elimination with distinct: For example left outer join. If the parent | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// only use the columns from left table with 'distinct' label. The left outer join can | ||
// be eliminated. | ||
func (o *outerJoinEliminator) tryToEliminateOuterJoin(p *LogicalJoin) LogicalPlan { | ||
if len(p.children) != 2 { | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil | ||
} | ||
switch p.JoinType { | ||
case LeftOuterJoin: | ||
return o.doEliminate(p, 1) | ||
case RightOuterJoin: | ||
return o.doEliminate(p, 0) | ||
default: | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil | ||
} | ||
} | ||
|
||
func (o *outerJoinEliminator) doEliminate(p *LogicalJoin, isLeft int) LogicalPlan { | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// outer join elimination with distinct | ||
if o.hasDistinct > 0 { | ||
cols := o.cols[o.hasDistinct-1] | ||
allColsInSchema := true | ||
for _, col := range cols { | ||
if !p.children[1^isLeft].Schema().Contains(col) { | ||
allColsInSchema = false | ||
break | ||
} | ||
} | ||
if allColsInSchema == true { | ||
return p.children[1^isLeft] | ||
} | ||
} | ||
|
||
// outer join elimination without distinct | ||
// first, check whether the parent's schema columns are all in left or right | ||
if len(o.schemas) < 1 { | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil | ||
} | ||
for _, col := range o.schemas[len(o.schemas)-1].Columns { | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
columnName := &ast.ColumnName{Schema: col.DBName, Table: col.TblName, Name: col.ColName} | ||
if c, _ := p.children[1^isLeft].Schema().FindColumn(columnName); c == nil { | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil | ||
} | ||
} | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// second, check whether the other side join keys are unique keys | ||
for _, keyInfo := range p.children[isLeft].Schema().Keys { | ||
keyInfoContainAllCols := true | ||
var joinKeys []*expression.Column | ||
if isLeft > 0 { | ||
joinKeys = p.RightJoinKeys | ||
} else { | ||
joinKeys = p.LeftJoinKeys | ||
} | ||
for _, col := range joinKeys { | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
columnName := &ast.ColumnName{Schema: col.DBName, Table: col.TblName, Name: col.ColName} | ||
if c, _ := expression.NewSchema(keyInfo...).FindColumn(columnName); c == nil { | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
keyInfoContainAllCols = false | ||
break | ||
} | ||
} | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if keyInfoContainAllCols { | ||
return p.children[1^isLeft] | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (o *outerJoinEliminator) optimize(p LogicalPlan) (LogicalPlan, error) { | ||
// check the distinct | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if agg, ok := p.(*LogicalAggregation); ok && len(agg.groupByCols) > 0 { | ||
isDistinctAgg := true | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for _, aggDesc := range agg.AggFuncs { | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if aggDesc.Name != ast.AggFuncFirstRow { | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
isDistinctAgg = false | ||
break | ||
} | ||
if len(aggDesc.Args) != 1 { | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
isDistinctAgg = false | ||
break | ||
} | ||
if _, ok := aggDesc.Args[0].(*expression.Column); !ok { | ||
isDistinctAgg = false | ||
break | ||
} | ||
} | ||
if isDistinctAgg { | ||
o.hasDistinct++ | ||
o.cols = append(o.cols, agg.groupByCols) | ||
defer func() { | ||
o.hasDistinct-- | ||
o.cols = o.cols[:o.hasDistinct] | ||
}() | ||
} | ||
} | ||
|
||
newChildren := make([]LogicalPlan, 0, len(p.Children())) | ||
for _, child := range p.Children() { | ||
// if child is logical join, then save the parent schema | ||
if _, ok := child.(*LogicalJoin); ok { | ||
o.schemas = append(o.schemas, p.Schema()) | ||
defer func() { | ||
o.schemas = o.schemas[:len(o.schemas)-1] | ||
}() | ||
eurekaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
newChild, _ := o.optimize(child) | ||
newChildren = append(newChildren, newChild) | ||
} | ||
p.SetChildren(newChildren...) | ||
join, ok := p.(*LogicalJoin) | ||
if !ok { | ||
return p, nil | ||
} | ||
if proj := o.tryToEliminateOuterJoin(join); proj != nil { | ||
return proj, nil | ||
} | ||
return p, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the optimization phase, the semi joins are also considered, I think we should add the optimization flag for these semi joins in here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and please add some explain tests to cover these optimizations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After my seriously considering. I think semi-joins can't be eliminated in any case. So I will remove semi-join in the eliminating rule.