-
Notifications
You must be signed in to change notification settings - Fork 153
/
map.go
282 lines (242 loc) · 6.66 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
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
package rows
import (
"context"
"github.com/apache/arrow/go/arrow/array"
"github.com/apache/arrow/go/arrow/memory"
"github.com/influxdata/flux"
"github.com/influxdata/flux/arrow"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/compiler"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/internal/execute/table"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/values"
)
const pkgpath = "contrib/jsternberg/rows"
const MapKind = pkgpath + ".map"
func init() {
runtime.RegisterPackageValue(pkgpath, "map", flux.MustValue(flux.FunctionValue(
"map",
createMapOpSpec,
runtime.MustLookupBuiltinType(pkgpath, "map"),
)))
plan.RegisterProcedureSpec(MapKind, newMapProcedure, MapKind)
execute.RegisterTransformation(MapKind, createMapTransformation)
}
type MapOpSpec struct {
Fn interpreter.ResolvedFunction
}
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 fn, err := args.GetRequiredFunction("fn"); err != nil {
return nil, err
} else if fn, err := interpreter.ResolveFunction(fn); err != nil {
return nil, err
} else {
spec.Fn = fn
}
return spec, nil
}
func (a *MapOpSpec) Kind() flux.OperationKind {
return MapKind
}
type MapProcedureSpec struct {
plan.DefaultCost
Fn interpreter.ResolvedFunction
}
func newMapProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*MapOpSpec)
if !ok {
return nil, errors.Newf(codes.Internal, "invalid spec type %T", qs)
}
return &MapProcedureSpec{
Fn: spec.Fn,
}, nil
}
func (s *MapProcedureSpec) Kind() plan.ProcedureKind {
return MapKind
}
func (s *MapProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(MapProcedureSpec)
ns.Fn = s.Fn.Copy()
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, errors.Newf(codes.Internal, "invalid spec type %T", spec)
}
return NewMapTransformation(a.Context(), s, id, a.Allocator())
}
type mapTransformation struct {
execute.ExecutionNode
d *execute.PassthroughDataset
ctx context.Context
fn *execute.RowMapFn
mem memory.Allocator
}
func NewMapTransformation(ctx context.Context, spec *MapProcedureSpec, id execute.DatasetID, mem memory.Allocator) (execute.Transformation, execute.Dataset, error) {
fn := execute.NewRowMapFn(spec.Fn.Fn, compiler.ToScope(spec.Fn.Scope))
t := &mapTransformation{
d: execute.NewPassthroughDataset(id),
ctx: ctx,
fn: fn,
mem: mem,
}
return t, t.d, 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 function for the column types.
cols := tbl.Cols()
fn, err := t.fn.Prepare(cols)
if err != nil {
return err
}
return t.yield(t.mapTable(tbl, fn))
}
func (t *mapTransformation) createSchema(in flux.Table, fn *execute.RowMapPreparedFn) ([]flux.ColMeta, error) {
mt := fn.Type()
nproperties, err := mt.NumProperties()
if err != nil {
return nil, err
}
key := in.Key()
cols := make([]flux.ColMeta, 0, nproperties+len(key.Cols()))
for i := 0; i < nproperties; i++ {
prop, err := mt.RecordProperty(i)
if err != nil {
return nil, err
}
name := prop.Name()
if execute.ColIdx(name, cols) >= 0 {
// A column with this name was already added so
// this was overwritten.
continue
}
typ, err := prop.TypeOf()
if err != nil {
return nil, err
}
cols = append(cols, flux.ColMeta{
Label: name,
Type: flux.ColumnType(typ),
})
}
for _, c := range key.Cols() {
if execute.ColIdx(c.Label, cols) >= 0 {
continue
}
cols = append(cols, c)
}
return cols, nil
}
func (t *mapTransformation) mapTable(in flux.Table, fn *execute.RowMapPreparedFn) (flux.Table, error) {
cols, err := t.createSchema(in, fn)
if err != nil {
return nil, err
}
return &mapTable{
Table: in,
ctx: t.ctx,
cols: cols,
fn: fn,
mem: t.mem,
}, nil
}
func (t *mapTransformation) yield(tbl flux.Table, err error) error {
if err != nil {
return err
}
return t.d.Process(tbl)
}
func (t *mapTransformation) UpdateWatermark(id execute.DatasetID, mark execute.Time) error {
return t.d.UpdateWatermark(mark)
}
func (t *mapTransformation) UpdateProcessingTime(id execute.DatasetID, ts execute.Time) error {
return t.d.UpdateProcessingTime(ts)
}
func (t *mapTransformation) Finish(id execute.DatasetID, err error) {
t.d.Finish(err)
}
type mapTable struct {
flux.Table
ctx context.Context
cols []flux.ColMeta
fn *execute.RowMapPreparedFn
mem memory.Allocator
}
func (t *mapTable) Cols() []flux.ColMeta {
return t.cols
}
func (t *mapTable) Do(f func(cr flux.ColReader) error) error {
return t.Table.Do(func(cr flux.ColReader) error {
buffer := &arrow.TableBuffer{
GroupKey: t.Table.Key(),
Columns: t.cols,
}
if err := t.mapValues(buffer, cr, t.fn); err != nil {
return err
}
return f(buffer)
})
}
func (t *mapTable) mapValues(w *arrow.TableBuffer, cr flux.ColReader, fn *execute.RowMapPreparedFn) error {
cols := w.Cols()
builders := make([]array.Builder, len(cols))
for i, c := range cols {
// If part of the group key, do not create
// a builder. We are going to retain the original
// column.
if execute.ColIdx(c.Label, cr.Key().Cols()) >= 0 {
continue
}
builders[i] = arrow.NewBuilder(c.Type, t.mem)
builders[i].Resize(cr.Len())
}
// Iterate over each row.
for i, n := 0, cr.Len(); i < n; i++ {
obj, err := fn.Eval(t.ctx, i, cr)
if err != nil {
return err
}
// Append each of the returned values to the
// appropriate column.
obj.Range(func(name string, v values.Value) {
if err != nil {
return
}
idx := execute.ColIdx(name, w.Cols())
if builders[idx] == nil {
// Part of the group key. Discard the value.
// Checking for equality is too expensive so we do not
// attempt to verify that it wasn't changed and instead
// opt to ignore changes.
return
}
err = arrow.AppendValue(builders[idx], v)
})
if err != nil {
return err
}
}
w.Values = make([]array.Interface, len(cols))
for i, c := range cols {
if builders[i] == nil {
idx := execute.ColIdx(c.Label, cr.Cols())
w.Values[i] = table.Values(cr, idx)
w.Values[i].Retain()
} else {
w.Values[i] = builders[i].NewArray()
}
}
return nil
}