forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 7
/
prepared.go
385 lines (347 loc) · 11.7 KB
/
prepared.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
// Copyright 2015 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 executor
import (
"context"
"math"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/log"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/planner"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/types"
driver "github.com/pingcap/tidb/types/parser_driver"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/hint"
"github.com/pingcap/tidb/util/sqlexec"
"github.com/pingcap/tidb/util/topsql"
topsqlstate "github.com/pingcap/tidb/util/topsql/state"
"go.uber.org/zap"
"golang.org/x/exp/slices"
)
var (
_ Executor = &DeallocateExec{}
_ Executor = &ExecuteExec{}
_ Executor = &PrepareExec{}
)
type paramMarkerExtractor struct {
markers []ast.ParamMarkerExpr
}
func (e *paramMarkerExtractor) Enter(in ast.Node) (ast.Node, bool) {
return in, false
}
func (e *paramMarkerExtractor) Leave(in ast.Node) (ast.Node, bool) {
if x, ok := in.(*driver.ParamMarkerExpr); ok {
e.markers = append(e.markers, x)
}
return in, true
}
// PrepareExec represents a PREPARE executor.
type PrepareExec struct {
baseExecutor
name string
sqlText string
ID uint32
ParamCount int
Fields []*ast.ResultField
IsGeneralStmt bool
// If it's generated from executing "prepare stmt from '...'", the process is parse -> plan -> executor
// If it's generated from the prepare protocol, the process is session.PrepareStmt -> NewPrepareExec
// They both generate a PrepareExec struct, but the second case needs to reset the statement context while the first already do that.
needReset bool
}
// NewPrepareExec creates a new PrepareExec.
func NewPrepareExec(ctx sessionctx.Context, sqlTxt string) *PrepareExec {
base := newBaseExecutor(ctx, nil, 0)
base.initCap = chunk.ZeroCapacity
return &PrepareExec{
baseExecutor: base,
sqlText: sqlTxt,
needReset: true,
}
}
// Next implements the Executor Next interface.
func (e *PrepareExec) Next(ctx context.Context, req *chunk.Chunk) error {
vars := e.ctx.GetSessionVars()
if e.ID != 0 {
// Must be the case when we retry a prepare.
// Make sure it is idempotent.
_, ok := vars.PreparedStmts[e.ID]
if ok {
return nil
}
}
charset, collation := vars.GetCharsetInfo()
var (
stmts []ast.StmtNode
err error
)
if sqlParser, ok := e.ctx.(sqlexec.SQLParser); ok {
// FIXME: ok... yet another parse API, may need some api interface clean.
stmts, _, err = sqlParser.ParseSQL(ctx, e.sqlText,
parser.CharsetConnection(charset),
parser.CollationConnection(collation))
} else {
p := parser.New()
p.SetParserConfig(vars.BuildParserConfig())
var warns []error
stmts, warns, err = p.ParseSQL(e.sqlText,
parser.CharsetConnection(charset),
parser.CollationConnection(collation))
for _, warn := range warns {
e.ctx.GetSessionVars().StmtCtx.AppendWarning(util.SyntaxWarn(warn))
}
}
if err != nil {
return util.SyntaxError(err)
}
if len(stmts) != 1 {
return ErrPrepareMulti
}
stmt := stmts[0]
if e.needReset {
err = ResetContextOfStmt(e.ctx, stmt)
if err != nil {
return err
}
}
var extractor paramMarkerExtractor
stmt.Accept(&extractor)
// DDL Statements can not accept parameters
if _, ok := stmt.(ast.DDLNode); ok && len(extractor.markers) > 0 {
return ErrPrepareDDL
}
switch stmt.(type) {
case *ast.LoadDataStmt, *ast.PrepareStmt, *ast.ExecuteStmt, *ast.DeallocateStmt, *ast.NonTransactionalDeleteStmt:
return ErrUnsupportedPs
}
// Prepare parameters should NOT over 2 bytes(MaxUint16)
// https://dev.mysql.com/doc/internals/en/com-stmt-prepare-response.html#packet-COM_STMT_PREPARE_OK.
if len(extractor.markers) > math.MaxUint16 {
return ErrPsManyParam
}
ret := &plannercore.PreprocessorReturn{}
err = plannercore.Preprocess(e.ctx, stmt, plannercore.InPrepare, plannercore.WithPreprocessorReturn(ret))
if err != nil {
return err
}
// The parameter markers are appended in visiting order, which may not
// be the same as the position order in the query string. We need to
// sort it by position.
slices.SortFunc(extractor.markers, func(i, j ast.ParamMarkerExpr) bool {
return i.(*driver.ParamMarkerExpr).Offset < j.(*driver.ParamMarkerExpr).Offset
})
e.ParamCount = len(extractor.markers)
for i := 0; i < e.ParamCount; i++ {
extractor.markers[i].SetOrder(i)
}
prepared := &ast.Prepared{
Stmt: stmt,
StmtType: GetStmtLabel(stmt),
Params: extractor.markers,
SchemaVersion: ret.InfoSchema.SchemaMetaVersion(),
}
normalizedSQL, digest := parser.NormalizeDigest(prepared.Stmt.Text())
if topsqlstate.TopSQLEnabled() {
e.ctx.GetSessionVars().StmtCtx.IsSQLRegistered.Store(true)
ctx = topsql.AttachAndRegisterSQLInfo(ctx, normalizedSQL, digest, vars.InRestrictedSQL)
}
var (
normalizedSQL4PC, digest4PC string
selectStmtNode ast.StmtNode
)
if !e.ctx.GetSessionVars().EnablePreparedPlanCache {
prepared.UseCache = false
} else {
prepared.UseCache = plannercore.CacheableWithCtx(e.ctx, stmt, ret.InfoSchema)
selectStmtNode, normalizedSQL4PC, digest4PC, err = planner.ExtractSelectAndNormalizeDigest(stmt, e.ctx.GetSessionVars().CurrentDB)
if err != nil || selectStmtNode == nil {
normalizedSQL4PC = ""
digest4PC = ""
}
}
// We try to build the real statement of preparedStmt.
for i := range prepared.Params {
param := prepared.Params[i].(*driver.ParamMarkerExpr)
param.Datum.SetNull()
param.InExecute = false
}
var p plannercore.Plan
e.ctx.GetSessionVars().PlanID = 0
e.ctx.GetSessionVars().PlanColumnID = 0
e.ctx.GetSessionVars().MapHashCode2UniqueID4ExtendedCol = nil
destBuilder, _ := plannercore.NewPlanBuilder().Init(e.ctx, ret.InfoSchema, &hint.BlockHintProcessor{})
p, err = destBuilder.Build(ctx, stmt)
if err != nil {
return err
}
// In MySQL prepare protocol, the server need to tell the client how many column the prepared statement would return when executing it.
// For a query with on result, e.g. an insert statement, there will be no result, so 'e.Fields' is not set.
// Usually, p.Schema().Len() == 0 means no result. A special case is the 'do' statement, it looks like 'select' but discard the result.
if !isNoResultPlan(p) {
e.Fields = colNames2ResultFields(p.Schema(), p.OutputNames(), vars.CurrentDB)
}
if e.ID == 0 && !e.IsGeneralStmt {
e.ID = vars.GetNextPreparedStmtID()
}
if e.name != "" && !e.IsGeneralStmt {
vars.PreparedStmtNameToID[e.name] = e.ID
}
preparedObj := &plannercore.PlanCacheStmt{
PreparedAst: prepared,
StmtDB: e.ctx.GetSessionVars().CurrentDB,
StmtText: stmt.Text(),
VisitInfos: destBuilder.GetVisitInfo(),
NormalizedSQL: normalizedSQL,
SQLDigest: digest,
ForUpdateRead: destBuilder.GetIsForUpdateRead(),
SnapshotTSEvaluator: ret.SnapshotTSEvaluator,
NormalizedSQL4PC: normalizedSQL4PC,
SQLDigest4PC: digest4PC,
}
if err = plannercore.CheckPreparedPriv(e.ctx, preparedObj, ret.InfoSchema); err != nil {
return err
}
if e.IsGeneralStmt {
vars.AddGeneralPlanCacheStmt(e.sqlText, preparedObj)
return nil
}
return vars.AddPreparedStmt(e.ID, preparedObj)
}
// ExecuteExec represents an EXECUTE executor.
// It cannot be executed by itself, all it needs to do is to build
// another Executor from a prepared statement.
type ExecuteExec struct {
baseExecutor
is infoschema.InfoSchema
name string
usingVars []expression.Expression
stmtExec Executor
stmt ast.StmtNode
plan plannercore.Plan
lowerPriority bool
outputNames []*types.FieldName
}
// Next implements the Executor Next interface.
func (e *ExecuteExec) Next(ctx context.Context, req *chunk.Chunk) error {
return nil
}
// Build builds a prepared statement into an executor.
// After Build, e.StmtExec will be used to do the real execution.
func (e *ExecuteExec) Build(b *executorBuilder) error {
stmtExec := b.build(e.plan)
if b.err != nil {
log.Warn("rebuild plan in EXECUTE statement failed", zap.String("labelName of PREPARE statement", e.name))
return errors.Trace(b.err)
}
e.stmtExec = stmtExec
if e.ctx.GetSessionVars().StmtCtx.Priority == mysql.NoPriority {
e.lowerPriority = needLowerPriority(e.plan)
}
return nil
}
// DeallocateExec represent a DEALLOCATE executor.
type DeallocateExec struct {
baseExecutor
Name string
}
// Next implements the Executor Next interface.
func (e *DeallocateExec) Next(ctx context.Context, req *chunk.Chunk) error {
vars := e.ctx.GetSessionVars()
id, ok := vars.PreparedStmtNameToID[e.Name]
if !ok {
return errors.Trace(plannercore.ErrStmtNotFound)
}
preparedPointer := vars.PreparedStmts[id]
preparedObj, ok := preparedPointer.(*plannercore.PlanCacheStmt)
if !ok {
return errors.Errorf("invalid PlanCacheStmt type")
}
prepared := preparedObj.PreparedAst
delete(vars.PreparedStmtNameToID, e.Name)
if e.ctx.GetSessionVars().EnablePreparedPlanCache {
bindSQL, _ := plannercore.GetBindSQL4PlanCache(e.ctx, preparedObj)
cacheKey, err := plannercore.NewPlanCacheKey(vars, preparedObj.StmtText, preparedObj.StmtDB, prepared.SchemaVersion,
0, bindSQL)
if err != nil {
return err
}
if !vars.IgnorePreparedCacheCloseStmt { // keep the plan in cache
e.ctx.GetPlanCache(false).Delete(cacheKey)
}
}
vars.RemovePreparedStmt(id)
return nil
}
// CompileExecutePreparedStmt compiles a session Execute command to a stmt.Statement.
func CompileExecutePreparedStmt(ctx context.Context, sctx sessionctx.Context,
execStmt *ast.ExecuteStmt, is infoschema.InfoSchema) (*ExecStmt, error) {
startTime := time.Now()
defer func() {
sctx.GetSessionVars().DurationCompile = time.Since(startTime)
}()
preparedObj, err := plannercore.GetPreparedStmt(execStmt, sctx.GetSessionVars())
if err != nil {
return nil, err
}
pointPlanShortPathOK, err := plannercore.IsPointPlanShortPathOK(sctx, is, preparedObj)
if err != nil {
return nil, err
}
execPlan, names, err := planner.Optimize(ctx, sctx, execStmt, is)
if err != nil {
return nil, err
}
failpoint.Inject("assertTxnManagerInCompile", func() {
sessiontxn.RecordAssert(sctx, "assertTxnManagerInCompile", true)
sessiontxn.AssertTxnManagerInfoSchema(sctx, is)
})
stmt := &ExecStmt{
GoCtx: ctx,
InfoSchema: is,
Plan: execPlan,
StmtNode: execStmt,
Ctx: sctx,
OutputNames: names,
Ti: &TelemetryInfo{},
}
stmtCtx := sctx.GetSessionVars().StmtCtx
stmt.Text = preparedObj.PreparedAst.Stmt.Text()
stmtCtx.OriginalSQL = stmt.Text
stmtCtx.InitSQLDigest(preparedObj.NormalizedSQL, preparedObj.SQLDigest)
// handle point plan short path specially
if pointPlanShortPathOK {
if ep, ok := execPlan.(*plannercore.Execute); ok {
if pointPlan, ok := ep.Plan.(*plannercore.PointGetPlan); ok {
stmtCtx.SetPlan(execPlan)
stmtCtx.SetPlanDigest(preparedObj.NormalizedPlan, preparedObj.PlanDigest)
stmt.Plan = pointPlan
stmt.PsStmt = preparedObj
} else {
// invalid the previous cached point plan
preparedObj.PreparedAst.CachedPlan = nil
}
}
}
return stmt, nil
}