-
Notifications
You must be signed in to change notification settings - Fork 67
/
map.go
71 lines (64 loc) · 1.62 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
package expr
import (
"github.com/brimdata/zed"
"github.com/brimdata/zed/zcode"
)
type mapCall struct {
builder zcode.Builder
eval Evaluator
inner Evaluator
zctx *zed.Context
// vals is used to reduce allocations
vals []zed.Value
// types is used to reduce allocations
types []zed.Type
}
func NewMapCall(zctx *zed.Context, e, inner Evaluator) Evaluator {
return &mapCall{eval: e, inner: inner, zctx: zctx}
}
func (a *mapCall) Eval(ectx Context, in zed.Value) zed.Value {
val := a.eval.Eval(ectx, in)
if val.IsError() {
return val
}
elems, err := val.Elements()
if err != nil {
return a.zctx.WrapError(err.Error(), in)
}
if len(elems) == 0 {
return val
}
a.vals = a.vals[:0]
a.types = a.types[:0]
for _, elem := range elems {
val := a.inner.Eval(ectx, elem)
a.vals = append(a.vals, val)
a.types = append(a.types, val.Type())
}
inner := a.innerType(a.types)
bytes := a.buildVal(inner, a.vals)
if _, ok := zed.TypeUnder(val.Type()).(*zed.TypeSet); ok {
return zed.NewValue(a.zctx.LookupTypeSet(inner), zed.NormalizeSet(bytes))
}
return zed.NewValue(a.zctx.LookupTypeArray(inner), bytes)
}
func (a *mapCall) buildVal(inner zed.Type, vals []zed.Value) []byte {
a.builder.Reset()
if union, ok := inner.(*zed.TypeUnion); ok {
for _, val := range a.vals {
zed.BuildUnion(&a.builder, union.TagOf(val.Type()), val.Bytes())
}
} else {
for _, val := range a.vals {
a.builder.Append(val.Bytes())
}
}
return a.builder.Bytes()
}
func (a *mapCall) innerType(types []zed.Type) zed.Type {
types = zed.UniqueTypes(types)
if len(types) == 1 {
return types[0]
}
return a.zctx.LookupTypeUnion(types)
}