-
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
15 changed files
with
200 additions
and
27 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
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
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,42 @@ | ||
### Function | ||
|
||
  **map** — calls a function on each element of an array and returns the results | ||
|
||
### Synopsis | ||
|
||
``` | ||
map(v: array|set, f: function) -> array|set | ||
``` | ||
|
||
### Description | ||
|
||
The _map_ function applies function f to every element in array/set v and | ||
returns the augmented array/set. Function f must be a function that takes | ||
only one argument. A user defined function can be used for f. | ||
|
||
### Examples | ||
|
||
Upper case each element of an array: | ||
|
||
```mdtest-command | ||
echo '["foo","bar","baz"]' | zq -z 'yield map(this, upper)' - | ||
``` | ||
=> | ||
```mdtest-output | ||
["FOO","BAR","BAZ"] | ||
``` | ||
|
||
Using a user defined function to convert an epoch float to a time: | ||
|
||
```mdtest-input udf.zed | ||
func floatToTime(x): ( cast(x*1000000000, <time>) ) | ||
yield map(this, floatToTime) | ||
``` | ||
|
||
```mdtest-command | ||
echo '[1697151533.41415,1697151540.716529]' | zq -z -I udf.zed - | ||
``` | ||
=> | ||
```mdtest-output | ||
[2023-10-12T22:58:53.414149888Z,2023-10-12T22:59:00.716528896Z] | ||
``` |
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 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 { | ||
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 *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) | ||
} |
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,17 @@ | ||
script: | | ||
echo '{a:["foo","bar","baz"]}' | zq -z 'a := map(a,upper)' - | ||
echo '["1","2","3"]' | zq -z 'yield map(this,int64)' - | ||
echo '[1,2,3]' | zq -z -I udf.zed - | ||
inputs: | ||
- name: udf.zed | ||
data: | | ||
func stringify(x): ( cast(x, <string>) ) | ||
yield map(this, stringify) | ||
outputs: | ||
- name: stdout | ||
data: | | ||
{a:["FOO","BAR","BAZ"]} | ||
[1,2,3] | ||
["1","2","3"] |