forked from brimdata/super
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply.go
50 lines (46 loc) · 1018 Bytes
/
apply.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
package op
import (
"github.com/brimdata/zed"
"github.com/brimdata/zed/runtime/expr"
"github.com/brimdata/zed/zbuf"
)
type applier struct {
pctx *Context
parent zbuf.Puller
expr expr.Applier
warned map[string]struct{}
}
func NewApplier(pctx *Context, parent zbuf.Puller, apply expr.Applier) *applier {
return &applier{
pctx: pctx,
parent: parent,
expr: apply,
warned: make(map[string]struct{}),
}
}
func (a *applier) Pull(done bool) (zbuf.Batch, error) {
for {
batch, err := a.parent.Pull(done)
if batch == nil || err != nil {
return nil, err
}
vals := batch.Values()
out := make([]zed.Value, 0, len(vals))
for i := range vals {
val := a.expr.Eval(batch, &vals[i])
if val.IsError() {
if val.IsQuiet() || val.IsMissing() {
continue
}
}
// Copy is necessary because Apply can return
// its argument.
out = append(out, *val.Copy())
}
if len(out) > 0 {
defer batch.Unref()
return zbuf.NewBatch(batch, out), nil
}
batch.Unref()
}
}