-
Notifications
You must be signed in to change notification settings - Fork 15
/
ops_rel.go
274 lines (253 loc) · 6.34 KB
/
ops_rel.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
package rel
import (
"fmt"
"github.com/arr-ai/frozen"
"github.com/go-errors/errors"
)
// RelationAttrs returns the set of names for a relation type, or false if the
// set isn't a regular relation.
func RelationAttrs(a Set) (Names, bool) {
e := a.Enumerator()
if !e.MoveNext() {
return Names{}, true
}
names := e.Current().(Tuple).Names()
for e.MoveNext() {
if !names.Equal(e.Current().(Tuple).Names()) {
return Names{}, false
}
}
return names, true
}
// Nest groups the given attributes into nested relations.
func Nest(a Set, attrs Names, attr string) Set {
if !a.IsTrue() {
return a
}
names, ok := RelationAttrs(a)
if !ok {
panic("Tuple names mismatch in nest lhs")
}
if !attrs.IsSubsetOf(names) {
panic(fmt.Errorf("nest attrs (%v) not a subset of relation attrs (%v)", attrs, names))
}
key := names.Minus(attrs)
return Reduce(
a,
func(value Value) Value {
return value.(Tuple).Project(key)
},
func(key Value, tuples Set) Set {
nested := None
for e := tuples.Enumerator(); e.MoveNext(); {
nested = nested.With(e.Current().(Tuple).Project(attrs))
}
return NewSet(Merge(key.(Tuple), NewTuple(Attr{attr, nested})))
},
)
}
// Unnest unpacks the attributes of a nested relation into the outer relation.
func Unnest(a Set, attr string) Set {
key, ok := RelationAttrs(a)
if !ok {
panic("Tuple names mismatch in unnest lhs")
}
if !key.Has(attr) {
panic("Unnest attr not found in relation")
}
return Reduce(
a,
func(value Value) Value {
return value.(Tuple).Project(key)
},
func(key Value, _ Set) Set {
unnested := None
t := key.(Tuple)
s, _ := t.Get(attr)
t = t.Without(attr)
for e := s.(Set).Enumerator(); e.MoveNext(); {
unnested = unnested.With(Merge(t, e.Current().(Tuple)))
}
return unnested
},
)
}
// Reduce reduces a set using the given key and reducer functions.
func Reduce(
a Set,
getKey func(value Value) Value,
reduce func(key Value, tuples Set) Set,
) Set {
var buckets frozen.Map
for e := a.Enumerator(); e.MoveNext(); {
value := e.Current()
key := getKey(value)
slot, found := buckets.Get(key)
if !found {
slot = None
}
slot = slot.(Set).With(value)
buckets = buckets.With(key, slot)
}
result := None
for i := buckets.Range(); i.Next(); {
result = Union(result, reduce(i.Key().(Value), i.Value().(Set)))
}
return result
}
// Join returns the relation join of a and b.
// Defn: Join(a{x…,y…}, b{y…,z…}) = ∀{x…,y…,z…}: {x…,y…} ∈ a ∧ {y…,z…} ∈ b
// for mutually disjoint x…, y…, z…
func Join(a, b Set) Set {
aNames, ok := RelationAttrs(a)
if !ok {
panic("Tuple names mismatch in join lhs")
}
bNames, ok := RelationAttrs(b)
if !ok {
panic("Tuple names mismatch in join rhs")
}
if a.Count() > b.Count() {
a, b = b, a
aNames, bNames = bNames, aNames
}
common := aNames.Intersect(bNames)
return GenericJoin(
a, b,
func(value Value) Value {
return value.(Tuple).Project(common)
},
func(key Value, a, b Set) Set {
values := []Value{}
for i := a.Enumerator(); i.MoveNext(); {
for j := b.Enumerator(); j.MoveNext(); {
values = append(values, Merge(
i.Current().(Tuple),
j.Current().(Tuple),
))
}
}
return NewSet(values...)
},
)
}
// func Join(a, b Set) Set {
// aNames, ok := RelationAttrs(a)
// if !ok {
// panic("Tuple names mismatch in join lhs")
// }
// bNames, ok := RelationAttrs(b)
// if !ok {
// panic("Tuple names mismatch in join rhs")
// }
// if a.Count() > b.Count() {
// a, b = b, a
// aNames, bNames = bNames, aNames
// }
// common := aNames.Intersect(bNames)
// var buckets frozen.Map
// for e := a.Enumerator(); e.MoveNext(); {
// tuple := e.Current().(Tuple)
// key := tuple.Project(common)
// if bucket, found := buckets.Get(key); found {
// buckets, _ = buckets.Set(key, bucket.(Set).With(tuple))
// } else {
// buckets, _ = buckets.Set(key, NewSet(tuple))
// }
// }
// result := None
// for e := b.Enumerator(); e.MoveNext(); {
// tuple := e.Current().(Tuple)
// key := tuple.Project(common)
// if bucket, found := buckets.Get(key); found {
// for e := bucket.(Set).Enumerator(); e.MoveNext(); {
// if merged := Merge(tuple, e.Current().(Tuple)); merged != nil {
// result = result.With(merged)
// }
// }
// }
// }
// return result
// }
// GenericJoin joins two sets using a key and a joiner
func GenericJoin(
a, b Set,
getKey func(value Value) Value,
join func(key Value, a, b Set) Set,
) Set {
var mb frozen.MapBuilder
accumulate := func(s Set, slotKey Value) {
for e := s.Enumerator(); e.MoveNext(); {
value := e.Current()
key := getKey(value)
slots, found := mb.Get(key)
if !found {
slots = frozen.Map{}
}
// False denotes lhs accumulator
slot, found := slots.(frozen.Map).Get(slotKey)
if !found {
slot = None
}
slot = slot.(Set).With(value)
slots = slots.(frozen.Map).With(slotKey, slot)
mb.Put(key, slots)
}
}
aSlot := NewNumber(0)
bSlot := NewNumber(1)
accumulate(a, aSlot)
accumulate(b, bSlot)
result := None
for i := mb.Finish().Range(); i.Next(); {
k, v := i.Entry()
key := k.(Value)
slots := v.(frozen.Map)
aSet := None
if aItem, ok := slots.Get(aSlot); ok {
aSet = aItem.(Set)
}
bSet := None
if bItem, ok := slots.Get(bSlot); ok {
bSet = bItem.(Set)
}
result = Union(result, join(key, aSet, bSet))
}
return result
}
// Concatenate is equivalent to a <&> (b => . + {.@ + a count}). Naturally, this
// assumes that every element in b is a tuple with at least an '@' attribute,
// which is numeric.
//
// E.g., [1, 2] + [3] = [1, 2, 3]; "hell" + "o" = "hello"
func Concatenate(a, b Set) (Set, error) {
offset := a.Count()
values := make([]Value, 0, a.Count()+b.Count())
for e := a.Enumerator(); e.MoveNext(); {
values = append(values, e.Current())
}
for e := b.Enumerator(); e.MoveNext(); {
elt := e.Current()
if t, ok := elt.(Tuple); ok {
if pos, found := t.Get("@"); found {
if n, ok := pos.(Number); ok {
t = t.With("@", NewNumber(float64(offset)+n.Float64()))
values = append(values, t)
continue
}
}
}
return nil, errors.Errorf("Mismatched elt in set + set: %v", elt)
}
return NewSet(values...), nil
}
func NConcatenate(a Set, bs ...Set) (Set, error) {
for _, b := range bs {
var err error
a, err = Concatenate(a, b)
if err != nil {
return nil, err
}
}
return a, nil
}