-
Notifications
You must be signed in to change notification settings - Fork 153
/
map.go
247 lines (220 loc) · 6.15 KB
/
map.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
package universe
import (
"fmt"
"sort"
"github.com/influxdata/flux"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
"github.com/pkg/errors"
)
const MapKind = "map"
type MapOpSpec struct {
Fn *semantic.FunctionExpression `json:"fn"`
MergeKey bool `json:"mergeKey"`
}
func init() {
mapSignature := flux.FunctionSignature(
map[string]semantic.PolyType{
"fn": semantic.NewFunctionPolyType(semantic.FunctionPolySignature{
Parameters: map[string]semantic.PolyType{
"r": semantic.Tvar(1),
},
Required: semantic.LabelSet{"r"},
Return: semantic.Tvar(2),
}),
"mergeKey": semantic.Bool,
},
[]string{"fn"},
)
flux.RegisterPackageValue("universe", MapKind, flux.FunctionValue(MapKind, createMapOpSpec, mapSignature))
flux.RegisterOpSpec(MapKind, newMapOp)
plan.RegisterProcedureSpec(MapKind, newMapProcedure, MapKind)
execute.RegisterTransformation(MapKind, createMapTransformation)
}
func createMapOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
spec := new(MapOpSpec)
if f, err := args.GetRequiredFunction("fn"); err != nil {
return nil, err
} else {
fn, err := interpreter.ResolveFunction(f)
if err != nil {
return nil, err
}
spec.Fn = fn
}
if m, ok, err := args.GetBool("mergeKey"); err != nil {
return nil, err
} else if ok {
spec.MergeKey = m
} else {
spec.MergeKey = true
}
return spec, nil
}
func newMapOp() flux.OperationSpec {
return new(MapOpSpec)
}
func (s *MapOpSpec) Kind() flux.OperationKind {
return MapKind
}
type MapProcedureSpec struct {
plan.DefaultCost
Fn *semantic.FunctionExpression
MergeKey bool
}
func newMapProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*MapOpSpec)
if !ok {
return nil, fmt.Errorf("invalid spec type %T", qs)
}
return &MapProcedureSpec{
Fn: spec.Fn,
MergeKey: spec.MergeKey,
}, nil
}
func (s *MapProcedureSpec) Kind() plan.ProcedureKind {
return MapKind
}
func (s *MapProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(MapProcedureSpec)
*ns = *s
ns.Fn = s.Fn.Copy().(*semantic.FunctionExpression)
return ns
}
func createMapTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
s, ok := spec.(*MapProcedureSpec)
if !ok {
return nil, nil, fmt.Errorf("invalid spec type %T", spec)
}
cache := execute.NewTableBuilderCache(a.Allocator())
d := execute.NewDataset(id, mode, cache)
t, err := NewMapTransformation(d, cache, s)
if err != nil {
return nil, nil, err
}
return t, d, nil
}
type mapTransformation struct {
d execute.Dataset
cache execute.TableBuilderCache
fn *execute.RowMapFn
mergeKey bool
}
func NewMapTransformation(d execute.Dataset, cache execute.TableBuilderCache, spec *MapProcedureSpec) (*mapTransformation, error) {
fn, err := execute.NewRowMapFn(spec.Fn)
if err != nil {
return nil, err
}
return &mapTransformation{
d: d,
cache: cache,
fn: fn,
mergeKey: spec.MergeKey,
}, nil
}
func (t *mapTransformation) RetractTable(id execute.DatasetID, key flux.GroupKey) error {
return t.d.RetractTable(key)
}
func (t *mapTransformation) Process(id execute.DatasetID, tbl flux.Table) error {
// Prepare the functions for the column types.
cols := tbl.Cols()
err := t.fn.Prepare(cols)
if err != nil {
// TODO(nathanielc): Should we not fail the query for failed compilation?
return err
}
// Determine keys return from function
var properties map[string]semantic.Type
var keys []string
var on map[string]bool
return tbl.Do(func(cr flux.ColReader) error {
l := cr.Len()
for i := 0; i < l; i++ {
m, err := t.fn.Eval(i, cr)
if err != nil {
return errors.Wrap(err, "failed to evaluate map function")
}
if i == 0 {
properties = m.Type().Properties()
keys = make([]string, 0, len(properties))
for k := range properties {
keys = append(keys, k)
}
sort.Strings(keys)
// Determine on which cols to group
on = make(map[string]bool, len(tbl.Key().Cols()))
for _, c := range tbl.Key().Cols() {
on[c.Label] = t.mergeKey || execute.ContainsStr(keys, c.Label)
}
}
key := groupKeyForObject(i, cr, m, on)
builder, created := t.cache.TableBuilder(key)
if created {
if t.mergeKey {
if err := execute.AddTableKeyCols(tbl.Key(), builder); err != nil {
return err
}
}
// Add columns from function in sorted order
for _, k := range keys {
if t.mergeKey && tbl.Key().HasCol(k) {
continue
}
if _, err := builder.AddCol(flux.ColMeta{
Label: k,
Type: execute.ConvertFromKind(properties[k].Nature()),
}); err != nil {
return err
}
}
}
for j, c := range builder.Cols() {
v, ok := m.Get(c.Label)
if !ok {
if idx := execute.ColIdx(c.Label, tbl.Key().Cols()); t.mergeKey && idx >= 0 {
v = tbl.Key().Value(idx)
} else {
// This should be unreachable
return fmt.Errorf("could not find value for column %q", c.Label)
}
}
if err := builder.AppendValue(j, v); err != nil {
return err
}
}
}
return nil
})
}
func groupKeyForObject(i int, cr flux.ColReader, obj values.Object, on map[string]bool) flux.GroupKey {
cols := make([]flux.ColMeta, 0, len(on))
vs := make([]values.Value, 0, len(on))
for j, c := range cr.Cols() {
if !on[c.Label] {
continue
}
cols = append(cols, c)
v, ok := obj.Get(c.Label)
if ok {
vs = append(vs, v)
} else {
vs = append(vs, execute.ValueForRow(cr, i, j))
}
}
return execute.NewGroupKey(cols, vs)
}
func (t *mapTransformation) UpdateWatermark(id execute.DatasetID, mark execute.Time) error {
return t.d.UpdateWatermark(mark)
}
func (t *mapTransformation) UpdateProcessingTime(id execute.DatasetID, pt execute.Time) error {
return t.d.UpdateProcessingTime(pt)
}
func (t *mapTransformation) Finish(id execute.DatasetID, err error) {
t.d.Finish(err)
}