Skip to content

Commit

Permalink
vam: Add preliminary quiet support
Browse files Browse the repository at this point in the history
This commit add the quiet function and support for error("quiet") in the
yield operator. Quiet errors are not supported elsewhere including cut
and put.
  • Loading branch information
mattnibs committed Oct 1, 2024
1 parent a6f8ac2 commit badff23
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 19 deletions.
10 changes: 0 additions & 10 deletions runtime/sam/op/yield/ztests/quiet.yaml

This file was deleted.

41 changes: 41 additions & 0 deletions runtime/vam/expr/function/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package function

import (
"github.com/brimdata/zed"
"github.com/brimdata/zed/vector"
)

// https://github.com/brimdata/zed/blob/main/docs/language/functions.md#quiet
type Quiet struct {
zctx *zed.Context
}

func (q *Quiet) Call(args ...vector.Any) vector.Any {
arg, ok := args[0].(*vector.Error)
if !ok {
return args[0]
}
if _, ok := arg.Vals.Type().(*zed.TypeOfString); !ok {
return args[0]
}
if c, ok := arg.Vals.(*vector.Const); ok {
// Fast path
if s, _ := c.AsString(); s == "missing" {
return vector.NewStringError(q.zctx, "quiet", c.Len())
}
return args[0]
}
n := arg.Len()
vec := vector.NewStringEmpty(n, vector.NewBoolEmpty(n, nil))
for i := uint32(0); i < n; i++ {
s, null := vector.StringValue(arg.Vals, i)
if null {
vec.Nulls.Set(i)
}
if s == "missing" {
s = "quiet"
}
vec.Append(s)
}
return vector.NewError(arg.Typ, vec, arg.Nulls)
}
2 changes: 2 additions & 0 deletions runtime/vam/expr/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func New(zctx *zed.Context, name string, narg int) (expr.Function, field.Path, e
f = &Levenshtein{zctx}
case "lower":
f = &ToLower{zctx}
case "quiet":
f = &Quiet{zctx}
case "replace":
argmin, argmax = 3, 3
f = &Replace{zctx}
Expand Down
47 changes: 42 additions & 5 deletions runtime/vam/op/yield.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ func (y *Yield) Pull(done bool) (vector.Any, error) {
}
}

func filterQuiet(val vector.Any) vector.Any {
// XXX this can't happen until we have functions
return val
}

// XXX should work for vector.Dynamic
func interleave(vals []vector.Any) vector.Any {
if len(vals) < 2 {
Expand All @@ -63,3 +58,45 @@ func interleave(vals []vector.Any) vector.Any {
}
return vector.NewDynamic(tags, vals)
}

func filterQuiet(vec vector.Any) vector.Any {
var filtered bool
mask := vector.Apply(true, func(vecs ...vector.Any) vector.Any {
mask, hasfiltered := quietMask(vecs[0])
filtered = filtered || hasfiltered
return mask
}, vec)
if !filtered {
return vec
}
masked, _ := applyMask(vec, mask)
return masked
}

func quietMask(vec vector.Any) (vector.Any, bool) {
errvec, ok := vec.(*vector.Error)
if !ok {
return vector.NewConst(zed.True, vec.Len(), nil), false
}
if _, ok := errvec.Vals.Type().(*zed.TypeOfBool); !ok {
return vector.NewConst(zed.True, vec.Len(), nil), false
}
if c, ok := errvec.Vals.(*vector.Const); ok {
if s, _ := c.AsString(); s == "quiet" {
return vector.NewConst(zed.False, vec.Len(), nil), true
}
return vector.NewConst(zed.True, vec.Len(), nil), false
}
n := vec.Len()
mask := vector.NewBoolEmpty(n, nil)
switch vec := vec.(type) {
case *vector.Error:
for i := uint32(0); i < n; i++ {
if s, _ := vector.StringValue(vec.Vals, i); s == "quiet" {
continue
}
mask.Set(i)
}
}
return mask, true
}
23 changes: 23 additions & 0 deletions runtime/ztests/expr/function/quiet.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
zed: quiet(this)

vector: true

input: |
1
[3,2,1,0]
error("missing")
error("missing")(=foo)
error("missing"(=bar))
null(error(string))
error(null(string))
error("quiet")
error({x:"missing"})
output: |
1
[3,2,1,0]
error("missing")(=foo)
error("missing")(error(bar=string))
null(error(string))
null(error(string))
error({x:"missing"})
3 changes: 2 additions & 1 deletion type.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ func CompareTypes(a, b Type) int {
if cmp := cmp.Compare(a.Kind(), b.Kind()); cmp != 0 {
return cmp
}
a, b = TypeUnder(a), TypeUnder(b)
switch a.Kind() {
case PrimitiveKind:
return cmp.Compare(aID, bID)
Expand Down Expand Up @@ -469,7 +470,7 @@ func CompareTypes(a, b Type) int {
}
return 0
case ErrorKind:
ea, eb := a.(*TypeError), b.(*TypeError)
ea, eb := a.(*TypeError), a.(*TypeError)
return CompareTypes(ea.Type, eb.Type)
}
return 0
Expand Down
4 changes: 1 addition & 3 deletions vector/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ func NewStringError(zctx *zed.Context, msg string, len uint32) *Error {
}

func NewMissing(zctx *zed.Context, len uint32) *Error {
missing := zctx.Missing()
vals := NewConst(missing, len, nil)
return &Error{Typ: missing.Type().(*zed.TypeError), Vals: vals}
return NewStringError(zctx, "missing", len)
}

func NewWrappedError(zctx *zed.Context, msg string, val Any) *Error {
Expand Down

0 comments on commit badff23

Please sign in to comment.