-
Notifications
You must be signed in to change notification settings - Fork 59
/
tc_new_executor.rb
400 lines (364 loc) · 8.36 KB
/
tc_new_executor.rb
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
require './test_common'
class PushTests < Minitest::Test
class SimplePush
include Bud
state do
table :r1
table :r2
end
bloom do
r2 <= r1
end
end
def test_simple
p = SimplePush.new
p.r1 <+ [[:a,1]]
p.tick
assert_equal([[:a,1]], p.r2.to_a)
p.tick
assert_equal([[:a,1]], p.r2.to_a)
end
class PushThru
include Bud
state do
table :r1
table :r2
scratch :sc
end
bloom do
r1 <= sc; r2 <= r1
end
end
def test_push_thru
p = PushThru.new
p.tick
assert_equal([], p.r2.to_a)
p.sc <+ [[:a,1]]
p.tick
assert_equal([[:a,1]], p.r1.to_a)
assert_equal([[:a,1]], p.r2.to_a)
end
class PushJoin
include Bud
state do
table :r1
table :r2
table :t1
end
bloom do
t1 <= (r1*r2).pairs(:key=>:key){|x,y| [x[0], x[1]+y[1]]}
end
end
def test_push_join
p = PushJoin.new
p.tick
p.r1 <+ [[:a,1]]
p.r2 <+ [[:a,2]]
p.tick
assert_equal([[:a,3]], p.t1.to_a)
end
class PushTwoJoins
include Bud
state do
table :r1
table :r2
table :r3
table :t1
end
bloom do
t1 <= (r1*r2*r3).pairs(r1.key=>r2.key, r2.key=>r3.key){|x,y,z| [x.key, x.val+y.val+z.val]}
end
end
def test_two_joins
p = PushTwoJoins.new
p.r1 <+ [[:a,1]]
p.r2 <+ [[:a,2]]
p.r3 <+ [[:a,3]]
p.tick
assert_equal([[:a,6]], p.t1.to_a)
end
class PushCartesian
include Bud
state do
table :r1
table :r2
table :t1
end
bloom do
t1 <= (r1*r2).pairs{|x,y| [x.key, y.key]}
end
end
def test_cartesian
p = PushCartesian.new
p.tick
p.r1 <+ [['a',1],['b',3]]
p.r2 <+ [['a',2]]
p.tick
p.tick
assert_equal([['a','a'],['b','a']], p.t1.to_a.sort)
end
class PushRecursion
include Bud
state do
table :link, [:src, :dest]
table :path, [:src, :dest]
end
bloom do
path <= link
path <= (link*path).pairs(:dest=>:src){|l,p| [l.src, p.dest]}
end
end
def test_recursion
p = PushRecursion.new
p.tick
p.link <+ [[1,2],[2,3],[3,4],[6,7],[2,7]]
p.tick
assert_equal([[1,2],[1,3],[1,4],[1,7],[2,3],[2,4],[2,7],[3,4],[6,7]], p.path.to_a.sort)
end
class PushGBTest
include Bud
state do
table :groupin, [:key,:val]
table :result
end
bloom do
result <= groupin.group([:key], sum(:val))
end
end
def test_gb
p = PushGBTest.new
p.tick
p.groupin <+ [[1,1],[1,2],[2,3],[2,4]]
p.tick
assert_equal([[1,3],[2,7]], p.result.to_a.sort)
end
class PushArgAggTest
include Bud
state do
scratch :r1, [:key, :val]
table :result, [:key,:val]
end
bloom do
result <= r1.argagg(:min, [:val], :key)
end
end
def test_argagg
p = PushArgAggTest.new
p.r1 <+ [[1,'a'],[1,'b'],[2,'b'],[2,'a']]
p.tick
assert_equal([[1,'a'],[1,'b']], p.result.to_a.sort)
end
class PushArgAggTestNoGroup
include Bud
state do
table :r1, [:key, :val]
table :result, [:key,:val]
end
bloom do
result <= r1.argagg(:min, [], :key)
end
end
def test_argagg_nogroup
p = PushArgAggTestNoGroup.new
p.tick
p.r1 <+ [[1,'a'],[1,'b'],[2,'b'],[2,'a']]
p.tick
assert([[1,'a'],[1,'b']].sort == p.result.to_a.sort)
end
class PushInspected
include Bud
state do
table :r1
table :result, [:str]
end
bloom do
result <= r1.inspected
end
end
def test_inspected
p = PushInspected.new
p.tick
p.r1 <+ [[1,1],[2,2]]
p.tick
assert_equal([[ "[1, 1]" ],[ "[2, 2]" ]], p.result.to_a.sort)
end
class PushStrata
include Bud
state do
table :link, [:src, :dest]
table :path, [:src, :dest]
table :win
table :result
end
bloom do
path <= link
path <= (link*path).pairs(:dest=>:src){|l,p| [l.src, p.dest]}
result <= win {|w| w unless path.include? [3,7]}
end
end
def test_strata
p = PushStrata.new
p.tick
p.link <+ [[1,2],[2,3],[3,4],[6,7],[2,7]]
p.win <+ [[1,1]]
p.tick
assert_equal([[1,2],[1,3],[1,4],[1,7],[2,3],[2,4],[2,7],[3,4],[6,7]], p.path.to_a.sort)
assert_equal([[1,1]], p.result.to_a)
end
class BudtimeRecompute
include Bud
state do
table :t1, [:x]
table :t2, [:tstamp, :x]
end
bloom do
t2 <= t1 {|t| [budtime, t.x]}
end
end
def test_budtime
b = BudtimeRecompute.new
b.t1 <+ [["foo"]]
b.tick
assert_equal(1, b.t2.to_a.length)
b.tick
assert_equal(2, b.t2.to_a.length)
b.t1 <+ [["bar"]]
b.tick
assert_equal(4, b.t2.to_a.length)
end
class DeleteRescan
include Bud
state do
table :src, [:str]
table :sink, src.schema
scratch :dummy, sink.schema
scratch :event_src, src.schema
end
bloom do
sink <= src
sink <= event_src
dummy <= sink
end
end
def test_delete_rescan
b = DeleteRescan.new
b.src <+ [["v1"], ["v2"]]
b.event_src <+ [["v3"]]
b.tick
assert_equal([["v1"], ["v2"], ["v3"]], b.sink.to_a.sort)
assert_equal([["v1"], ["v2"], ["v3"]], b.dummy.to_a.sort)
b.sink <- [["v1"], ["v3"]]
b.tick
assert_equal([["v1"], ["v2"]], b.sink.to_a.sort)
assert_equal([["v1"], ["v2"]], b.dummy.to_a.sort)
b.src <- [["v1"]]
b.tick
assert_equal([["v1"], ["v2"]], b.sink.to_a.sort)
assert_equal([["v1"], ["v2"]], b.dummy.to_a.sort)
b.sink <- [["v1"], ["v2"]]
b.tick
assert_equal([["v2"]], b.sink.to_a.sort)
assert_equal([["v2"]], b.dummy.to_a.sort)
end
class DeleteRescanNM
include Bud
state do
table :src, [:str]
table :src_del, [:str]
table :xyz, [:str]
table :abc, [:str]
table :sink, src.schema
scratch :dummy, sink.schema
end
bloom do
src_del <= xyz.notin(abc)
sink <= src.notin(src_del, :str => :str)
dummy <= sink
end
end
def test_delete_rescan_nm
b = DeleteRescanNM.new
b.src <+ [["v1"], ["v2"], ["v4"]]
b.src_del <+ [["v1"], ["v3"]]
b.tick
assert_equal([["v2"], ["v4"]], b.sink.to_a.sort)
assert_equal([["v2"], ["v4"]], b.dummy.to_a.sort)
b.sink <- [["v2"]]
b.tick
assert_equal([["v2"], ["v4"]], b.sink.to_a.sort)
assert_equal([["v2"], ["v4"]], b.dummy.to_a.sort)
b.src_del <+ [["v2"]]
b.tick
assert_equal([["v2"], ["v4"]], b.sink.to_a.sort)
assert_equal([["v2"], ["v4"]], b.dummy.to_a.sort)
b.sink <- [["v2"]]
b.tick
assert_equal([["v4"]], b.sink.to_a.sort)
assert_equal([["v4"]], b.dummy.to_a.sort)
end
# Identical to DeleteRescan, except that "sink" does not appear on the RHS of
# any rules. Hence it doesn't get a scanner, so the previous invalidation
# coding didn't operate correctly.
class DeleteRescanOrphan
include Bud
state do
table :src, [:str]
table :sink, src.schema
end
bloom do
sink <= src
end
end
def test_delete_rescan_orphan
b = DeleteRescanOrphan.new
b.src <+ [["v1"], ["v2"]]
b.tick
assert_equal([["v1"], ["v2"]], b.sink.to_a.sort)
b.sink <- [["v1"]]
b.tick
assert_equal([["v1"], ["v2"]], b.sink.to_a.sort)
b.src <- [["v1"]]
b.tick
assert_equal([["v1"], ["v2"]], b.sink.to_a.sort)
b.sink <- [["v1"], ["v2"]]
b.tick
assert_equal([["v2"]], b.sink.to_a.sort)
end
class DeleteRescanPending
include Bud
state do
table :src, [:str]
table :sink, src.schema
end
bloom do
sink <+ src
end
end
def test_delete_rescan_pending
b = DeleteRescanPending.new
# Check that the <+ rule is not regarded as non-monotonic. We should
# probably move this into a separate test.
deps = b.t_depends.select {|d| d.lhs == "sink" and d.body == "src"}
assert_equal(1, deps.size)
assert_equal(false, deps.first.nm)
b.src <+ [["v1"], ["v2"]]
b.tick
assert_equal([], b.sink.to_a)
b.tick
assert_equal([["v1"], ["v2"]], b.sink.to_a.sort)
b.sink <- [["v1"]]
b.tick
assert_equal([["v1"], ["v2"]], b.sink.to_a.sort)
b.tick
assert_equal([["v1"], ["v2"]], b.sink.to_a.sort)
b.src <- [["v1"]]
b.sink <- [["v1"]]
b.tick
assert_equal([["v1"], ["v2"]], b.sink.to_a.sort)
b.tick
assert_equal([["v1"], ["v2"]], b.sink.to_a.sort)
b.sink <- [["v1"]]
b.tick
assert_equal([["v2"]], b.sink.to_a.sort)
end
end