-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
resolve_indices.go
262 lines (240 loc) · 7.29 KB
/
resolve_indices.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
// Copyright 2016 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 plan
import (
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/util/disjointset"
)
// ResolveIndices implements Plan interface.
func (p *PhysicalProjection) ResolveIndices() {
p.physicalSchemaProducer.ResolveIndices()
for _, expr := range p.Exprs {
expr.ResolveIndices(p.children[0].Schema())
}
childProj, isProj := p.children[0].(*PhysicalProjection)
if !isProj {
return
}
refine4NeighbourProj(p, childProj)
}
// refine4NeighbourProj refines the index for p.Exprs whose type is *Column when
// there is two neighbouring Projections.
// This function is introduced because that different childProj.Expr may refer
// to the same index of childProj.Schema, so we need to keep this relation
// between the specified expressions in the parent Projection.
func refine4NeighbourProj(p, childProj *PhysicalProjection) {
inputIdx2OutputIdxes := make(map[int][]int)
for i, expr := range childProj.Exprs {
col, isCol := expr.(*expression.Column)
if !isCol {
continue
}
inputIdx2OutputIdxes[col.Index] = append(inputIdx2OutputIdxes[col.Index], i)
}
childSchemaUnionSet := disjointset.NewIntSet(childProj.schema.Len())
for _, outputIdxes := range inputIdx2OutputIdxes {
if len(outputIdxes) <= 1 {
continue
}
for i := 1; i < len(outputIdxes); i++ {
childSchemaUnionSet.Union(outputIdxes[0], outputIdxes[i])
}
}
for _, expr := range p.Exprs {
col, isCol := expr.(*expression.Column)
if !isCol {
continue
}
col.Index = childSchemaUnionSet.FindRoot(col.Index)
}
}
// ResolveIndices implements Plan interface.
func (p *PhysicalHashJoin) ResolveIndices() {
p.physicalSchemaProducer.ResolveIndices()
lSchema := p.children[0].Schema()
rSchema := p.children[1].Schema()
for _, fun := range p.EqualConditions {
fun.GetArgs()[0].ResolveIndices(lSchema)
fun.GetArgs()[1].ResolveIndices(rSchema)
}
for _, expr := range p.LeftConditions {
expr.ResolveIndices(lSchema)
}
for _, expr := range p.RightConditions {
expr.ResolveIndices(rSchema)
}
for _, expr := range p.OtherConditions {
expr.ResolveIndices(expression.MergeSchema(lSchema, rSchema))
}
}
// ResolveIndices implements Plan interface.
func (p *PhysicalMergeJoin) ResolveIndices() {
p.physicalSchemaProducer.ResolveIndices()
lSchema := p.children[0].Schema()
rSchema := p.children[1].Schema()
for _, col := range p.LeftKeys {
col.ResolveIndices(lSchema)
}
for _, col := range p.RightKeys {
col.ResolveIndices(rSchema)
}
for _, expr := range p.LeftConditions {
expr.ResolveIndices(lSchema)
}
for _, expr := range p.RightConditions {
expr.ResolveIndices(rSchema)
}
for _, expr := range p.OtherConditions {
expr.ResolveIndices(expression.MergeSchema(lSchema, rSchema))
}
}
// ResolveIndices implements Plan interface.
func (p *PhysicalIndexJoin) ResolveIndices() {
p.physicalSchemaProducer.ResolveIndices()
lSchema := p.children[0].Schema()
rSchema := p.children[1].Schema()
for i := range p.InnerJoinKeys {
p.OuterJoinKeys[i].ResolveIndices(p.children[p.OuterIndex].Schema())
p.InnerJoinKeys[i].ResolveIndices(p.children[1-p.OuterIndex].Schema())
}
for _, expr := range p.LeftConditions {
expr.ResolveIndices(lSchema)
}
for _, expr := range p.RightConditions {
expr.ResolveIndices(rSchema)
}
for _, expr := range p.OtherConditions {
expr.ResolveIndices(expression.MergeSchema(lSchema, rSchema))
}
}
// ResolveIndices implements Plan interface.
func (p *PhysicalUnionScan) ResolveIndices() {
p.basePhysicalPlan.ResolveIndices()
for _, expr := range p.Conditions {
expr.ResolveIndices(p.children[0].Schema())
}
}
// ResolveIndices implements Plan interface.
func (p *PhysicalTableReader) ResolveIndices() {
p.tablePlan.ResolveIndices()
}
// ResolveIndices implements Plan interface.
func (p *PhysicalIndexReader) ResolveIndices() {
p.physicalSchemaProducer.ResolveIndices()
p.indexPlan.ResolveIndices()
for _, col := range p.OutputColumns {
if col.ID != model.ExtraHandleID {
col.ResolveIndices(p.indexPlan.Schema())
} else {
// If this is extra handle, then it must be the tail.
col.Index = len(p.OutputColumns) - 1
}
}
}
// ResolveIndices implements Plan interface.
func (p *PhysicalIndexLookUpReader) ResolveIndices() {
p.tablePlan.ResolveIndices()
p.indexPlan.ResolveIndices()
}
// ResolveIndices implements Plan interface.
func (p *PhysicalSelection) ResolveIndices() {
p.basePhysicalPlan.ResolveIndices()
for _, expr := range p.Conditions {
expr.ResolveIndices(p.children[0].Schema())
}
}
// ResolveIndices implements Plan interface.
func (p *basePhysicalAgg) ResolveIndices() {
p.physicalSchemaProducer.ResolveIndices()
for _, aggFun := range p.AggFuncs {
for _, arg := range aggFun.Args {
arg.ResolveIndices(p.children[0].Schema())
}
}
for _, item := range p.GroupByItems {
item.ResolveIndices(p.children[0].Schema())
}
}
// ResolveIndices implements Plan interface.
func (p *PhysicalSort) ResolveIndices() {
p.basePhysicalPlan.ResolveIndices()
for _, item := range p.ByItems {
item.Expr.ResolveIndices(p.children[0].Schema())
}
}
// ResolveIndices implements Plan interface.
func (p *PhysicalTopN) ResolveIndices() {
p.basePhysicalPlan.ResolveIndices()
for _, item := range p.ByItems {
item.Expr.ResolveIndices(p.children[0].Schema())
}
}
// ResolveIndices implements Plan interface.
func (p *PhysicalApply) ResolveIndices() {
p.PhysicalJoin.ResolveIndices()
for _, col := range p.schema.Columns {
col.ResolveIndices(p.PhysicalJoin.schema)
}
for _, col := range p.OuterSchema {
col.Column.ResolveIndices(p.children[0].Schema())
}
}
// ResolveIndices implements Plan interface.
func (p *Update) ResolveIndices() {
schema := p.SelectPlan.Schema()
for _, assign := range p.OrderedList {
assign.Col.ResolveIndices(schema)
assign.Expr.ResolveIndices(schema)
}
}
// ResolveIndices implements Plan interface.
func (p *Insert) ResolveIndices() {
for _, asgn := range p.OnDuplicate {
asgn.Col.ResolveIndices(p.tableSchema)
asgn.Expr.ResolveIndices(p.tableSchema)
}
for _, set := range p.Setlist {
set.Col.ResolveIndices(p.tableSchema)
set.Expr.ResolveIndices(p.tableSchema)
}
for _, expr := range p.GenCols.Exprs {
expr.ResolveIndices(p.tableSchema)
}
for _, asgn := range p.GenCols.OnDuplicates {
asgn.Col.ResolveIndices(p.tableSchema)
asgn.Expr.ResolveIndices(p.tableSchema)
}
}
// ResolveIndices implements Plan interface.
func (p *Show) ResolveIndices() {
for _, expr := range p.Conditions {
expr.ResolveIndices(p.schema)
}
}
func (p *physicalSchemaProducer) ResolveIndices() {
p.basePhysicalPlan.ResolveIndices()
if p.schema != nil {
for _, cols := range p.schema.TblID2Handle {
for _, col := range cols {
col.ResolveIndices(p.schema)
}
}
}
}
// ResolveIndices implements Plan interface.
func (p *basePhysicalPlan) ResolveIndices() {
for _, child := range p.children {
child.ResolveIndices()
}
}