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

Implement vector rename operator #5291

Merged
merged 2 commits into from
Sep 18, 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
32 changes: 20 additions & 12 deletions compiler/kernel/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,9 @@ func (b *Builder) compileLeaf(o dag.Op, parent zbuf.Puller) (zbuf.Puller, error)
return op.NewApplier(b.rctx, parent, putter, b.resetters), nil
case *dag.Rename:
b.resetResetters()
var srcs, dsts []*expr.Lval
for _, a := range v.Args {
src, err := b.compileLval(a.RHS)
if err != nil {
return nil, err
}
dst, err := b.compileLval(a.LHS)
if err != nil {
return nil, err
}
srcs = append(srcs, src)
dsts = append(dsts, dst)
srcs, dsts, err := b.compileAssignmentsToLvals(v.Args)
if err != nil {
return nil, err
}
renamer := expr.NewRenamer(b.rctx.Zctx, srcs, dsts)
return op.NewApplier(b.rctx, parent, renamer, b.resetters), nil
Expand Down Expand Up @@ -447,6 +438,23 @@ func (b *Builder) compileAssignments(assignments []dag.Assignment) ([]expr.Assig
return keys, nil
}

func (b *Builder) compileAssignmentsToLvals(assignments []dag.Assignment) ([]*expr.Lval, []*expr.Lval, error) {
var srcs, dsts []*expr.Lval
for _, a := range assignments {
src, err := b.compileLval(a.RHS)
if err != nil {
return nil, nil, err
}
dst, err := b.compileLval(a.LHS)
if err != nil {
return nil, nil, err
}
srcs = append(srcs, src)
dsts = append(dsts, dst)
}
return srcs, dsts, nil
}

func splitAssignments(assignments []expr.Assignment) ([]*expr.Lval, []expr.Evaluator) {
n := len(assignments)
lhs := make([]*expr.Lval, 0, n)
Expand Down
7 changes: 7 additions & 0 deletions compiler/kernel/vop.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ func (b *Builder) compileVamLeaf(o dag.Op, parent vector.Puller) (vector.Puller,
return nil, err
}
return vamop.NewYield(b.zctx(), parent, []expr.Evaluator{expr.NewPutter(b.zctx(), e)}), nil
case *dag.Rename:
srcs, dsts, err := b.compileAssignmentsToLvals(o.Args)
if err != nil {
return nil, err
}
renamer := expr.NewRenamer(b.zctx(), srcs, dsts)
return vamop.NewYield(b.zctx(), parent, []expr.Evaluator{renamer}), nil
case *dag.Yield:
exprs, err := b.compileVamExprs(o.Exprs)
if err != nil {
Expand Down
16 changes: 12 additions & 4 deletions runtime/sam/expr/renamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@ func NewRenamer(zctx *zed.Context, srcs, dsts []*Lval) *Renamer {
}

func (r *Renamer) Eval(ectx Context, this zed.Value) zed.Value {
val, err := r.EvalToValAndError(ectx, this)
if err != nil {
return r.zctx.WrapError(err.Error(), this)
}
return val
}

func (r *Renamer) EvalToValAndError(ectx Context, this zed.Value) (zed.Value, error) {
if !zed.IsRecordType(this.Type()) {
return this
return this, nil
}
srcs, dsts, err := r.evalFields(ectx, this)
if err != nil {
return r.zctx.WrapError(fmt.Sprintf("rename: %s", err), this)
return zed.Null, fmt.Errorf("rename: %w", err)
}
id := this.Type().ID()
m, ok := r.typeMap[id]
Expand All @@ -48,11 +56,11 @@ func (r *Renamer) Eval(ectx Context, this zed.Value) zed.Value {
var err error
typ, err = r.computeType(zed.TypeRecordOf(this.Type()), srcs, dsts)
if err != nil {
return r.zctx.WrapError(fmt.Sprintf("rename: %s", err), this)
return zed.Null, fmt.Errorf("rename: %w", err)
}
m[string(r.fieldsStr)] = typ
}
return zed.NewValue(typ, this.Bytes())
return zed.NewValue(typ, this.Bytes()), nil
}

func CheckRenameField(src, dst field.Path) error {
Expand Down
35 changes: 35 additions & 0 deletions runtime/vam/expr/renamer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package expr

import (
"github.com/brimdata/zed"
"github.com/brimdata/zed/runtime/sam/expr"
"github.com/brimdata/zed/vector"
)

// Renamer renames one or more fields in a record. See [expr.Renamer], on which
// it relies, for more detail.
type Renamer struct {
zctx *zed.Context
renamer *expr.Renamer
}

func NewRenamer(zctx *zed.Context, srcs, dsts []*expr.Lval) *Renamer {
return &Renamer{zctx, expr.NewRenamer(zctx, srcs, dsts)}
}

func (r *Renamer) Eval(vec vector.Any) vector.Any {
return vector.Apply(false, r.eval, vec)
}

func (r *Renamer) eval(vecs ...vector.Any) vector.Any {
vec := vecs[0]
recVec, ok := vector.Under(vec).(*vector.Record)
if !ok {
return vec
}
val, err := r.renamer.EvalToValAndError(nil, zed.NewValue(vec.Type(), nil))
if err != nil {
return vector.NewWrappedError(r.zctx, err.Error(), vec)
}
return vector.NewRecord(val.Type().(*zed.TypeRecord), recVec.Fields, recVec.Len(), recVec.Nulls)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
zed: rename foo:=goo

vector: true

input: |
{foo:"foo1",goo:"goo1"}
{foo:"foo2",goo:"goo2"}

output: |
error({message:"rename: duplicate field: \"foo\"",on:{foo:"foo1",goo:"goo1"}})
error({message:"rename: duplicate field: \"foo\"",on:{foo:"foo2",goo:"goo2"}})
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
zed: rename dst:=id.resp_h

vector: true

input: |
{id:{orig_h:10.164.94.120,orig_p:39681(port=uint16),resp_h:10.47.3.155,resp_p:3389(port)}}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
zed: rename f:=foo, b:=bar

vector: true

input: |
{foo:"foo0"}
{foo:"foo1",goo:"goo1"}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
zed: rename id.dst:=id.resp_h, id.src:=id.orig_h, net:=id

vector: true

input: |
{id:{orig_h:10.164.94.120,orig_p:39681(port=uint16),resp_h:10.47.3.155,resp_p:3389(port)}}
{id:{orig_h:10.164.94.121,orig_p:39681(port=uint16),resp_h:10.47.3.155,resp_p:3390(port)}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
zed: rename a := b

vector: true

input: |
1

Expand Down