-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_test.go
433 lines (401 loc) · 10.5 KB
/
project_test.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
package rel
import (
"fmt"
"testing"
)
// tests for projection
func TestProject(t *testing.T) {
// TODO(jonlawlor): replace with table driven test?
type r1tup struct {
PNO int
SNO int
Qty int
}
r1 := orders().Project(r1tup{})
if r1.GoString() != orders().GoString() {
t.Errorf("orders.Project(PNO, SNO, Qty) = \"%s\", want \"%s\"", r1.GoString(), orders().GoString())
}
type r2tup struct {
PNO int
SNO int
}
r2 := orders().Project(r2tup{})
r2GoString := `rel.New([]struct {
PNO int
SNO int
}{
{1, 1, },
{1, 2, },
{1, 3, },
{1, 4, },
{1, 5, },
{1, 6, },
{2, 1, },
{2, 2, },
{3, 2, },
{4, 2, },
{4, 4, },
{4, 5, },
})`
if GoString(r2) != r2GoString {
t.Errorf("orders.Project(PNO, SNO) = \"%s\", want \"%s\"", GoString(r2), r2GoString)
}
type r3tup struct {
PNO int
Qty int
}
r3 := orders().Project(r3tup{})
if Deg(r3) != 2 || Card(r3) != 10 {
t.Errorf("orders.Project(PNO, Qty) has Deg %d, Card %d, want Deg %d, Card %d", Deg(r3), Card(r3), 2, 10)
}
// test the degrees, cardinality, and string representation
type pTup struct {
PNO int
PName string
Weight float64
City string
}
rel := parts().Project(pTup{})
type distinctTup struct {
PNO int
PName string
}
type nonDistinctTup struct {
PName string
City string
}
type titleCaseTup struct {
Pno int
PName string
Weight float64
City string
}
type joinTup struct {
PNO int
PName string
Weight float64
City string
SNO int
}
type groupByTup struct {
City string
Weight float64
}
type valTup struct {
Weight float64
}
groupFcn := func(val <-chan valTup) valTup {
res := valTup{}
for vi := range val {
res.Weight += vi.Weight
}
return res
}
type mapRes struct {
PNO int
PName string
Weight2 float64
}
mapFcn := func(tup1 pTup) mapRes {
return mapRes{tup1.PNO, tup1.PName, tup1.Weight * 2}
}
mapKeys := [][]string{
[]string{"PNO"},
}
var relTest = []struct {
rel Relation
expectString string
expectDeg int
expectCard int
}{
{rel, "π{PNO, PName, Weight, City}(Relation(PNO, PName, Color, Weight, City))", 4, 6},
{rel.Restrict(Attribute("PNO").EQ(1)), "π{PNO, PName, Weight, City}(σ{PNO == 1}(Relation(PNO, PName, Color, Weight, City)))", 4, 1},
{rel.Project(distinctTup{}), "π{PNO, PName}(Relation(PNO, PName, Color, Weight, City))", 2, 6},
{rel.Project(nonDistinctTup{}), "π{PName, City}(Relation(PNO, PName, Color, Weight, City))", 2, 6},
{rel.Rename(titleCaseTup{}), "ρ{Pno, PName, Weight, City}/{PNO, PName, Weight, City}(π{PNO, PName, Weight, City}(Relation(PNO, PName, Color, Weight, City)))", 4, 6},
{rel.Diff(rel.Restrict(Attribute("Weight").LT(15.0))), "π{PNO, PName, Weight, City}(Relation(PNO, PName, Color, Weight, City)) − π{PNO, PName, Weight, City}(σ{Weight < 15}(Relation(PNO, PName, Color, Weight, City)))", 4, 3},
{rel.Union(rel.Restrict(Attribute("Weight").LE(12.0))), "π{PNO, PName, Weight, City}(Relation(PNO, PName, Color, Weight, City)) ∪ π{PNO, PName, Weight, City}(σ{Weight <= 12}(Relation(PNO, PName, Color, Weight, City)))", 4, 6},
{rel.Join(suppliers(), joinTup{}), "π{PNO, PName, Weight, City}(Relation(PNO, PName, Color, Weight, City)) ⋈ Relation(SNO, SName, Status, City)", 5, 10},
{rel.GroupBy(groupByTup{}, groupFcn), "π{PNO, PName, Weight, City}(Relation(PNO, PName, Color, Weight, City)).GroupBy({City, Weight}->{Weight})", 2, 3},
{rel.Map(mapFcn, mapKeys), "π{PNO, PName, Weight, City}(Relation(PNO, PName, Color, Weight, City)).Map({PNO, PName, Weight, City}->{PNO, PName, Weight2})", 3, 6},
{rel.Map(mapFcn, [][]string{}), "π{PNO, PName, Weight, City}(Relation(PNO, PName, Color, Weight, City)).Map({PNO, PName, Weight, City}->{PNO, PName, Weight2})", 3, 6},
}
for i, tt := range relTest {
if err := tt.rel.Err(); err != nil {
t.Errorf("%d has Err() => %s", i, err.Error())
continue
}
if str := tt.rel.String(); str != tt.expectString {
t.Errorf("%d has String() => %v, want %v", i, str, tt.expectString)
}
if deg := Deg(tt.rel); deg != tt.expectDeg {
t.Errorf("%d %s has Deg() => %v, want %v", i, tt.expectString, deg, tt.expectDeg)
}
if card := Card(tt.rel); card != tt.expectCard {
t.Errorf("%d %s has Card() => %v, want %v", i, tt.expectString, card, tt.expectCard)
}
}
// test cancellation
res := make(chan pTup)
cancel := rel.TupleChan(res)
close(cancel)
select {
case <-res:
t.Errorf("cancel did not end tuple generation")
default:
// passed test
}
// test errors
err := fmt.Errorf("testing error")
rel1 := parts().Project(pTup{}).(*projectExpr)
rel1.err = err
rel2 := parts().Project(pTup{}).(*projectExpr)
rel2.err = err
res = make(chan pTup)
_ = rel1.TupleChan(res)
if _, ok := <-res; ok {
t.Errorf("did not short circuit TupleChan")
}
errTest := []Relation{
rel1.Rename(titleCaseTup{}),
rel1.Union(rel2),
rel.Union(rel2),
rel1.Diff(rel2),
rel.Diff(rel2),
rel1.Join(rel2, orderTup{}),
rel.Join(rel2, orderTup{}),
rel1.GroupBy(groupByTup{}, groupFcn),
rel1.Map(mapFcn, mapKeys),
}
for i, errRel := range errTest {
if errRel.Err() != err {
t.Errorf("%d did not short circuit error", i)
}
}
errRel := (&errorRel{distinctTup{}, 1, nil}).Project(distinctTup{})
if c := Card(errRel); c != 1 {
t.Errorf("errored relation had Card() => %v, wanted %v", c, 1)
}
}
func BenchmarkProjectTinyIdent(b *testing.B) {
// test the time it takes to do an identity projection for a
// Tiny relation.
exRel := New(exampleRelSlice2(10), [][]string{[]string{"Foo"}})
b.ResetTimer()
r1 := exRel.Project(exTup2{})
for i := 0; i < b.N; i++ {
t := make(chan exTup2)
r1.TupleChan(t)
for _ = range t {
// do nothing
}
}
}
// this doesn't produce much benefit because Project has a short circuit
// for identity Projection. However, that might get removed once a query
// rewriter is implemented.
func BenchmarkProjectTinyIdentNative(b *testing.B) {
// test the time it takes to do an identity projection for a
// Tiny relation.
exRel := exampleRelSlice2(10)
NativeTups := func(t chan exTup2) {
go func() {
for _, tup := range exRel {
t <- tup
}
close(t)
}()
return
}
NativeProject := func(src chan exTup2, res chan exTup2) {
go func() {
for tup := range src {
res <- exTup2{tup.Foo, tup.Bar}
}
close(res)
}()
return
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
src := make(chan exTup2)
NativeTups(src)
res := make(chan exTup2)
NativeProject(src, res)
for _ = range res {
}
}
}
func BenchmarkProjectTinyDistinct(b *testing.B) {
// test the time it takes to do an projection for a
// Tiny relation where we don't need to call distinct
// on the result
exRel := New(exampleRelSlice2(10), [][]string{[]string{"Foo"}})
type fooOnly struct {
Foo int
}
b.ResetTimer()
r1 := exRel.Project(fooOnly{})
for i := 0; i < b.N; i++ {
t := make(chan fooOnly)
r1.TupleChan(t)
for _ = range t {
// do nothing
}
}
}
// this is more indicative of typical project performance
// initial tests show that project incurs a 50% - 100% overhead per attribute
func BenchmarkProjectTinyDistinctNative(b *testing.B) {
// test the time it takes to do an identity projection for a
// Tiny relation.
exRel := exampleRelSlice2(10)
type fooOnly struct {
Foo int
}
NativeTups := func(t chan exTup2) {
go func() {
for _, tup := range exRel {
t <- tup
}
close(t)
}()
return
}
NativeProject := func(src chan exTup2, res chan fooOnly) {
go func() {
for tup := range src {
res <- fooOnly{tup.Foo}
}
close(res)
}()
return
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
src := make(chan exTup2)
NativeTups(src)
res := make(chan fooOnly)
NativeProject(src, res)
for _ = range res {
}
}
}
func BenchmarkProjectTinyNonDistinct(b *testing.B) {
// test the time it takes to do an projection for a
// Tiny relation where we need to call distinct
// on the result
exRel := New(exampleRelSlice2(10), [][]string{[]string{"Foo"}})
type barOnly struct {
Bar string
}
b.ResetTimer()
r1 := exRel.Project(barOnly{})
for i := 0; i < b.N; i++ {
t := make(chan barOnly)
r1.TupleChan(t)
for _ = range t {
// do nothing
}
}
}
func BenchmarkProjectSmallIdent(b *testing.B) {
// test the time it takes to do an identity projection for a
// small relation.
exRel := New(exampleRelSlice2(1000), [][]string{[]string{"Foo"}})
b.ResetTimer()
r1 := exRel.Project(exTup2{})
for i := 0; i < b.N; i++ {
t := make(chan exTup2)
r1.TupleChan(t)
for _ = range t {
// do nothing
}
}
}
func BenchmarkProjectSmallDistinct(b *testing.B) {
// test the time it takes to do an projection for a
// small relation where we don't need to call distinct
// on the result
exRel := New(exampleRelSlice2(1000), [][]string{[]string{"Foo"}})
type fooOnly struct {
Foo int
}
b.ResetTimer()
r1 := exRel.Project(fooOnly{})
for i := 0; i < b.N; i++ {
t := make(chan fooOnly)
r1.TupleChan(t)
for _ = range t {
// do nothing
}
}
}
func BenchmarkProjectSmallNonDistinct(b *testing.B) {
// test the time it takes to do an projection for a
// small relation where we need to call distinct
// on the result
exRel := New(exampleRelSlice2(1000), [][]string{[]string{"Foo"}})
type barOnly struct {
Bar string
}
b.ResetTimer()
r1 := exRel.Project(barOnly{})
for i := 0; i < b.N; i++ {
t := make(chan barOnly)
r1.TupleChan(t)
for _ = range t {
// do nothing
}
}
}
func BenchmarkProjectMediumIdent(b *testing.B) {
// test the time it takes to do an identity projection for a
// Medium relation.
exRel := New(exampleRelSlice2(100000), [][]string{[]string{"Foo"}})
b.ResetTimer()
r1 := exRel.Project(exTup2{})
for i := 0; i < b.N; i++ {
t := make(chan exTup2)
r1.TupleChan(t)
for _ = range t {
// do nothing
}
}
}
func BenchmarkProjectMediumDistinct(b *testing.B) {
// test the time it takes to do an projection for a
// Medium relation where we don't need to call distinct
// on the result
exRel := New(exampleRelSlice2(100000), [][]string{[]string{"Foo"}})
type fooOnly struct {
Foo int
}
b.ResetTimer()
r1 := exRel.Project(fooOnly{})
for i := 0; i < b.N; i++ {
t := make(chan fooOnly)
r1.TupleChan(t)
for _ = range t {
// do nothing
}
}
}
func BenchmarkProjectMediumNonDistinct(b *testing.B) {
// test the time it takes to do an projection for a
// Medium relation where we need to call distinct
// on the result
exRel := New(exampleRelSlice2(100000), [][]string{[]string{"Foo"}})
type barOnly struct {
Bar string
}
b.ResetTimer()
r1 := exRel.Project(barOnly{})
for i := 0; i < b.N; i++ {
t := make(chan barOnly)
r1.TupleChan(t)
for _ = range t {
// do nothing
}
}
}