-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.go
521 lines (454 loc) · 14.6 KB
/
expr.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package expr
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"github.com/google/cel-go/common/operators"
"github.com/google/uuid"
)
var (
ErrEvaluableNotFound = fmt.Errorf("Evaluable instance not found in aggregator")
ErrInvalidType = fmt.Errorf("invalid type for tree")
ErrExpressionPartNotFound = fmt.Errorf("expression part not found")
)
// errEngineUnimplemented is used while we develop the aggregate tree library when trees
// are not yet implemented.
var errEngineUnimplemented = fmt.Errorf("tree type unimplemented")
// ExpressionEvaluator is a function which evalues an expression given input data, returning
// a boolean and error.
type ExpressionEvaluator func(ctx context.Context, e Evaluable, input map[string]any) (bool, error)
// EvaluableLoader returns one or more evaluable items given IDs.
type EvaluableLoader func(ctx context.Context, evaluableIDs ...uuid.UUID) ([]Evaluable, error)
// AggregateEvaluator represents a group of expressions that must be evaluated for a single
// event received.
//
// An AggregateEvaluator instance exists for every event name being matched.
type AggregateEvaluator interface {
// Add adds an expression to the tree evaluator. This returns true
// if the expression is aggregateable, or false if the expression will be
// evaluated each time an event is received.
Add(ctx context.Context, eval Evaluable) (bool, error)
// Remove removes an expression from the aggregate evaluator
Remove(ctx context.Context, eval Evaluable) error
// Evaluate checks input data against all exrpesssions in the aggregate in an optimal
// manner, only evaluating expressions when necessary (based off of tree matching).
//
// Note that any expressions added that cannot be evaluated optimally by trees
// are evaluated every time this function is called.
//
// Evaluate returns all matching Evaluables, plus the total number of evaluations
// executed.
Evaluate(ctx context.Context, data map[string]any) ([]Evaluable, int32, error)
// AggregateMatch returns all expression parts which are evaluable given the input data.
AggregateMatch(ctx context.Context, data map[string]any) ([]*StoredExpressionPart, error)
// Len returns the total number of aggregateable and constantly matched expressions
// stored in the evaluator.
Len() int
// AggregateableLen returns the number of expressions being matched by aggregated trees.
AggregateableLen() int
// ConstantLen returns the total number of expressions that must constantly
// be matched due to non-aggregateable clauses in their expressions.
ConstantLen() int
}
func NewAggregateEvaluator(
parser TreeParser,
eval ExpressionEvaluator,
evalLoader EvaluableLoader,
) AggregateEvaluator {
return &aggregator{
eval: eval,
parser: parser,
loader: evalLoader,
engines: map[EngineType]MatchingEngine{
EngineTypeStringHash: newStringEqualityMatcher(),
EngineTypeNullMatch: newNullMatcher(),
EngineTypeBTree: newNumberMatcher(),
},
lock: &sync.RWMutex{},
}
}
type aggregator struct {
eval ExpressionEvaluator
parser TreeParser
loader EvaluableLoader
// engines records all engines
engines map[EngineType]MatchingEngine
// lock prevents concurrent updates of data
lock *sync.RWMutex
// len stores the current len of aggregable expressions.
len int32
// constants tracks evaluable IDs that must always be evaluated, due to
// the expression containing non-aggregateable clauses.
constants []uuid.UUID
}
// Len returns the total number of aggregateable and constantly matched expressions
// stored in the evaluator.
func (a aggregator) Len() int {
return int(a.len) + len(a.constants)
}
// AggregateableLen returns the number of expressions being matched by aggregated trees.
func (a aggregator) AggregateableLen() int {
return int(a.len)
}
// ConstantLen returns the total number of expressions that must constantly
// be matched due to non-aggregateable clauses in their expressions.
func (a aggregator) ConstantLen() int {
return len(a.constants)
}
func (a *aggregator) Evaluate(ctx context.Context, data map[string]any) ([]Evaluable, int32, error) {
var (
err error
matched = int32(0)
result = []Evaluable{}
)
// TODO: Concurrently match constant expressions using a semaphore for capacity.
// Match constant expressions always.
constantEvals, err := a.loader(ctx, a.constants...)
if err != nil {
return nil, 0, err
}
for _, expr := range constantEvals {
atomic.AddInt32(&matched, 1)
if expr.GetExpression() == "" {
result = append(result, expr)
continue
}
// NOTE: We don't need to add lifted expression variables,
// because match.Parsed.Evaluable() returns the original expression
// string.
ok, evalerr := a.eval(ctx, expr, data)
if evalerr != nil {
err = errors.Join(err, evalerr)
continue
}
if ok {
result = append(result, expr)
}
}
matches, merr := a.AggregateMatch(ctx, data)
if merr != nil {
err = errors.Join(err, merr)
}
// Load all evaluable instances directly.
uuids := make([]uuid.UUID, len(matches))
for n, m := range matches {
uuids[n] = m.Parsed.EvaluableID
}
evaluables, lerr := a.loader(ctx, uuids...)
if err != nil {
err = errors.Join(err, lerr)
}
// Each match here is a potential success. When other trees and operators which are walkable
// are added (eg. >= operators on strings), ensure that we find the correct number of matches
// for each group ID and then skip evaluating expressions if the number of matches is <= the group
// ID's length.
seen := map[uuid.UUID]struct{}{}
for _, match := range evaluables {
if _, ok := seen[match.GetID()]; ok {
continue
}
atomic.AddInt32(&matched, 1)
// NOTE: We don't need to add lifted expression variables,
// because match.Parsed.Evaluable() returns the original expression
// string.
ok, evalerr := a.eval(ctx, match, data)
seen[match.GetID()] = struct{}{}
if evalerr != nil {
err = errors.Join(err, evalerr)
continue
}
if ok {
result = append(result, match)
}
}
return result, matched, err
}
// AggregateMatch attempts to match incoming data to all PredicateTrees, resulting in a selection
// of parts of an expression that have matched.
func (a *aggregator) AggregateMatch(ctx context.Context, data map[string]any) ([]*StoredExpressionPart, error) {
result := []*StoredExpressionPart{}
a.lock.RLock()
defer a.lock.RUnlock()
// Each match here is a potential success. Ensure that we find the correct number of matches
// for each group ID and then skip evaluating expressions if the number of matches is <= the group
// ID's length. For example, (A && B && C) is a single group ID and must have a count >= 3,
// else we know a required comparason did not match.
//
// Note that having a count >= the group ID value does not guarantee that the expression is valid.
counts := map[groupID]int{}
// Store all expression parts per group ID for returning.
found := map[groupID][]*StoredExpressionPart{}
// protect the above locks with a map.
lock := &sync.Mutex{}
for _, engine := range a.engines {
matched, err := engine.Match(ctx, data)
if err != nil {
return nil, err
}
// Add all found items from the engine to the above list.
lock.Lock()
for _, eval := range matched {
counts[eval.GroupID] += 1
if _, ok := found[eval.GroupID]; !ok {
found[eval.GroupID] = []*StoredExpressionPart{}
}
found[eval.GroupID] = append(found[eval.GroupID], eval)
}
lock.Unlock()
}
// Validate that groups meet the minimum size.
for k, count := range counts {
// if int(k.Size()) > count {
// // The GroupID required more comparisons to equate to true than
// // we had, so this could never evaluate to true. Skip this.
// //
// // TODO: Optimize and fix.
// continue
// }
_ = count
result = append(result, found[k]...)
}
return result, nil
}
// Add adds an Evaluable to the aggregate tree engine for matching. It returns
// a boolean indicating whether the expression is suitable for aggregate tree
// matching, allowing rapid exclusion of non-matching expressions.
func (a *aggregator) Add(ctx context.Context, eval Evaluable) (bool, error) {
// parse the expression using our tree parser.
parsed, err := a.parser.Parse(ctx, eval)
if err != nil {
return false, err
}
if eval.GetExpression() == "" || parsed.HasMacros {
// This is an empty expression which always matches.
a.lock.Lock()
a.constants = append(a.constants, parsed.EvaluableID)
a.lock.Unlock()
return false, nil
}
for _, g := range parsed.RootGroups() {
ok, err := a.iterGroup(ctx, g, parsed, a.addNode)
if err != nil || !ok {
// This is the first time we're seeing a non-aggregateable
// group, so add it to the constants list and don't do anything else.
a.lock.Lock()
a.constants = append(a.constants, parsed.EvaluableID)
a.lock.Unlock()
return false, err
}
}
// Track the number of added expressions correctly.
atomic.AddInt32(&a.len, 1)
return true, nil
}
func (a *aggregator) Remove(ctx context.Context, eval Evaluable) error {
if eval.GetExpression() == "" {
return a.removeConstantEvaluable(ctx, eval)
}
// parse the expression using our tree parser.
parsed, err := a.parser.Parse(ctx, eval)
if err != nil {
return err
}
aggregateable := true
for _, g := range parsed.RootGroups() {
ok, err := a.iterGroup(ctx, g, parsed, a.removeNode)
if err == ErrExpressionPartNotFound {
return ErrEvaluableNotFound
}
if err != nil {
return err
}
if !ok && aggregateable {
if err := a.removeConstantEvaluable(ctx, eval); err != nil {
return err
}
aggregateable = false
}
}
if aggregateable {
atomic.AddInt32(&a.len, -1)
}
return nil
}
func (a *aggregator) removeConstantEvaluable(ctx context.Context, eval Evaluable) error {
// Find the index of the evaluable in constants and yank out.
idx := -1
for n, item := range a.constants {
if item == eval.GetID() {
idx = n
break
}
}
if idx == -1 {
return ErrEvaluableNotFound
}
a.lock.Lock()
a.constants = append(a.constants[:idx], a.constants[idx+1:]...)
a.lock.Unlock()
return nil
}
func (a *aggregator) iterGroup(ctx context.Context, node *Node, parsed *ParsedExpression, op nodeOp) (bool, error) {
if len(node.Ors) > 0 {
// If there are additional branches, don't bother to add this to the aggregate tree.
// Mark this as a non-exhaustive addition and skip immediately.
//
// TODO: Allow ORs _only if_ the ORs are not nested, eg. the ORs are basic predicate
// groups that themselves have no branches.
return false, nil
}
if len(node.Ands) > 0 {
for _, n := range node.Ands {
if !n.HasPredicate() || len(n.Ors) > 0 {
// Don't handle sub-branching for now.
return false, nil
}
if !isAggregateable(n) {
return false, nil
}
}
}
all := node.Ands
if node.Predicate != nil {
if !isAggregateable(node) {
return false, nil
}
// Merge all of the nodes together and check whether each node is aggregateable.
all = append(node.Ands, node)
}
// Create a new group ID which tracks the number of expressions that must match
// within this group in order for the group to pass.
//
// This includes ALL ands, plus at least one OR.
//
// When checking an incoming event, we match the event against each node's
// ident/variable. Using the group ID, we can see if we've matched N necessary
// items from the same identifier. If so, the evaluation is true.
for _, n := range all {
err := op(ctx, n, parsed)
if err == errEngineUnimplemented {
return false, nil
}
if err != nil {
return false, err
}
}
return true, nil
}
func engineType(p Predicate) EngineType {
// switch on type of literal AND operator type. int64/float64 literals require
// btrees, texts require ARTs, and so on.
switch p.Literal.(type) {
case int, int64, float64:
if p.Operator == operators.NotEquals {
// StringHash is only used for matching on equality.
return EngineTypeNone
}
// return EngineTypeNone
return EngineTypeBTree
case string:
if p.Operator == operators.Equals {
// StringHash is only used for matching on equality.
return EngineTypeStringHash
}
case nil:
// Only allow this if we're not comparing two idents.
if p.LiteralIdent != nil {
return EngineTypeNone
}
return EngineTypeNullMatch
}
// case int64, float64:
// return EngineTypeBTree
return EngineTypeNone
}
// nodeOp represents an op eg. addNode or removeNode
type nodeOp func(ctx context.Context, n *Node, parsed *ParsedExpression) error
func (a *aggregator) addNode(ctx context.Context, n *Node, parsed *ParsedExpression) error {
if n.Predicate == nil {
return nil
}
e := a.engine(n)
if e == nil {
return errEngineUnimplemented
}
// Don't allow anything to update in parallel. This ensures that Add() can be called
// concurrently.
a.lock.Lock()
defer a.lock.Unlock()
return e.Add(ctx, ExpressionPart{
GroupID: n.GroupID,
Predicate: n.Predicate,
Parsed: parsed,
})
}
func (a *aggregator) removeNode(ctx context.Context, n *Node, parsed *ParsedExpression) error {
if n.Predicate == nil {
return nil
}
e := a.engine(n)
if e == nil {
return errEngineUnimplemented
}
// Don't allow anything to update in parallel. This enrues that Add() can be called
// concurrently.
a.lock.Lock()
defer a.lock.Unlock()
return e.Remove(ctx, ExpressionPart{
GroupID: n.GroupID,
Predicate: n.Predicate,
Parsed: parsed,
})
}
func (a *aggregator) engine(n *Node) MatchingEngine {
requiredEngine := engineType(*n.Predicate)
if requiredEngine == EngineTypeNone {
return nil
}
for _, engine := range a.engines {
if engine.Type() != requiredEngine {
continue
}
return engine
}
return nil
}
func isAggregateable(n *Node) bool {
if n.Predicate == nil {
// This is a parent node. We skip aggregateable checks and only
// return false based off of predicate information.
return true
}
if n.Predicate.LiteralIdent != nil {
// We're matching idents together, so this is not aggregateable.
return false
}
if n.Predicate.Operator == "comprehension" {
return false
}
switch v := n.Predicate.Literal.(type) {
case string:
if len(v) == 0 {
return false
}
if n.Predicate.Operator == operators.NotEquals {
// NOTE: NotEquals is _not_ supported. This requires selecting all leaf nodes _except_
// a given leaf, iterating over a tree. We may as well execute every expressiona s the difference
// is negligible.
return false
}
// Right now, we only support equality checking.
// TODO: Add GT(e)/LT(e) matching with tree iteration.
return n.Predicate.Operator == operators.Equals
case int, int64, float64:
return true
case nil:
// This is null, which is supported and a simple lookup to check
// if the event's key in question is present and is not nil.
return true
default:
return false
}
}