forked from cert-lv/graphoscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
330 lines (268 loc) · 8.4 KB
/
parse.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
package main
import (
"fmt"
"regexp"
"strconv"
"github.com/blastrain/vitess-sqlparser/sqlparser"
)
var (
// Regex to remove "datetime" field from the query.
// Useful when data source doesn't contain such field
reDT = regexp.MustCompile(`(?i) +and +datetime +between +('|")\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?Z('|") +and +('|")\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?Z('|")`)
)
/*
* Parse SQL query for the later processing by the collectors,
* textual SQL query into a logical object.
*
* Receives a query to parse, whether result should contain a "datetime" field,
* whether data source supports SQL features
*/
func parseSQL(sql string, includeDatetime bool, includeFields []string, replaceFields map[string]string, supportsSQL bool) ([]*sqlparser.Select, error) {
// Remove "datetime" field from the query if must be ignored
if !includeDatetime {
sql = reDT.ReplaceAllString(sql, "")
}
/*
* Parse and validate received SQL query
*/
// Parse final SQL query
ast, err := sqlparser.Parse("SELECT * " + sql)
if err != nil {
return nil, fmt.Errorf("Can't parse SQL query: %s", err.Error())
}
// Include required fields only
if len(includeFields) != 0 {
// Remove default "*" as field
ast.(*sqlparser.Select).SelectExprs = sqlparser.SelectExprs{}
for _, field := range includeFields {
colName := &sqlparser.ColName{
Name: sqlparser.NewColIdent(field),
}
aliasedExpr := &sqlparser.AliasedExpr{
Expr: colName,
}
ast.(*sqlparser.Select).SelectExprs = append(ast.(*sqlparser.Select).SelectExprs, aliasedExpr)
}
}
query, ok := ast.(*sqlparser.Select)
if !ok {
return nil, fmt.Errorf("Only SELECT statement is allowed")
}
// Handle WHERE
if query.Where == nil {
return nil, fmt.Errorf("WHERE filters are missing")
}
switch query.Where.Expr.(type) {
case *sqlparser.ParenExpr,
*sqlparser.AndExpr,
*sqlparser.OrExpr,
*sqlparser.ComparisonExpr:
default:
return nil, fmt.Errorf("WHERE statement is not a list of filters")
}
// Handle LIMIT
if query.Limit != nil {
// Handle offset
if query.Limit.Offset != nil {
offset := sqlparser.String(query.Limit.Offset)
_, err := strconv.Atoi(offset)
if err != nil {
return nil, fmt.Errorf("\"LIMIT ?,x\" value is not an integer: %v (%T)", offset, offset)
}
} else {
query.Limit.Offset = &sqlparser.SQLVal{Type: 1, Val: []uint8{0x30}} // Start from 0
}
// Handle rowcount
if query.Limit.Rowcount != nil {
rowcount := sqlparser.String(query.Limit.Rowcount)
limit, err := strconv.Atoi(rowcount)
if err != nil {
return nil, fmt.Errorf("\"LIMIT x,?\" value is not an integer: %v (%T)", rowcount, rowcount)
}
if limit < 0 {
return nil, fmt.Errorf("LIMIT rowcount can't be less than 0")
}
// Decrease to the server-side value if user's custom value is too large
if limit > config.Limit {
query.Limit.Rowcount = &sqlparser.SQLVal{Type: 1, Val: []uint8(strconv.Itoa(config.Limit))}
}
}
} else {
query.Limit = &sqlparser.Limit{
Offset: &sqlparser.SQLVal{Type: 1, Val: []uint8{0x30}}, // Start from 0
Rowcount: &sqlparser.SQLVal{Type: 1, Val: []uint8(strconv.Itoa(config.Limit))},
}
}
// Handle multiple FROM
if len(query.From) != 1 {
return nil, fmt.Errorf("Multiple FROM currently not supported")
}
// Handle DISTINCT
if query.Distinct != "" {
return nil, fmt.Errorf("DISTINCT shouldn't be used, API service already returns unique nodes pairs only")
}
/*
* Split OR and IN queries into a list of separate queries
* for the data sources that don't support such queries directly
*/
queries := []*sqlparser.Select{}
if !supportsSQL {
queries, err = splitQuery(query)
if err != nil {
return nil, fmt.Errorf("Can't split query: " + err.Error())
}
} else {
queries = append(queries, query)
}
/*
* Replace user defined fields
*/
for _, node := range queries {
err = replaceSQL(node, replaceFields)
}
/*
* Return possibly modified result
*/
return queries, err
}
/*
* Split clean OR and IN queries into a list of separate queries.
*
* Supported query types:
* - ip='8.8.8.8'
* - ip='8.8.8.8' OR ip='1.2.3.4'
* - domain IN ('example.com','google.com')
*
* Operators have to be "=" only, mix of different operators is not supported yet
*/
func splitQuery(expr *sqlparser.Select) ([]*sqlparser.Select, error) {
fields := sqlparser.String(expr.SelectExprs)
where := expr.Where.Expr
lefts := []sqlparser.Expr{}
// A list of independend SELECT single-field queries to return
selects := []*sqlparser.Select{}
var err error
//fmt.Printf("Expr: %#v\n", where)
if and, ok := where.(*sqlparser.AndExpr); ok {
if and.Right.(*sqlparser.RangeCond).Operator == "between" &&
sqlparser.String(and.Right.(*sqlparser.RangeCond).Left) == "datetime" {
// Handle:
// field='...' AND datetime BETWEEN ...
// field IN ('...','...') AND datetime BETWEEN ...
if comp, ok := and.Left.(*sqlparser.ComparisonExpr); ok {
lefts, err = splitOrIn(fields, comp, nil)
if err != nil {
return nil, err
}
// Handle:
// (field='...') AND datetime BETWEEN ...
// (field='...' OR field='...') AND datetime BETWEEN ...
// (field IN ('...','...')) AND datetime BETWEEN ...
} else if paren, ok := and.Left.(*sqlparser.ParenExpr); ok {
lefts, err = splitOrIn(fields, paren.Expr, nil)
if err != nil {
return nil, err
}
}
}
// Handle:
// (field='...' OR field=...')
// (field IN ('...','...'))
// (field='...' AND param='...')
} else if paren, ok := where.(*sqlparser.ParenExpr); ok {
if _, ok := paren.Expr.(*sqlparser.AndExpr); ok {
return selects, nil
}
lefts, err = splitOrIn(fields, paren.Expr, nil)
if err != nil {
return nil, err
}
// Handle: field='...' OR field=...'
} else if or, ok := where.(*sqlparser.OrExpr); ok {
lefts, err = splitOrIn(fields, or, nil)
if err != nil {
return nil, err
}
// Handle:
// field='...'
// field IN ('...','...')
} else if comp, ok := where.(*sqlparser.ComparisonExpr); ok {
lefts, err = splitOrIn(fields, comp, nil)
if err != nil {
return nil, err
}
}
// Generate multiple independent single-field queries
for _, left := range lefts {
if and, ok := where.(*sqlparser.AndExpr); ok {
and.Left = left
} else if paren, ok := where.(*sqlparser.ParenExpr); ok {
paren.Expr = left
}
// SQL -> string -> SQL
// as it is hard to deep copy 'expr' to prevent rewriting it
ast, err := sqlparser.Parse(sqlparser.String(expr))
if err != nil {
return nil, fmt.Errorf("Can't parse splitted SQL query: %s", err.Error())
}
query := ast.(*sqlparser.Select)
selects = append(selects, query)
}
return selects, nil
}
/*
* A loop to detect and split OR/IN query
*/
func splitOrIn(fields string, expr sqlparser.Expr, list []sqlparser.Expr) ([]sqlparser.Expr, error) {
if list == nil {
list = []sqlparser.Expr{}
}
var err error
// Detect a clean OR query
// and avoid more complex queries like "... OR (... AND ...)"
switch left := expr.(type) {
case *sqlparser.OrExpr:
_, okOr := left.Left.(*sqlparser.OrExpr)
comp, okComp := left.Left.(*sqlparser.ComparisonExpr)
if okOr || (okComp && comp.Operator == "=") {
if c, ok := left.Right.(*sqlparser.ComparisonExpr); ok {
if c.Operator != "=" {
return list, nil
}
list = append(list, left.Right)
if c, ok := left.Left.(*sqlparser.ComparisonExpr); ok {
list = append(list, c)
} else if or, ok := left.Left.(*sqlparser.OrExpr); ok {
list, err = splitOrIn(fields, or, list)
if err != nil {
return nil, err
}
} else {
return list, nil
}
}
}
// Detect a clean IN query.
// Single 'field=value' can be represented as 1 element array
case *sqlparser.ComparisonExpr:
field := sqlparser.String(left.Left)
if left.Operator == "in" {
for _, value := range left.Right.(sqlparser.ValTuple) {
ast, err := sqlparser.Parse("SELECT " + fields + " WHERE " + field + "=" + sqlparser.String(value))
if err != nil {
return nil, err
}
list = append(list, ast.(*sqlparser.Select).Where.Expr)
}
} else if left.Operator == "=" {
ast, err := sqlparser.Parse("SELECT " + fields + " WHERE " + field + "=" + sqlparser.String(left.Right))
if err != nil {
return nil, err
}
list = append(list, ast.(*sqlparser.Select).Where.Expr)
}
default:
log.Error().Msgf("Unexpected \"Left\" part of the query to split: %#v\n", left)
}
return list, nil
}