This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
/
mini.go
372 lines (335 loc) · 9.41 KB
/
mini.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
/*
Copyright (c) 2016, Percona LLC and/or its affiliates. All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package query
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"os/exec"
"strings"
queryProto "github.com/percona/pmm/proto/query"
"github.com/youtube/vitess/go/vt/sqlparser"
)
type QueryInfo struct {
Fingerprint string
Abstract string
Tables []queryProto.Table
}
type parseTry struct {
query string
q QueryInfo
s sqlparser.Statement
queryChan chan QueryInfo
crashChan chan bool
}
type protoTables []queryProto.Table
func (t protoTables) String() string {
s := ""
sep := ""
for _, table := range t {
s += sep + table.String()
sep = " "
}
return s
}
const (
MAX_JOIN_DEPTH = 100
)
var (
ErrNotSupported = errors.New("SQL parser does not support the query")
)
type Mini struct {
Debug bool
cwd string
queryIn chan string
miniOut chan string
parseChan chan parseTry
onlyTables bool
stopChan chan struct{}
}
func NewMini(cwd string) *Mini {
m := &Mini{
cwd: cwd,
onlyTables: cwd == "", // only tables if no path to mini.pl given
queryIn: make(chan string), // XXX see note below
miniOut: make(chan string), // XXX see note below
parseChan: make(chan parseTry, 1),
stopChan: make(chan struct{}),
}
return m
/// XXX DO NOT BUFFER queryIn or miniOut, else everything will break!
// There's only 1 mini.pl proc per Mini instance, and the Mini instance
// can be shared (e.g. processing QAN data for mulitple agents).
// Unbuffered chans serialize access to mini.pl in usePerl(). If either
// one of the chans is buffered, a race condition is created which
// results in goroutines receiving the wrong data. -- parseChan is a
/// different approach; it can be buffered.
}
func (m *Mini) Stop() {
close(m.stopChan)
}
func (m *Mini) Run() {
// Go-based SQL parsing
go m.parse()
// Perl-based SQL parsing
if !m.onlyTables {
cmd := exec.Command(m.cwd + "/mini.pl")
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
r := bufio.NewReader(stdout)
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
for {
select {
case query := <-m.queryIn:
// Do not use buffered IO so input/output is immediate.
// Do not forget "\n" because mini.pl is reading lines.
if _, err := io.WriteString(stdin, query+"\n"); err != nil {
log.Fatal(err)
}
q, err := r.ReadString('\n')
if err != nil {
log.Fatal(err)
}
m.miniOut <- q
case <-m.stopChan:
return
}
}
}
}
func (m *Mini) Parse(fingerprint, example, defaultDb string) (QueryInfo, error) {
fingerprint = strings.TrimSpace(fingerprint)
example = strings.TrimSpace(example)
q := QueryInfo{
Fingerprint: fingerprint,
Tables: []queryProto.Table{},
}
defer func() {
q.Abstract = strings.TrimSpace(q.Abstract)
}()
if m.Debug {
fmt.Printf("\n\nexample: %s\n", example)
fmt.Printf("\n\nfingerprint: %s\n", fingerprint)
}
query := fingerprint
// If we have a query example, that's better to parse than a fingerprint.
if example != "" {
query = example
}
// Fingerprints replace IN (1, 2) -> in (?+) but "?+" is not valid SQL so
// it breaks sqlparser/.
query = strings.Replace(query, "?+", "? ", -1)
// Internal newlines break everything.
query = strings.Replace(query, "\n", " ", -1)
s, err := sqlparser.Parse(query)
if err != nil {
if m.Debug {
fmt.Println("ERROR:", err)
}
return m.usePerl(query, q, err)
}
// Parse the SQL structure. The sqlparser is rather terrible, incomplete code,
// so it's prone to crash. If that happens, fall back to using the Perl code
// which only gets the abstract. Be sure to re-run the parse() goroutine for
// other callers and queries.
try := parseTry{
query: query,
q: q,
s: s,
queryChan: make(chan QueryInfo, 1),
crashChan: make(chan bool, 1),
}
m.parseChan <- try
select {
case q = <-try.queryChan:
case <-try.crashChan:
fmt.Printf("WARN: query crashes sqlparser: %s\n", query)
go m.parse()
return m.usePerl(query, q, err)
}
if defaultDb != "" {
for n, t := range q.Tables {
if t.Db == "" {
q.Tables[n].Db = defaultDb
}
}
}
return q, nil
}
func (m *Mini) parse() {
var crashChan chan bool
defer func() {
if err := recover(); err != nil {
crashChan <- true
}
}()
for {
select {
case p := <-m.parseChan:
q := p.q
crashChan = p.crashChan
switch s := p.s.(type) {
case *sqlparser.Select:
q.Abstract = "SELECT"
if m.Debug {
fmt.Printf("struct: %#v\n", s)
}
tables := getTablesFromTableExprs(s.From)
if len(tables) > 0 {
q.Tables = append(q.Tables, tables...)
q.Abstract += " " + tables.String()
}
case *sqlparser.Insert:
// REPLACEs will be recognized by sqlparser as INSERTs and the Action field
// will have the real command
q.Abstract = strings.ToUpper(s.Action)
if m.Debug {
fmt.Printf("struct: %#v\n", s)
}
table := queryProto.Table{
Db: s.Table.Qualifier.String(),
Table: s.Table.Name.String(),
}
q.Tables = append(q.Tables, table)
q.Abstract += " " + table.String()
case *sqlparser.Update:
q.Abstract = "UPDATE"
if m.Debug {
fmt.Printf("struct: %#v\n", s)
}
tables := getTablesFromTableExprs(s.TableExprs)
if len(tables) > 0 {
q.Tables = append(q.Tables, tables...)
q.Abstract += " " + tables.String()
}
case *sqlparser.Delete:
q.Abstract = "DELETE"
if m.Debug {
fmt.Printf("struct: %#v\n", s)
}
tables := getTablesFromTableExprs(s.TableExprs)
if len(tables) > 0 {
q.Tables = append(q.Tables, tables...)
q.Abstract += " " + tables.String()
}
case *sqlparser.Use:
q.Abstract = "USE"
case *sqlparser.Show:
sql := sqlparser.NewTrackedBuffer(nil)
s.Format(sql)
q.Abstract = strings.ToUpper(sql.String())
default:
if m.Debug {
fmt.Printf("unsupported type: %#v\n", p.s)
}
q, _ = m.usePerl(p.query, q, ErrNotSupported)
switch use := p.s.(type) {
case *sqlparser.DDL:
table := queryProto.Table{
Db: use.NewName.Qualifier.String(),
Table: use.NewName.Name.String(),
}
q.Tables = append(q.Tables, table)
}
}
p.queryChan <- q
case <-m.stopChan:
return
}
}
}
func (m *Mini) usePerl(query string, q QueryInfo, originalErr error) (QueryInfo, error) {
if m.onlyTables {
// Caller wants only tables but we can't get them because sqlparser
// failed for this query.
return q, originalErr
}
m.queryIn <- query
abstract := <-m.miniOut
q.Abstract = strings.Replace(abstract, "\n", "", -1)
return q, nil
}
func getTablesFromTableExprs(tes sqlparser.TableExprs) (tables protoTables) {
for _, te := range tes {
tables = append(tables, getTablesFromTableExpr(te, 0)...)
}
return tables
}
func getTablesFromTableExpr(te sqlparser.TableExpr, depth uint) (tables protoTables) {
if depth > MAX_JOIN_DEPTH {
return nil
}
depth++
switch a := te.(type) {
case *sqlparser.AliasedTableExpr:
n := a.Expr.(sqlparser.TableName)
db := n.Qualifier.String()
tbl := parseTableName(n.Name.String())
if db != "" || tbl != "" {
table := queryProto.Table{
Db: db,
Table: tbl,
}
tables = append(tables, table)
}
case *sqlparser.JoinTableExpr:
// This case happens for JOIN clauses. It recurses to the bottom
// of the tree via the left expressions, then it unwinds. E.g. with
// "a JOIN b JOIN c" the tree is:
//
// Left Right
// a b c AliasedTableExpr (case above)
// | | |
// +--+--+ |
// | |
// t2----+----+ JoinTableExpr
// |
// var t (t @ depth=1) JoinTableExpr
//
// Code will go left twice to arrive at "a". Then it will unwind and
// store the right-side values: "b" then "c". Because of this, if
// MAX_JOIN_DEPTH is reached, we lose the whole tree because if we take
// the existing right-side tables, we'll generate a misleading partial
// list of tables, e.g. "SELECT b c".
tables = append(tables, getTablesFromTableExpr(a.LeftExpr, depth)...)
tables = append(tables, getTablesFromTableExpr(a.RightExpr, depth)...)
}
return tables
}
func parseTableName(tableName string) string {
// https://dev.mysql.com/doc/refman/5.7/en/select.html#idm140358784149168
// You are permitted to specify DUAL as a dummy table name in situations where no tables are referenced:
//
// ```
// mysql> SELECT 1 + 1 FROM DUAL;
// -> 2
// ```
// DUAL is purely for the convenience of people who require that all SELECT statements
// should have FROM and possibly other clauses. MySQL may ignore the clauses.
// MySQL does not require FROM DUAL if no tables are referenced.
if tableName == "dual" {
tableName = ""
}
return tableName
}