Skip to content

Commit

Permalink
Implement vector kind() function (#5313)
Browse files Browse the repository at this point in the history
This includes changes to runtime/vam/expr.Call that allow
runtime/vam/expr/function.Kind to tell it not to rip unions by
implementing a "RipUnions() bool" method.
  • Loading branch information
nwt authored Oct 3, 2024
1 parent 28b16d6 commit b8fe312
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
20 changes: 13 additions & 7 deletions runtime/vam/expr/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,28 @@ type Function interface {
}

type Call struct {
fn Function
exprs []Evaluator
args []vector.Any
fn Function
exprs []Evaluator
ripUnions bool
args []vector.Any
}

func NewCall(fn Function, exprs []Evaluator) *Call {
ripUnions := true
if fn, ok := fn.(interface{ RipUnions() bool }); ok {
ripUnions = fn.RipUnions()
}
return &Call{
fn: fn,
exprs: exprs,
args: make([]vector.Any, len(exprs)),
fn: fn,
exprs: exprs,
ripUnions: ripUnions,
args: make([]vector.Any, len(exprs)),
}
}

func (c *Call) Eval(this vector.Any) vector.Any {
for k, e := range c.exprs {
c.args[k] = e.Eval(this)
}
return vector.Apply(true, c.fn.Call, c.args...)
return vector.Apply(c.ripUnions, c.fn.Call, c.args...)
}
2 changes: 2 additions & 0 deletions runtime/vam/expr/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func New(zctx *zed.Context, name string, narg int) (expr.Function, field.Path, e
case "join":
argmax = 2
f = &Join{zctx: zctx}
case "kind":
f = &Kind{zctx: zctx}
case "len":
f = &Len{zctx}
case "levenshtein":
Expand Down
34 changes: 34 additions & 0 deletions runtime/vam/expr/function/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,37 @@ func (t *TypeOf) Call(args ...vector.Any) vector.Any {
val := t.zctx.LookupTypeValue(args[0].Type())
return vector.NewConst(val, args[0].Len(), nil)
}

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

func NewKind(zctx *zed.Context) *Kind {
return &Kind{zctx}
}

func (k *Kind) Call(args ...vector.Any) vector.Any {
vec := vector.Under(args[0])
if typ := vec.Type(); typ.ID() != zed.IDType {
s := typ.Kind().String()
return vector.NewConst(zed.NewString(s), vec.Len(), nil)
}
out := vector.NewStringEmpty(vec.Len(), nil)
for i, n := uint32(0), vec.Len(); i < n; i++ {
var s string
if bytes, null := vector.TypeValueValue(vec, i); !null {
typ, err := k.zctx.LookupByValue(bytes)
if err != nil {
panic(err)
}
s = typ.Kind().String()
}
out.Append(s)
}
return out
}

func (*Kind) RipUnions() bool {
return false
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
zed: yield {val:this, kind:kind(this)}

vector: true

input: |
null
null(bytes)
Expand Down

0 comments on commit b8fe312

Please sign in to comment.