-
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.
The map function that applies a function to every element in an array or set value. Also: rename the map aggregator to collect_map to avoid collision with this new function. Closes #4610
- Loading branch information
Showing
11 changed files
with
139 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 |
---|---|---|
|
@@ -31,6 +31,7 @@ var unpacker = unpack.New( | |
Lister{}, | ||
Literal{}, | ||
Load{}, | ||
MapCall{}, | ||
MapExpr{}, | ||
Merge{}, | ||
Over{}, | ||
|
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
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
2 changes: 1 addition & 1 deletion
2
runtime/expr/agg/ztests/map-union.yaml → ...me/expr/agg/ztests/collect-map-union.yaml
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
zed: "map(|{k:v}|)" | ||
zed: "collect_map(|{k:v}|)" | ||
|
||
output-flags: -pretty 2 | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
runtime/expr/agg/ztests/map.yaml → runtime/expr/agg/ztests/collect-map.yaml
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
zed: "map(|{stock: price}|)" | ||
zed: "collect_map(|{stock: price}|)" | ||
|
||
output-flags: -pretty 2 | ||
|
||
|
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,71 @@ | ||
package expr | ||
|
||
import ( | ||
"github.com/brimdata/zed" | ||
"github.com/brimdata/zed/zcode" | ||
) | ||
|
||
type mapFunc 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 NewMapFunc(zctx *zed.Context, e, inner Evaluator) Evaluator { | ||
return &mapFunc{eval: e, inner: inner, zctx: zctx} | ||
} | ||
|
||
func (a *mapFunc) 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.inner.Eval(ectx, &elem) | ||
a.vals = append(a.vals, *out) | ||
a.types = append(a.types, out.Type) | ||
} | ||
inner := a.innerType(a.types) | ||
bytes := a.buildVal(inner, a.vals) | ||
if _, ok := zed.TypeUnder(in.Type).(*zed.TypeSet); ok { | ||
return ectx.NewValue(a.zctx.LookupTypeSet(inner), zed.NormalizeSet(bytes)) | ||
} | ||
return ectx.NewValue(a.zctx.LookupTypeArray(inner), bytes) | ||
} | ||
|
||
func (a *mapFunc) 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 *mapFunc) 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,8 @@ | ||
script: | | ||
echo '{a:["foo","bar","baz"]}' | zq -z 'a := map(a,upper)' - | ||
echo '["1","2","3"]' | zq -z 'yield map(this,int64)' - | ||
outputs: | ||
- name: stdout | ||
data: | | ||
{a:["FOO","BAR","BAZ"]} | ||
[1,2,3] |