-
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.
Implement vector grep() function (#5523)
- Loading branch information
Showing
5 changed files
with
84 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 was deleted.
Oops, something went wrong.
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,50 @@ | ||
package function | ||
|
||
import ( | ||
"github.com/brimdata/super" | ||
"github.com/brimdata/super/runtime/vam/expr" | ||
"github.com/brimdata/super/vector" | ||
"golang.org/x/text/unicode/norm" | ||
) | ||
|
||
type Grep struct { | ||
zctx *super.Context | ||
grep expr.Evaluator | ||
pattern string | ||
} | ||
|
||
func (g *Grep) Call(args ...vector.Any) vector.Any { | ||
patternVec, inputVec := args[0], args[1] | ||
if patternVec.Type().ID() != super.IDString { | ||
return vector.NewWrappedError(g.zctx, "grep(): pattern argument must be a string", patternVec) | ||
} | ||
if inputVec.Len() == 0 { | ||
return vector.NewBoolEmpty(0, nil) | ||
} | ||
if c, ok := vector.Under(patternVec).(*vector.Const); ok { | ||
pattern, _ := c.AsString() | ||
if g.grep == nil || g.pattern != pattern { | ||
pattern = norm.NFC.String(pattern) | ||
g.grep = expr.NewSearchString(pattern, &expr.This{}) | ||
g.pattern = pattern | ||
} | ||
return g.grep.Eval(inputVec) | ||
} | ||
var index [1]uint32 | ||
nulls := vector.Or(vector.NullsOf(patternVec), vector.NullsOf(inputVec)) | ||
out := vector.NewBoolEmpty(patternVec.Len(), nulls) | ||
for i := range patternVec.Len() { | ||
if nulls.Value(i) { | ||
continue | ||
} | ||
pattern, _ := vector.StringValue(patternVec, i) | ||
pattern = norm.NFC.String(pattern) | ||
search := expr.NewSearchString(pattern, &expr.This{}) | ||
index[0] = i | ||
view := vector.NewView(inputVec, index[:]) | ||
if match, _ := vector.BoolValue(search.Eval(view), 0); match { | ||
out.Set(i) | ||
} | ||
} | ||
return out | ||
} |
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,28 @@ | ||
# This test tests grep as a function call which only happens when the pattern | ||
# arg is not a glob, regular expression, or resolvable to a string at compile | ||
# time. | ||
|
||
zed: | | ||
[grep(pattern),grep(pattern,input)] | ||
vector: true | ||
|
||
input: | | ||
{pattern:"a",input:"a"} | ||
{pattern:"z",input:"a"} | ||
{pattern:"b",input:{a:{b:1}}} | ||
{pattern:"z",input:{a:{b:1}}} | ||
{pattern:"c",input:{a:{b:"c"}}} | ||
{pattern:"z",input:{a:{b:"c"}}} | ||
{pattern:1,input:""} | ||
{pattern:null(string),input:"a"} | ||
output: | | ||
[true,true] | ||
[true,false] | ||
[true,true] | ||
[true,false] | ||
[true,true] | ||
[true,false] | ||
[error({message:"grep(): pattern argument must be a string",on:1}),error({message:"grep(): pattern argument must be a string",on:1})] | ||
[null(bool),null(bool)] |