Skip to content

Commit

Permalink
Simplify vam/expr.Compare and align with Arith (#5151)
Browse files Browse the repository at this point in the history
* Simplify gencomparefuncs.go to use a single loop template

* Move the vector.Const x vector.Const case into gencomparefuncs.go

* Align names and style in Compare and gencomparefuncs.go with Arith and
  genarithfuncs.go
  • Loading branch information
nwt authored Jun 28, 2024
1 parent a1052a0 commit dfdf3e6
Show file tree
Hide file tree
Showing 4 changed files with 1,471 additions and 1,500 deletions.
50 changes: 10 additions & 40 deletions runtime/vam/expr/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,33 @@ package expr

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

type Compare struct {
zctx *zed.Context
op string
lhs Evaluator
rhs Evaluator
zctx *zed.Context
opCode int
lhs Evaluator
rhs Evaluator
}

func NewCompare(zctx *zed.Context, lhs, rhs Evaluator, operator string) *Compare {
return &Compare{zctx, operator, lhs, rhs}
func NewCompare(zctx *zed.Context, lhs, rhs Evaluator, op string) *Compare {
return &Compare{zctx, vector.CompareOpFromString(op), lhs, rhs}
}

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

func (c *Compare) compare(lhs, rhs vector.Any) vector.Any {
func (c *Compare) eval(lhs, rhs vector.Any) vector.Any {
lhs = vector.Under(lhs)
rhs = vector.Under(rhs)
lhs, rhs, _ = coerceVals(c.zctx, lhs, rhs)
//XXX need to handle overflow (see sam)
//XXX unions and variants and single-value-with-error variant
//XXX nulls... for primitives we just do the compare but we need
// to or the nulls together
if lc, ok := lhs.(*vector.Const); ok {
if rc, ok := rhs.(*vector.Const); ok {
return compareConsts(c.op, lc, rc)
}
}
kind := vector.KindOf(lhs)
if kind != vector.KindOf(rhs) {
panic("vector kind mismatch after coerce")
Expand All @@ -50,32 +43,9 @@ func (c *Compare) compare(lhs, rhs vector.Any) vector.Any {
if !ok {
return vector.NewStringError(c.zctx, coerce.ErrIncompatibleTypes.Error(), lhs.Len())
}
compare, ok := compareFuncs[vector.CompareOpCode(c.op, kind, lform, rform)]
f, ok := compareFuncs[vector.FuncCode(c.opCode, kind, lform, rform)]
if !ok {
return vector.NewStringError(c.zctx, coerce.ErrIncompatibleTypes.Error(), lhs.Len())
}
return compare(lhs, rhs)
}

func compareConsts(op string, lhs, rhs *vector.Const) vector.Any {
compare := expr.NewValueCompareFn(order.Asc, false)
compareResult := compare(lhs.Value(), rhs.Value())
var result bool
switch op {
case "==":
result = compareResult == 0
case "!=":
result = compareResult != 0
case "<":
result = compareResult < 0
case "<=":
result = compareResult <= 0
case ">":
result = compareResult > 0
case ">=":
result = compareResult >= 0
default:
panic(op)
}
return vector.NewConst(nil, zed.NewBool(result), lhs.Len(), nil)
return f(lhs, rhs)
}
Loading

0 comments on commit dfdf3e6

Please sign in to comment.