This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 230
/
mask.go
399 lines (350 loc) · 9.12 KB
/
mask.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
// Copyright 2020 Pilosa Corp.
//
// 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 sql
import (
"strings"
"vitess.io/vitess/go/vt/sqlparser"
)
type selectPart int
const (
SelectPartDistinct selectPart = 1 << iota
SelectPartID
SelectPartStar
SelectPartField
SelectPartFields
SelectPartCountStar
SelectPartCountField
SelectPartCountDistinctField
SelectPartMinField
SelectPartMaxField
SelectPartSumField
SelectPartAvgField
)
type fromPart int
const (
FromPartTable fromPart = 1 << iota
FromPartTables
FromPartJoin
)
type wherePart int
const (
WherePartIDCondition wherePart = 1 << iota
WherePartFieldCondition
WherePartMultiFieldCondition
)
type groupByPart int
const (
GroupByPartField groupByPart = 1 << iota
GroupByPartFields
)
type havingPart int
const (
HavingPartCondition havingPart = 1 << iota
)
type orderByPart int
const (
OrderByPartField orderByPart = 1 << iota
OrderByPartFields
)
type limitPart int
const (
LimitPartLimit limitPart = 1 << iota
LimitPartOffset
)
type QueryMask struct {
SelectMask selectPart
FromMask fromPart
WhereMask wherePart
GroupByMask groupByPart
HavingMask havingPart
OrderByMask orderByPart
LimitMask limitPart
}
func NewQueryMask(sp selectPart, fp fromPart, wp wherePart, gp groupByPart, hp havingPart) QueryMask {
qm := QueryMask{}
qm.orSelect(sp)
qm.orFrom(fp)
qm.orWhere(wp)
qm.orGroupBy(gp)
qm.orHaving(hp)
return qm
}
// ApplyFilter returns true if m passes the filter f.
// Note: only certain query parts are included; namely,
// the orderBy and limit masks are not applied to the
// filter.
func (m *QueryMask) ApplyFilter(f QueryMask) bool {
if m.SelectMask&f.SelectMask != m.SelectMask {
return false
}
if m.FromMask&f.FromMask != m.FromMask {
return false
}
if m.WhereMask&f.WhereMask != m.WhereMask {
return false
}
if m.GroupByMask&f.GroupByMask != m.GroupByMask {
return false
}
if m.HavingMask&f.HavingMask != m.HavingMask {
return false
}
return true
}
// orSelect applies the bitwise-OR operation to the selectMask.
func (m *QueryMask) orSelect(o selectPart) {
m.SelectMask |= o
}
// orFrom applies the bitwise-OR operation to the fromMask.
func (m *QueryMask) orFrom(o fromPart) {
m.FromMask |= o
}
// orWhere applies the bitwise-OR operation to the whereMask.
func (m *QueryMask) orWhere(parts ...wherePart) {
for _, o := range parts {
m.WhereMask |= o
}
}
// orGroupBy applies the bitwise-OR operation to the groupByMask.
func (m *QueryMask) orGroupBy(o groupByPart) {
m.GroupByMask |= o
}
// orHaving applies the bitwise-OR operation to the havingMask.
func (m *QueryMask) orHaving(o havingPart) {
m.HavingMask |= o
}
// orOrderBy applies the bitwise-OR operation to the orderByMask.
func (m *QueryMask) orOrderBy(o orderByPart) {
m.OrderByMask |= o
}
// orLimit applies the bitwise-OR operation to the limitMask.
func (m *QueryMask) orLimit(o limitPart) {
m.LimitMask |= o
}
// HasSelect returns true if the mask contains a supported select clause.
func (m *QueryMask) HasSelect() bool {
return m.SelectMask > 0
}
// HasSelectPart returns true if the mask contains the provided select part.
func (m *QueryMask) HasSelectPart(p selectPart) bool {
return (m.SelectMask & p) > 0
}
// HasFrom returns true if the mask contains a supported from clause.
func (m *QueryMask) HasFrom() bool {
return m.FromMask > 0
}
// HasWhere returns true if the mask contains a supported where clause.
func (m *QueryMask) HasWhere() bool {
return m.WhereMask > 0
}
// HasGroupBy returns true if the mask contains a supported group by clause.
func (m *QueryMask) HasGroupBy() bool {
return m.GroupByMask > 0
}
// HasHaving returns true if the mask contains a supported having clause.
func (m *QueryMask) HasHaving() bool {
return m.HavingMask > 0
}
// HasOrderBy returns true if the mask contains a supported order by clause.
func (m *QueryMask) HasOrderBy() bool {
return m.OrderByMask > 0
}
// HasLimit returns true if the mask contains a supported limit clause.
func (m *QueryMask) HasLimit() bool {
return m.LimitMask > 0
}
//////////////////////////////////////////////////////////////////////////
func MustGenerateMask(sql string) QueryMask {
parsed, err := sqlparser.Parse(sql)
if err != nil {
return QueryMask{}
}
return GenerateMask(parsed)
}
func GenerateMask(parsed sqlparser.Statement) QueryMask {
qm := QueryMask{}
switch stmt := parsed.(type) {
case *sqlparser.Select:
if strings.ToLower(strings.TrimSpace(stmt.Distinct)) == "distinct" {
qm.orSelect(SelectPartDistinct)
}
// select parts
var fldCount int
for _, item := range stmt.SelectExprs {
switch expr := item.(type) {
case *sqlparser.AliasedExpr:
switch colExpr := expr.Expr.(type) {
case *sqlparser.ColName:
name := colExpr.Name.String()
if name == ColID {
qm.orSelect(SelectPartID)
} else {
fldCount++
}
case *sqlparser.FuncExpr:
funcName := FuncName(strings.ToLower(colExpr.Name.String()))
switch len(colExpr.Exprs) {
case 1:
var isStar bool
var isField bool
var isDistinctField bool
switch exp := colExpr.Exprs[0].(type) {
case *sqlparser.AliasedExpr:
switch exp.Expr.(type) {
case *sqlparser.ColName:
isField = true
isDistinctField = colExpr.Distinct
}
case *sqlparser.StarExpr:
isStar = true
}
switch funcName {
case FuncCount:
if isStar {
qm.orSelect(SelectPartCountStar)
} else if isDistinctField {
qm.orSelect(SelectPartCountDistinctField)
} else if isField {
qm.orSelect(SelectPartCountField)
}
case FuncMin:
if isField {
qm.orSelect(SelectPartMinField)
}
case FuncMax:
if isField {
qm.orSelect(SelectPartMaxField)
}
case FuncSum:
if isField {
qm.orSelect(SelectPartSumField)
}
case FuncAvg:
if isField {
qm.orSelect(SelectPartAvgField)
}
}
}
}
case *sqlparser.StarExpr:
qm.orSelect(SelectPartStar)
}
}
if fldCount == 1 {
qm.orSelect(SelectPartField)
} else if fldCount > 1 {
qm.orSelect(SelectPartFields)
}
// from parts
switch len(stmt.From) {
case 1:
switch stmt.From[0].(type) {
case *sqlparser.AliasedTableExpr:
qm.orFrom(FromPartTable)
case *sqlparser.JoinTableExpr:
qm.orFrom(FromPartJoin)
}
case 2:
switch stmt.From[0].(type) {
case *sqlparser.AliasedTableExpr:
switch stmt.From[1].(type) {
case *sqlparser.AliasedTableExpr:
qm.orFrom(FromPartTables)
}
}
}
// where parts
where := stmt.Where
if where != nil {
switch where.Type {
case "where":
qm.orWhere(generateWhereMask(where.Expr)...)
}
}
// group by parts
var groupByFieldCount int
for _, item := range stmt.GroupBy {
switch item.(type) {
case *sqlparser.ColName:
groupByFieldCount++
}
}
if groupByFieldCount == 1 {
qm.orGroupBy(GroupByPartField)
} else if groupByFieldCount > 1 {
qm.orGroupBy(GroupByPartFields)
}
// having parts
if stmt.Having != nil {
qm.orHaving(HavingPartCondition)
}
// order by parts
var orderByFieldCount int
for _, item := range stmt.OrderBy {
switch item.Expr.(type) {
case *sqlparser.ColName:
orderByFieldCount++
}
}
if orderByFieldCount == 1 {
qm.orOrderBy(OrderByPartField)
} else if orderByFieldCount > 1 {
qm.orOrderBy(OrderByPartFields)
}
// limit parts
if stmt.Limit != nil {
switch stmt.Limit.Rowcount.(type) {
case *sqlparser.SQLVal:
qm.orLimit(LimitPartLimit)
}
switch stmt.Limit.Offset.(type) {
case *sqlparser.SQLVal:
qm.orLimit(LimitPartOffset)
}
}
}
return qm
}
// TODO: add more recursion within the comparison operators (left/right parts)
func generateWhereMask(e sqlparser.Expr) []wherePart {
var wp []wherePart
switch expr := e.(type) {
case *sqlparser.ComparisonExpr:
var leftName string
if colExpr, ok := expr.Left.(*sqlparser.ColName); ok {
leftName = colExpr.Name.String()
}
switch leftName {
case ColID:
wp = append(wp, WherePartIDCondition)
case "":
//
default:
wp = append(wp, WherePartFieldCondition)
}
case *sqlparser.RangeCond:
wp = append(wp, WherePartFieldCondition)
case *sqlparser.AndExpr, *sqlparser.OrExpr:
// TODO: we need to recursively ensure that the left/right
// sides of these and/or expressions are field-op-val, and
// that none of the fields are "_id"
// TODO: could we use extractComparison or something like it?
wp = append(wp, WherePartMultiFieldCondition)
case *sqlparser.NotExpr:
wp = append(wp, generateWhereMask(expr.Expr)...)
case *sqlparser.IsExpr:
wp = append(wp, WherePartFieldCondition)
}
return wp
}