-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
point_get_plan.go
514 lines (483 loc) · 14.2 KB
/
point_get_plan.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
// Copyright 2018 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 core
import (
"bytes"
"fmt"
"github.com/pingcap/errors"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/opcode"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/planner/property"
"github.com/pingcap/tidb/privilege"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/types/parser_driver"
"github.com/pingcap/tipb/go-tipb"
)
// PointGetPlan is a fast plan for simple point get.
// When we detect that the statement has a unique equal access condition, this plan is used.
// This plan is much faster to build and to execute because it avoid the optimization and coprocessor cost.
type PointGetPlan struct {
basePlan
schema *expression.Schema
TblInfo *model.TableInfo
IndexInfo *model.IndexInfo
Handle int64
HandleParam *driver.ParamMarkerExpr
UnsignedHandle bool
IndexValues []types.Datum
IndexValueParams []*driver.ParamMarkerExpr
expr expression.Expression
ctx sessionctx.Context
}
type nameValuePair struct {
colName string
value types.Datum
param *driver.ParamMarkerExpr
}
// Schema implements the Plan interface.
func (p *PointGetPlan) Schema() *expression.Schema {
return p.schema
}
// attach2Task makes the current physical plan as the father of task's physicalPlan and updates the cost of
// current task. If the child's task is cop task, some operator may close this task and return a new rootTask.
func (p *PointGetPlan) attach2Task(...task) task {
return nil
}
// ToPB converts physical plan to tipb executor.
func (p *PointGetPlan) ToPB(ctx sessionctx.Context) (*tipb.Executor, error) {
return nil, nil
}
// ExplainInfo returns operator information to be explained.
func (p *PointGetPlan) ExplainInfo() string {
buffer := bytes.NewBufferString("")
tblName := p.TblInfo.Name.O
fmt.Fprintf(buffer, "table:%s", tblName)
if p.IndexInfo != nil {
fmt.Fprintf(buffer, ", index:")
for i, col := range p.IndexInfo.Columns {
buffer.WriteString(col.Name.O)
if i < len(p.IndexInfo.Columns)-1 {
buffer.WriteString(" ")
}
}
} else {
fmt.Fprintf(buffer, ", handle:%d", p.Handle)
}
return buffer.String()
}
// GetChildReqProps gets the required property by child index.
func (p *PointGetPlan) GetChildReqProps(idx int) *property.PhysicalProperty {
return nil
}
// StatsCount will return the the RowCount of property.StatsInfo for this plan.
func (p *PointGetPlan) StatsCount() float64 {
return 1
}
// statsInfo will return the the RowCount of property.StatsInfo for this plan.
func (p *PointGetPlan) statsInfo() *property.StatsInfo {
if p.stats == nil {
p.stats = &property.StatsInfo{}
}
p.stats.RowCount = 1
return p.stats
}
// Children gets all the children.
func (p *PointGetPlan) Children() []PhysicalPlan {
return nil
}
// SetChildren sets the children for the plan.
func (p *PointGetPlan) SetChildren(...PhysicalPlan) {}
// ResolveIndices resolves the indices for columns. After doing this, the columns can evaluate the rows by their indices.
func (p *PointGetPlan) ResolveIndices() error {
return nil
}
// TryFastPlan tries to use the PointGetPlan for the query.
func TryFastPlan(ctx sessionctx.Context, node ast.Node) Plan {
switch x := node.(type) {
case *ast.SelectStmt:
fp := tryPointGetPlan(ctx, x)
if fp != nil {
if checkFastPlanPrivilege(ctx, fp, mysql.SelectPriv) != nil {
return nil
}
return fp
}
case *ast.UpdateStmt:
return tryUpdatePointPlan(ctx, x)
case *ast.DeleteStmt:
return tryDeletePointPlan(ctx, x)
}
return nil
}
// tryPointGetPlan determine if the SelectStmt can use a PointGetPlan.
// Returns nil if not applicable.
// To use the PointGetPlan the following rules must be satisfied:
// 1. For the limit clause, the count should at least 1 and the offset is 0.
// 2. It must be a single table select.
// 3. All the columns must be public and generated.
// 4. The condition is an access path that the range is a unique key.
func tryPointGetPlan(ctx sessionctx.Context, selStmt *ast.SelectStmt) *PointGetPlan {
if selStmt.Having != nil || selStmt.LockTp != ast.SelectLockNone {
return nil
} else if selStmt.Limit != nil {
count, offset, err := extractLimitCountOffset(ctx, selStmt.Limit)
if err != nil || count == 0 || offset > 0 {
return nil
}
}
tblName := getSingleTableName(selStmt.From)
if tblName == nil {
return nil
}
tbl := tblName.TableInfo
if tbl == nil {
return nil
}
// Do not handle partitioned table.
// Table partition implementation translates LogicalPlan from `DataSource` to
// `Union -> DataSource` in the logical plan optimization pass, since PointGetPlan
// bypass the logical plan optimization, it can't support partitioned table.
if tbl.GetPartitionInfo() != nil {
return nil
}
for _, col := range tbl.Columns {
// Do not handle generated columns.
if col.IsGenerated() {
return nil
}
// Only handle tables that all columns are public.
if col.State != model.StatePublic {
return nil
}
}
pairs := make([]nameValuePair, 0, 4)
pairs = getNameValuePairs(pairs, selStmt.Where)
if pairs == nil {
return nil
}
handlePair, unsigned := findPKHandle(tbl, pairs)
if handlePair.value.Kind() != types.KindNull && len(pairs) == 1 {
schema := buildSchemaFromFields(ctx, tblName.Schema, tbl, selStmt.Fields.Fields)
if schema == nil {
return nil
}
p := newPointGetPlan(ctx, schema, tbl)
var err error
p.Handle, err = handlePair.value.ToInt64(ctx.GetSessionVars().StmtCtx)
if err != nil {
return nil
}
p.UnsignedHandle = unsigned
p.HandleParam = handlePair.param
return p
}
for _, idxInfo := range tbl.Indices {
if !idxInfo.Unique {
continue
}
if idxInfo.State != model.StatePublic {
continue
}
idxValues, idxValueParams := getIndexValues(idxInfo, pairs)
if idxValues == nil {
continue
}
schema := buildSchemaFromFields(ctx, tblName.Schema, tbl, selStmt.Fields.Fields)
if schema == nil {
return nil
}
p := newPointGetPlan(ctx, schema, tbl)
p.IndexInfo = idxInfo
p.IndexValues = idxValues
p.IndexValueParams = idxValueParams
return p
}
return nil
}
func newPointGetPlan(ctx sessionctx.Context, schema *expression.Schema, tbl *model.TableInfo) *PointGetPlan {
p := &PointGetPlan{
basePlan: newBasePlan(ctx, "Point_Get"),
schema: schema,
TblInfo: tbl,
}
return p
}
func checkFastPlanPrivilege(ctx sessionctx.Context, fastPlan *PointGetPlan, checkTypes ...mysql.PrivilegeType) error {
pm := privilege.GetPrivilegeManager(ctx)
if pm == nil {
return nil
}
dbName := ctx.GetSessionVars().CurrentDB
for _, checkType := range checkTypes {
if !pm.RequestVerification(ctx.GetSessionVars().ActiveRoles, dbName, fastPlan.TblInfo.Name.L, "", checkType) {
return errors.New("privilege check fail")
}
}
return nil
}
func buildSchemaFromFields(ctx sessionctx.Context, dbName model.CIStr, tbl *model.TableInfo, fields []*ast.SelectField) *expression.Schema {
if dbName.L == "" {
dbName = model.NewCIStr(ctx.GetSessionVars().CurrentDB)
}
columns := make([]*expression.Column, 0, len(tbl.Columns)+1)
if len(fields) == 1 && fields[0].WildCard != nil {
for _, col := range tbl.Columns {
columns = append(columns, colInfoToColumn(dbName, tbl.Name, col.Name, col, len(columns)))
}
return expression.NewSchema(columns...)
}
if len(fields) > 0 {
for _, field := range fields {
colNameExpr, ok := field.Expr.(*ast.ColumnNameExpr)
if !ok {
return nil
}
col := findCol(tbl, colNameExpr.Name)
if col == nil {
return nil
}
asName := col.Name
if field.AsName.L != "" {
asName = field.AsName
}
columns = append(columns, colInfoToColumn(dbName, tbl.Name, asName, col, len(columns)))
}
return expression.NewSchema(columns...)
}
// fields len is 0 for update and delete.
var handleCol *expression.Column
for _, col := range tbl.Columns {
column := colInfoToColumn(dbName, tbl.Name, col.Name, col, len(columns))
if tbl.PKIsHandle && mysql.HasPriKeyFlag(col.Flag) {
handleCol = column
}
columns = append(columns, column)
}
if handleCol == nil {
handleCol = colInfoToColumn(dbName, tbl.Name, model.ExtraHandleName, model.NewExtraHandleColInfo(), len(columns))
columns = append(columns, handleCol)
}
schema := expression.NewSchema(columns...)
schema.TblID2Handle = make(map[int64][]*expression.Column)
schema.TblID2Handle[tbl.ID] = []*expression.Column{handleCol}
return schema
}
func getSingleTableName(tableRefs *ast.TableRefsClause) *ast.TableName {
if tableRefs == nil || tableRefs.TableRefs == nil || tableRefs.TableRefs.Right != nil {
return nil
}
tblSrc, ok := tableRefs.TableRefs.Left.(*ast.TableSource)
if !ok {
return nil
}
if tblSrc.AsName.L != "" {
return nil
}
tblName, ok := tblSrc.Source.(*ast.TableName)
if !ok {
return nil
}
return tblName
}
// getNameValuePairs extracts `column = constant/paramMarker` conditions from expr as name value pairs.
func getNameValuePairs(nvPairs []nameValuePair, expr ast.ExprNode) []nameValuePair {
binOp, ok := expr.(*ast.BinaryOperationExpr)
if !ok {
return nil
}
if binOp.Op == opcode.LogicAnd {
nvPairs = getNameValuePairs(nvPairs, binOp.L)
if nvPairs == nil {
return nil
}
nvPairs = getNameValuePairs(nvPairs, binOp.R)
if nvPairs == nil {
return nil
}
return nvPairs
} else if binOp.Op == opcode.EQ {
var d types.Datum
var colName *ast.ColumnNameExpr
var param *driver.ParamMarkerExpr
var ok bool
if colName, ok = binOp.L.(*ast.ColumnNameExpr); ok {
switch x := binOp.R.(type) {
case *driver.ValueExpr:
d = x.Datum
case *driver.ParamMarkerExpr:
d = x.Datum
param = x
}
} else if colName, ok = binOp.R.(*ast.ColumnNameExpr); ok {
switch x := binOp.L.(type) {
case *driver.ValueExpr:
d = x.Datum
case *driver.ParamMarkerExpr:
d = x.Datum
param = x
}
} else {
return nil
}
if d.IsNull() {
return nil
}
return append(nvPairs, nameValuePair{colName: colName.Name.Name.L, value: d, param: param})
}
return nil
}
func findPKHandle(tblInfo *model.TableInfo, pairs []nameValuePair) (handlePair nameValuePair, unsigned bool) {
if !tblInfo.PKIsHandle {
return handlePair, unsigned
}
for _, col := range tblInfo.Columns {
if mysql.HasPriKeyFlag(col.Flag) {
i := findInPairs(col.Name.L, pairs)
if i == -1 {
return handlePair, unsigned
}
return pairs[i], mysql.HasUnsignedFlag(col.Flag)
}
}
return handlePair, unsigned
}
func getIndexValues(idxInfo *model.IndexInfo, pairs []nameValuePair) ([]types.Datum, []*driver.ParamMarkerExpr) {
idxValues := make([]types.Datum, 0, 4)
idxValueParams := make([]*driver.ParamMarkerExpr, 0, 4)
if len(idxInfo.Columns) != len(pairs) {
return nil, nil
}
if idxInfo.HasPrefixIndex() {
return nil, nil
}
for _, idxCol := range idxInfo.Columns {
i := findInPairs(idxCol.Name.L, pairs)
if i == -1 {
return nil, nil
}
idxValues = append(idxValues, pairs[i].value)
idxValueParams = append(idxValueParams, pairs[i].param)
}
if len(idxValues) > 0 {
return idxValues, idxValueParams
}
return nil, nil
}
func findInPairs(colName string, pairs []nameValuePair) int {
for i, pair := range pairs {
if pair.colName == colName {
return i
}
}
return -1
}
func tryUpdatePointPlan(ctx sessionctx.Context, updateStmt *ast.UpdateStmt) Plan {
selStmt := &ast.SelectStmt{
Fields: &ast.FieldList{},
From: updateStmt.TableRefs,
Where: updateStmt.Where,
OrderBy: updateStmt.Order,
Limit: updateStmt.Limit,
}
fastSelect := tryPointGetPlan(ctx, selStmt)
if fastSelect == nil {
return nil
}
if checkFastPlanPrivilege(ctx, fastSelect, mysql.SelectPriv, mysql.UpdatePriv) != nil {
return nil
}
orderedList := buildOrderedList(ctx, fastSelect, updateStmt.List)
if orderedList == nil {
return nil
}
updatePlan := Update{
SelectPlan: fastSelect,
OrderedList: orderedList,
}.Init(ctx)
updatePlan.SetSchema(fastSelect.schema)
return updatePlan
}
func buildOrderedList(ctx sessionctx.Context, fastSelect *PointGetPlan, list []*ast.Assignment) []*expression.Assignment {
orderedList := make([]*expression.Assignment, 0, len(list))
for _, assign := range list {
col, err := fastSelect.schema.FindColumn(assign.Column)
if err != nil {
return nil
}
if col == nil {
return nil
}
newAssign := &expression.Assignment{
Col: col,
}
expr, err := expression.RewriteSimpleExprWithSchema(ctx, assign.Expr, fastSelect.schema)
if err != nil {
return nil
}
expr = expression.BuildCastFunction(ctx, expr, col.GetType())
newAssign.Expr, err = expr.ResolveIndices(fastSelect.schema)
if err != nil {
return nil
}
orderedList = append(orderedList, newAssign)
}
return orderedList
}
func tryDeletePointPlan(ctx sessionctx.Context, delStmt *ast.DeleteStmt) Plan {
if delStmt.IsMultiTable {
return nil
}
selStmt := &ast.SelectStmt{
Fields: &ast.FieldList{},
From: delStmt.TableRefs,
Where: delStmt.Where,
OrderBy: delStmt.Order,
Limit: delStmt.Limit,
}
fastSelect := tryPointGetPlan(ctx, selStmt)
if fastSelect == nil {
return nil
}
if checkFastPlanPrivilege(ctx, fastSelect, mysql.SelectPriv, mysql.DeletePriv) != nil {
return nil
}
delPlan := Delete{
SelectPlan: fastSelect,
}.Init(ctx)
delPlan.SetSchema(fastSelect.schema)
return delPlan
}
func findCol(tbl *model.TableInfo, colName *ast.ColumnName) *model.ColumnInfo {
for _, col := range tbl.Columns {
if col.Name.L == colName.Name.L {
return col
}
}
return nil
}
func colInfoToColumn(db model.CIStr, tblName model.CIStr, asName model.CIStr, col *model.ColumnInfo, idx int) *expression.Column {
return &expression.Column{
ColName: asName,
OrigTblName: tblName,
DBName: db,
TblName: tblName,
RetType: &col.FieldType,
ID: col.ID,
UniqueID: int64(col.Offset),
Index: idx,
}
}