forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
558 lines (509 loc) · 15 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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// 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/expression"
"github.com/pingcap/tidb/expression/aggregation"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/charset"
)
// 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.StatsInfo().count
}
return t.indexPlan.StatsInfo().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).stats = t.indexPlan.StatsInfo()
t.cst += t.count() * scanFactor
}
}
}
func (p *basePhysicalPlan) attach2Task(tasks ...task) task {
if tasks[0].invalid() {
return invalidTask
}
t := finishCopTask(p.ctx, tasks[0].copy())
return attachPlan2Task(p.self, t)
}
func (p *PhysicalApply) attach2Task(tasks ...task) task {
if tasks[0].invalid() || tasks[1].invalid() {
return invalidTask
}
lTask := finishCopTask(p.ctx, tasks[0].copy())
rTask := finishCopTask(p.ctx, tasks[1].copy())
p.SetChildren(lTask.plan(), rTask.plan())
p.PhysicalJoin.SetChildren(lTask.plan(), rTask.plan())
p.schema = buildPhysicalJoinSchema(p.PhysicalJoin.JoinType, p)
return &rootTask{
p: p,
cst: lTask.cost() + lTask.count()*rTask.cost(),
}
}
func (p *PhysicalIndexJoin) attach2Task(tasks ...task) task {
if tasks[p.OuterIndex].invalid() {
return invalidTask
}
outerTask := finishCopTask(p.ctx, tasks[p.OuterIndex].copy())
if p.OuterIndex == 0 {
p.SetChildren(outerTask.plan(), p.innerPlan)
} else {
p.SetChildren(p.innerPlan, outerTask.plan())
}
p.schema = buildPhysicalJoinSchema(p.JoinType, p)
return &rootTask{
p: p,
cst: outerTask.cost() + p.getCost(outerTask.count()),
}
}
func (p *PhysicalIndexJoin) getCost(lCnt float64) float64 {
if lCnt < 1 {
lCnt = 1
}
cst := lCnt * netWorkFactor
batchSize := p.ctx.GetSessionVars().IndexJoinBatchSize
cst += lCnt * math.Log2(math.Min(float64(batchSize), lCnt)) * 2
cst += lCnt / float64(batchSize) * netWorkStartFactor
return cst
}
func (p *PhysicalHashJoin) getCost(lCnt, rCnt float64) float64 {
smallTableCnt := lCnt
if p.InnerChildIdx == 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 {
if tasks[0].invalid() || tasks[1].invalid() {
return invalidTask
}
lTask := finishCopTask(p.ctx, tasks[0].copy())
rTask := finishCopTask(p.ctx, tasks[1].copy())
p.SetChildren(lTask.plan(), rTask.plan())
p.schema = buildPhysicalJoinSchema(p.JoinType, p)
return &rootTask{
p: p,
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 {
if tasks[0].invalid() || tasks[1].invalid() {
return invalidTask
}
lTask := finishCopTask(p.ctx, tasks[0].copy())
rTask := finishCopTask(p.ctx, tasks[1].copy())
p.SetChildren(lTask.plan(), rTask.plan())
p.schema = buildPhysicalJoinSchema(p.JoinType, p)
return &rootTask{
p: p,
cst: lTask.cost() + rTask.cost() + p.getCost(lTask.count(), rTask.count()),
}
}
// finishCopTask means we close the coprocessor task and create a root task.
func finishCopTask(ctx sessionctx.Context, task task) 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(ctx)
p.stats = t.tablePlan.StatsInfo()
newTask.p = p
} else if t.indexPlan != nil {
p := PhysicalIndexReader{indexPlan: t.indexPlan}.init(ctx)
p.stats = t.indexPlan.StatsInfo()
newTask.p = p
} else {
p := PhysicalTableReader{tablePlan: t.tablePlan}.init(ctx)
p.stats = t.tablePlan.StatsInfo()
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.StatsInfo().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 *PhysicalLimit) attach2Task(tasks ...task) task {
// If task is invalid, keep it remained.
if tasks[0].invalid() {
return invalidTask
}
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 := PhysicalLimit{Count: p.Offset + p.Count}.init(p.ctx, p.stats)
cop = attachPlan2Task(pushedDownLimit, cop).(*copTask)
}
t = finishCopTask(p.ctx, cop)
}
if !p.partial {
t = attachPlan2Task(p, t)
}
return t
}
func (p *PhysicalSort) getCost(count float64) float64 {
if count < 2.0 {
count = 2.0
}
return count*cpuFactor + count*memoryFactor
}
func (p *PhysicalTopN) 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 *PhysicalTopN) 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 *PhysicalTopN) allColsFromSchema(schema *expression.Schema) bool {
cols := make([]*expression.Column, 0, len(p.ByItems))
for _, item := range p.ByItems {
cols = append(cols, expression.ExtractColumns(item.Expr)...)
}
return len(schema.ColumnsIndices(cols)) > 0
}
func (p *PhysicalSort) attach2Task(tasks ...task) task {
if tasks[0].invalid() {
return invalidTask
}
t := tasks[0].copy()
t = attachPlan2Task(p, t)
t.addCost(p.getCost(t.count()))
return t
}
func (p *NominalSort) attach2Task(tasks ...task) task {
return tasks[0]
}
func (p *PhysicalTopN) getPushedDownTopN() *PhysicalTopN {
newByItems := make([]*ByItems, 0, len(p.ByItems))
for _, expr := range p.ByItems {
newByItems = append(newByItems, expr.Clone())
}
topN := PhysicalTopN{
ByItems: newByItems,
Count: p.Offset + p.Count,
}.init(p.ctx, p.stats)
return topN
}
func (p *PhysicalTopN) attach2Task(tasks ...task) task {
// If task is invalid, keep it remained.
if tasks[0].invalid() {
return invalidTask
}
t := tasks[0].copy()
// This is a topN plan.
if copTask, ok := t.(*copTask); ok && p.canPushDown() {
pushedDownTopN := p.getPushedDownTopN()
// 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
} 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
}
copTask.addCost(pushedDownTopN.getCost(t.count()))
}
t = finishCopTask(p.ctx, t)
if !p.partial {
t = attachPlan2Task(p, t)
t.addCost(p.getCost(t.count()))
}
return t
}
func (p *PhysicalProjection) attach2Task(tasks ...task) task {
if tasks[0].invalid() {
return invalidTask
}
t := tasks[0].copy()
switch tp := t.(type) {
case *copTask:
// TODO: Support projection push down.
t = finishCopTask(p.ctx, t)
t = attachPlan2Task(p, t)
return t
case *rootTask:
return attachPlan2Task(p, tp)
}
return nil
}
func (p *PhysicalUnionAll) attach2Task(tasks ...task) task {
newTask := &rootTask{p: p}
newChildren := make([]PhysicalPlan, 0, len(p.children))
for _, task := range tasks {
if task.invalid() {
return invalidTask
}
task = finishCopTask(p.ctx, task)
newTask.cst += task.cost()
newChildren = append(newChildren, task.plan())
}
p.SetChildren(newChildren...)
return newTask
}
func (sel *PhysicalSelection) attach2Task(tasks ...task) task {
if tasks[0].invalid() {
return invalidTask
}
t := finishCopTask(sel.ctx, tasks[0].copy())
t.addCost(t.count() * cpuFactor)
t = attachPlan2Task(sel, t)
return t
}
func (p *basePhysicalAgg) newPartialAggregate() (partial, final PhysicalPlan) {
// 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 nil, p.self
}
}
_, _, remained := expression.ExpressionsToPB(sc, p.GroupByItems, client)
if len(remained) > 0 {
return nil, p.self
}
finalSchema := p.schema
partialSchema := expression.NewSchema()
p.schema = partialSchema
partialAgg := p.self
// TODO: Refactor the way of constructing aggregation functions.
partialCursor := 0
finalAggFuncs := make([]*aggregation.AggFuncDesc, len(p.AggFuncs))
for i, aggFun := range p.AggFuncs {
finalAggFunc := &aggregation.AggFuncDesc{Name: aggFun.Name, HasDistinct: false}
args := make([]expression.Expression, 0, len(aggFun.Args))
if needCount(finalAggFunc) {
ft := types.NewFieldType(mysql.TypeLonglong)
ft.Flen, ft.Charset, ft.Collate = 21, charset.CharsetBin, charset.CollationBin
partialSchema.Append(&expression.Column{
FromID: p.ID(),
Position: partialCursor,
ColName: model.NewCIStr(fmt.Sprintf("col_%d", partialCursor)),
RetType: ft,
})
args = append(args, partialSchema.Columns[partialCursor].Clone())
partialCursor++
}
if needValue(finalAggFunc) {
partialSchema.Append(&expression.Column{
FromID: p.ID(),
Position: partialCursor,
ColName: model.NewCIStr(fmt.Sprintf("col_%d", partialCursor)),
RetType: finalSchema.Columns[i].GetType(),
})
args = append(args, partialSchema.Columns[partialCursor].Clone())
partialCursor++
}
finalAggFunc.Args = args
finalAggFunc.Mode = aggregation.FinalMode
finalAggFunc.RetTp = aggFun.RetTp
finalAggFuncs[i] = finalAggFunc
}
// add group by columns
groupByItems := make([]expression.Expression, 0, len(p.GroupByItems))
for i, gbyExpr := range p.GroupByItems {
gbyCol := &expression.Column{
FromID: p.ID(),
Position: partialCursor + i,
ColName: model.NewCIStr(fmt.Sprintf("col_%d", partialCursor+i)),
RetType: gbyExpr.GetType(),
}
partialSchema.Append(gbyCol)
groupByItems = append(groupByItems, gbyCol.Clone())
}
// Create physical "final" aggregation.
if p.tp == TypeStreamAgg {
finalAgg := basePhysicalAgg{
AggFuncs: finalAggFuncs,
GroupByItems: groupByItems,
}.initForStream(p.ctx, p.stats)
finalAgg.schema = finalSchema
return partialAgg, finalAgg
}
finalAgg := basePhysicalAgg{
AggFuncs: finalAggFuncs,
GroupByItems: groupByItems,
}.initForHash(p.ctx, p.stats)
finalAgg.schema = finalSchema
return partialAgg, finalAgg
}
func (p *PhysicalStreamAgg) attach2Task(tasks ...task) task {
// If task is invalid, keep it remained.
if tasks[0].invalid() {
return invalidTask
}
t := tasks[0].copy()
if cop, ok := t.(*copTask); ok {
partialAgg, finalAgg := p.newPartialAggregate()
if partialAgg != nil {
if cop.tablePlan != nil {
partialAgg.SetChildren(cop.tablePlan)
cop.tablePlan = partialAgg
} else {
partialAgg.SetChildren(cop.indexPlan)
cop.indexPlan = partialAgg
}
}
t = finishCopTask(p.ctx, cop)
attachPlan2Task(finalAgg, t)
t.addCost(t.count() * cpuFactor)
} else {
attachPlan2Task(p, t)
t.addCost(t.count() * cpuFactor)
}
return t
}
func (p *PhysicalHashAgg) attach2Task(tasks ...task) task {
// If task is invalid, keep it remained.
if tasks[0].invalid() {
return invalidTask
}
cardinality := p.StatsInfo().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(p.ctx, cop)
attachPlan2Task(finalAgg, task)
task.addCost(task.count()*cpuFactor + cardinality*hashAggMemFactor)
} else {
attachPlan2Task(p, task)
task.addCost(task.count()*cpuFactor + cardinality*hashAggMemFactor)
}
return task
}