/
rule_join_elimination.go
287 lines (267 loc) · 9.36 KB
/
rule_join_elimination.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package core
import (
"bytes"
"context"
"fmt"
"github.com/wuhuizuo/tidb6/expression"
"github.com/wuhuizuo/tidb6/parser/ast"
"github.com/wuhuizuo/tidb6/util/set"
)
type outerJoinEliminator struct {
}
// 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, aggCols []*expression.Column, parentCols []*expression.Column, opt *logicalOptimizeOp) (LogicalPlan, bool, error) {
var innerChildIdx int
switch p.JoinType {
case LeftOuterJoin:
innerChildIdx = 1
case RightOuterJoin:
innerChildIdx = 0
default:
return p, false, nil
}
outerPlan := p.children[1^innerChildIdx]
innerPlan := p.children[innerChildIdx]
outerUniqueIDs := set.NewInt64Set()
for _, outerCol := range outerPlan.Schema().Columns {
outerUniqueIDs.Insert(outerCol.UniqueID)
}
matched := IsColsAllFromOuterTable(parentCols, outerUniqueIDs)
if !matched {
return p, false, nil
}
// 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
innerJoinKeys := o.extractInnerJoinKeys(p, innerChildIdx)
contain, err := o.isInnerJoinKeysContainUniqueKey(innerPlan, innerJoinKeys)
if err != nil {
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
}
return p, false, nil
}
// extract join keys as a schema for inner child of a outer join
func (*outerJoinEliminator) extractInnerJoinKeys(join *LogicalJoin, innerChildIdx int) *expression.Schema {
joinKeys := make([]*expression.Column, 0, len(join.EqualConditions))
for _, eqCond := range join.EqualConditions {
joinKeys = append(joinKeys, eqCond.GetArgs()[innerChildIdx].(*expression.Column))
}
return expression.NewSchema(joinKeys...)
}
// IsColsAllFromOuterTable check whether the cols all from outer plan
func IsColsAllFromOuterTable(cols []*expression.Column, outerUniqueIDs set.Int64Set) bool {
// There are two cases "return false" here:
// 1. If cols represents aggCols, then "len(cols) == 0" means not all aggregate functions are duplicate agnostic before.
// 2. If cols represents parentCols, then "len(cols) == 0" means no parent logical plan of this join plan.
if len(cols) == 0 {
return false
}
for _, col := range cols {
if !outerUniqueIDs.Exist(col.UniqueID) {
return false
}
}
return true
}
// check whether one of unique keys sets is contained by inner join keys
func (*outerJoinEliminator) isInnerJoinKeysContainUniqueKey(innerPlan LogicalPlan, joinKeys *expression.Schema) (bool, error) {
for _, keyInfo := range innerPlan.Schema().Keys {
joinKeysContainKeyInfo := true
for _, col := range keyInfo {
if !joinKeys.Contains(col) {
joinKeysContainKeyInfo = false
break
}
}
if joinKeysContainKeyInfo {
return true, nil
}
}
return false, nil
}
// check whether one of index sets is contained by inner join index
func (*outerJoinEliminator) isInnerJoinKeysContainIndex(innerPlan LogicalPlan, joinKeys *expression.Schema) (bool, error) {
ds, ok := innerPlan.(*DataSource)
if !ok {
return false, nil
}
for _, path := range ds.possibleAccessPaths {
if path.IsIntHandlePath || !path.Index.Unique || len(path.IdxCols) == 0 {
continue
}
joinKeysContainIndex := true
for _, idxCol := range path.IdxCols {
if !joinKeys.Contains(idxCol) {
joinKeysContainIndex = false
break
}
}
if joinKeysContainIndex {
return true, nil
}
}
return false, nil
}
// GetDupAgnosticAggCols checks whether a LogicalPlan is LogicalAggregation.
// It extracts all the columns from the duplicate agnostic aggregate functions.
// The returned column set is nil if not all the aggregate functions are duplicate agnostic.
// Only the following functions are considered to be duplicate agnostic:
// 1. MAX(arg)
// 2. MIN(arg)
// 3. FIRST_ROW(arg)
// 4. Other agg functions with DISTINCT flag, like SUM(DISTINCT arg)
func GetDupAgnosticAggCols(
p LogicalPlan,
oldAggCols []*expression.Column, // Reuse the original buffer.
) (isAgg bool, newAggCols []*expression.Column) {
agg, ok := p.(*LogicalAggregation)
if !ok {
return false, nil
}
newAggCols = oldAggCols[:0]
for _, aggDesc := range agg.AggFuncs {
if !aggDesc.HasDistinct &&
aggDesc.Name != ast.AggFuncFirstRow &&
aggDesc.Name != ast.AggFuncMax &&
aggDesc.Name != ast.AggFuncMin &&
aggDesc.Name != ast.AggFuncApproxCountDistinct {
// If not all aggregate functions are duplicate agnostic,
// we should clean the aggCols, so `return true, newAggCols[:0]`.
return true, newAggCols[:0]
}
for _, expr := range aggDesc.Args {
newAggCols = append(newAggCols, expression.ExtractColumns(expr)...)
}
}
return true, newAggCols
}
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, opt)
if err != nil {
return p, err
}
if !isEliminated {
break
}
}
switch x := p.(type) {
case *LogicalProjection:
parentCols = parentCols[:0]
for _, expr := range x.Exprs {
parentCols = append(parentCols, expression.ExtractColumns(expr)...)
}
case *LogicalAggregation:
parentCols = parentCols[:0]
for _, groupByItem := range x.GroupByItems {
parentCols = append(parentCols, expression.ExtractColumns(groupByItem)...)
}
for _, aggDesc := range x.AggFuncs {
for _, expr := range aggDesc.Args {
parentCols = append(parentCols, expression.ExtractColumns(expr)...)
}
for _, byItem := range aggDesc.OrderByItems {
parentCols = append(parentCols, expression.ExtractColumns(byItem.Expr)...)
}
}
default:
parentCols = append(parentCols[:0], p.Schema().Columns...)
}
if ok, newCols := GetDupAgnosticAggCols(p, aggCols); ok {
aggCols = newCols
}
for i, child := range p.Children() {
newChild, err := o.doOptimize(child, aggCols, parentCols, opt)
if err != nil {
return nil, err
}
p.SetChild(i, newChild)
}
return p, nil
}
func (o *outerJoinEliminator) optimize(_ context.Context, p LogicalPlan, opt *logicalOptimizeOp) (LogicalPlan, error) {
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 := func() string {
return fmt.Sprintf("Outer %v_%v is eliminated and become %v_%v", join.TP(), 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 := func() string {
return fmt.Sprintf("Outer %v_%v is eliminated and become %v_%v", join.TP(), join.ID(), outerPlan.TP(), outerPlan.ID())
}
opt.appendStepToCurrent(join.ID(), join.TP(), reason, action)
}