forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logical_plans.go
381 lines (319 loc) · 11.1 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
// 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/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/types"
)
var (
_ LogicalPlan = &LogicalJoin{}
_ LogicalPlan = &LogicalAggregation{}
_ LogicalPlan = &LogicalProjection{}
_ LogicalPlan = &LogicalSelection{}
_ LogicalPlan = &LogicalApply{}
_ LogicalPlan = &LogicalExists{}
_ LogicalPlan = &LogicalMaxOneRow{}
_ LogicalPlan = &LogicalTableDual{}
_ LogicalPlan = &DataSource{}
_ LogicalPlan = &LogicalUnionAll{}
_ LogicalPlan = &LogicalSort{}
_ LogicalPlan = &LogicalLock{}
_ LogicalPlan = &LogicalLimit{}
)
// 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
// AntiSemiJoin means if row a in table A does not match any row in B, then output a.
AntiSemiJoin
// LeftOuterSemiJoin means if row a in table A matches some rows in B, output (a, true), otherwise, output (a, false).
LeftOuterSemiJoin
// AntiLeftOuterSemiJoin means if row a in table A matches some rows in B, output (a, false), otherwise, output (a, true).
AntiLeftOuterSemiJoin
)
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 AntiSemiJoin:
return "anti semi join"
case LeftOuterSemiJoin:
return "left outer semi join"
case AntiLeftOuterSemiJoin:
return "anti left outer semi join"
}
return "unsupported join type"
}
const (
preferLeftAsIndexOuter = 1 << iota
preferRightAsIndexOuter
preferHashJoin
preferMergeJoin
)
// LogicalJoin is the logical join plan.
type LogicalJoin struct {
logicalSchemaProducer
JoinType JoinType
reordered bool
cartesianJoin bool
StraightJoin bool
preferJoinType uint
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 left/right outer join, which is values the inner row's should be when the outer table
// doesn't match any inner table's row.
// That it's nil just means the default values is a slice of NULL.
// Currently, only `aggregation push down` phase will set this.
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 := len(p.EqualConditions) - 1; i >= 0; i-- {
p.EqualConditions[i] = expression.ColumnSubstitute(p.EqualConditions[i], schema, exprs).(*expression.ScalarFunction)
// After the column substitute, the equal condition may become single side condition.
if p.children[0].Schema().Contains(p.EqualConditions[i].GetArgs()[1].(*expression.Column)) {
p.LeftConditions = append(p.LeftConditions, p.EqualConditions[i])
p.EqualConditions = append(p.EqualConditions[:i], p.EqualConditions[i+1:]...)
} else if p.children[1].Schema().Contains(p.EqualConditions[i].GetArgs()[0].(*expression.Column)) {
p.RightConditions = append(p.RightConditions, p.EqualConditions[i])
p.EqualConditions = append(p.EqualConditions[:i], p.EqualConditions[i+1:]...)
}
}
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.baseLogicalPlan.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
}
// LogicalProjection represents a select fields plan.
type LogicalProjection struct {
logicalSchemaProducer
Exprs []expression.Expression
// calculateGenCols indicates the projection is for calculating generated columns.
// In *UPDATE*, we should know this to tell different projections.
calculateGenCols bool
// calculateNoDelay indicates this Projection is the root Plan and should be
// calculated without delay and will not return any result to client.
// Currently it is "true" only when the current sql query is a "DO" statement.
// See "https://dev.mysql.com/doc/refman/5.7/en/do.html" for more detail.
calculateNoDelay bool
}
func (p *LogicalProjection) extractCorrelatedCols() []*expression.CorrelatedColumn {
corCols := p.baseLogicalPlan.extractCorrelatedCols()
for _, expr := range p.Exprs {
corCols = append(corCols, extractCorColumns(expr)...)
}
return corCols
}
// LogicalAggregation represents an aggregate plan.
type LogicalAggregation struct {
logicalSchemaProducer
AggFuncs []*aggregation.AggFuncDesc
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 (la *LogicalAggregation) extractCorrelatedCols() []*expression.CorrelatedColumn {
corCols := la.baseLogicalPlan.extractCorrelatedCols()
for _, expr := range la.GroupByItems {
corCols = append(corCols, extractCorColumns(expr)...)
}
for _, fun := range la.AggFuncs {
for _, arg := range fun.Args {
corCols = append(corCols, extractCorColumns(arg)...)
}
}
return corCols
}
// LogicalSelection represents a where or having predicate.
type LogicalSelection struct {
baseLogicalPlan
// 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
}
func (p *LogicalSelection) extractCorrelatedCols() []*expression.CorrelatedColumn {
corCols := p.baseLogicalPlan.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 (la *LogicalApply) extractCorrelatedCols() []*expression.CorrelatedColumn {
corCols := la.LogicalJoin.extractCorrelatedCols()
for i := len(corCols) - 1; i >= 0; i-- {
if la.children[0].Schema().Contains(&corCols[i].Column) {
corCols = append(corCols[:i], corCols[i+1:]...)
}
}
return corCols
}
// LogicalExists checks if a query returns result.
type LogicalExists struct {
logicalSchemaProducer
}
// LogicalMaxOneRow checks if a query returns no more than one row.
type LogicalMaxOneRow struct {
baseLogicalPlan
}
// LogicalTableDual represents a dual table plan.
type LogicalTableDual struct {
logicalSchemaProducer
RowCount int
}
// LogicalUnionScan is only used in non read-only txn.
type LogicalUnionScan struct {
baseLogicalPlan
conditions []expression.Expression
}
// DataSource represents a tablescan without condition push down.
type DataSource struct {
logicalSchemaProducer
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
// relevantIndices means the indices match the push down conditions
relevantIndices []bool
// statsAfterSelect is the statsInfo for dataSource and selection.
statsAfterSelect *statsInfo
statisticTable *statistics.Table
// availableIndices is used for storing result of availableIndices function.
availableIndices *availableIndices
}
type availableIndices struct {
indices []*model.IndexInfo
includeTableScan bool
}
func (ds *DataSource) getPKIsHandleCol() *expression.Column {
if !ds.tableInfo.PKIsHandle {
return nil
}
for i, col := range ds.Columns {
if mysql.HasPriKeyFlag(col.Flag) {
return ds.schema.Columns[i]
}
}
return nil
}
// TableInfo returns the *TableInfo of data source.
func (ds *DataSource) TableInfo() *model.TableInfo {
return ds.tableInfo
}
// LogicalUnionAll represents LogicalUnionAll plan.
type LogicalUnionAll struct {
baseLogicalPlan
}
// LogicalSort stands for the order by plan.
type LogicalSort struct {
baseLogicalPlan
ByItems []*ByItems
}
func (ls *LogicalSort) extractCorrelatedCols() []*expression.CorrelatedColumn {
corCols := ls.baseLogicalPlan.extractCorrelatedCols()
for _, item := range ls.ByItems {
corCols = append(corCols, extractCorColumns(item.Expr)...)
}
return corCols
}
// LogicalTopN represents a top-n plan.
type LogicalTopN struct {
baseLogicalPlan
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 (lt *LogicalTopN) isLimit() bool {
return len(lt.ByItems) == 0
}
// LogicalLimit represents offset and limit plan.
type LogicalLimit struct {
baseLogicalPlan
Offset uint64
Count uint64
// partial is true if this topn is generated by push-down optimization.
partial bool
}
// LogicalLock represents a select lock plan.
type LogicalLock struct {
baseLogicalPlan
Lock ast.SelectLockType
}