-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a function that applies a function to every element in an array or set value.
- Loading branch information
Showing
7 changed files
with
147 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
|
||
var unpacker = unpack.New( | ||
Agg{}, | ||
ApplyExpr{}, | ||
ArrayExpr{}, | ||
Assignment{}, | ||
BinaryExpr{}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package expr | ||
|
||
import ( | ||
"github.com/brimdata/zed" | ||
"github.com/brimdata/zed/zcode" | ||
) | ||
|
||
type apply struct { | ||
builder zcode.Builder | ||
eval Evaluator | ||
fn Function | ||
zctx *zed.Context | ||
|
||
// vals is used to reduce allocations | ||
vals []zed.Value | ||
// types is used to reduce allocations | ||
types []zed.Type | ||
} | ||
|
||
func NewApplyFunc(zctx *zed.Context, e Evaluator, fn Function) Evaluator { | ||
return &apply{eval: e, fn: fn, zctx: zctx} | ||
} | ||
|
||
func (a *apply) Eval(ectx Context, in *zed.Value) *zed.Value { | ||
v := a.eval.Eval(ectx, in) | ||
if v.IsError() { | ||
return v | ||
} | ||
elems, err := v.Elements() | ||
if err != nil { | ||
return ectx.CopyValue(*a.zctx.WrapError(err.Error(), in)) | ||
} | ||
if len(elems) == 0 { | ||
return v | ||
} | ||
a.vals = a.vals[:0] | ||
a.types = a.types[:0] | ||
for _, elem := range elems { | ||
out := a.fn.Call(ectx, []zed.Value{elem}) | ||
a.vals = append(a.vals, *out) | ||
a.types = append(a.types, out.Type) | ||
} | ||
inner := a.innerType(a.types) | ||
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()) | ||
} | ||
} | ||
if _, ok := zed.TypeUnder(in.Type).(*zed.TypeSet); ok { | ||
return ectx.NewValue(a.zctx.LookupTypeSet(inner), zed.NormalizeSet(a.builder.Bytes())) | ||
} | ||
return ectx.NewValue(a.zctx.LookupTypeArray(inner), a.builder.Bytes()) | ||
} | ||
|
||
func (a *apply) innerType(types []zed.Type) zed.Type { | ||
types = zed.UniqueTypes(types) | ||
if len(types) == 1 { | ||
return types[0] | ||
} | ||
return a.zctx.LookupTypeUnion(types) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
script: | | ||
echo '{a:["foo","bar","baz"]}' | zq -z 'a := apply(a,upper)' - | ||
outputs: | ||
- name: stdout | ||
data: | | ||
{a:["FOO","BAR","BAZ"]} |