Skip to content

Commit be98b19

Browse files
AilinKidzeminzhou
authored andcommitted
planner : refactor the usage of int set. (pingcap#59346)
ref pingcap#51664
1 parent 3633fe3 commit be98b19

File tree

5 files changed

+31
-28
lines changed

5 files changed

+31
-28
lines changed

pkg/planner/cascades/old/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ go_library(
3030
"//pkg/planner/util/coreusage",
3131
"//pkg/types",
3232
"//pkg/util/dbterror/plannererrors",
33+
"//pkg/util/intset",
3334
"//pkg/util/ranger",
34-
"//pkg/util/set",
3535
"@com_github_pingcap_errors//:errors",
3636
],
3737
)

pkg/planner/cascades/old/transformation_rules.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import (
3232
"github.com/pingcap/tidb/pkg/planner/util"
3333
"github.com/pingcap/tidb/pkg/planner/util/coreusage"
3434
"github.com/pingcap/tidb/pkg/types"
35+
"github.com/pingcap/tidb/pkg/util/intset"
3536
"github.com/pingcap/tidb/pkg/util/ranger"
36-
"github.com/pingcap/tidb/pkg/util/set"
3737
)
3838

3939
// Transformation defines the interface for the transformation rules.
@@ -1876,7 +1876,7 @@ func (r *PushLimitDownTiKVSingleGather) OnTransform(old *memo.ExprIter) (newExpr
18761876
type outerJoinEliminator struct {
18771877
}
18781878

1879-
func (*outerJoinEliminator) prepareForEliminateOuterJoin(joinExpr *memo.GroupExpr) (ok bool, innerChildIdx int, outerGroup *memo.Group, innerGroup *memo.Group, outerUniqueIDs set.Int64Set) {
1879+
func (*outerJoinEliminator) prepareForEliminateOuterJoin(joinExpr *memo.GroupExpr) (ok bool, innerChildIdx int, outerGroup *memo.Group, innerGroup *memo.Group, outerUniqueIDs intset.FastIntSet) {
18801880
join := joinExpr.ExprNode.(*logicalop.LogicalJoin)
18811881

18821882
switch join.JoinType {
@@ -1891,9 +1891,9 @@ func (*outerJoinEliminator) prepareForEliminateOuterJoin(joinExpr *memo.GroupExp
18911891
outerGroup = joinExpr.Children[1^innerChildIdx]
18921892
innerGroup = joinExpr.Children[innerChildIdx]
18931893

1894-
outerUniqueIDs = set.NewInt64Set()
1894+
outerUniqueIDs = intset.NewFastIntSet()
18951895
for _, outerCol := range outerGroup.Prop.Schema.Columns {
1896-
outerUniqueIDs.Insert(outerCol.UniqueID)
1896+
outerUniqueIDs.Insert(int(outerCol.UniqueID))
18971897
}
18981898

18991899
ok = true
@@ -1956,7 +1956,7 @@ func (r *EliminateOuterJoinBelowAggregation) OnTransform(old *memo.ExprIter) (ne
19561956
}
19571957

19581958
// only when agg only use the columns from outer table can eliminate outer join.
1959-
if !plannercore.IsColsAllFromOuterTable(agg.GetUsedCols(), outerUniqueIDs) {
1959+
if !ruleutil.IsColsAllFromOuterTable(agg.GetUsedCols(), &outerUniqueIDs) {
19601960
return nil, false, false, nil
19611961
}
19621962
// outer join elimination with duplicate agnostic aggregate functions.
@@ -2018,7 +2018,7 @@ func (r *EliminateOuterJoinBelowProjection) OnTransform(old *memo.ExprIter) (new
20182018
}
20192019

20202020
// only when proj only use the columns from outer table can eliminate outer join.
2021-
if !plannercore.IsColsAllFromOuterTable(proj.GetUsedCols(), outerUniqueIDs) {
2021+
if !ruleutil.IsColsAllFromOuterTable(proj.GetUsedCols(), &outerUniqueIDs) {
20222022
return nil, false, false, nil
20232023
}
20242024

pkg/planner/core/rule/util/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ go_library(
1313
"//pkg/meta/model",
1414
"//pkg/parser/mysql",
1515
"//pkg/planner/core/base",
16+
"//pkg/util/intset",
1617
],
1718
)

pkg/planner/core/rule/util/misc.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package util
1616

1717
import (
1818
"github.com/pingcap/tidb/pkg/expression"
19+
"github.com/pingcap/tidb/pkg/util/intset"
1920
)
2021

2122
// ResolveExprAndReplace replaces columns fields of expressions by children logical plans.
@@ -42,5 +43,21 @@ func ResolveColumnAndReplace(origin *expression.Column, replace map[string]*expr
4243
}
4344
}
4445

46+
// IsColsAllFromOuterTable check whether the cols all from outer plan
47+
func IsColsAllFromOuterTable(cols []*expression.Column, outerUniqueIDs *intset.FastIntSet) bool {
48+
// There are two cases "return false" here:
49+
// 1. If cols represents aggCols, then "len(cols) == 0" means not all aggregate functions are duplicate agnostic before.
50+
// 2. If cols represents parentCols, then "len(cols) == 0" means no parent logical plan of this join plan.
51+
if len(cols) == 0 {
52+
return false
53+
}
54+
for _, col := range cols {
55+
if !outerUniqueIDs.Has(int(col.UniqueID)) {
56+
return false
57+
}
58+
}
59+
return true
60+
}
61+
4562
// SetPredicatePushDownFlag is a hook for other packages to set rule flag.
4663
var SetPredicatePushDownFlag func(uint64) uint64

pkg/planner/core/rule_join_elimination.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ import (
2424
"github.com/pingcap/tidb/pkg/parser/ast"
2525
"github.com/pingcap/tidb/pkg/planner/core/base"
2626
"github.com/pingcap/tidb/pkg/planner/core/operator/logicalop"
27+
ruleutil "github.com/pingcap/tidb/pkg/planner/core/rule/util"
2728
"github.com/pingcap/tidb/pkg/planner/util/optimizetrace"
28-
"github.com/pingcap/tidb/pkg/util/set"
29+
"github.com/pingcap/tidb/pkg/util/intset"
2930
)
3031

3132
// OuterJoinEliminator is used to eliminate outer join.
@@ -52,24 +53,24 @@ func (o *OuterJoinEliminator) tryToEliminateOuterJoin(p *logicalop.LogicalJoin,
5253

5354
outerPlan := p.Children()[1^innerChildIdx]
5455
innerPlan := p.Children()[innerChildIdx]
55-
outerUniqueIDs := set.NewInt64Set()
56+
outerUniqueIDs := intset.NewFastIntSet()
5657
for _, outerCol := range outerPlan.Schema().Columns {
57-
outerUniqueIDs.Insert(outerCol.UniqueID)
58+
outerUniqueIDs.Insert(int(outerCol.UniqueID))
5859
}
5960

6061
// in case of count(*) FROM R LOJ S, the parentCols is empty, but
6162
// still need to proceed to check whether we can eliminate outer join.
6263
// In fact, we only care about whether there is any column from inner
6364
// table, if there is none, we are good.
6465
if len(parentCols) > 0 {
65-
matched := IsColsAllFromOuterTable(parentCols, outerUniqueIDs)
66+
matched := ruleutil.IsColsAllFromOuterTable(parentCols, &outerUniqueIDs)
6667
if !matched {
6768
return p, false, nil
6869
}
6970
}
7071

7172
// outer join elimination with duplicate agnostic aggregate functions
72-
matched := IsColsAllFromOuterTable(aggCols, outerUniqueIDs)
73+
matched := ruleutil.IsColsAllFromOuterTable(aggCols, &outerUniqueIDs)
7374
if matched {
7475
appendOuterJoinEliminateAggregationTraceStep(p, outerPlan, aggCols, opt)
7576
return outerPlan, true, nil
@@ -105,22 +106,6 @@ func (*OuterJoinEliminator) extractInnerJoinKeys(join *logicalop.LogicalJoin, in
105106
return expression.NewSchema(joinKeys...)
106107
}
107108

108-
// IsColsAllFromOuterTable check whether the cols all from outer plan
109-
func IsColsAllFromOuterTable(cols []*expression.Column, outerUniqueIDs set.Int64Set) bool {
110-
// There are two cases "return false" here:
111-
// 1. If cols represents aggCols, then "len(cols) == 0" means not all aggregate functions are duplicate agnostic before.
112-
// 2. If cols represents parentCols, then "len(cols) == 0" means no parent logical plan of this join plan.
113-
if len(cols) == 0 {
114-
return false
115-
}
116-
for _, col := range cols {
117-
if !outerUniqueIDs.Exist(col.UniqueID) {
118-
return false
119-
}
120-
}
121-
return true
122-
}
123-
124109
// check whether one of unique keys sets is contained by inner join keys
125110
func (*OuterJoinEliminator) isInnerJoinKeysContainUniqueKey(innerPlan base.LogicalPlan, joinKeys *expression.Schema) (bool, error) {
126111
for _, keyInfo := range innerPlan.Schema().PKOrUK {

0 commit comments

Comments
 (0)