forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logical_plans.go
449 lines (382 loc) · 11.3 KB
/
logical_plans.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
// 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/juju/errors"
"github.com/pingcap/tidb/ast"
"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/statistics"
"github.com/pingcap/tidb/util/types"
)
var (
_ LogicalPlan = &LogicalJoin{}
_ LogicalPlan = &LogicalAggregation{}
_ LogicalPlan = &Projection{}
_ LogicalPlan = &Selection{}
_ LogicalPlan = &LogicalApply{}
_ LogicalPlan = &Exists{}
_ LogicalPlan = &MaxOneRow{}
_ LogicalPlan = &TableDual{}
_ LogicalPlan = &DataSource{}
_ LogicalPlan = &Union{}
_ LogicalPlan = &Sort{}
_ LogicalPlan = &Update{}
_ LogicalPlan = &Delete{}
_ LogicalPlan = &SelectLock{}
_ LogicalPlan = &Limit{}
_ LogicalPlan = &Show{}
_ LogicalPlan = &Insert{}
)
// JoinType contains CrossJoin, InnerJoin, LeftOuterJoin, RightOuterJoin, FullOuterJoin, SemiJoin.
type JoinType int
const (
// InnerJoin means inner join.
InnerJoin JoinType = iota
// LeftOuterJoin means left join.
LeftOuterJoin
// RightOuterJoin means right join.
RightOuterJoin
// SemiJoin means if row a in table A matches some rows in B, just output a.
SemiJoin
// LeftOuterSemiJoin means if row a in table A matches some rows in B, output (a, true), otherwise, output (a, false).
LeftOuterSemiJoin
)
func (tp JoinType) String() string {
switch tp {
case InnerJoin:
return "inner join"
case LeftOuterJoin:
return "left outer join"
case RightOuterJoin:
return "right outer join"
case SemiJoin:
return "semi join"
case LeftOuterSemiJoin:
return "left outer semi join"
}
return "unsupported join type"
}
const (
preferLeftAsOuter = 1 << iota
preferRightAsOuter
)
// LogicalJoin is the logical join plan.
type LogicalJoin struct {
*basePlan
baseLogicalPlan
JoinType JoinType
anti bool
reordered bool
cartesianJoin bool
preferINLJ int
preferMergeJoin bool
EqualConditions []*expression.ScalarFunction
LeftConditions expression.CNFExprs
RightConditions expression.CNFExprs
OtherConditions expression.CNFExprs
LeftJoinKeys []*expression.Column
RightJoinKeys []*expression.Column
leftProperties [][]*expression.Column
rightProperties [][]*expression.Column
// DefaultValues is only used for outer join, which stands for the default values when the outer table cannot find join partner
// instead of null padding.
DefaultValues []types.Datum
// redundantSchema contains columns which are eliminated in join.
// For select * from a join b using (c); a.c will in output schema, and b.c will in redundantSchema.
redundantSchema *expression.Schema
}
func (p *LogicalJoin) columnSubstitute(schema *expression.Schema, exprs []expression.Expression) {
for i, fun := range p.EqualConditions {
p.EqualConditions[i] = expression.ColumnSubstitute(fun, schema, exprs).(*expression.ScalarFunction)
}
for i, fun := range p.LeftConditions {
p.LeftConditions[i] = expression.ColumnSubstitute(fun, schema, exprs)
}
for i, fun := range p.RightConditions {
p.RightConditions[i] = expression.ColumnSubstitute(fun, schema, exprs)
}
for i, fun := range p.OtherConditions {
p.OtherConditions[i] = expression.ColumnSubstitute(fun, schema, exprs)
}
}
func (p *LogicalJoin) attachOnConds(onConds []expression.Expression) {
eq, left, right, other := extractOnCondition(onConds, p.children[0].(LogicalPlan), p.children[1].(LogicalPlan))
p.EqualConditions = append(eq, p.EqualConditions...)
p.LeftConditions = append(left, p.LeftConditions...)
p.RightConditions = append(right, p.RightConditions...)
p.OtherConditions = append(other, p.OtherConditions...)
}
func (p *LogicalJoin) extractCorrelatedCols() []*expression.CorrelatedColumn {
corCols := p.basePlan.extractCorrelatedCols()
for _, fun := range p.EqualConditions {
corCols = append(corCols, extractCorColumns(fun)...)
}
for _, fun := range p.LeftConditions {
corCols = append(corCols, extractCorColumns(fun)...)
}
for _, fun := range p.RightConditions {
corCols = append(corCols, extractCorColumns(fun)...)
}
for _, fun := range p.OtherConditions {
corCols = append(corCols, extractCorColumns(fun)...)
}
return corCols
}
// Projection represents a select fields plan.
type Projection struct {
*basePlan
baseLogicalPlan
basePhysicalPlan
Exprs []expression.Expression
// calculateGenCols indicates the projection is for calculating generated columns.
// In *UPDATE*, we should know this to tell different projections.
calculateGenCols bool
}
func (p *Projection) extractCorrelatedCols() []*expression.CorrelatedColumn {
corCols := p.basePlan.extractCorrelatedCols()
for _, expr := range p.Exprs {
corCols = append(corCols, extractCorColumns(expr)...)
}
return corCols
}
// LogicalAggregation represents an aggregate plan.
type LogicalAggregation struct {
*basePlan
baseLogicalPlan
AggFuncs []aggregation.Aggregation
GroupByItems []expression.Expression
// groupByCols stores the columns that are group-by items.
groupByCols []*expression.Column
possibleProperties [][]*expression.Column
inputCount float64 // inputCount is the input count of this plan.
}
func (p *LogicalAggregation) extractCorrelatedCols() []*expression.CorrelatedColumn {
corCols := p.basePlan.extractCorrelatedCols()
for _, expr := range p.GroupByItems {
corCols = append(corCols, extractCorColumns(expr)...)
}
for _, fun := range p.AggFuncs {
for _, arg := range fun.GetArgs() {
corCols = append(corCols, extractCorColumns(arg)...)
}
}
return corCols
}
// Selection means a filter.
type Selection struct {
*basePlan
baseLogicalPlan
basePhysicalPlan
// Originally the WHERE or ON condition is parsed into a single expression,
// but after we converted to CNF(Conjunctive normal form), it can be
// split into a list of AND conditions.
Conditions []expression.Expression
// onTable means if this selection's child is a table scan or index scan.
onTable bool
// If ScanController is true, then the child of this selection is a scan,
// which use pk or index. we will record the accessConditions, idxConditions,
// and tblConditions to control the below plan.
ScanController bool
// We will check this at decorrelate phase.
controllerStatus int
}
func (p *Selection) extractCorrelatedCols() []*expression.CorrelatedColumn {
corCols := p.basePlan.extractCorrelatedCols()
for _, cond := range p.Conditions {
corCols = append(corCols, extractCorColumns(cond)...)
}
return corCols
}
// LogicalApply gets one row from outer executor and gets one row from inner executor according to outer row.
type LogicalApply struct {
LogicalJoin
corCols []*expression.CorrelatedColumn
}
func (p *LogicalApply) extractCorrelatedCols() []*expression.CorrelatedColumn {
corCols := p.LogicalJoin.extractCorrelatedCols()
for i := len(corCols) - 1; i >= 0; i-- {
if p.children[0].Schema().Contains(&corCols[i].Column) {
corCols = append(corCols[:i], corCols[i+1:]...)
}
}
return corCols
}
// Exists checks if a query returns result.
type Exists struct {
*basePlan
baseLogicalPlan
basePhysicalPlan
}
// MaxOneRow checks if a query returns no more than one row.
type MaxOneRow struct {
*basePlan
baseLogicalPlan
basePhysicalPlan
}
// TableDual represents a dual table plan.
type TableDual struct {
*basePlan
baseLogicalPlan
basePhysicalPlan
RowCount int
}
// DataSource represents a tablescan without condition push down.
type DataSource struct {
*basePlan
baseLogicalPlan
indexHints []*ast.IndexHint
tableInfo *model.TableInfo
Columns []*model.ColumnInfo
DBName model.CIStr
TableAsName *model.CIStr
LimitCount *int64
// pushedDownConds are the conditions that will be pushed down to coprocessor.
pushedDownConds []expression.Expression
statisticTable *statistics.Table
// NeedColHandle is used in execution phase.
NeedColHandle bool
// This is schema the PhysicalUnionScan should be.
unionScanSchema *expression.Schema
}
func (p *DataSource) getPKIsHandleCol() *expression.Column {
if !p.tableInfo.PKIsHandle {
return nil
}
for i, col := range p.Columns {
if mysql.HasPriKeyFlag(col.Flag) {
return p.schema.Columns[i]
}
}
return nil
}
// TableInfo returns the *TableInfo of data source.
func (p *DataSource) TableInfo() *model.TableInfo {
return p.tableInfo
}
// Schema implements the plan interface.
func (p *DataSource) Schema() *expression.Schema {
if p.unionScanSchema != nil {
return p.unionScanSchema
}
return p.schema
}
// Union represents Union plan.
type Union struct {
*basePlan
baseLogicalPlan
basePhysicalPlan
}
// Sort stands for the order by plan.
type Sort struct {
*basePlan
baseLogicalPlan
basePhysicalPlan
ByItems []*ByItems
ExecLimit *Limit // no longer be used by new plan
}
func (p *Sort) extractCorrelatedCols() []*expression.CorrelatedColumn {
corCols := p.basePlan.extractCorrelatedCols()
for _, item := range p.ByItems {
corCols = append(corCols, extractCorColumns(item.Expr)...)
}
return corCols
}
// TopN represents a top-n plan.
type TopN struct {
*basePlan
baseLogicalPlan
basePhysicalPlan
ByItems []*ByItems
Offset uint64
Count uint64
// partial is true if this topn is generated by push-down optimization.
partial bool
}
// isLimit checks if TopN is a limit plan.
func (t *TopN) isLimit() bool {
return len(t.ByItems) == 0
}
// Limit represents offset and limit plan.
type Limit struct {
*basePlan
baseLogicalPlan
basePhysicalPlan
Offset uint64
Count uint64
// partial is true if this topn is generated by push-down optimization.
partial bool
expectedProp *requiredProp
}
// Update represents Update plan.
type Update struct {
*basePlan
baseLogicalPlan
basePhysicalPlan
OrderedList []*expression.Assignment
IgnoreErr bool
}
// Delete represents a delete plan.
type Delete struct {
*basePlan
baseLogicalPlan
basePhysicalPlan
Tables []*ast.TableName
IsMultiTable bool
}
// setParentAndChildren sets parent and children relationship.
func setParentAndChildren(parent Plan, children ...Plan) {
if children == nil || parent == nil {
return
}
for _, child := range children {
child.SetParents(parent)
}
parent.SetChildren(children...)
}
// InsertPlan means inserting plan between two plans.
func InsertPlan(parent Plan, child Plan, insert Plan) error {
err := child.ReplaceParent(parent, insert)
if err != nil {
return errors.Trace(err)
}
err = parent.ReplaceChild(child, insert)
if err != nil {
return errors.Trace(err)
}
insert.AddChild(child)
insert.AddParent(parent)
return nil
}
// RemovePlan means removing a plan.
func RemovePlan(p Plan) error {
parents := p.Parents()
children := p.Children()
if len(parents) > 1 || len(children) != 1 {
return SystemInternalErrorType.Gen("can't remove this plan")
}
if len(parents) == 0 {
child := children[0]
child.SetParents()
return nil
}
parent, child := parents[0], children[0]
err := parent.ReplaceChild(p, child)
if err != nil {
return errors.Trace(err)
}
err = child.ReplaceParent(p, parent)
return errors.Trace(err)
}