/
analyzer.go
407 lines (350 loc) · 11.6 KB
/
analyzer.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
// Copyright 2020-2021 Dolthub, 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 analyzer
import (
"fmt"
"os"
"reflect"
"strings"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pmezard/go-difflib/difflib"
"github.com/sirupsen/logrus"
"gopkg.in/src-d/go-errors.v1"
"github.com/Ciyfly/FakerMysql/sql"
)
const debugAnalyzerKey = "DEBUG_ANALYZER"
const maxAnalysisIterations = 8
// ErrMaxAnalysisIters is thrown when the analysis iterations are exceeded
var ErrMaxAnalysisIters = errors.NewKind("exceeded max analysis iterations (%d)")
// ErrInAnalysis is thrown for generic analyzer errors
var ErrInAnalysis = errors.NewKind("error in analysis: %s")
// ErrInvalidNodeType is thrown when the analyzer can't handle a particular kind of node type
var ErrInvalidNodeType = errors.NewKind("%s: invalid node of type: %T")
// Builder provides an easy way to generate Analyzer with custom rules and options.
type Builder struct {
preAnalyzeRules []Rule
postAnalyzeRules []Rule
preValidationRules []Rule
postValidationRules []Rule
onceBeforeRules []Rule
defaultRules []Rule
onceAfterRules []Rule
validationRules []Rule
afterAllRules []Rule
provider sql.DatabaseProvider
debug bool
parallelism int
}
// NewBuilder creates a new Builder from a specific catalog.
// This builder allow us add custom Rules and modify some internal properties.
func NewBuilder(pro sql.DatabaseProvider) *Builder {
return &Builder{
provider: pro,
onceBeforeRules: OnceBeforeDefault,
defaultRules: DefaultRules,
onceAfterRules: OnceAfterDefault,
validationRules: DefaultValidationRules,
afterAllRules: OnceAfterAll,
}
}
// WithDebug activates debug on the Analyzer.
func (ab *Builder) WithDebug() *Builder {
ab.debug = true
return ab
}
// WithParallelism sets the parallelism level on the analyzer.
func (ab *Builder) WithParallelism(parallelism int) *Builder {
ab.parallelism = parallelism
return ab
}
// AddPreAnalyzeRule adds a new rule to the analyze before the standard analyzer rules.
func (ab *Builder) AddPreAnalyzeRule(name string, fn RuleFunc) *Builder {
ab.preAnalyzeRules = append(ab.preAnalyzeRules, Rule{name, fn})
return ab
}
// AddPostAnalyzeRule adds a new rule to the analyzer after standard analyzer rules.
func (ab *Builder) AddPostAnalyzeRule(name string, fn RuleFunc) *Builder {
ab.postAnalyzeRules = append(ab.postAnalyzeRules, Rule{name, fn})
return ab
}
// AddPreValidationRule adds a new rule to the analyzer before standard validation rules.
func (ab *Builder) AddPreValidationRule(name string, fn RuleFunc) *Builder {
ab.preValidationRules = append(ab.preValidationRules, Rule{name, fn})
return ab
}
// AddPostValidationRule adds a new rule to the analyzer after standard validation rules.
func (ab *Builder) AddPostValidationRule(name string, fn RuleFunc) *Builder {
ab.postValidationRules = append(ab.postValidationRules, Rule{name, fn})
return ab
}
func duplicateRulesWithout(rules []Rule, excludedRuleName string) []Rule {
newRules := make([]Rule, 0, len(rules))
for _, rule := range rules {
if rule.Name != excludedRuleName {
newRules = append(newRules, rule)
}
}
return newRules
}
// RemoveOnceBeforeRule removes a default rule from the analyzer which would occur before other rules
func (ab *Builder) RemoveOnceBeforeRule(name string) *Builder {
ab.onceBeforeRules = duplicateRulesWithout(ab.onceBeforeRules, name)
return ab
}
// RemoveDefaultRule removes a default rule from the analyzer that is executed as part of the analysis
func (ab *Builder) RemoveDefaultRule(name string) *Builder {
ab.defaultRules = duplicateRulesWithout(ab.defaultRules, name)
return ab
}
// RemoveOnceAfterRule removes a default rule from the analyzer which would occur just once after the default analysis
func (ab *Builder) RemoveOnceAfterRule(name string) *Builder {
ab.onceAfterRules = duplicateRulesWithout(ab.onceAfterRules, name)
return ab
}
// RemoveValidationRule removes a default rule from the analyzer which would occur as part of the validation rules
func (ab *Builder) RemoveValidationRule(name string) *Builder {
ab.validationRules = duplicateRulesWithout(ab.validationRules, name)
return ab
}
// RemoveAfterAllRule removes a default rule from the analyzer which would occur after all other rules
func (ab *Builder) RemoveAfterAllRule(name string) *Builder {
ab.afterAllRules = duplicateRulesWithout(ab.afterAllRules, name)
return ab
}
var log = logrus.New()
func init() {
// TODO: give the option for debug analyzer logging format to match the global one
log.SetFormatter(simpleLogFormatter{})
}
type simpleLogFormatter struct{}
func (s simpleLogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
lvl := ""
switch entry.Level {
case logrus.PanicLevel:
lvl = "PANIC"
case logrus.FatalLevel:
lvl = "FATAL"
case logrus.ErrorLevel:
lvl = "ERROR"
case logrus.WarnLevel:
lvl = "WARN"
case logrus.InfoLevel:
lvl = "INFO"
case logrus.DebugLevel:
lvl = "DEBUG"
case logrus.TraceLevel:
lvl = "TRACE"
}
msg := fmt.Sprintf("%s: %s\n", lvl, entry.Message)
return ([]byte)(msg), nil
}
// Build creates a new Analyzer using all previous data setted to the Builder
func (ab *Builder) Build() *Analyzer {
_, debug := os.LookupEnv(debugAnalyzerKey)
var batches = []*Batch{
{
Desc: "pre-analyzer",
Iterations: maxAnalysisIterations,
Rules: ab.preAnalyzeRules,
},
{
Desc: "once-before",
Iterations: 1,
Rules: ab.onceBeforeRules,
},
{
Desc: "default-rules",
Iterations: maxAnalysisIterations,
Rules: ab.defaultRules,
},
{
Desc: "once-after",
Iterations: 1,
Rules: ab.onceAfterRules,
},
{
Desc: "post-analyzer",
Iterations: maxAnalysisIterations,
Rules: ab.postAnalyzeRules,
},
{
Desc: "pre-validation",
Iterations: 1,
Rules: ab.preValidationRules,
},
{
Desc: "validation",
Iterations: 1,
Rules: ab.validationRules,
},
{
Desc: "post-validation",
Iterations: 1,
Rules: ab.postValidationRules,
},
{
Desc: "after-all",
Iterations: 1,
Rules: ab.afterAllRules,
},
}
return &Analyzer{
Debug: debug || ab.debug,
contextStack: make([]string, 0),
Batches: batches,
Catalog: NewCatalog(ab.provider),
Parallelism: ab.parallelism,
ProcedureCache: NewProcedureCache(),
}
}
// Analyzer analyzes nodes of the execution plan and applies rules and validations
// to them.
type Analyzer struct {
// Whether to log various debugging messages
Debug bool
// Whether to output the query plan at each step of the analyzer
Verbose bool
// A stack of debugger context. See PushDebugContext, PopDebugContext
contextStack []string
Parallelism int
// Batches of Rules to apply.
Batches []*Batch
// Catalog of databases and registered functions.
Catalog sql.Catalog
// ProcedureCache is a cache of stored procedures.
ProcedureCache *ProcedureCache
}
// NewDefault creates a default Analyzer instance with all default Rules and configuration.
// To add custom rules, the easiest way is use the Builder.
func NewDefault(provider sql.DatabaseProvider) *Analyzer {
return NewBuilder(provider).Build()
}
// Log prints an INFO message to stdout with the given message and args
// if the analyzer is in debug mode.
func (a *Analyzer) Log(msg string, args ...interface{}) {
if a != nil && a.Debug {
if len(a.contextStack) > 0 {
ctx := strings.Join(a.contextStack, "/")
log.Infof("%s: "+msg, append([]interface{}{ctx}, args...)...)
} else {
log.Infof(msg, args...)
}
}
}
// LogNode prints the node given if Verbose logging is enabled.
func (a *Analyzer) LogNode(n sql.Node) {
if a != nil && n != nil && a.Verbose {
if len(a.contextStack) > 0 {
ctx := strings.Join(a.contextStack, "/")
log.Infof("%s:\n%s", ctx, sql.DebugString(n))
} else {
log.Infof("%s", sql.DebugString(n))
}
}
}
// LogDiff logs the diff between the query plans after a transformation rules has been applied.
// Only can print a diff when the string representations of the nodes differ, which isn't always the case.
func (a *Analyzer) LogDiff(prev, next sql.Node) {
if a.Debug && a.Verbose {
if !reflect.DeepEqual(next, prev) {
diff, err := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
A: difflib.SplitLines(sql.DebugString(prev)),
B: difflib.SplitLines(sql.DebugString(next)),
FromFile: "Prev",
FromDate: "",
ToFile: "Next",
ToDate: "",
Context: 1,
})
if err != nil {
panic(err)
}
if len(diff) > 0 {
a.Log(diff)
}
}
}
}
// PushDebugContext pushes the given context string onto the context stack, to use when logging debug messages.
func (a *Analyzer) PushDebugContext(msg string) {
if a != nil {
a.contextStack = append(a.contextStack, msg)
}
}
// PopDebugContext pops a context message off the context stack.
func (a *Analyzer) PopDebugContext() {
if a != nil && len(a.contextStack) > 0 {
a.contextStack = a.contextStack[:len(a.contextStack)-1]
}
}
func analyzeAll(batchName string) bool {
return true
}
// Analyze applies the transformation rules to the node given. In the case of an error, the last successfully
// transformed node is returned along with the error.
func (a *Analyzer) Analyze(ctx *sql.Context, n sql.Node, scope *Scope) (sql.Node, error) {
return a.analyzeWithSelector(ctx, n, scope, analyzeAll)
}
func (a *Analyzer) analyzeThroughBatch(ctx *sql.Context, n sql.Node, scope *Scope, until string) (sql.Node, error) {
stop := false
return a.analyzeWithSelector(ctx, n, scope, func(desc string) bool {
if stop {
return false
}
if desc == until {
stop = true
}
// we return true even for the matching description; only start
// returning false after this batch.
return true
})
}
func (a *Analyzer) analyzeWithSelector(ctx *sql.Context, n sql.Node, scope *Scope, selector func(d string) bool) (sql.Node, error) {
span, ctx := ctx.Span("analyze", opentracing.Tags{
//"plan": , n.String(),
})
var err error
a.Log("starting analysis of node of type: %T", n)
for _, batch := range a.Batches {
if selector(batch.Desc) {
a.PushDebugContext(batch.Desc)
n, err = batch.Eval(ctx, a, n, scope)
if err != nil {
a.Log("Encountered error: %v", err)
a.PopDebugContext()
return n, err
}
a.PopDebugContext()
}
}
defer func() {
if n != nil {
span.SetTag("IsResolved", n.Resolved())
}
span.Finish()
}()
return n, err
}
func (a *Analyzer) analyzeStartingAtBatch(ctx *sql.Context, n sql.Node, scope *Scope, startAt string) (sql.Node, error) {
start := false
return a.analyzeWithSelector(ctx, n, scope, func(desc string) bool {
if desc == startAt {
start = true
}
if start {
return true
}
return false
})
}