forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.go
313 lines (284 loc) · 7.77 KB
/
compiler.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
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package executor
import (
"context"
"fmt"
"github.com/opentracing/opentracing-go"
"github.com/pingcap/errors"
"github.com/pingcap/parser/ast"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/planner"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx"
log "github.com/sirupsen/logrus"
)
// Compiler compiles an ast.StmtNode to a physical plan.
type Compiler struct {
Ctx sessionctx.Context
}
// Compile compiles an ast.StmtNode to a physical plan.
func (c *Compiler) Compile(ctx context.Context, stmtNode ast.StmtNode) (*ExecStmt, error) {
if span := opentracing.SpanFromContext(ctx); span != nil && span.Tracer() != nil {
span1 := span.Tracer().StartSpan("executor.Compile", opentracing.ChildOf(span.Context()))
defer span1.Finish()
}
infoSchema := GetInfoSchema(c.Ctx)
if err := plannercore.Preprocess(c.Ctx, stmtNode, infoSchema); err != nil {
return nil, errors.Trace(err)
}
finalPlan, err := planner.Optimize(c.Ctx, stmtNode, infoSchema)
if err != nil {
return nil, errors.Trace(err)
}
CountStmtNode(stmtNode, c.Ctx.GetSessionVars().InRestrictedSQL)
isExpensive := logExpensiveQuery(stmtNode, finalPlan)
return &ExecStmt{
InfoSchema: infoSchema,
Plan: finalPlan,
Expensive: isExpensive,
Cacheable: plannercore.Cacheable(stmtNode),
Text: stmtNode.Text(),
StmtNode: stmtNode,
Ctx: c.Ctx,
}, nil
}
func logExpensiveQuery(stmtNode ast.StmtNode, finalPlan plannercore.Plan) (expensive bool) {
expensive = isExpensiveQuery(finalPlan)
if !expensive {
return
}
const logSQLLen = 1024
sql := stmtNode.Text()
if len(sql) > logSQLLen {
sql = fmt.Sprintf("%s len(%d)", sql[:logSQLLen], len(sql))
}
log.Warnf("[EXPENSIVE_QUERY] %s", sql)
return
}
func isExpensiveQuery(p plannercore.Plan) bool {
switch x := p.(type) {
case plannercore.PhysicalPlan:
return isPhysicalPlanExpensive(x)
case *plannercore.Execute:
return isExpensiveQuery(x.Plan)
case *plannercore.Insert:
if x.SelectPlan != nil {
return isPhysicalPlanExpensive(x.SelectPlan)
}
case *plannercore.Delete:
if x.SelectPlan != nil {
return isPhysicalPlanExpensive(x.SelectPlan)
}
case *plannercore.Update:
if x.SelectPlan != nil {
return isPhysicalPlanExpensive(x.SelectPlan)
}
}
return false
}
func isPhysicalPlanExpensive(p plannercore.PhysicalPlan) bool {
expensiveRowThreshold := int64(config.GetGlobalConfig().Log.ExpensiveThreshold)
if int64(p.StatsCount()) > expensiveRowThreshold {
return true
}
for _, child := range p.Children() {
if isPhysicalPlanExpensive(child) {
return true
}
}
return false
}
// CountStmtNode records the number of statements with the same type.
func CountStmtNode(stmtNode ast.StmtNode, inRestrictedSQL bool) {
if inRestrictedSQL {
return
}
typeLabel := GetStmtLabel(stmtNode)
metrics.StmtNodeCounter.WithLabelValues(typeLabel).Inc()
if !config.GetGlobalConfig().Status.RecordQPSbyDB {
return
}
dbLabels := getStmtDbLabel(stmtNode)
for dbLabel := range dbLabels {
metrics.DbStmtNodeCounter.WithLabelValues(dbLabel, typeLabel).Inc()
}
}
func getStmtDbLabel(stmtNode ast.StmtNode) map[string]struct{} {
dbLabelSet := make(map[string]struct{})
switch x := stmtNode.(type) {
case *ast.AlterTableStmt:
dbLabel := x.Table.Schema.O
dbLabelSet[dbLabel] = struct{}{}
case *ast.CreateIndexStmt:
dbLabel := x.Table.Schema.O
dbLabelSet[dbLabel] = struct{}{}
case *ast.CreateTableStmt:
dbLabel := x.Table.Schema.O
dbLabelSet[dbLabel] = struct{}{}
case *ast.InsertStmt:
dbLabels := getDbFromResultNode(x.Table.TableRefs)
for _, db := range dbLabels {
dbLabelSet[db] = struct{}{}
}
dbLabels = getDbFromResultNode(x.Select)
for _, db := range dbLabels {
dbLabelSet[db] = struct{}{}
}
case *ast.DropIndexStmt:
dbLabel := x.Table.Schema.O
dbLabelSet[dbLabel] = struct{}{}
case *ast.DropTableStmt:
tables := x.Tables
for _, table := range tables {
dbLabel := table.Schema.O
if _, ok := dbLabelSet[dbLabel]; !ok {
dbLabelSet[dbLabel] = struct{}{}
}
}
case *ast.SelectStmt:
dbLabels := getDbFromResultNode(x)
for _, db := range dbLabels {
dbLabelSet[db] = struct{}{}
}
case *ast.UpdateStmt:
if x.TableRefs != nil {
dbLabels := getDbFromResultNode(x.TableRefs.TableRefs)
for _, db := range dbLabels {
dbLabelSet[db] = struct{}{}
}
}
case *ast.DeleteStmt:
if x.TableRefs != nil {
dbLabels := getDbFromResultNode(x.TableRefs.TableRefs)
for _, db := range dbLabels {
dbLabelSet[db] = struct{}{}
}
}
}
return dbLabelSet
}
func getDbFromResultNode(resultNode ast.ResultSetNode) []string { //may have duplicate db name
var dbLabels []string
if resultNode == nil {
return dbLabels
}
switch x := resultNode.(type) {
case *ast.TableSource:
return getDbFromResultNode(x.Source)
case *ast.SelectStmt:
if x.From != nil {
return getDbFromResultNode(x.From.TableRefs)
}
case *ast.TableName:
dbLabels = append(dbLabels, x.DBInfo.Name.O)
case *ast.Join:
if x.Left != nil {
dbs := getDbFromResultNode(x.Left)
if dbs != nil {
for _, db := range dbs {
dbLabels = append(dbLabels, db)
}
}
}
if x.Right != nil {
dbs := getDbFromResultNode(x.Right)
if dbs != nil {
for _, db := range dbs {
dbLabels = append(dbLabels, db)
}
}
}
}
return dbLabels
}
// GetStmtLabel generates a label for a statement.
func GetStmtLabel(stmtNode ast.StmtNode) string {
switch x := stmtNode.(type) {
case *ast.AlterTableStmt:
return "AlterTable"
case *ast.AnalyzeTableStmt:
return "AnalyzeTable"
case *ast.BeginStmt:
return "Begin"
case *ast.CommitStmt:
return "Commit"
case *ast.CreateDatabaseStmt:
return "CreateDatabase"
case *ast.CreateIndexStmt:
return "CreateIndex"
case *ast.CreateTableStmt:
return "CreateTable"
case *ast.CreateViewStmt:
return "CreateView"
case *ast.CreateUserStmt:
return "CreateUser"
case *ast.DeleteStmt:
return "Delete"
case *ast.DropDatabaseStmt:
return "DropDatabase"
case *ast.DropIndexStmt:
return "DropIndex"
case *ast.DropTableStmt:
return "DropTable"
case *ast.ExplainStmt:
return "Explain"
case *ast.InsertStmt:
if x.IsReplace {
return "Replace"
}
return "Insert"
case *ast.LoadDataStmt:
return "LoadData"
case *ast.RollbackStmt:
return "RollBack"
case *ast.SelectStmt:
return "Select"
case *ast.SetStmt, *ast.SetPwdStmt:
return "Set"
case *ast.ShowStmt:
return "Show"
case *ast.TruncateTableStmt:
return "TruncateTable"
case *ast.UpdateStmt:
return "Update"
case *ast.GrantStmt:
return "Grant"
case *ast.RevokeStmt:
return "Revoke"
case *ast.DeallocateStmt:
return "Deallocate"
case *ast.ExecuteStmt:
return "Execute"
case *ast.PrepareStmt:
return "Prepare"
case *ast.UseStmt:
return "Use"
}
return "other"
}
// GetInfoSchema gets TxnCtx InfoSchema if snapshot schema is not set,
// Otherwise, snapshot schema is returned.
func GetInfoSchema(ctx sessionctx.Context) infoschema.InfoSchema {
sessVar := ctx.GetSessionVars()
var is infoschema.InfoSchema
if snap := sessVar.SnapshotInfoschema; snap != nil {
is = snap.(infoschema.InfoSchema)
log.Infof("con:%d use snapshot schema %d", sessVar.ConnectionID, is.SchemaMetaVersion())
} else {
is = sessVar.TxnCtx.InfoSchema.(infoschema.InfoSchema)
}
return is
}