-
Notifications
You must be signed in to change notification settings - Fork 153
/
vector_fn.go
63 lines (53 loc) · 1.43 KB
/
vector_fn.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
package execute
import (
"context"
"github.com/influxdata/flux"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/compiler"
"github.com/influxdata/flux/execute/table"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)
type VectorMapFn struct {
dynamicFn
}
func NewVectorMapFn(fn *semantic.FunctionExpression, scope compiler.Scope) *VectorMapFn {
return &VectorMapFn{
dynamicFn: newDynamicFn(fn, scope),
}
}
func (f *VectorMapFn) Prepare(cols []flux.ColMeta) (*VectorMapPreparedFn, error) {
fn, err := f.prepare(cols, nil, true)
if err != nil {
return nil, err
} else if k := fn.returnType().Nature(); k != semantic.Object {
return nil, errors.Newf(codes.Invalid, "map function must return an object, got %s", k.String())
}
return &VectorMapPreparedFn{
vectorFn: vectorFn{preparedFn: fn},
}, nil
}
type VectorMapPreparedFn struct {
vectorFn
}
func (f *VectorMapPreparedFn) Type() semantic.MonoType {
return f.fn.Type()
}
type vectorFn struct {
preparedFn
}
func (f *vectorFn) Eval(ctx context.Context, chunk table.Chunk) (values.Object, error) {
for j, col := range chunk.Cols() {
arr := chunk.Values(j)
arr.Retain()
v := values.NewVectorValue(arr, flux.SemanticType(col.Type))
f.arg0.Set(col.Label, v)
}
defer f.arg0.Release()
res, err := f.fn.Eval(ctx, f.args)
if err != nil {
return nil, err
}
return res.Object(), nil
}