Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vector: Rudimentary function support #5222

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions compiler/kernel/vexpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/brimdata/zed/compiler/ast/dag"
"github.com/brimdata/zed/pkg/field"
vamexpr "github.com/brimdata/zed/runtime/vam/expr"
vamfunc "github.com/brimdata/zed/runtime/vam/expr/function"
"github.com/brimdata/zed/zson"
)

Expand Down Expand Up @@ -35,8 +36,8 @@ func (b *Builder) compileVamExpr(e dag.Expr) (vamexpr.Evaluator, error) {
return b.compileVamBinary(e)
//case *dag.Conditional:
// return b.compileVamConditional(*e)
//case *dag.Call:
// return b.compileVamCall(*e)
case *dag.Call:
return b.compileVamCall(e)
//case *dag.RegexpMatch:
// return b.compileVamRegexpMatch(e)
//case *dag.RegexpSearch:
Expand Down Expand Up @@ -142,3 +143,20 @@ func (b *Builder) compileVamExprs(in []dag.Expr) ([]vamexpr.Evaluator, error) {
}
return exprs, nil
}

func (b *Builder) compileVamCall(call *dag.Call) (vamexpr.Evaluator, error) {
fn, path, err := vamfunc.New(b.zctx(), call.Name, len(call.Args))
if err != nil {
return nil, err
}
args := call.Args
if path != nil {
dagPath := &dag.This{Kind: "This", Path: path}
args = append([]dag.Expr{dagPath}, args...)
}
exprs, err := b.compileVamExprs(args)
if err != nil {
return nil, err
}
return vamexpr.NewCall(fn, exprs), nil
}
29 changes: 28 additions & 1 deletion runtime/vam/expr/eval.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
package expr

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

type Evaluator interface {
Eval(vector.Any) vector.Any
}

type Function interface {
Call([]vector.Any) vector.Any
}

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

func NewCall(fn Function, exprs []Evaluator) *Call {
return &Call{
fn: fn,
exprs: exprs,
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 c.fn.Call(c.args)
}
25 changes: 25 additions & 0 deletions runtime/vam/expr/function/function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package function

import (
"github.com/brimdata/zed"
"github.com/brimdata/zed/pkg/field"
"github.com/brimdata/zed/runtime/sam/expr/function"
"github.com/brimdata/zed/runtime/vam/expr"
)

func New(zctx *zed.Context, name string, narg int) (expr.Function, field.Path, error) {
argmin := 1
argmax := 1
var path field.Path
var f expr.Function
switch name {
case "lower":
f = &ToLower{zctx}
default:
return nil, nil, function.ErrNoSuchFunction
}
if err := function.CheckArgCount(narg, argmin, argmax); err != nil {
return nil, nil, err
}
return f, path, nil
}
30 changes: 30 additions & 0 deletions runtime/vam/expr/function/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package function

import (
"strings"

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

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

func (t *ToLower) Call(args []vector.Any) vector.Any {
v := vector.Under(args[0])
if v.Type() != zed.TypeString {
// XXX This should be a wrapped error as seen in sequential world.
return vector.NewStringError(t.zctx, "lower: string arg required", v.Len())
}
out := vector.NewStringEmpty(v.Len(), vector.NewBoolEmpty(v.Len(), nil))
for i := uint32(0); i < v.Len(); i++ {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want a pattern where there is a string function applied to string/const/dict/view and you get back a vector.Any. So instead of looping over slots you apply a function to a vector and get a vector back. So you would pass the go func strings.ToLower to the apply function and get back the vector. We would have to make an applier for every possible type signature but that seems reasonable since it's linear in problem size.

s, null := vector.StringValue(v, i)
if null {
out.Nulls.Set(i)
}
out.Append(strings.ToLower(s))
}
return out
}
29 changes: 29 additions & 0 deletions vector/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,32 @@ func (s *String) Serialize(b *zcode.Builder, slot uint32) {
b.Append(zed.EncodeString(s.Value(slot)))
}
}

func StringValue(val Any, slot uint32) (string, bool) {
switch val := val.(type) {
case *String:
if val.Nulls.Value(slot) {
return "", true
}
return val.Value(slot), false
case *Const:
if val.Nulls.Value(slot) {
return "", true
}
s, _ := val.AsString()
return s, false
case *Dict:
if val.Nulls.Value(slot) {
return "", true
}
slot = uint32(val.Index[slot])
return val.Any.(*String).Value(slot), false
case *View:
slot = val.Index[slot]
if val.Any.(*String).Nulls.Value(slot) {
return "", true
}
return val.Any.(*String).Value(slot), false
}
panic(val)
}