-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
initialize.go
373 lines (325 loc) · 11.4 KB
/
initialize.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Copyright 2017 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/sessionctx"
const (
// TypeSel is the type of Selection.
TypeSel = "Selection"
// TypeSet is the type of Set.
TypeSet = "Set"
// TypeProj is the type of Projection.
TypeProj = "Projection"
// TypeAgg is the type of Aggregation.
TypeAgg = "Aggregation"
// TypeStreamAgg is the type of StreamAgg.
TypeStreamAgg = "StreamAgg"
// TypeHashAgg is the type of HashAgg.
TypeHashAgg = "HashAgg"
// TypeShow is the type of show.
TypeShow = "Show"
// TypeJoin is the type of Join.
TypeJoin = "Join"
// TypeUnion is the type of Union.
TypeUnion = "Union"
// TypeTableScan is the type of TableScan.
TypeTableScan = "TableScan"
// TypeMemTableScan is the type of TableScan.
TypeMemTableScan = "MemTableScan"
// TypeUnionScan is the type of UnionScan.
TypeUnionScan = "UnionScan"
// TypeIdxScan is the type of IndexScan.
TypeIdxScan = "IndexScan"
// TypeSort is the type of Sort.
TypeSort = "Sort"
// TypeTopN is the type of TopN.
TypeTopN = "TopN"
// TypeLimit is the type of Limit.
TypeLimit = "Limit"
// TypeHashLeftJoin is the type of left hash join.
TypeHashLeftJoin = "HashLeftJoin"
// TypeHashRightJoin is the type of right hash join.
TypeHashRightJoin = "HashRightJoin"
// TypeMergeJoin is the type of merge join.
TypeMergeJoin = "MergeJoin"
// TypeIndexJoin is the type of index look up join.
TypeIndexJoin = "IndexJoin"
// TypeApply is the type of Apply.
TypeApply = "Apply"
// TypeMaxOneRow is the type of MaxOneRow.
TypeMaxOneRow = "MaxOneRow"
// TypeExists is the type of Exists.
TypeExists = "Exists"
// TypeDual is the type of TableDual.
TypeDual = "TableDual"
// TypeLock is the type of SelectLock.
TypeLock = "SelectLock"
// TypeInsert is the type of Insert
TypeInsert = "Insert"
// TypeUpdate is the type of Update.
TypeUpdate = "Update"
// TypeDelete is the type of Delete.
TypeDelete = "Delete"
// TypeIndexLookUp is the type of IndexLookUp.
TypeIndexLookUp = "IndexLookUp"
// TypeTableReader is the type of TableReader.
TypeTableReader = "TableReader"
// TypeIndexReader is the type of IndexReader.
TypeIndexReader = "IndexReader"
)
func (la LogicalAggregation) init(ctx sessionctx.Context) *LogicalAggregation {
la.baseLogicalPlan = newBaseLogicalPlan(ctx, TypeAgg, &la)
return &la
}
func (p LogicalJoin) init(ctx sessionctx.Context) *LogicalJoin {
p.baseLogicalPlan = newBaseLogicalPlan(ctx, TypeJoin, &p)
return &p
}
func (ds DataSource) init(ctx sessionctx.Context) *DataSource {
ds.baseLogicalPlan = newBaseLogicalPlan(ctx, TypeTableScan, &ds)
return &ds
}
func (la LogicalApply) init(ctx sessionctx.Context) *LogicalApply {
la.baseLogicalPlan = newBaseLogicalPlan(ctx, TypeApply, &la)
return &la
}
func (p LogicalSelection) init(ctx sessionctx.Context) *LogicalSelection {
p.baseLogicalPlan = newBaseLogicalPlan(ctx, TypeSel, &p)
return &p
}
func (p PhysicalSelection) init(ctx sessionctx.Context, stats *StatsInfo, props ...*requiredProp) *PhysicalSelection {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeSel, &p)
p.childrenReqProps = props
p.stats = stats
return &p
}
func (p LogicalUnionScan) init(ctx sessionctx.Context) *LogicalUnionScan {
p.baseLogicalPlan = newBaseLogicalPlan(ctx, TypeUnionScan, &p)
return &p
}
func (p LogicalProjection) init(ctx sessionctx.Context) *LogicalProjection {
p.baseLogicalPlan = newBaseLogicalPlan(ctx, TypeProj, &p)
return &p
}
func (p PhysicalProjection) init(ctx sessionctx.Context, stats *StatsInfo, props ...*requiredProp) *PhysicalProjection {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeProj, &p)
p.childrenReqProps = props
p.stats = stats
return &p
}
func (p LogicalUnionAll) init(ctx sessionctx.Context) *LogicalUnionAll {
p.baseLogicalPlan = newBaseLogicalPlan(ctx, TypeUnion, &p)
return &p
}
func (p PhysicalUnionAll) init(ctx sessionctx.Context, stats *StatsInfo, props ...*requiredProp) *PhysicalUnionAll {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeUnion, &p)
p.childrenReqProps = props
p.stats = stats
return &p
}
func (ls LogicalSort) init(ctx sessionctx.Context) *LogicalSort {
ls.baseLogicalPlan = newBaseLogicalPlan(ctx, TypeSort, &ls)
return &ls
}
func (p PhysicalSort) init(ctx sessionctx.Context, stats *StatsInfo, props ...*requiredProp) *PhysicalSort {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeSort, &p)
p.childrenReqProps = props
p.stats = stats
return &p
}
func (p NominalSort) init(ctx sessionctx.Context, props ...*requiredProp) *NominalSort {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeSort, &p)
p.childrenReqProps = props
return &p
}
func (lt LogicalTopN) init(ctx sessionctx.Context) *LogicalTopN {
lt.baseLogicalPlan = newBaseLogicalPlan(ctx, TypeTopN, <)
return <
}
func (p PhysicalTopN) init(ctx sessionctx.Context, stats *StatsInfo, props ...*requiredProp) *PhysicalTopN {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeTopN, &p)
p.childrenReqProps = props
p.stats = stats
return &p
}
func (p LogicalLimit) init(ctx sessionctx.Context) *LogicalLimit {
p.baseLogicalPlan = newBaseLogicalPlan(ctx, TypeLimit, &p)
return &p
}
func (p PhysicalLimit) init(ctx sessionctx.Context, stats *StatsInfo, props ...*requiredProp) *PhysicalLimit {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeLimit, &p)
p.childrenReqProps = props
p.stats = stats
return &p
}
func (p LogicalTableDual) init(ctx sessionctx.Context) *LogicalTableDual {
p.baseLogicalPlan = newBaseLogicalPlan(ctx, TypeDual, &p)
return &p
}
func (p PhysicalTableDual) init(ctx sessionctx.Context, stats *StatsInfo) *PhysicalTableDual {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeDual, &p)
p.stats = stats
return &p
}
func (p LogicalExists) init(ctx sessionctx.Context) *LogicalExists {
p.baseLogicalPlan = newBaseLogicalPlan(ctx, TypeExists, &p)
return &p
}
func (p PhysicalExists) init(ctx sessionctx.Context, stats *StatsInfo, props ...*requiredProp) *PhysicalExists {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeExists, &p)
p.childrenReqProps = props
p.stats = stats
return &p
}
func (p LogicalMaxOneRow) init(ctx sessionctx.Context) *LogicalMaxOneRow {
p.baseLogicalPlan = newBaseLogicalPlan(ctx, TypeMaxOneRow, &p)
return &p
}
func (p PhysicalMaxOneRow) init(ctx sessionctx.Context, stats *StatsInfo, props ...*requiredProp) *PhysicalMaxOneRow {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeMaxOneRow, &p)
p.childrenReqProps = props
p.stats = stats
return &p
}
func (p Update) init(ctx sessionctx.Context) *Update {
p.basePlan = newBasePlan(ctx, TypeUpdate)
return &p
}
func (p Delete) init(ctx sessionctx.Context) *Delete {
p.basePlan = newBasePlan(ctx, TypeDelete)
return &p
}
func (p Insert) init(ctx sessionctx.Context) *Insert {
p.basePlan = newBasePlan(ctx, TypeInsert)
return &p
}
func (p Show) init(ctx sessionctx.Context) *Show {
p.basePlan = newBasePlan(ctx, TypeShow)
return &p
}
func (p LogicalLock) init(ctx sessionctx.Context) *LogicalLock {
p.baseLogicalPlan = newBaseLogicalPlan(ctx, TypeLock, &p)
return &p
}
func (p PhysicalLock) init(ctx sessionctx.Context, stats *StatsInfo, props ...*requiredProp) *PhysicalLock {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeLock, &p)
p.childrenReqProps = props
p.stats = stats
return &p
}
func (p PhysicalTableScan) init(ctx sessionctx.Context) *PhysicalTableScan {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeTableScan, &p)
return &p
}
func (p PhysicalIndexScan) init(ctx sessionctx.Context) *PhysicalIndexScan {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeIdxScan, &p)
return &p
}
func (p PhysicalMemTable) init(ctx sessionctx.Context, stats *StatsInfo) *PhysicalMemTable {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeMemTableScan, &p)
p.stats = stats
return &p
}
func (p PhysicalHashJoin) init(ctx sessionctx.Context, stats *StatsInfo, props ...*requiredProp) *PhysicalHashJoin {
tp := TypeHashRightJoin
if p.InnerChildIdx == 1 {
tp = TypeHashLeftJoin
}
p.basePhysicalPlan = newBasePhysicalPlan(ctx, tp, &p)
p.childrenReqProps = props
p.stats = stats
return &p
}
func (p PhysicalMergeJoin) init(ctx sessionctx.Context, stats *StatsInfo) *PhysicalMergeJoin {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeMergeJoin, &p)
p.stats = stats
return &p
}
func (base basePhysicalAgg) init(ctx sessionctx.Context, stats *StatsInfo) *basePhysicalAgg {
base.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeHashAgg, &base)
base.stats = stats
return &base
}
func (base basePhysicalAgg) initForHash(ctx sessionctx.Context, stats *StatsInfo, props ...*requiredProp) *PhysicalHashAgg {
p := &PhysicalHashAgg{base}
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeHashAgg, p)
p.childrenReqProps = props
p.stats = stats
return p
}
func (base basePhysicalAgg) initForStream(ctx sessionctx.Context, stats *StatsInfo, props ...*requiredProp) *PhysicalStreamAgg {
p := &PhysicalStreamAgg{base}
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeStreamAgg, p)
p.childrenReqProps = props
p.stats = stats
return p
}
func (p PhysicalApply) init(ctx sessionctx.Context, stats *StatsInfo, props ...*requiredProp) *PhysicalApply {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeApply, &p)
p.childrenReqProps = props
p.stats = stats
return &p
}
func (p PhysicalUnionScan) init(ctx sessionctx.Context, stats *StatsInfo, props ...*requiredProp) *PhysicalUnionScan {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeUnionScan, &p)
p.childrenReqProps = props
p.stats = stats
return &p
}
func (p PhysicalIndexLookUpReader) init(ctx sessionctx.Context) *PhysicalIndexLookUpReader {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeIndexLookUp, &p)
p.TablePlans = flattenPushDownPlan(p.tablePlan)
p.IndexPlans = flattenPushDownPlan(p.indexPlan)
p.schema = p.tablePlan.Schema()
return &p
}
func (p PhysicalTableReader) init(ctx sessionctx.Context) *PhysicalTableReader {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeTableReader, &p)
p.TablePlans = flattenPushDownPlan(p.tablePlan)
p.schema = p.tablePlan.Schema()
return &p
}
func (p PhysicalIndexReader) init(ctx sessionctx.Context) *PhysicalIndexReader {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeIndexReader, &p)
p.IndexPlans = flattenPushDownPlan(p.indexPlan)
switch p.indexPlan.(type) {
case *PhysicalHashAgg, *PhysicalStreamAgg:
p.schema = p.indexPlan.Schema()
default:
is := p.IndexPlans[0].(*PhysicalIndexScan)
p.schema = is.dataSourceSchema
}
p.OutputColumns = p.schema.Clone().Columns
return &p
}
func (p PhysicalIndexJoin) init(ctx sessionctx.Context, stats *StatsInfo, props ...*requiredProp) *PhysicalIndexJoin {
p.basePhysicalPlan = newBasePhysicalPlan(ctx, TypeIndexJoin, &p)
p.childrenReqProps = props
p.stats = stats
return &p
}
// flattenPushDownPlan converts a plan tree to a list, whose head is the leaf node like table scan.
func flattenPushDownPlan(p PhysicalPlan) []PhysicalPlan {
plans := make([]PhysicalPlan, 0, 5)
for {
plans = append(plans, p)
if len(p.Children()) == 0 {
break
}
p = p.Children()[0]
}
for i := 0; i < len(plans)/2; i++ {
j := len(plans) - i - 1
plans[i], plans[j] = plans[j], plans[i]
}
return plans
}