-
Notifications
You must be signed in to change notification settings - Fork 0
/
join.go
293 lines (254 loc) · 8.19 KB
/
join.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
// join implements a natural join expression in relational algebra
package rel
import (
"reflect"
"runtime"
"sync"
)
type joinExpr struct {
// source1 & source2 are the two relations going into the join operation
source1 Relation
source2 Relation
// zero is the type of the resulting relation
zero interface{}
// err is the first error encountered during construction or evaluation.
err error
}
// This implementation of join uses a nested loop join, which is definitely
// slower and in most cases less memory efficient than a merge join. However,
// I haven't implemented sorting yet so it was much easier to implement.
// TupleChan sends each tuple in the relation to a channel
func (r1 *joinExpr) TupleChan(t interface{}) chan<- struct{} {
cancel := make(chan struct{})
// reflect on the channel
chv := reflect.ValueOf(t)
err := EnsureChan(chv.Type(), r1.zero)
if err != nil {
r1.err = err
return cancel
}
if r1.err != nil {
chv.Close()
return cancel
}
mc := runtime.GOMAXPROCS(-1)
e3 := reflect.TypeOf(r1.zero)
// create indexes between the three headings
h1 := Heading(r1.source1)
h2 := Heading(r1.source2)
h3 := Heading(r1)
map12 := AttributeMap(h1, h2) // used to determine equality
map31 := AttributeMap(h3, h1) // used to construct returned values
map32 := AttributeMap(h3, h2) // used to construct returned values
// the types of the source tuples
e1 := reflect.TypeOf(r1.source1.Zero())
e2 := reflect.TypeOf(r1.source2.Zero())
// create channels over the body of the source relations
body1 := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, e1), 0)
bcancel1 := r1.source1.TupleChan(body1.Interface())
body2 := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, e2), 0)
bcancel2 := r1.source2.TupleChan(body2.Interface())
// Create the memory of previously sent tuples so that the joins can
// continue to compare against old values.
var (
mu sync.Mutex
mem1 []reflect.Value
mem2 []reflect.Value
)
// wg is used to signal when each of the worker goroutines finishes
// processing the join operation
var wg sync.WaitGroup
wg.Add(mc)
go func(res reflect.Value) {
wg.Wait()
// if we've been cancelled, send it up to the source
select {
case <-cancel:
close(bcancel1)
close(bcancel2)
default:
if err := r1.source1.Err(); err != nil {
r1.err = err
} else if err := r1.source2.Err(); err != nil {
r1.err = err
}
res.Close()
}
}(chv)
// create a go routine that generates the join for each of the input tuples
for i := 0; i < mc; i++ {
go func(b1, b2, res reflect.Value) {
// input channels
source1Sel := reflect.SelectCase{Dir: reflect.SelectRecv, Chan: b1}
source2Sel := reflect.SelectCase{Dir: reflect.SelectRecv, Chan: b2}
canSel := reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(cancel)}
neverRecv := reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(make(chan struct{}))}
inCases := []reflect.SelectCase{canSel, source1Sel, source2Sel}
// output channels
resSel := reflect.SelectCase{Dir: reflect.SelectSend, Chan: res}
mtups := []reflect.Value{}
openSources := 2
for openSources > 0 {
chosen, rtup, ok := reflect.Select(inCases)
if chosen == 0 {
// cancel channel was closed
break
}
if chosen > 0 && !ok {
// one of the bodies completed
// TODO(jonlawlor): remove memory for the other body, because
// we won't have anything to compare it to from now on.
inCases[chosen] = neverRecv
openSources--
continue
}
// If we've gotten this far, then one of the bodies has
// produced a new tuple.
// lock both memories
mu.Lock()
// depending on which body sent a value, append the tuple to
// that memory
if chosen == 1 {
mem1 = append(mem1, rtup)
mtups = mem2[:]
} else {
mem2 = append(mem2, rtup)
mtups = mem1[:]
}
mu.Unlock()
// Send tuples that match previously retrieved tuples in
// the opposite relation.
if chosen == 1 {
for _, rtup2 := range mtups {
if PartialEquals(rtup, rtup2, map12) {
tup3 := reflect.Indirect(reflect.New(e3))
CombineTuples2(&tup3, rtup, map31)
CombineTuples2(&tup3, rtup2, map32)
resSel.Send = tup3
chosen, _, ok = reflect.Select([]reflect.SelectCase{canSel, resSel})
if chosen == 0 {
openSources = 0
break
}
}
}
} else {
for _, rtup1 := range mtups {
if PartialEquals(rtup1, rtup, map12) {
tup3 := reflect.Indirect(reflect.New(e3))
CombineTuples2(&tup3, rtup1, map31)
CombineTuples2(&tup3, rtup, map32)
resSel.Send = tup3
chosen, _, ok = reflect.Select([]reflect.SelectCase{canSel, resSel})
if chosen == 0 {
openSources = 0
break
}
}
}
}
}
wg.Done()
}(body1, body2, chv)
}
return cancel
}
// Zero returns the zero value of the relation (a blank tuple)
func (r1 *joinExpr) Zero() interface{} {
return r1.zero
}
// CKeys is the set of candidate keys in the relation
func (r1 *joinExpr) CKeys() CandKeys {
// the candidate keys of a join are a join of the candidate keys as well
cKeys1 := r1.source1.CKeys()
cKeys2 := r1.source2.CKeys()
var cKeysRes [][]Attribute
// kind of merge join
for _, ck1 := range cKeys1 {
for _, ck2 := range cKeys2 {
ck := make([]Attribute, len(ck1))
copy(ck, ck1)
Loop:
for j := range ck2 {
for i := range ck {
if ck2[j] == ck[i] {
continue Loop
}
}
ck = append(ck, ck2[j])
}
cKeysRes = append(cKeysRes, ck)
}
}
OrderCandidateKeys(cKeysRes)
return cKeysRes
}
// GoString returns a text representation of the Relation
func (r1 *joinExpr) GoString() string {
return r1.source1.GoString() + ".Join(" + r1.source2.GoString() + ")"
}
// String returns a text representation of the Relation
func (r1 *joinExpr) String() string {
return r1.source1.String() + " ⋈ " + r1.source2.String()
}
// Project creates a new relation with less than or equal degree
// t2 has to be a new type which is a subdomain of r.
func (r1 *joinExpr) Project(z2 interface{}) Relation {
// TODO(jonlawlor): this can be sped up if we compare the candidate keys
// used in the relation to the new domain, along with the source relations
// domains.
return NewProject(r1, z2)
}
// Restrict creates a new relation with less than or equal cardinality
// p has to be a func(tup T) bool where tup is a subdomain of the input r.
// This can be rewritten if the predicate is a subdomain of either source
// relation.
func (r1 *joinExpr) Restrict(p Predicate) Relation {
// decompose compound predicates
if andPred, ok := p.(AndPred); ok {
// this covers some theta joins
return r1.Restrict(andPred.P1).Restrict(andPred.P2)
}
dom := p.Domain()
h1 := Heading(r1.source1)
h2 := Heading(r1.source2)
if IsSubDomain(dom, h1) {
if IsSubDomain(dom, h2) {
return r1.source1.Restrict(p).Join(r1.source2.Restrict(p), r1.zero)
}
return r1.source1.Restrict(p).Join(r1.source2, r1.zero)
}
if IsSubDomain(dom, h2) {
return r1.source1.Join(r1.source2.Restrict(p), r1.zero)
}
return NewRestrict(r1, p)
}
// Rename creates a new relation with new column names
// z2 has to be a struct with the same number of fields as the input relation
func (r1 *joinExpr) Rename(z2 interface{}) Relation {
return NewRename(r1, z2)
}
// Union creates a new relation by unioning the bodies of both inputs
func (r1 *joinExpr) Union(r2 Relation) Relation {
return NewUnion(r1, r2)
}
// Diff creates a new relation by set minusing the two inputs
func (r1 *joinExpr) Diff(r2 Relation) Relation {
return NewDiff(r1, r2)
}
// Join creates a new relation by performing a natural join on the inputs
func (r1 *joinExpr) Join(r2 Relation, zero interface{}) Relation {
return NewJoin(r1, r2, zero)
}
// GroupBy creates a new relation by grouping and applying a user defined func
func (r1 *joinExpr) GroupBy(t2, gfcn interface{}) Relation {
return NewGroupBy(r1, t2, gfcn)
}
// Map creates a new relation by applying a function to tuples in the source
func (r1 *joinExpr) Map(mfcn interface{}, ckeystr [][]string) Relation {
return NewMap(r1, mfcn, ckeystr)
}
// Err returns an error encountered during construction or computation
func (r1 *joinExpr) Err() error {
return r1.err
}