forked from couchbaselabs/gojsonsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.go
654 lines (542 loc) · 14.9 KB
/
transform.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
// Copyright 2018 Couchbase, Inc. All rights reserved.
package gojsonsm
import (
"errors"
"fmt"
"regexp"
"strings"
)
type resolvedFieldRef struct {
Context *compileContext
Path []string
}
func (ref resolvedFieldRef) String() string {
outStr := "$ROOT"
if ref.Context != nil {
outStr = fmt.Sprintf("%s", ref.Context)
}
if len(ref.Path) > 0 {
return outStr + "." + strings.Join(ref.Path, ".")
}
return outStr
}
type mergeExpr struct {
exprs []Expression
bucketIDs []BucketID
}
func (expr mergeExpr) String() string {
if len(expr.exprs) == 0 {
return "%%ERROR%%"
} else if len(expr.exprs) == 1 {
return expr.exprs[0].String()
} else {
value := reindentString(expr.exprs[0].String(), " ")
for i := 1; i < len(expr.exprs); i++ {
value += "\nOR\n"
value += reindentString(expr.exprs[i].String(), " ")
}
return value
}
}
type compileContext struct {
Depth int
Var VariableID
Node *ExecNode
}
func (ctx *compileContext) String() string {
return fmt.Sprintf("$%d@%d", ctx.Var, ctx.Depth)
}
type Transformer struct {
SlotIdx SlotID
BucketIdx BucketID
RootExec *ExecNode
RootTree binTree
ContextStack []*compileContext
ActiveBucketIdx BucketID
}
func (t *Transformer) getExecNode(field resolvedFieldRef) *ExecNode {
node := t.RootExec
if field.Context != nil {
node = field.Context.Node
}
for _, entry := range field.Path {
if node.Elems == nil {
node.Elems = make(map[string]*ExecNode)
} else if newNode, ok := node.Elems[entry]; ok {
node = newNode
continue
}
newNode := &ExecNode{}
node.Elems[entry] = newNode
node = newNode
}
return node
}
func (t *Transformer) storeExecNode(node *ExecNode) SlotID {
if node.StoreId == 0 {
node.StoreId = t.newSlot()
}
return node.StoreId
}
func (t *Transformer) getAfterNode(node *ExecNode) *AfterNode {
if node.After == nil {
node.After = &AfterNode{}
}
return node.After
}
func (t *Transformer) newBucket() BucketID {
newBucketIdx := t.BucketIdx
t.BucketIdx++
t.RootTree.data = append(t.RootTree.data, *NewBinTreeNode(
nodeTypeLeaf,
int(t.ActiveBucketIdx),
0,
0,
))
t.ActiveBucketIdx = newBucketIdx
return newBucketIdx
}
func (t *Transformer) newSlot() SlotID {
newSlotID := t.SlotIdx
t.SlotIdx++
return newSlotID + 1
}
func (t *Transformer) pushContext(varID VariableID, execNode *ExecNode) {
t.ContextStack = append(t.ContextStack, &compileContext{
Depth: len(t.ContextStack) + 1,
Var: varID,
Node: execNode,
})
}
func (t *Transformer) popContext(execNode *ExecNode) {
topContext := t.ContextStack[len(t.ContextStack)-1]
if topContext.Node != execNode {
panic("unexpected context in the stack")
}
t.ContextStack = t.ContextStack[0 : len(t.ContextStack)-1]
}
func (t *Transformer) gatherResolvedFieldRefs(expr Expression) []resolvedFieldRef {
fieldRefs := fetchExprFieldRefs(expr)
var resolvedFieldRefs []resolvedFieldRef
for _, fieldRef := range fieldRefs {
resolvedFieldRefs = append(resolvedFieldRefs, t.resolveRef(fieldRef))
}
return resolvedFieldRefs
}
func (t *Transformer) getContext(varID VariableID) *compileContext {
if varID == 0 {
return nil
}
for i := len(t.ContextStack) - 1; i >= 0; i++ {
if t.ContextStack[i].Var == varID {
return t.ContextStack[i]
}
}
panic("reference to out-of-context variable was encountered")
}
func (t *Transformer) resolveRef(fieldExpr FieldExpr) resolvedFieldRef {
return resolvedFieldRef{
Context: t.getContext(fieldExpr.Root),
Path: fieldExpr.Path,
}
}
func (t *Transformer) findFieldRefsBestRoot(fieldRefs []resolvedFieldRef) (resolvedFieldRef, bool) {
var currentContext *compileContext
if len(t.ContextStack) > 0 {
currentContext = t.ContextStack[len(t.ContextStack)-1]
}
var contextFields []resolvedFieldRef
for _, fieldRef := range fieldRefs {
if fieldRef.Context == currentContext {
contextFields = append(contextFields, fieldRef)
}
}
if len(contextFields) == 0 {
return resolvedFieldRef{
Context: currentContext,
Path: []string{},
}, false
}
// Pick the base path as being the longest of all the paths
basePath := contextFields[0].Path
for i := 1; i < len(contextFields); i++ {
if len(contextFields[i].Path) > len(basePath) {
basePath = contextFields[i].Path
}
}
var commonPath []string
PathLoop:
for j := 0; j < len(basePath); j++ {
for i := 0; i < len(contextFields); i++ {
deepField := contextFields[i]
if len(deepField.Path) < j || deepField.Path[j] != basePath[j] {
break PathLoop
}
}
commonPath = append(commonPath, basePath[j])
}
needsAfter := len(commonPath) < len(basePath)
return resolvedFieldRef{
Context: currentContext,
Path: commonPath,
}, needsAfter
}
type nodeRef struct {
node *ExecNode
after *AfterNode
}
func (ref *nodeRef) AddOp(op OpNode) {
if ref.node != nil {
ref.node.Ops = append(ref.node.Ops, op)
} else if ref.after != nil {
ref.after.Ops = append(ref.after.Ops, op)
} else {
panic("cannot add an op to a null node reference")
}
}
func (ref *nodeRef) AddLoop(loop LoopNode) {
// TODO(brett19): This function currently validates that there
// is only 1 valid possible loop target used depending on which
// loop type its going into. Someday we may implement function
// support which will invalidate this error check. We do this
// here to ensure that the error is caught at compilation rather
// than at match time.
if ref.node != nil {
if loop.Target != nil {
panic("loops must always target the active state")
}
ref.node.Loops = append(ref.node.Loops, loop)
} else if ref.after != nil {
if _, ok := loop.Target.(SlotRef); !ok {
panic("after-loops must always target a slot")
}
ref.after.Loops = append(ref.after.Loops, loop)
} else {
panic("cannot add a loop to a null node reference")
}
}
func (t *Transformer) pickBaseNode(expr Expression) nodeRef {
fieldRefs := t.gatherResolvedFieldRefs(expr)
bestBase, needsAfter := t.findFieldRefsBestRoot(fieldRefs)
baseNode := t.getExecNode(bestBase)
if !needsAfter {
return nodeRef{
node: baseNode,
after: nil,
}
}
afterNode := t.getAfterNode(baseNode)
return nodeRef{
node: nil,
after: afterNode,
}
}
func (t *Transformer) makeDataRefRecurse(expr Expression, context nodeRef, isRoot bool) (DataRef, error) {
switch expr := expr.(type) {
case FieldExpr:
resField := t.resolveRef(expr)
fieldNode := t.getExecNode(resField)
if context.node == fieldNode {
if isRoot {
return nil, nil
} else {
return activeLitRef{}, nil
}
}
slot := t.storeExecNode(fieldNode)
return SlotRef{slot}, nil
case ValueExpr:
val := NewFastVal(expr.Value)
if val.IsString() {
val, _ = val.ToJsonString()
}
val.userDefined = true
return val, nil
case RegexExpr:
regex, err := regexp.Compile(expr.Regex.(string))
if err != nil {
return nil, errors.New("failed to compile RegexExpr: " + err.Error())
}
val := NewFastVal(regex)
val.userDefined = true
return val, nil
case PcreExpr:
pcreWrapper, err := MakePcreWrapper(expr.Pcre.(string))
val := NewFastVal(pcreWrapper)
val.userDefined = true
return val, err
case FuncExpr:
var params []DataRef
for _, paramExpr := range expr.Params {
param, err := t.makeDataRefRecurse(paramExpr, context, false)
if err != nil {
return nil, err
}
params = append(params, param)
}
return FuncRef{
FuncName: expr.FuncName,
Params: params,
}, nil
case TimeExpr:
val, err := GetNewTimeFastVal(expr.Time.(string))
val.userDefined = true
return val, err
}
return nil, errors.New("unsupported expression in parameter")
}
func (t *Transformer) makeDataRef(expr Expression, context nodeRef) (DataRef, error) {
return t.makeDataRefRecurse(expr, context, true)
}
func (t *Transformer) transformMergePiece(expr mergeExpr, i int) *ExecNode {
if i == len(expr.exprs)-1 {
expr.bucketIDs[i] = t.ActiveBucketIdx
return t.transformOne(expr.exprs[i])
}
baseBucketIdx := t.ActiveBucketIdx
t.RootTree.data[baseBucketIdx].NodeType = nodeTypeNeor
t.newBucket()
expr.bucketIDs[i] = t.ActiveBucketIdx
t.RootTree.data[baseBucketIdx].Left = int(t.ActiveBucketIdx)
t.transformOne(expr.exprs[i])
t.ActiveBucketIdx = baseBucketIdx
t.newBucket()
t.RootTree.data[baseBucketIdx].Right = int(t.ActiveBucketIdx)
t.transformMergePiece(expr, i+1)
return nil
}
func (t *Transformer) transformMerge(expr mergeExpr) *ExecNode {
return t.transformMergePiece(expr, 0)
}
func (t *Transformer) transformNot(expr NotExpr) *ExecNode {
baseBucketIdx := t.ActiveBucketIdx
t.RootTree.data[baseBucketIdx].NodeType = nodeTypeNot
t.newBucket()
t.RootTree.data[baseBucketIdx].Left = int(t.ActiveBucketIdx)
t.transformOne(expr.SubExpr)
return nil
}
func (t *Transformer) transformOr(expr OrExpr) *ExecNode {
if len(expr) == 1 {
return t.transformOne(expr[0])
}
baseBucketIdx := t.ActiveBucketIdx
t.RootTree.data[baseBucketIdx].NodeType = nodeTypeOr
t.newBucket()
t.RootTree.data[baseBucketIdx].Left = int(t.ActiveBucketIdx)
t.transformOne(expr[0])
t.ActiveBucketIdx = baseBucketIdx
t.newBucket()
t.RootTree.data[baseBucketIdx].Right = int(t.ActiveBucketIdx)
t.transformOr(expr[1:])
return nil
}
func (t *Transformer) transformAnd(expr AndExpr) *ExecNode {
if len(expr) == 1 {
return t.transformOne(expr[0])
}
baseBucketIdx := t.ActiveBucketIdx
t.RootTree.data[baseBucketIdx].NodeType = nodeTypeAnd
t.newBucket()
t.RootTree.data[baseBucketIdx].Left = int(t.ActiveBucketIdx)
t.transformOne(expr[0])
t.ActiveBucketIdx = baseBucketIdx
t.newBucket()
t.RootTree.data[baseBucketIdx].Right = int(t.ActiveBucketIdx)
t.transformAnd(expr[1:])
return nil
}
func (t *Transformer) transformLoop(expr Expression, loopType LoopType, varID VariableID, inExpr, subExpr Expression) *ExecNode {
baseNode := t.pickBaseNode(expr)
newNode := &ExecNode{}
loopTarget, err := t.makeDataRef(inExpr, baseNode)
if err != nil {
panic(err)
}
baseBucketIdx := t.ActiveBucketIdx
t.RootTree.data[baseBucketIdx].NodeType = nodeTypeLoop
t.newBucket()
t.RootTree.data[baseBucketIdx].Left = int(t.ActiveBucketIdx)
baseNode.AddLoop(LoopNode{
t.ActiveBucketIdx,
loopType,
loopTarget,
newNode,
})
// Push this context to the stack
t.pushContext(varID, newNode)
// Transform the loops expression body
t.transformOne(subExpr)
// Pop from the context stack
t.popContext(newNode)
return nil
}
func (t *Transformer) transformAnyIn(expr AnyInExpr) *ExecNode {
return t.transformLoop(expr, LoopTypeAny, expr.VarId, expr.InExpr, expr.SubExpr)
}
func (t *Transformer) transformEveryIn(expr EveryInExpr) *ExecNode {
return t.transformLoop(expr, LoopTypeEvery, expr.VarId, expr.InExpr, expr.SubExpr)
}
func (t *Transformer) transformAnyEveryIn(expr AnyEveryInExpr) *ExecNode {
return t.transformLoop(expr, LoopTypeAnyEvery, expr.VarId, expr.InExpr, expr.SubExpr)
}
func (t *Transformer) transformExists(expr ExistsExpr) *ExecNode {
baseNode := t.pickBaseNode(expr)
lhsDataRef, err := t.makeDataRef(expr.SubExpr, baseNode)
if err != nil {
panic(err)
}
baseNode.AddOp(OpNode{
t.ActiveBucketIdx,
OpTypeExists,
lhsDataRef,
nil,
})
return nil
}
func (t *Transformer) transformNotExists(expr NotExistsExpr) *ExecNode {
return t.transformOne(NotExpr{
ExistsExpr{
expr.SubExpr,
},
})
}
func (t *Transformer) transformComparison(expr Expression, op OpType, lhs, rhs Expression) *ExecNode {
baseNode := t.pickBaseNode(expr)
lhsRef, err := t.makeDataRef(lhs, baseNode)
if err != nil {
panic(err)
}
rhsRef, err := t.makeDataRef(rhs, baseNode)
if err != nil {
panic(err)
}
baseNode.AddOp(OpNode{
t.ActiveBucketIdx,
op,
lhsRef,
rhsRef,
})
return nil
}
func (t *Transformer) transformEquals(expr EqualsExpr) *ExecNode {
return t.transformComparison(expr, OpTypeEquals, expr.Lhs, expr.Rhs)
}
func (t *Transformer) transformNotEquals(expr NotEqualsExpr) *ExecNode {
return t.transformOne(NotExpr{EqualsExpr{expr.Lhs, expr.Rhs}})
}
func (t *Transformer) transformLessThan(expr LessThanExpr) *ExecNode {
return t.transformComparison(expr, OpTypeLessThan, expr.Lhs, expr.Rhs)
}
func (t *Transformer) transformLessEquals(expr LessEqualsExpr) *ExecNode {
return t.transformComparison(expr, OpTypeLessEquals, expr.Lhs, expr.Rhs)
}
func (t *Transformer) transformGreaterThan(expr GreaterThanExpr) *ExecNode {
return t.transformComparison(expr, OpTypeGreaterThan, expr.Lhs, expr.Rhs)
}
func (t *Transformer) transformGreaterEquals(expr GreaterEqualsExpr) *ExecNode {
return t.transformComparison(expr, OpTypeGreaterEquals, expr.Lhs, expr.Rhs)
}
func (t *Transformer) transformLike(expr LikeExpr) *ExecNode {
return t.transformComparison(expr, OpTypeMatches, expr.Lhs, expr.Rhs)
}
func (t *Transformer) transformOne(expr Expression) *ExecNode {
switch expr := expr.(type) {
case mergeExpr:
return t.transformMerge(expr)
case AnyInExpr:
return t.transformAnyIn(expr)
case EveryInExpr:
return t.transformEveryIn(expr)
case AnyEveryInExpr:
return t.transformAnyEveryIn(expr)
case NotExpr:
return t.transformNot(expr)
case OrExpr:
return t.transformOr(expr)
case AndExpr:
return t.transformAnd(expr)
case ExistsExpr:
return t.transformExists(expr)
case NotExistsExpr:
return t.transformNotExists(expr)
case EqualsExpr:
return t.transformEquals(expr)
case NotEqualsExpr:
return t.transformNotEquals(expr)
case LessThanExpr:
return t.transformLessThan(expr)
case LessEqualsExpr:
return t.transformLessEquals(expr)
case GreaterThanExpr:
return t.transformGreaterThan(expr)
case GreaterEqualsExpr:
return t.transformGreaterEquals(expr)
case LikeExpr:
return t.transformLike(expr)
}
return nil
}
var AlwaysTrueIdent = -1
var AlwaysFalseIdent = -2
func (t *Transformer) Transform(exprs []Expression) *MatchDef {
t.RootExec = &ExecNode{}
t.ContextStack = nil
t.BucketIdx = 1
t.ActiveBucketIdx = 0
t.RootTree = binTree{[]binTreeNode{
{
NodeType: nodeTypeLeaf,
},
}}
// This does two things, it 'predefines' true and false values
// within it, and then addition provides an index to which generated
// expression contains the bucket index we need for that expression.
exprBucketIDs := make([]int, len(exprs))
var genExprs []Expression
for i, expr := range exprs {
switch expr.(type) {
case TrueExpr:
exprBucketIDs[i] = AlwaysTrueIdent
continue
case FalseExpr:
exprBucketIDs[i] = AlwaysFalseIdent
continue
}
genExprs = append(genExprs, expr)
exprBucketIDs[i] = len(genExprs) - 1
}
if len(genExprs) > 0 {
mergeExpr := mergeExpr{
exprs: genExprs,
bucketIDs: make([]BucketID, len(exprs)),
}
t.transformOne(mergeExpr)
for i, index := range exprBucketIDs {
if index >= 0 {
exprBucketIDs[i] = int(mergeExpr.bucketIDs[index])
}
}
} else {
t.RootExec = nil
t.RootTree = binTree{}
t.BucketIdx = 0
t.SlotIdx = 0
}
if t.RootExec != nil {
err := t.RootTree.Validate()
if err != nil {
panic(err)
}
if t.RootTree.NumNodes() != int(t.BucketIdx) {
panic("bucket count did not match tree size")
}
}
return &MatchDef{
ParseNode: t.RootExec,
MatchTree: t.RootTree,
MatchBuckets: exprBucketIDs,
NumBuckets: int(t.BucketIdx),
NumSlots: int(t.SlotIdx),
}
}