forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
physical_plans.go
359 lines (289 loc) · 9.54 KB
/
physical_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
// 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/sessionctx/stmtctx"
"github.com/pingcap/tidb/statistics"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/ranger"
)
var (
_ PhysicalPlan = &PhysicalSelection{}
_ PhysicalPlan = &PhysicalProjection{}
_ PhysicalPlan = &PhysicalTopN{}
_ PhysicalPlan = &PhysicalExists{}
_ PhysicalPlan = &PhysicalMaxOneRow{}
_ PhysicalPlan = &PhysicalTableDual{}
_ PhysicalPlan = &PhysicalUnionAll{}
_ PhysicalPlan = &PhysicalSort{}
_ PhysicalPlan = &NominalSort{}
_ PhysicalPlan = &PhysicalLock{}
_ PhysicalPlan = &PhysicalLimit{}
_ PhysicalPlan = &PhysicalIndexScan{}
_ PhysicalPlan = &PhysicalTableScan{}
_ PhysicalPlan = &PhysicalTableReader{}
_ PhysicalPlan = &PhysicalIndexReader{}
_ PhysicalPlan = &PhysicalIndexLookUpReader{}
_ PhysicalPlan = &PhysicalHashAgg{}
_ PhysicalPlan = &PhysicalStreamAgg{}
_ PhysicalPlan = &PhysicalApply{}
_ PhysicalPlan = &PhysicalIndexJoin{}
_ PhysicalPlan = &PhysicalHashJoin{}
_ PhysicalPlan = &PhysicalMergeJoin{}
_ PhysicalPlan = &PhysicalUnionScan{}
)
// PhysicalTableReader is the table reader in tidb.
type PhysicalTableReader struct {
physicalSchemaProducer
// TablePlans flats the tablePlan to construct executor pb.
TablePlans []PhysicalPlan
tablePlan PhysicalPlan
}
// PhysicalIndexReader is the index reader in tidb.
type PhysicalIndexReader struct {
physicalSchemaProducer
// IndexPlans flats the indexPlan to construct executor pb.
IndexPlans []PhysicalPlan
indexPlan PhysicalPlan
// OutputColumns represents the columns that index reader should return.
OutputColumns []*expression.Column
}
// PhysicalIndexLookUpReader is the index look up reader in tidb. It's used in case of double reading.
type PhysicalIndexLookUpReader struct {
physicalSchemaProducer
// IndexPlans flats the indexPlan to construct executor pb.
IndexPlans []PhysicalPlan
// TablePlans flats the tablePlan to construct executor pb.
TablePlans []PhysicalPlan
indexPlan PhysicalPlan
tablePlan PhysicalPlan
}
// PhysicalIndexScan represents an index scan plan.
type PhysicalIndexScan struct {
physicalSchemaProducer
// AccessCondition is used to calculate range.
AccessCondition []expression.Expression
filterCondition []expression.Expression
Table *model.TableInfo
Index *model.IndexInfo
Ranges []*ranger.NewRange
Columns []*model.ColumnInfo
DBName model.CIStr
Desc bool
KeepOrder bool
// DoubleRead means if the index executor will read kv two times.
// If the query requires the columns that don't belong to index, DoubleRead will be true.
DoubleRead bool
TableAsName *model.CIStr
// dataSourceSchema is the original schema of DataSource. The schema of index scan in KV and index reader in TiDB
// will be different. The schema of index scan will decode all columns of index but the TiDB only need some of them.
dataSourceSchema *expression.Schema
// Hist is the histogram when the query was issued.
// It is used for query feedback.
Hist *statistics.Histogram
}
// PhysicalMemTable reads memory table.
type PhysicalMemTable struct {
physicalSchemaProducer
DBName model.CIStr
Table *model.TableInfo
Columns []*model.ColumnInfo
Ranges []ranger.IntColumnRange
TableAsName *model.CIStr
}
func needCount(af *aggregation.AggFuncDesc) bool {
return af.Name == ast.AggFuncCount || af.Name == ast.AggFuncAvg
}
func needValue(af *aggregation.AggFuncDesc) bool {
return af.Name == ast.AggFuncSum || af.Name == ast.AggFuncAvg || af.Name == ast.AggFuncFirstRow ||
af.Name == ast.AggFuncMax || af.Name == ast.AggFuncMin || af.Name == ast.AggFuncGroupConcat ||
af.Name == ast.AggFuncBitOr || af.Name == ast.AggFuncBitAnd || af.Name == ast.AggFuncBitXor
}
// PhysicalTableScan represents a table scan plan.
type PhysicalTableScan struct {
physicalSchemaProducer
// AccessCondition is used to calculate range.
AccessCondition []expression.Expression
filterCondition []expression.Expression
Table *model.TableInfo
Columns []*model.ColumnInfo
DBName model.CIStr
Desc bool
Ranges []*ranger.NewRange
pkCol *expression.Column
TableAsName *model.CIStr
// KeepOrder is true, if sort data by scanning pkcol,
KeepOrder bool
// Hist is the histogram when the query was issued.
// It is used for query feedback.
Hist *statistics.Histogram
}
// PhysicalProjection is the physical operator of projection.
type PhysicalProjection struct {
physicalSchemaProducer
Exprs []expression.Expression
CalculateNoDelay bool
AvoidColumnEvaluator bool
}
// PhysicalTopN is the physical operator of topN.
type PhysicalTopN struct {
basePhysicalPlan
ByItems []*ByItems
Offset uint64
Count uint64
}
// PhysicalApply represents apply plan, only used for subquery.
type PhysicalApply struct {
physicalSchemaProducer
PhysicalJoin *PhysicalHashJoin
OuterSchema []*expression.CorrelatedColumn
rightChOffset int
}
// PhysicalHashJoin represents hash join for inner/ outer join.
type PhysicalHashJoin struct {
physicalSchemaProducer
JoinType JoinType
EqualConditions []*expression.ScalarFunction
LeftConditions []expression.Expression
RightConditions []expression.Expression
OtherConditions []expression.Expression
// InnerChildIdx indicates which child is to build the hash table.
// For inner join, the smaller one will be chosen.
// For outer join or semi join, it's exactly the inner one.
InnerChildIdx int
Concurrency uint
DefaultValues []types.Datum
}
// PhysicalIndexJoin represents the plan of index look up join.
type PhysicalIndexJoin struct {
physicalSchemaProducer
JoinType JoinType
OuterJoinKeys []*expression.Column
InnerJoinKeys []*expression.Column
LeftConditions expression.CNFExprs
RightConditions expression.CNFExprs
OtherConditions expression.CNFExprs
OuterIndex int
outerSchema *expression.Schema
innerPlan PhysicalPlan
DefaultValues []types.Datum
// Ranges stores the IndexRanges when the inner plan is index scan.
Ranges []*ranger.NewRange
// KeyOff2IdxOff maps the offsets in join key to the offsets in the index.
KeyOff2IdxOff []int
}
// PhysicalMergeJoin represents merge join for inner/ outer join.
type PhysicalMergeJoin struct {
physicalSchemaProducer
JoinType JoinType
LeftConditions []expression.Expression
RightConditions []expression.Expression
OtherConditions []expression.Expression
DefaultValues []types.Datum
LeftKeys []*expression.Column
RightKeys []*expression.Column
}
// PhysicalLock is the physical operator of lock, which is used for `select ... for update` clause.
type PhysicalLock struct {
basePhysicalPlan
Lock ast.SelectLockType
}
// PhysicalLimit is the physical operator of Limit.
type PhysicalLimit struct {
basePhysicalPlan
Offset uint64
Count uint64
}
// PhysicalUnionAll is the physical operator of UnionAll.
type PhysicalUnionAll struct {
basePhysicalPlan
}
// AggregationType stands for the mode of aggregation plan.
type AggregationType int
const (
// StreamedAgg supposes its input is sorted by group by key.
StreamedAgg AggregationType = iota
// FinalAgg supposes its input is partial results.
FinalAgg
// CompleteAgg supposes its input is original results.
CompleteAgg
)
// String implements fmt.Stringer interface.
func (at AggregationType) String() string {
switch at {
case StreamedAgg:
return "stream"
case FinalAgg:
return "final"
case CompleteAgg:
return "complete"
}
return "unsupported aggregation type"
}
type basePhysicalAgg struct {
physicalSchemaProducer
AggFuncs []*aggregation.AggFuncDesc
GroupByItems []expression.Expression
}
// PhysicalHashAgg is hash operator of aggregate.
type PhysicalHashAgg struct {
basePhysicalAgg
}
// PhysicalStreamAgg is stream operator of aggregate.
type PhysicalStreamAgg struct {
basePhysicalAgg
}
// PhysicalSort is the physical operator of sort, which implements a memory sort.
type PhysicalSort struct {
basePhysicalPlan
ByItems []*ByItems
}
// NominalSort asks sort properties for its child. It is a fake operator that will not
// appear in final physical operator tree.
type NominalSort struct {
basePhysicalPlan
}
// PhysicalUnionScan represents a union scan operator.
type PhysicalUnionScan struct {
basePhysicalPlan
Conditions []expression.Expression
}
// IsPointGetByUniqueKey checks whether is a point get by unique key.
func (p *PhysicalIndexScan) IsPointGetByUniqueKey(sc *stmtctx.StatementContext) bool {
return len(p.Ranges) == 1 &&
p.Index.Unique &&
len(p.Ranges[0].LowVal) == len(p.Index.Columns) &&
p.Ranges[0].IsPoint(sc)
}
// PhysicalSelection represents a filter.
type PhysicalSelection struct {
basePhysicalPlan
Conditions []expression.Expression
}
// PhysicalExists is the physical operator of Exists.
type PhysicalExists struct {
physicalSchemaProducer
}
// PhysicalMaxOneRow is the physical operator of maxOneRow.
type PhysicalMaxOneRow struct {
basePhysicalPlan
}
// PhysicalTableDual is the physical operator of dual.
type PhysicalTableDual struct {
physicalSchemaProducer
RowCount int
}