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

Replace vam/expr.stitch with vector.Apply #5224

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
11 changes: 4 additions & 7 deletions runtime/vam/expr/arith.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ func NewArith(zctx *zed.Context, lhs, rhs Evaluator, op string) *Arith {
}

func (a *Arith) Eval(val vector.Any) vector.Any {
return a.eval(a.lhs.Eval(val), a.rhs.Eval(val))
return vector.Apply(true, a.eval, a.lhs.Eval(val), a.rhs.Eval(val))
}

func (a *Arith) eval(lhs, rhs vector.Any) vector.Any {
lhs = vector.Under(lhs)
rhs = vector.Under(rhs)
if val, ok := stitch(lhs, rhs, a.eval); ok {
return val
}
func (a *Arith) eval(vecs ...vector.Any) vector.Any {
lhs := vector.Under(vecs[0])
rhs := vector.Under(vecs[1])
lhs, rhs, errVal := coerceVals(a.zctx, lhs, rhs)
if errVal != nil {
return errVal
Expand Down
2 changes: 2 additions & 0 deletions runtime/vam/expr/coerce.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ func intToFloat(val vector.Any) vector.Any {
panic("ToFloat failed")
}
return vector.NewConst(nil, zed.NewFloat64(f), val.Len(), val.Nulls)
case *vector.Dict:
return vector.NewDict(intToFloat(val.Any), val.Index, val.Counts, val.Nulls)
case *vector.View:
return vector.NewView(val.Index, intToFloat(val.Any))
default:
Expand Down
11 changes: 4 additions & 7 deletions runtime/vam/expr/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ func NewCompare(zctx *zed.Context, lhs, rhs Evaluator, op string) *Compare {
}

func (c *Compare) Eval(val vector.Any) vector.Any {
return c.eval(c.lhs.Eval(val), c.rhs.Eval(val))
return vector.Apply(true, c.eval, c.lhs.Eval(val), c.rhs.Eval(val))
}

func (c *Compare) eval(lhs, rhs vector.Any) vector.Any {
lhs = vector.Under(lhs)
rhs = vector.Under(rhs)
if val, ok := stitch(lhs, rhs, c.eval); ok {
return val
}
func (c *Compare) eval(vecs ...vector.Any) vector.Any {
lhs := vector.Under(vecs[0])
rhs := vector.Under(vecs[1])
lhs, rhs, errVal := coerceVals(c.zctx, lhs, rhs)
if errVal != nil {
return errVal
Expand Down
40 changes: 0 additions & 40 deletions runtime/vam/expr/stitch.go

This file was deleted.

50 changes: 50 additions & 0 deletions vector/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package vector

// Apply applies eval to vecs. If any element of vecs is a Variant, Apply rips
// vecs accordingly, applies eval to the ripped vectors, and stitches the
// results together into a Variant. If ripUnions is true, Apply also rips
// Unions.
func Apply(ripUnions bool, eval func(...Any) Any, vecs ...Any) Any {
if ripUnions {
for k, vec := range vecs {
if union, ok := Under(vec).(*Union); ok {
vecs[k] = union.Variant
}
}
}
variant, ok := findVariant(vecs)
if !ok {
return eval(vecs...)
}
var results []Any
for _, ripped := range rip(vecs, variant.TagMap.Reverse) {
results = append(results, Apply(ripUnions, eval, ripped...))
}
// Stitch results together by creating a Variant.
return NewVariant(variant.Tags, results)
}

func findVariant(vecs []Any) (*Variant, bool) {
for _, vec := range vecs {
if variant, ok := vec.(*Variant); ok {
return variant, true
}
}
return nil, false
}

func rip(vecs []Any, reverse [][]uint32) [][]Any {
var ripped [][]Any
for j, rev := range reverse {
var newVecs []Any
for _, vec := range vecs {
if variant, ok := vec.(*Variant); ok {
newVecs = append(newVecs, variant.Values[j])
} else {
newVecs = append(newVecs, NewView(rev, vec))
}
}
ripped = append(ripped, newVecs)
}
return ripped
}