-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vector: Rudimentary support functions
This commit adds rudimentary support for functions. There are a lot of shortcomings compared to sequence notably: only supports function lower(), does not variants/unions and wrapped errors are not handled, and it is not currently tested. Future work will brings things in feature parity.
- Loading branch information
Showing
5 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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. | ||
// XXX If this is a View we probably need to transfer. | ||
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++ { | ||
s, null := vector.StringValue(v, i) | ||
if null { | ||
out.Nulls.Set(i) | ||
} | ||
out.Append(strings.ToLower(s)) | ||
} | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters