-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
513 lines (470 loc) · 14.4 KB
/
task.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// 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 (
"fmt"
"math"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/expression/aggregation"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/util/charset"
"github.com/pingcap/tidb/util/types"
)
// task is a new version of `PhysicalPlanInfo`. It stores cost information for a task.
// A task may be CopTask, RootTask, MPPTask or a ParallelTask.
type task interface {
count() float64
addCost(cost float64)
cost() float64
copy() task
plan() PhysicalPlan
invalid() bool
}
// copTask is a task that runs in a distributed kv store.
// TODO: In future, we should split copTask to indexTask and tableTask.
type copTask struct {
indexPlan PhysicalPlan
tablePlan PhysicalPlan
cst float64
// indexPlanFinished means we have finished index plan.
indexPlanFinished bool
// keepOrder indicates if the plan scans data by order.
keepOrder bool
}
func (t *copTask) invalid() bool {
return t.tablePlan == nil && t.indexPlan == nil
}
func (t *rootTask) invalid() bool {
return t.p == nil
}
func (t *copTask) count() float64 {
if t.indexPlanFinished {
return t.tablePlan.statsProfile().count
}
return t.indexPlan.statsProfile().count
}
func (t *copTask) addCost(cst float64) {
t.cst += cst
}
func (t *copTask) cost() float64 {
return t.cst
}
func (t *copTask) copy() task {
nt := *t
return &nt
}
func (t *copTask) plan() PhysicalPlan {
if t.indexPlanFinished {
return t.tablePlan
}
return t.indexPlan
}
func attachPlan2Task(p PhysicalPlan, t task) task {
switch v := t.(type) {
case *copTask:
if v.indexPlanFinished {
p.SetChildren(v.tablePlan)
v.tablePlan = p
} else {
p.SetChildren(v.indexPlan)
v.indexPlan = p
}
case *rootTask:
p.SetChildren(v.p)
v.p = p
}
return t
}
// finishIndexPlan means we no longer add plan to index plan, and compute the network cost for it.
func (t *copTask) finishIndexPlan() {
if !t.indexPlanFinished {
t.cst += t.count() * netWorkFactor
t.indexPlanFinished = true
if t.tablePlan != nil {
t.tablePlan.(*PhysicalTableScan).profile = t.indexPlan.statsProfile()
t.cst += t.count() * scanFactor
}
}
}
func (p *basePhysicalPlan) attach2Task(tasks ...task) task {
t := finishCopTask(tasks[0].copy(), p.basePlan.ctx, p.basePlan.allocator)
return attachPlan2Task(p.basePlan.self.(PhysicalPlan).Copy(), t)
}
func (p *PhysicalApply) attach2Task(tasks ...task) task {
lTask := finishCopTask(tasks[0].copy(), p.ctx, p.allocator)
rTask := finishCopTask(tasks[1].copy(), p.ctx, p.allocator)
np := p.Copy().(*PhysicalApply)
np.SetChildren(lTask.plan(), rTask.plan())
np.PhysicalJoin.SetChildren(lTask.plan(), rTask.plan())
return &rootTask{
p: np,
cst: lTask.cost() + lTask.count()*rTask.cost(),
}
}
func (p *PhysicalIndexJoin) attach2Task(tasks ...task) task {
lTask := finishCopTask(tasks[p.outerIndex].copy(), p.ctx, p.allocator)
np := p.Copy()
np.SetChildren(lTask.plan(), p.innerPlan)
return &rootTask{
p: np,
cst: lTask.cost() + p.getCost(lTask.count()),
}
}
func (p *PhysicalIndexJoin) getCost(lCnt float64) float64 {
if lCnt < 1 {
lCnt = 1
}
cst := lCnt * netWorkFactor
batchSize := p.ctx.GetSessionVars().IndexJoinBatchSize
if p.KeepOrder {
batchSize = 1
}
cst += lCnt * math.Log2(math.Min(float64(batchSize), lCnt)) * 2
cst += lCnt / float64(batchSize) * netWorkStartFactor
if p.KeepOrder {
return cst * 2
}
return cst
}
func (p *PhysicalHashJoin) getCost(lCnt, rCnt float64) float64 {
smallTableCnt := lCnt
if p.SmallTable == 1 {
smallTableCnt = rCnt
}
if smallTableCnt <= 1 {
smallTableCnt = 1
}
return (lCnt + rCnt) * (1 + math.Log2(smallTableCnt)/float64(p.Concurrency))
}
func (p *PhysicalHashJoin) attach2Task(tasks ...task) task {
lTask := finishCopTask(tasks[0].copy(), p.ctx, p.allocator)
rTask := finishCopTask(tasks[1].copy(), p.ctx, p.allocator)
np := p.Copy()
np.SetChildren(lTask.plan(), rTask.plan())
return &rootTask{
p: np,
cst: lTask.cost() + rTask.cost() + p.getCost(lTask.count(), rTask.count()),
}
}
func (p *PhysicalMergeJoin) getCost(lCnt, rCnt float64) float64 {
return lCnt + rCnt
}
func (p *PhysicalMergeJoin) attach2Task(tasks ...task) task {
lTask := finishCopTask(tasks[0].copy(), p.ctx, p.allocator)
rTask := finishCopTask(tasks[1].copy(), p.ctx, p.allocator)
np := p.Copy()
np.SetChildren(lTask.plan(), rTask.plan())
return &rootTask{
p: np,
cst: lTask.cost() + rTask.cost() + p.getCost(lTask.count(), rTask.count()),
}
}
func (p *PhysicalHashSemiJoin) getCost(lCnt, rCnt float64) float64 {
if rCnt <= 1 {
rCnt = 1
}
return (lCnt + rCnt) * (1 + math.Log2(rCnt))
}
func (p *PhysicalHashSemiJoin) attach2Task(tasks ...task) task {
lTask := finishCopTask(tasks[0].copy(), p.ctx, p.allocator)
rTask := finishCopTask(tasks[1].copy(), p.ctx, p.allocator)
np := p.Copy()
np.SetChildren(lTask.plan(), rTask.plan())
task := &rootTask{
p: np,
cst: lTask.cost() + rTask.cost() + p.getCost(lTask.count(), rTask.count()),
}
return task
}
// finishCopTask means we close the coprocessor task and create a root task.
func finishCopTask(task task, ctx context.Context, allocator *idAllocator) task {
t, ok := task.(*copTask)
if !ok {
return task
}
// FIXME: When it is a double reading. The cost should be more expensive. The right cost should add the
// `NetWorkStartCost` * (totalCount / perCountIndexRead)
t.finishIndexPlan()
if t.tablePlan != nil {
t.cst += t.count() * netWorkFactor
}
newTask := &rootTask{
cst: t.cst,
}
if t.indexPlan != nil && t.tablePlan != nil {
p := PhysicalIndexLookUpReader{tablePlan: t.tablePlan, indexPlan: t.indexPlan}.init(allocator, ctx)
p.profile = t.tablePlan.statsProfile()
newTask.p = p
} else if t.indexPlan != nil {
p := PhysicalIndexReader{indexPlan: t.indexPlan}.init(allocator, ctx)
p.profile = t.indexPlan.statsProfile()
newTask.p = p
} else {
p := PhysicalTableReader{tablePlan: t.tablePlan}.init(allocator, ctx)
p.profile = t.tablePlan.statsProfile()
newTask.p = p
}
return newTask
}
// rootTask is the final sink node of a plan graph. It should be a single goroutine on tidb.
type rootTask struct {
p PhysicalPlan
cst float64
}
func (t *rootTask) copy() task {
return &rootTask{
p: t.p,
cst: t.cst,
}
}
func (t *rootTask) count() float64 {
return t.p.statsProfile().count
}
func (t *rootTask) addCost(cst float64) {
t.cst += cst
}
func (t *rootTask) cost() float64 {
return t.cst
}
func (t *rootTask) plan() PhysicalPlan {
return t.p
}
func (p *Limit) attach2Task(tasks ...task) task {
// If task is invalid, keep it remained.
if tasks[0].plan() == nil {
return tasks[0]
}
t := tasks[0].copy()
if cop, ok := t.(*copTask); ok {
// If the table/index scans data by order and applies a double read, the limit cannot be pushed to the table side.
if !cop.keepOrder || !cop.indexPlanFinished || cop.indexPlan == nil {
// When limit be pushed down, it should remove its offset.
pushedDownLimit := Limit{Count: p.Offset + p.Count}.init(p.allocator, p.ctx)
pushedDownLimit.profile = p.profile
if cop.tablePlan != nil {
pushedDownLimit.SetSchema(cop.tablePlan.Schema())
} else {
pushedDownLimit.SetSchema(cop.indexPlan.Schema())
}
cop = attachPlan2Task(pushedDownLimit, cop).(*copTask)
}
t = finishCopTask(cop, p.ctx, p.allocator)
}
if !p.partial {
t = attachPlan2Task(p.Copy(), t)
}
return t
}
func (p *Sort) getCost(count float64) float64 {
if count < 2.0 {
count = 2.0
}
return count*cpuFactor + count*memoryFactor
}
func (p *TopN) getCost(count float64) float64 {
return count*cpuFactor + float64(p.Count)*memoryFactor
}
// canPushDown checks if this topN can be pushed down. If each of the expression can be converted to pb, it can be pushed.
func (p *TopN) canPushDown() bool {
exprs := make([]expression.Expression, 0, len(p.ByItems))
for _, item := range p.ByItems {
exprs = append(exprs, item.Expr)
}
_, _, remained := expression.ExpressionsToPB(p.ctx.GetSessionVars().StmtCtx, exprs, p.ctx.GetClient())
return len(remained) == 0
}
func (p *TopN) allColsFromSchema(schema *expression.Schema) bool {
var cols []*expression.Column
for _, item := range p.ByItems {
cols = append(cols, expression.ExtractColumns(item.Expr)...)
}
return len(schema.ColumnsIndices(cols)) > 0
}
func (p *Sort) attach2Task(tasks ...task) task {
t := tasks[0].copy()
t = attachPlan2Task(p.Copy(), t)
t.addCost(p.getCost(t.count()))
return t
}
func (p *TopN) attach2Task(tasks ...task) task {
// If task is invalid, keep it remained.
if tasks[0].plan() == nil {
return tasks[0]
}
t := tasks[0].copy()
// This is a topN plan.
if copTask, ok := t.(*copTask); ok && p.canPushDown() {
pushedDownTopN := p.Copy().(*TopN)
newByItems := make([]*ByItems, 0, len(p.ByItems))
for _, expr := range p.ByItems {
newByItems = append(newByItems, expr.Clone())
}
pushedDownTopN.ByItems = newByItems
// When topN is pushed down, it should remove its offset.
pushedDownTopN.Count, pushedDownTopN.Offset = p.Count+p.Offset, 0
// If all columns in topN are from index plan, we can push it to index plan. Or we finish the index plan and
// push it to table plan.
if !copTask.indexPlanFinished && p.allColsFromSchema(copTask.indexPlan.Schema()) {
pushedDownTopN.SetChildren(copTask.indexPlan)
copTask.indexPlan = pushedDownTopN
pushedDownTopN.SetSchema(copTask.indexPlan.Schema())
} else {
// FIXME: When we pushed down a top-N plan to table plan branch in case of double reading. The cost should
// be more expensive in case of single reading, because we may execute table scan multi times.
copTask.finishIndexPlan()
pushedDownTopN.SetChildren(copTask.tablePlan)
copTask.tablePlan = pushedDownTopN
pushedDownTopN.SetSchema(copTask.tablePlan.Schema())
}
copTask.addCost(pushedDownTopN.getCost(t.count()))
}
t = finishCopTask(t, p.ctx, p.allocator)
if !p.partial {
t = attachPlan2Task(p.Copy(), t)
t.addCost(p.getCost(t.count()))
}
return t
}
func (p *Projection) attach2Task(tasks ...task) task {
t := tasks[0].copy()
np := p.Copy()
switch tp := t.(type) {
case *copTask:
// TODO: Support projection push down.
t = finishCopTask(t, p.ctx, p.allocator)
t = attachPlan2Task(np, t)
return t
case *rootTask:
return attachPlan2Task(np, tp)
}
return nil
}
func (p *Union) attach2Task(tasks ...task) task {
np := p.Copy()
newTask := &rootTask{p: np}
newChildren := make([]Plan, 0, len(p.children))
for _, task := range tasks {
task = finishCopTask(task, p.ctx, p.allocator)
newTask.cst += task.cost()
newChildren = append(newChildren, task.plan())
}
np.SetChildren(newChildren...)
return newTask
}
func (sel *Selection) attach2Task(tasks ...task) task {
t := finishCopTask(tasks[0].copy(), sel.ctx, sel.allocator)
t.addCost(t.count() * cpuFactor)
t = attachPlan2Task(sel.Copy(), t)
return t
}
func (p *PhysicalAggregation) newPartialAggregate() (partialAgg, finalAgg *PhysicalAggregation) {
finalAgg = p.Copy().(*PhysicalAggregation)
// Check if this aggregation can push down.
sc := p.ctx.GetSessionVars().StmtCtx
client := p.ctx.GetClient()
for _, aggFunc := range p.AggFuncs {
pb := aggregation.AggFuncToPBExpr(sc, client, aggFunc)
if pb == nil {
return
}
}
_, _, remained := expression.ExpressionsToPB(sc, p.GroupByItems, client)
if len(remained) > 0 {
return
}
partialAgg = p.Copy().(*PhysicalAggregation)
// TODO: It's toooooo ugly here. Refactor in the future !!
gkType := types.NewFieldType(mysql.TypeBlob)
gkType.Charset = charset.CharsetBin
gkType.Collate = charset.CollationBin
partialSchema := expression.NewSchema()
partialAgg.SetSchema(partialSchema)
cursor := 0
finalAggFuncs := make([]aggregation.Aggregation, len(finalAgg.AggFuncs))
for i, aggFun := range p.AggFuncs {
fun := aggregation.NewAggFunction(aggFun.GetName(), nil, false)
var args []expression.Expression
colName := model.NewCIStr(fmt.Sprintf("col_%d", cursor))
if needCount(fun) {
ft := types.NewFieldType(mysql.TypeLonglong)
ft.Flen = 21
ft.Charset = charset.CharsetBin
ft.Collate = charset.CollationBin
partialSchema.Append(&expression.Column{FromID: partialAgg.id, Position: cursor, ColName: colName, RetType: ft})
args = append(args, partialSchema.Columns[cursor].Clone())
cursor++
}
if needValue(fun) {
ft := p.schema.Columns[i].GetType()
partialSchema.Append(&expression.Column{FromID: partialAgg.id, Position: cursor, ColName: colName, RetType: ft})
args = append(args, partialSchema.Columns[cursor].Clone())
cursor++
}
fun.SetArgs(args)
fun.SetMode(aggregation.FinalMode)
finalAggFuncs[i] = fun
}
finalAgg = PhysicalAggregation{
HasGby: p.HasGby, // TODO: remove this field
AggType: FinalAgg,
AggFuncs: finalAggFuncs,
}.init(p.allocator, p.ctx)
finalAgg.profile = p.profile
finalAgg.SetSchema(p.schema)
// add group by columns
for i, gbyExpr := range p.GroupByItems {
gbyCol := &expression.Column{
FromID: partialAgg.id,
Position: cursor + i,
RetType: gbyExpr.GetType(),
}
partialSchema.Append(gbyCol)
finalAgg.GroupByItems = append(finalAgg.GroupByItems, gbyCol.Clone())
}
return
}
func (p *PhysicalAggregation) attach2Task(tasks ...task) task {
// If task is invalid, keep it remained.
if tasks[0].plan() == nil {
return tasks[0]
}
cardinality := p.statsProfile().count
task := tasks[0].copy()
if cop, ok := task.(*copTask); ok {
partialAgg, finalAgg := p.newPartialAggregate()
if partialAgg != nil {
if cop.tablePlan != nil {
cop.finishIndexPlan()
partialAgg.SetChildren(cop.tablePlan)
cop.tablePlan = partialAgg
} else {
partialAgg.SetChildren(cop.indexPlan)
cop.indexPlan = partialAgg
}
}
task = finishCopTask(cop, p.ctx, p.allocator)
task.addCost(task.count()*cpuFactor + cardinality*hashAggMemFactor)
attachPlan2Task(finalAgg, task)
} else {
np := p.Copy()
attachPlan2Task(np, task)
if p.AggType == StreamedAgg {
task.addCost(task.count() * cpuFactor)
} else {
task.addCost(task.count()*cpuFactor + cardinality*hashAggMemFactor)
}
}
return task
}