Skip to content

Commit

Permalink
Implement vector error() function (#5539)
Browse files Browse the repository at this point in the history
This includes a ztest for the assert operator because the error()
function is the final missing piece required to make that operator work
on vectors.
  • Loading branch information
nwt authored Dec 18, 2024
1 parent 5b29662 commit 1a79b43
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions runtime/vam/expr/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func New(zctx *super.Context, name string, narg int) (expr.Function, field.Path,
case "every":
path = field.Path{"ts"}
f = &Bucket{zctx: zctx, name: name}
case "error":
f = &Error{zctx}
case "fields":
f = NewFields(zctx)
case "grep":
Expand Down
10 changes: 10 additions & 0 deletions runtime/vam/expr/function/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ func (t *TypeOf) Call(args ...vector.Any) vector.Any {
return vector.NewConst(val, args[0].Len(), nil)
}

// https://github.com/brimdata/super/blob/main/docs/language/functions.md#error
type Error struct {
zctx *super.Context
}

func (e *Error) Call(args ...vector.Any) vector.Any {
vec := args[0]
return vector.NewError(e.zctx.LookupTypeError(vec.Type()), vec, nil)
}

// https://github.com/brimdata/super/blob/main/docs/language/functions.md#kind
type Kind struct {
zctx *super.Context
Expand Down
17 changes: 17 additions & 0 deletions runtime/ztests/expr/function/error.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
zed: yield error(this)

vector: true

input: |
1
"s"
{a:1}
null
null(int64)
output: |
error(1)
error("s")
error({a:1})
null(error(null))
null(error(int64))
13 changes: 13 additions & 0 deletions runtime/ztests/op/assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
zed: assert a==1

vector: true

input: |
{a:1}
{a:2}
1
output: |
{a:1}
error({message:"assertion failed",expr:"a==1",on:{a:2}})
error({message:"?-operator: bool predicate required",on:error("missing")})

0 comments on commit 1a79b43

Please sign in to comment.