Skip to content

Commit 938ce01

Browse files
mattnibsnwt
andcommitted
Update runtime/vam/expr/function/grok.go
Co-authored-by: Noah Treuhaft <noah.treuhaft@gmail.com>
1 parent 47110f7 commit 938ce01

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

runtime/sam/expr/function/grok.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ func (g *Grok) Call(_ super.Allocator, args []super.Value) super.Value {
4040
if err != nil {
4141
return g.error(err.Error(), defArg)
4242
}
43+
if patternArg.IsNull() || inputArg.IsNull() {
44+
return super.NewValue(g.zctx.MustLookupTypeRecord(nil), nil)
45+
}
4346
p, err := h.getPattern(patternArg.AsString())
4447
if err != nil {
4548
return g.error(err.Error(), patternArg)

runtime/vam/expr/function/grok.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func newGrok(zctx *super.Context) *Grok {
2626

2727
func (g *Grok) Call(args ...vector.Any) vector.Any {
2828
patternArg, inputArg := args[0], args[1]
29-
var defArg vector.Any = vector.NewConst(super.NullString, args[0].Len(), nil)
29+
defArg := vector.Any(vector.NewConst(super.NullString, args[0].Len(), nil))
3030
if len(args) == 3 {
3131
defArg = args[2]
3232
}
@@ -49,14 +49,22 @@ func (g *Grok) Call(args ...vector.Any) vector.Any {
4949
defErrsIdx = append(defErrsIdx, i)
5050
continue
5151
}
52-
pat, _ := vector.StringValue(patternArg, i)
52+
pat, isnull := vector.StringValue(patternArg, i)
53+
if isnull {
54+
builder.Write(super.NewValue(g.zctx.MustLookupTypeRecord(nil), nil))
55+
continue
56+
}
5357
p, err := h.getPattern(pat)
5458
if err != nil {
5559
patErrs = append(patErrs, err.Error())
5660
patErrsIdx = append(patErrsIdx, i)
5761
continue
5862
}
59-
in, _ := vector.StringValue(inputArg, i)
63+
in, isnull := vector.StringValue(inputArg, i)
64+
if isnull {
65+
builder.Write(super.NewValue(g.zctx.MustLookupTypeRecord(nil), nil))
66+
continue
67+
}
6068
keys, vals, match := p.ParseKeyValues(in)
6169
if !match {
6270
inErrsIdx = append(inErrsIdx, i)

runtime/ztests/expr/function/grok.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ input: |
3737
input: "foo",
3838
defs: null(string)
3939
}
40+
{pattern: null(string),input: "foo",defs: null(string)}
41+
{pattern: "%{INT:int}",input:null(string),defs: null(string)}
4042
// Error cases
4143
{
4244
pattern: "%{INT:int}",
@@ -48,6 +50,9 @@ input: |
4850
input: "foo",
4951
defs: null(string)
5052
}
53+
{pattern: 1,input: "foo",defs: null(string)}
54+
{pattern: "%{INT:int}",input:1,defs: null(string)}
55+
{pattern: "%{INT:int}",input:"1",defs:1}
5156
5257
output: |
5358
{event_time:"2020-09-16T04:20:42.45+01:00",log_level:"DEBUG",log_message:"This is a sample debug log message"}
@@ -56,5 +61,10 @@ output: |
5661
{one:"2"}
5762
{}
5863
error({message:"grok(): value does not match pattern",on:"foo"})
64+
null({})
65+
null({})
5966
error({message:"grok(): value does not match pattern",on:"string value"})
6067
error({message:"grok(): the 'DOESNOTEXIST' pattern doesn't exist",on:"%{DOESNOTEXIST:dne}"})
68+
error({message:"grok(): pattern argument must be a string",on:1})
69+
error({message:"grok(): input argument must be a string",on:1})
70+
error({message:"grok(): definitions argument must be a string",on:1})

0 commit comments

Comments
 (0)