-
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 22 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,184 @@ | ||
// 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/parser/ast" | ||
"github.com/pingcap/tidb/expression" | ||
) | ||
|
||
type outerJoinEliminator struct { | ||
// save the duplicate agnostic aggregate function's args in recursion | ||
cols [][]*expression.Column | ||
// save the LogicalJoin's parent schema in recursion | ||
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 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) LogicalPlan { | ||
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, innerChildIdx int) LogicalPlan { | ||
// outer join elimination with duplicate agnostic aggregate functions | ||
if len(o.cols) > 0 { | ||
cols := o.cols[len(o.cols)-1] | ||
allColsInSchema := true | ||
for _, col := range cols { | ||
columnName := &ast.ColumnName{Schema: col.DBName, Table: col.TblName, Name: col.ColName} | ||
if c, _ := p.children[1^innerChildIdx].Schema().FindColumn(columnName); c == nil { | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
allColsInSchema = false | ||
break | ||
} | ||
} | ||
if allColsInSchema == true { | ||
return p.children[1^innerChildIdx] | ||
} | ||
} | ||
|
||
// outer join elimination without duplicate agnostic aggregate functions | ||
// first, check whether the parent's schema columns are all in left or right | ||
if len(o.schemas) == 0 { | ||
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^innerChildIdx].Schema().FindColumn(columnName); c == nil { | ||
return nil | ||
} | ||
} | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// second, check whether the other side join keys are unique keys | ||
p.children[innerChildIdx].buildKeyInfo() | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var joinKeys []*expression.Column | ||
for _, eqCond := range p.EqualConditions { | ||
joinKeys = append(joinKeys, eqCond.GetArgs()[innerChildIdx].(*expression.Column)) | ||
} | ||
tmpSchema := expression.NewSchema(joinKeys...) | ||
for _, keyInfo := range p.children[innerChildIdx].Schema().Keys { | ||
joinKeysContainKeyInfo := true | ||
for _, col := range keyInfo { | ||
columnName := &ast.ColumnName{Schema: col.DBName, Table: col.TblName, Name: col.ColName} | ||
if c, _ := tmpSchema.FindColumn(columnName); c == nil { | ||
zz-jason marked this conversation as resolved.
Show resolved
Hide resolved
|
||
joinKeysContainKeyInfo = false | ||
break | ||
} | ||
} | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if joinKeysContainKeyInfo { | ||
return p.children[1^innerChildIdx] | ||
} | ||
} | ||
// Third, if p.children[innerChildIdx] is datasource, we must check specially index. | ||
// Because buildKeyInfo() don't save the index without notnull flag. | ||
// But in outer join, null==null don't return true. The null index do not affect the res. | ||
if ds, ok := p.children[innerChildIdx].(*DataSource); ok { | ||
eurekaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for _, path := range ds.possibleAccessPaths { | ||
if path.isTablePath { | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
continue | ||
} | ||
idx := path.index | ||
if !idx.Unique { | ||
continue | ||
} | ||
joinKeysContainIndex := true | ||
for _, idxCol := range idx.Columns { | ||
columnName := &ast.ColumnName{Schema: ds.DBName, Table: ds.tableInfo.Name, Name: idxCol.Name} | ||
if c, _ := tmpSchema.FindColumn(columnName); c == nil { | ||
joinKeysContainIndex = false | ||
break | ||
} | ||
} | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if joinKeysContainIndex { | ||
return p.children[1^innerChildIdx] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we remove other possible paths in this case? |
||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Check whether a LogicalPlan is a LogicalAggregation and its all aggregate functions is duplicate aggnostic. | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Also, check all the args are expression.Column. | ||
func (o *outerJoinEliminator) isDuplicateAgnosticAgg(p LogicalPlan) (isDuplicateAgnosticAgg bool, cols []*expression.Column) { | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if agg, ok := p.(*LogicalAggregation); ok { | ||
XuHuaiyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
isDuplicateAgnosticAgg = true | ||
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
|
||
aggDesc.Name != ast.AggFuncMax && | ||
aggDesc.Name != ast.AggFuncMin && | ||
(aggDesc.Name != ast.AggFuncAvg || !aggDesc.HasDistinct) && | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(aggDesc.Name != ast.AggFuncSum || !aggDesc.HasDistinct) && | ||
(aggDesc.Name != ast.AggFuncCount || !aggDesc.HasDistinct) { | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
isDuplicateAgnosticAgg = false | ||
break | ||
} | ||
for _, expr := range aggDesc.Args { | ||
if col, ok := expr.(*expression.Column); ok { | ||
cols = append(cols, col) | ||
} else { | ||
isDuplicateAgnosticAgg = false | ||
break | ||
} | ||
} | ||
if !isDuplicateAgnosticAgg { | ||
lzmhhh123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break | ||
} | ||
} | ||
return | ||
} | ||
return false, nil | ||
} | ||
|
||
func (o *outerJoinEliminator) optimize(p LogicalPlan) (LogicalPlan, error) { | ||
// check the duplicate agnostic aggregate functions | ||
if ok, cols := o.isDuplicateAgnosticAgg(p); ok { | ||
o.cols = append(o.cols, cols) | ||
defer func() { | ||
o.cols = o.cols[:len(o.cols)-1] | ||
}() | ||
} | ||
|
||
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.