forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhint_processor.go
488 lines (451 loc) · 14.8 KB
/
hint_processor.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
// Copyright 2020 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hint
import (
"fmt"
"strconv"
"strings"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/format"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/util/dbterror"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
)
var supportedHintNameForInsertStmt = map[string]struct{}{}
var errWarnConflictingHint = dbterror.ClassUtil.NewStd(errno.ErrWarnConflictingHint)
func init() {
supportedHintNameForInsertStmt["memory_quota"] = struct{}{}
}
// HintsSet contains all hints of a query.
type HintsSet struct {
tableHints [][]*ast.TableOptimizerHint // Slice offset is the traversal order of `SelectStmt` in the ast.
indexHints [][]*ast.IndexHint // Slice offset is the traversal order of `TableName` in the ast.
}
// GetFirstTableHints gets the first table hints.
func (hs *HintsSet) GetFirstTableHints() []*ast.TableOptimizerHint {
if len(hs.tableHints) > 0 {
return hs.tableHints[0]
}
return nil
}
// ContainTableHint checks whether the table hint set contains a hint.
func (hs *HintsSet) ContainTableHint(hint string) bool {
for _, tableHintsForBlock := range hs.tableHints {
for _, tableHint := range tableHintsForBlock {
if tableHint.HintName.String() == hint {
return true
}
}
}
return false
}
// setTableHints4StmtNode sets table hints for select/update/delete.
func setTableHints4StmtNode(node ast.Node, hints []*ast.TableOptimizerHint) {
switch x := node.(type) {
case *ast.SelectStmt:
x.TableHints = hints
case *ast.UpdateStmt:
x.TableHints = hints
case *ast.DeleteStmt:
x.TableHints = hints
}
}
// ExtractTableHintsFromStmtNode extracts table hints from this node.
func ExtractTableHintsFromStmtNode(node ast.Node, sctx sessionctx.Context) []*ast.TableOptimizerHint {
switch x := node.(type) {
case *ast.SelectStmt:
return x.TableHints
case *ast.UpdateStmt:
return x.TableHints
case *ast.DeleteStmt:
return x.TableHints
case *ast.InsertStmt:
// check duplicated hints
checkInsertStmtHintDuplicated(node, sctx)
return x.TableHints
case *ast.ExplainStmt:
return ExtractTableHintsFromStmtNode(x.Stmt, sctx)
default:
return nil
}
}
// checkInsertStmtHintDuplicated check whether existed the duplicated hints in both insertStmt and its selectStmt.
// If existed, it would send a warning message.
func checkInsertStmtHintDuplicated(node ast.Node, sctx sessionctx.Context) {
switch x := node.(type) {
case *ast.InsertStmt:
if len(x.TableHints) > 0 {
var supportedHint *ast.TableOptimizerHint
for _, hint := range x.TableHints {
if _, ok := supportedHintNameForInsertStmt[hint.HintName.L]; ok {
supportedHint = hint
break
}
}
if supportedHint != nil {
var duplicatedHint *ast.TableOptimizerHint
for _, hint := range ExtractTableHintsFromStmtNode(x.Select, nil) {
if hint.HintName.L == supportedHint.HintName.L {
duplicatedHint = hint
break
}
}
if duplicatedHint != nil {
hint := fmt.Sprintf("%s(`%v`)", duplicatedHint.HintName.O, duplicatedHint.HintData)
sctx.GetSessionVars().StmtCtx.AppendWarning(errWarnConflictingHint.FastGenByArgs(hint))
}
}
}
default:
return
}
}
// RestoreOptimizerHints restores these hints.
func RestoreOptimizerHints(hints []*ast.TableOptimizerHint) string {
hintsStr := make([]string, 0, len(hints))
hintsMap := make(map[string]struct{}, len(hints))
for _, hint := range hints {
hintStr := RestoreTableOptimizerHint(hint)
if _, ok := hintsMap[hintStr]; ok {
continue
}
hintsMap[hintStr] = struct{}{}
hintsStr = append(hintsStr, hintStr)
}
return strings.Join(hintsStr, ", ")
}
// RestoreTableOptimizerHint returns string format of TableOptimizerHint.
func RestoreTableOptimizerHint(hint *ast.TableOptimizerHint) string {
var sb strings.Builder
ctx := format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)
err := hint.Restore(ctx)
// There won't be any error for optimizer hint.
if err != nil {
logutil.BgLogger().Debug("restore TableOptimizerHint failed", zap.Error(err))
}
return strings.ToLower(sb.String())
}
// RestoreIndexHint returns string format of IndexHint.
func RestoreIndexHint(hint *ast.IndexHint) (string, error) {
var sb strings.Builder
ctx := format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)
err := hint.Restore(ctx)
if err != nil {
logutil.BgLogger().Debug("restore IndexHint failed", zap.Error(err))
return "", err
}
return strings.ToLower(sb.String()), nil
}
// Restore returns the string format of HintsSet.
func (hs *HintsSet) Restore() (string, error) {
hintsStr := make([]string, 0, len(hs.tableHints)+len(hs.indexHints))
for _, tblHints := range hs.tableHints {
for _, tblHint := range tblHints {
hintsStr = append(hintsStr, RestoreTableOptimizerHint(tblHint))
}
}
for _, idxHints := range hs.indexHints {
for _, idxHint := range idxHints {
str, err := RestoreIndexHint(idxHint)
if err != nil {
return "", err
}
hintsStr = append(hintsStr, str)
}
}
return strings.Join(hintsStr, ", "), nil
}
type hintProcessor struct {
*HintsSet
// bindHint2Ast indicates the behavior of the processor, `true` for bind hint to ast, `false` for extract hint from ast.
bindHint2Ast bool
tableCounter int
indexCounter int
blockCounter int
}
func (hp *hintProcessor) Enter(in ast.Node) (ast.Node, bool) {
switch v := in.(type) {
case *ast.SelectStmt, *ast.UpdateStmt, *ast.DeleteStmt:
if hp.bindHint2Ast {
if hp.tableCounter < len(hp.tableHints) {
setTableHints4StmtNode(in, hp.tableHints[hp.tableCounter])
} else {
setTableHints4StmtNode(in, nil)
}
hp.tableCounter++
} else {
hp.tableHints = append(hp.tableHints, ExtractTableHintsFromStmtNode(in, nil))
}
hp.blockCounter++
case *ast.TableName:
// Insert cases.
if hp.blockCounter == 0 {
return in, false
}
if hp.bindHint2Ast {
if hp.indexCounter < len(hp.indexHints) {
v.IndexHints = hp.indexHints[hp.indexCounter]
} else {
v.IndexHints = nil
}
hp.indexCounter++
} else {
hp.indexHints = append(hp.indexHints, v.IndexHints)
}
}
return in, false
}
func (hp *hintProcessor) Leave(in ast.Node) (ast.Node, bool) {
switch in.(type) {
case *ast.SelectStmt, *ast.UpdateStmt, *ast.DeleteStmt:
hp.blockCounter--
}
return in, true
}
// CollectHint collects hints for a statement.
func CollectHint(in ast.StmtNode) *HintsSet {
hp := hintProcessor{HintsSet: &HintsSet{tableHints: make([][]*ast.TableOptimizerHint, 0, 4), indexHints: make([][]*ast.IndexHint, 0, 4)}}
in.Accept(&hp)
return hp.HintsSet
}
// BindHint will add hints for stmt according to the hints in `hintsSet`.
func BindHint(stmt ast.StmtNode, hintsSet *HintsSet) ast.StmtNode {
hp := hintProcessor{HintsSet: hintsSet, bindHint2Ast: true}
stmt.Accept(&hp)
return stmt
}
// ParseHintsSet parses a SQL string, then collects and normalizes the HintsSet.
func ParseHintsSet(p *parser.Parser, sql, charset, collation, db string) (*HintsSet, ast.StmtNode, []error, error) {
stmtNodes, warns, err := p.ParseSQL(sql,
parser.CharsetConnection(charset),
parser.CollationConnection(collation))
if err != nil {
return nil, nil, nil, err
}
if len(stmtNodes) != 1 {
return nil, nil, nil, errors.New(fmt.Sprintf("bind_sql must be a single statement: %s", sql))
}
hs := CollectHint(stmtNodes[0])
processor := &BlockHintProcessor{}
stmtNodes[0].Accept(processor)
topNodeType := nodeType4Stmt(stmtNodes[0])
for i, tblHints := range hs.tableHints {
newHints := make([]*ast.TableOptimizerHint, 0, len(tblHints))
curOffset := i + 1
if topNodeType == TypeDelete || topNodeType == TypeUpdate {
curOffset = curOffset - 1
}
for _, tblHint := range tblHints {
if tblHint.HintName.L == hintQBName {
continue
}
offset := processor.GetHintOffset(tblHint.QBName, curOffset)
if offset < 0 || !processor.checkTableQBName(tblHint.Tables) {
hintStr := RestoreTableOptimizerHint(tblHint)
return nil, nil, nil, errors.New(fmt.Sprintf("Unknown query block name in hint %s", hintStr))
}
tblHint.QBName, err = GenerateQBName(topNodeType, offset)
if err != nil {
return nil, nil, nil, err
}
for i, tbl := range tblHint.Tables {
if tbl.DBName.String() == "" {
tblHint.Tables[i].DBName = model.NewCIStr(db)
}
}
newHints = append(newHints, tblHint)
}
hs.tableHints[i] = newHints
}
return hs, stmtNodes[0], extractHintWarns(warns), nil
}
func extractHintWarns(warns []error) []error {
for _, w := range warns {
if parser.ErrParse.Equal(w) ||
parser.ErrWarnOptimizerHintUnsupportedHint.Equal(w) ||
parser.ErrWarnOptimizerHintInvalidToken.Equal(w) ||
parser.ErrWarnMemoryQuotaOverflow.Equal(w) ||
parser.ErrWarnOptimizerHintParseError.Equal(w) ||
parser.ErrWarnOptimizerHintInvalidInteger.Equal(w) {
// Just one warning is enough, however we use a slice here to stop golint complaining
// "error should be the last type when returning multiple items" for `ParseHintsSet`.
return []error{w}
}
}
return nil
}
// BlockHintProcessor processes hints at different level of sql statement.
type BlockHintProcessor struct {
QbNameMap map[string]int // Map from query block name to select stmt offset.
QbHints map[int][]*ast.TableOptimizerHint // Group all hints at same query block.
Ctx sessionctx.Context
selectStmtOffset int
}
// MaxSelectStmtOffset returns the current stmt offset.
func (p *BlockHintProcessor) MaxSelectStmtOffset() int {
return p.selectStmtOffset
}
// Enter implements Visitor interface.
func (p *BlockHintProcessor) Enter(in ast.Node) (ast.Node, bool) {
switch node := in.(type) {
case *ast.UpdateStmt:
p.checkQueryBlockHints(node.TableHints, 0)
case *ast.DeleteStmt:
p.checkQueryBlockHints(node.TableHints, 0)
case *ast.SelectStmt:
p.selectStmtOffset++
node.QueryBlockOffset = p.selectStmtOffset
p.checkQueryBlockHints(node.TableHints, node.QueryBlockOffset)
}
return in, false
}
// Leave implements Visitor interface.
func (p *BlockHintProcessor) Leave(in ast.Node) (ast.Node, bool) {
return in, true
}
const hintQBName = "qb_name"
// checkQueryBlockHints checks the validity of query blocks and records the map of query block name to select offset.
func (p *BlockHintProcessor) checkQueryBlockHints(hints []*ast.TableOptimizerHint, offset int) {
var qbName string
for _, hint := range hints {
if hint.HintName.L != hintQBName {
continue
}
if qbName != "" {
if p.Ctx != nil {
p.Ctx.GetSessionVars().StmtCtx.AppendWarning(errors.New(fmt.Sprintf("There are more than two query names in same query block,, using the first one %s", qbName)))
}
} else {
qbName = hint.QBName.L
}
}
if qbName == "" {
return
}
if p.QbNameMap == nil {
p.QbNameMap = make(map[string]int)
}
if _, ok := p.QbNameMap[qbName]; ok {
if p.Ctx != nil {
p.Ctx.GetSessionVars().StmtCtx.AppendWarning(errors.New(fmt.Sprintf("Duplicate query block name %s, only the first one is effective", qbName)))
}
} else {
p.QbNameMap[qbName] = offset
}
}
const (
defaultUpdateBlockName = "upd_1"
defaultDeleteBlockName = "del_1"
defaultSelectBlockPrefix = "sel_"
)
// NodeType indicates if the node is for SELECT / UPDATE / DELETE.
type NodeType int
const (
// TypeUpdate for Update.
TypeUpdate NodeType = iota
// TypeDelete for DELETE.
TypeDelete
// TypeSelect for SELECT.
TypeSelect
// TypeInvalid for unexpected statements.
TypeInvalid
)
// nodeType4Stmt returns the NodeType for a statement. The type is used for SQL bind.
func nodeType4Stmt(node ast.StmtNode) NodeType {
switch node.(type) {
// This type is used by SQL bind, we only handle SQL bind for INSERT INTO SELECT, so we treat InsertStmt as TypeSelect.
case *ast.SelectStmt, *ast.InsertStmt:
return TypeSelect
case *ast.UpdateStmt:
return TypeUpdate
case *ast.DeleteStmt:
return TypeDelete
}
return TypeInvalid
}
// getBlockName finds the offset of query block name. It uses 0 as offset for top level update or delete,
// -1 for invalid block name.
func (p *BlockHintProcessor) getBlockOffset(blockName model.CIStr) int {
if p.QbNameMap != nil {
level, ok := p.QbNameMap[blockName.L]
if ok {
return level
}
}
// Handle the default query block name.
if blockName.L == defaultUpdateBlockName || blockName.L == defaultDeleteBlockName {
return 0
}
if strings.HasPrefix(blockName.L, defaultSelectBlockPrefix) {
suffix := blockName.L[len(defaultSelectBlockPrefix):]
level, err := strconv.ParseInt(suffix, 10, 64)
if err != nil || level > int64(p.selectStmtOffset) {
return -1
}
return int(level)
}
return -1
}
// GetHintOffset gets the offset of stmt that the hints take effects.
func (p *BlockHintProcessor) GetHintOffset(qbName model.CIStr, currentOffset int) int {
if qbName.L != "" {
return p.getBlockOffset(qbName)
}
return currentOffset
}
func (p *BlockHintProcessor) checkTableQBName(tables []ast.HintTable) bool {
for _, table := range tables {
if table.QBName.L != "" && p.getBlockOffset(table.QBName) < 0 {
return false
}
}
return true
}
// GetCurrentStmtHints extracts all hints that take effects at current stmt.
func (p *BlockHintProcessor) GetCurrentStmtHints(hints []*ast.TableOptimizerHint, currentOffset int) []*ast.TableOptimizerHint {
if p.QbHints == nil {
p.QbHints = make(map[int][]*ast.TableOptimizerHint)
}
for _, hint := range hints {
if hint.HintName.L == hintQBName {
continue
}
offset := p.GetHintOffset(hint.QBName, currentOffset)
if offset < 0 || !p.checkTableQBName(hint.Tables) {
hintStr := RestoreTableOptimizerHint(hint)
p.Ctx.GetSessionVars().StmtCtx.AppendWarning(errors.New(fmt.Sprintf("Hint %s is ignored due to unknown query block name", hintStr)))
continue
}
p.QbHints[offset] = append(p.QbHints[offset], hint)
}
return p.QbHints[currentOffset]
}
// GenerateQBName builds QBName from offset.
func GenerateQBName(nodeType NodeType, blockOffset int) (model.CIStr, error) {
if blockOffset == 0 {
if nodeType == TypeDelete {
return model.NewCIStr(defaultDeleteBlockName), nil
}
if nodeType == TypeUpdate {
return model.NewCIStr(defaultUpdateBlockName), nil
}
return model.NewCIStr(""), errors.New(fmt.Sprintf("Unexpected NodeType %d when block offset is 0", nodeType))
}
return model.NewCIStr(fmt.Sprintf("%s%d", defaultSelectBlockPrefix, blockOffset)), nil
}