-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestrict_test.go
275 lines (252 loc) · 6.57 KB
/
restrict_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
package rel
import (
"fmt"
"testing"
)
// tests for restrict op
func TestRestrict(t *testing.T) {
exRel := New(exampleRelSlice2(10), [][]string{[]string{"Foo"}})
var restrictTests = []struct {
in Relation
out int
}{
{exRel.Restrict(AdHoc{func(i struct{}) bool { return true }}), 10},
{exRel.Restrict(AdHoc{func(i struct{}) bool { return false }}), 0},
{exRel.Restrict(AdHoc{func(i struct{ Foo int }) bool { return i.Foo > 5 }}), 4},
}
for _, tt := range restrictTests {
c := Card(tt.in)
if c != tt.out {
t.Errorf("Card(%s) => %v, want %v", tt.in.GoString(), c, tt.out)
}
}
// test the degrees, cardinality, and string representation
rel := orders().Restrict(Attribute("Qty").GT(100))
type distinctTup struct {
PNO int
SNO int
}
type nonDistinctTup struct {
PNO int
Qty int
}
type titleCaseTup struct {
Pno int
Sno int
Qty int
}
type joinTup struct {
PNO int
SNO int
Qty int
SName string
Status int
City string
}
type groupByTup struct {
PNO int
Qty int
}
type valTup struct {
Qty int
}
groupFcn := func(val <-chan valTup) valTup {
res := valTup{}
for vi := range val {
res.Qty += vi.Qty
}
return res
}
type mapRes struct {
PNO int
SNO int
Qty1 int
Qty2 int
}
mapFcn := func(tup1 orderTup) mapRes {
return mapRes{tup1.PNO, tup1.SNO, tup1.Qty, tup1.Qty * 2}
}
mapKeys := [][]string{
[]string{"PNO", "SNO"},
}
var relTest = []struct {
rel Relation
expectString string
expectDeg int
expectCard int
}{
{rel, "σ{Qty > 100}(Relation(PNO, SNO, Qty))", 3, 10},
{rel.Restrict(Attribute("PNO").EQ(1).And(Attribute("Qty").GT(200))), "σ{Qty > 100}(σ{(PNO == 1) && (Qty > 200)}(Relation(PNO, SNO, Qty)))", 3, 2},
{rel.Project(distinctTup{}), "π{PNO, SNO}(σ{Qty > 100}(Relation(PNO, SNO, Qty)))", 2, 10},
{rel.Project(nonDistinctTup{}), "σ{Qty > 100}(π{PNO, Qty}(Relation(PNO, SNO, Qty)))", 2, 9},
{rel.Rename(titleCaseTup{}), "ρ{Pno, Sno, Qty}/{PNO, SNO, Qty}(σ{Qty > 100}(Relation(PNO, SNO, Qty)))", 3, 10},
{rel.Diff(orders()), "σ{Qty > 100}(Relation(PNO, SNO, Qty)) − Relation(PNO, SNO, Qty)", 3, 0},
{rel.Union(orders()), "σ{Qty > 100}(Relation(PNO, SNO, Qty)) ∪ Relation(PNO, SNO, Qty)", 3, 12},
{rel.Join(suppliers(), joinTup{}), "σ{Qty > 100}(Relation(PNO, SNO, Qty)) ⋈ Relation(SNO, SName, Status, City)", 6, 10},
{rel.GroupBy(groupByTup{}, groupFcn), "σ{Qty > 100}(Relation(PNO, SNO, Qty)).GroupBy({PNO, Qty}->{Qty})", 2, 4},
{rel.Map(mapFcn, mapKeys), "σ{Qty > 100}(Relation(PNO, SNO, Qty)).Map({PNO, SNO, Qty}->{PNO, SNO, Qty1, Qty2})", 4, 10},
{rel.Map(mapFcn, [][]string{}), "σ{Qty > 100}(Relation(PNO, SNO, Qty)).Map({PNO, SNO, Qty}->{PNO, SNO, Qty1, Qty2})", 4, 10},
}
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 orderTup)
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")
r1 := orders().Restrict(Attribute("Qty").GT(100)).(*restrictExpr)
r1.err = err
r2 := orders().Restrict(Attribute("Qty").GT(100)).(*restrictExpr)
r2.err = err
res = make(chan orderTup)
_ = r1.TupleChan(res)
if _, ok := <-res; ok {
t.Errorf("restrict did not short circuit TupleChan")
}
errTest := []Relation{
r1.Project(distinctTup{}),
r1.Rename(titleCaseTup{}),
r1.Union(r2),
rel.Union(r2),
r1.Diff(r2),
rel.Diff(r2),
r1.Join(r2, orderTup{}),
rel.Join(r2, orderTup{}),
r1.GroupBy(groupByTup{}, groupFcn),
r1.Map(mapFcn, mapKeys),
}
for i, errRel := range errTest {
if errRel.Err() != err {
t.Errorf("%d did not short circuit error", i)
}
}
}
func BenchmarkRestrictIdent(b *testing.B) {
// test the time it takes to pull all of the tuples after passing in an
// identity predicate (always true)
exRel := New(exampleRelSlice2(10), [][]string{[]string{"Foo"}})
pred := AdHoc{func(i struct{}) bool {
return true
}}
r1 := exRel.Restrict(pred)
b.ResetTimer()
for i := 0; i < b.N; i++ {
t := make(chan exTup2)
r1.TupleChan(t)
for _ = range t {
}
}
}
func BenchmarkRestrictZero(b *testing.B) {
// test the time it takes to pull all of the tuples after passing in an
// zero predicate (always false)
exRel := New(exampleRelSlice2(10), [][]string{[]string{"Foo"}})
pred := AdHoc{func(i struct{}) bool {
return false
}}
r1 := exRel.Restrict(pred)
b.ResetTimer()
for i := 0; i < b.N; i++ {
t := make(chan exTup2)
r1.TupleChan(t)
for _ = range t {
}
}
}
// These Native functions are useful to determine what kind of overhead
// reflection is incuring. My measurements show Ident is ~2.5 times slower
// and Zero is ~4 times slower than native.
func BenchmarkRestrictIdentNative(b *testing.B) {
// test the time it takes to pull all of the tuples after passing in an
// identity predicate (always true)
exRel := exampleRelSlice2(10)
Pred := func(exTup2) bool {
return true
}
NativeTups := func(t chan exTup2) {
go func() {
for _, tup := range exRel {
t <- tup
}
close(t)
}()
return
}
NativeRestrict := func(src chan exTup2, res chan exTup2) {
go func() {
for tup := range src {
if Pred(tup) {
res <- tup
}
}
close(res)
}()
return
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
src := make(chan exTup2)
NativeTups(src)
res := make(chan exTup2)
NativeRestrict(src, res)
for _ = range res {
}
}
}
func BenchmarkRestrictZeroNative(b *testing.B) {
exRel := exampleRelSlice2(10)
// test the time it takes to pull all of the tuples after passing in an
// identity predicate (always false)
Pred := func(exTup2) bool {
return false
}
NativeTups := func(t chan exTup2) {
go func() {
for _, tup := range exRel {
t <- tup
}
close(t)
}()
return
}
NativeRestrict := func(src chan exTup2, res chan exTup2) {
go func() {
for tup := range src {
if Pred(tup) {
res <- tup
}
}
close(res)
}()
return
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
src := make(chan exTup2)
NativeTups(src)
res := make(chan exTup2)
NativeRestrict(src, res)
for _ = range res {
}
}
}