-
Notifications
You must be signed in to change notification settings - Fork 67
/
over.go
127 lines (118 loc) · 3.06 KB
/
over.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
package traverse
import (
"context"
"github.com/brimdata/zed"
"github.com/brimdata/zed/runtime/expr"
"github.com/brimdata/zed/runtime/op"
"github.com/brimdata/zed/zbuf"
"github.com/brimdata/zed/zcode"
)
type Over struct {
parent zbuf.Puller
exprs []expr.Evaluator
outer []zed.Value
batch zbuf.Batch
enter *Enter
zctx *zed.Context
}
func NewOver(pctx *op.Context, parent zbuf.Puller, exprs []expr.Evaluator) *Over {
return &Over{
parent: parent,
exprs: exprs,
zctx: pctx.Zctx,
}
}
func (o *Over) AddScope(ctx context.Context, names []string, exprs []expr.Evaluator) *Scope {
scope := newScope(ctx, o, names, exprs)
o.enter = scope.enter
return scope
}
func (o *Over) Pull(done bool) (zbuf.Batch, error) {
if done {
o.outer = nil
return o.parent.Pull(true)
}
for {
if len(o.outer) == 0 {
batch, err := o.parent.Pull(false)
if batch == nil || err != nil {
return nil, err
}
o.batch = batch
o.outer = batch.Values()
}
this := &o.outer[0]
o.outer = o.outer[1:]
ectx := o.batch
if o.enter != nil {
ectx = o.enter.addLocals(ectx, this)
}
innerBatch := o.over(ectx, this)
if len(o.outer) == 0 {
o.batch.Unref()
}
if innerBatch != nil {
return innerBatch, nil
}
}
}
func (o *Over) over(batch zbuf.Batch, this *zed.Value) zbuf.Batch {
// Copy the vars into a new scope since downstream, nested subgraphs
// can have concurrent operators. We can optimize these copies out
// later depending on the nested subgraph.
var vals []zed.Value
for _, e := range o.exprs {
val := e.Eval(batch, this)
// Propagate errors but skip missing values.
if !val.IsMissing() {
vals = appendOver(o.zctx, vals, *val)
}
}
if len(vals) == 0 {
return nil
}
return zbuf.NewBatch(batch, vals)
}
func appendOver(zctx *zed.Context, vals []zed.Value, zv zed.Value) []zed.Value {
zv = *zv.Under()
switch typ := zed.TypeUnder(zv.Type).(type) {
case *zed.TypeArray, *zed.TypeSet:
typ = zed.InnerType(typ)
for it := zv.Bytes.Iter(); !it.Done(); {
// XXX when we do proper expr.Context, we can allocate
// this copy through the batch.
val := zed.NewValue(typ, it.Next()).Under()
vals = append(vals, *val.Copy())
}
return vals
case *zed.TypeMap:
rtyp := zctx.MustLookupTypeRecord([]zed.Field{
zed.NewField("key", typ.KeyType),
zed.NewField("value", typ.ValType),
})
for it := zv.Bytes.Iter(); !it.Done(); {
bytes := zcode.Append(zcode.Append(nil, it.Next()), it.Next())
vals = append(vals, *zed.NewValue(rtyp, bytes))
}
return vals
case *zed.TypeRecord:
builder := zcode.NewBuilder()
columns := typ.Fields
for i, it := 0, zv.Bytes.Iter(); !it.Done(); i++ {
builder.Reset()
col := columns[i]
typ := zctx.MustLookupTypeRecord([]zed.Field{
{Name: "key", Type: zctx.LookupTypeArray(zed.TypeString)},
{Name: "value", Type: col.Type},
})
builder.BeginContainer()
builder.Append(zed.EncodeString(col.Name))
builder.EndContainer()
builder.Append(it.Next())
vals = append(vals, *zed.NewValue(typ, builder.Bytes()).Copy())
}
return vals
default:
return append(vals, zv)
}
}