-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add map function #4806
Merged
Add map function #4806
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
mattnibs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Synopsis | ||
|
||
``` | ||
map(v: array|set, f: function) -> array|set | ||
nwt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
### 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 | ||
mattnibs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 - | ||
``` | ||
mattnibs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
=> | ||
```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) | ||
mattnibs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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"] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a downside to using
Call
rather than addingMapCall
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its kind of a different thing than a call. A call is made of args that you get the value of all by passing
this
into it, a map call you evaluate this, then iterate over each value an call the inner eval on each value. So I kind of think this should remain MapCall