-
Notifications
You must be signed in to change notification settings - Fork 3
/
relation.go
208 lines (190 loc) · 4.8 KB
/
relation.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
package rel
import (
"log"
"github.com/arr-ai/frozen"
)
type (
Tuple = frozen.Map[string, any]
Relation = frozen.Set[Tuple]
RelationBuilder = frozen.SetBuilder[Tuple]
)
func NewTuple(kvs ...frozen.KeyValue[string, any]) Tuple {
return frozen.NewMap(kvs...)
}
var trueSet = frozen.NewSet(NewTuple())
// New returns a new relation.
func New(header []string, tuples ...[]any) Relation {
r := Relation{}
for _, row := range tuples {
if len(row) != len(header) {
panic("header/row size mismatch")
}
t := NewTuple()
for i, h := range header {
t = t.With(h, row[i])
}
r = r.With(t)
}
return r
}
// Project returns a Set with the result of projecting each map.
func Project(s Relation, attrs ...string) Relation {
return frozen.SetMap(s, func(t Tuple) Tuple {
return t.Project(attrs...)
})
}
// Join returns all {x, y, z} such that s has {x, y} and t has {y, z}.
// x, y and z represent sets of keys:
//
// x: keys unique to maps in s
// y: keys common to maps in both
// z: keys unique to maps in t
//
// It is assumed that all maps in s have the same keys and likewise for t.
func Join(relations ...Relation) Relation {
s := relations[0]
for _, t := range relations[1:] {
s = join(s, t)
}
return s
}
func join(s, t Relation) Relation {
if s.IsEmpty() || t.IsEmpty() {
return s
}
if s.Equal(trueSet) {
return t
}
if t.Equal(trueSet) {
return s
}
sAttrs := s.Any().Keys()
tAttrs := t.Any().Keys()
commonAttrs := sAttrs.Intersection(tAttrs).Elements()
if len(commonAttrs) == 0 {
return CartesianProduct(s, t)
}
projectCommon := func(t Tuple) frozen.Map[string, any] {
return t.Project(commonAttrs...)
}
sGroup := frozen.SetGroupBy(s, projectCommon)
tGroup := frozen.SetGroupBy(t, projectCommon)
commonKeys := sGroup.Keys().Intersection(tGroup.Keys())
var rb RelationBuilder
for i := commonKeys.Range(); i.Next(); {
key := i.Value()
a, has := sGroup.Get(key)
if !has {
panic("wat?")
}
b, has := tGroup.Get(key)
if !has {
panic("wat?")
}
buildCartesianProduct(&rb, Tuple{}, a, b)
}
return rb.Finish()
}
func CartesianProduct(relations ...Relation) Relation {
var b RelationBuilder
buildCartesianProduct(&b, Tuple{}, relations...)
return b.Finish()
}
func buildCartesianProduct(b *RelationBuilder, t Tuple, relations ...Relation) {
if len(relations) > 0 {
for i := relations[0].Range(); i.Next(); {
buildCartesianProduct(b, t.Update(i.Value()), relations[1:]...)
}
} else {
b.Add(t)
}
}
// Nest returns a relation with some attributes nested as subrelations.
//
// Example:
//
// input:
// _c_ _a__
// |_1_|_10_|
// |_1_|_11_|
// |_2_|_13_|
// |_3_|_11_|
// |_4_|_14_|
// |_3_|_10_|
// |_4_|_13_|
//
// nest(input, {aa: {a}}):
// _c_ ___aa___
// | 1 | _a__ |
// | | |_10_| |
// |___|_|_11_|_|
// | 2 | _a__ |
// |___|_|_13_|_|
// | 3 | _a__ |
// | | |_10_| |
// |___|_|_11_|_|
// | 4 | _a__ |
// | | |_13_| |
// |___|_|_14_|_|
func Nest(s Relation, attrAttrs frozen.Map[string, frozen.Set[string]]) Relation {
log.Print("s = ", s)
// attrAttrs = {aa: {a}}
// {a}
keyAttrs := frozen.Intersection(attrAttrs.Values().Elements()...).Elements()
log.Print("keyAttrs = ", keyAttrs)
// {
// {c: 1}: {{a: 10, c: 1}, {a: 11, c: 1}},
// {c: 2}: {{a: 13, c: 2}},
// {c: 3}: {{a: 10, c: 3}, {a: 11, c: 3}},
// {c: 4}: {{a: 13, c: 4}, {a: 14, c: 4}},
// }
grouped := frozen.SetGroupBy(s, func(el Tuple) Tuple {
for _, attr := range keyAttrs {
el = el.Without(attr)
}
return el
})
log.Print("grouped = ", grouped)
// {
// {c: 1}: {c: 1, aa: {{a: 10}, {a: 11}}},
// {c: 2}: {c: 2, aa: {{a: 13}}},
// {c: 3}: {c: 3, aa: {{a: 10}, {a: 11}}},
// {c: 4}: {c: 4, aa: {{a: 13}, {a: 14}}},
// }
mapped := frozen.MapMap(grouped, func(key Tuple, group Relation) Tuple {
// {c: 1} => {aa: {{a: 10}, {a: 11}}}
a := frozen.MapMap(attrAttrs, func(_ string, attrs frozen.Set[string]) any {
return Project(group, attrs.Elements()...)
})
// {c: 1} => {c: 1, aa: {{a: 10}, {a: 11}}}
return a.Update(key)
})
log.Print("mapped = ", mapped)
// {
// {c: 1, aa: {{a: 10}, {a: 11}}},
// {c: 2, aa: {{a: 13}}},
// {c: 3, aa: {{a: 10}, {a: 11}}},
// {c: 4, aa: {{a: 13}, {a: 14}}},
// }
result := mapped.Values()
log.Print("result = ", result)
return result
}
// Unnest returns a relation with some subrelations unnested. This is the
// reverse of Nest.
func Unnest(s Relation, attr string) Relation {
var b RelationBuilder
for i := s.Range(); i.Next(); {
t := i.Value()
key := t.Without(attr)
nestedValues := frozen.MapMap(t.Project(attr), func(_ string, val any) Relation {
return val.(Relation)
}).Values()
all := nestedValues.With(frozen.NewSet(key))
joined := Join(all.Elements()...)
for j := joined.Range(); j.Next(); {
b.Add(j.Value())
}
}
return b.Finish()
}