Skip to content

Commit

Permalink
vam: type value comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnibs committed Nov 21, 2024
1 parent c06e81a commit e090566
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
24 changes: 24 additions & 0 deletions runtime/vam/expr/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package expr
//go:generate go run gencomparefuncs.go

import (
"bytes"

"github.com/brimdata/super"
"github.com/brimdata/super/runtime/sam/expr/coerce"
"github.com/brimdata/super/vector"
Expand Down Expand Up @@ -43,6 +45,9 @@ func (c *Compare) eval(vecs ...vector.Any) vector.Any {
if kind != vector.KindOf(rhs) {
panic("vector kind mismatch after coerce")
}
if kind == vector.KindType {
return c.compareTypeVals(lhs, rhs)
}
lform, ok := vector.FormOf(lhs)
if !ok {
return vector.NewStringError(c.zctx, coerce.ErrIncompatibleTypes.Error(), lhs.Len())
Expand All @@ -60,6 +65,25 @@ func (c *Compare) eval(vecs ...vector.Any) vector.Any {
return out
}

func (c *Compare) compareTypeVals(lhs, rhs vector.Any) vector.Any {
if c.opCode == vector.CompLT || c.opCode == vector.CompGT {
return vector.NewConst(super.False, lhs.Len(), nil)
}
out := vector.NewBoolEmpty(lhs.Len(), nil)
for i := range lhs.Len() {
l, _ := vector.TypeValueValue(lhs, i)
r, _ := vector.TypeValueValue(rhs, i)
v := bytes.Equal(l, r)
if c.opCode == vector.CompNE {
v = !v
}
if v {
out.Set(i)
}
}
return out
}

type isNull struct {
expr Evaluator
}
Expand Down
16 changes: 16 additions & 0 deletions runtime/ztests/expr/compare-typevals.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
zed: yield a == b, a != b

vector: true

input: |
{a:<string>,b:<string>}
{a:<{x:ip}>,b:<{x:ip}>}
{a:<{x:net}>,b:<{y:net}>}
output: |
true
false
true
false
false
true
4 changes: 4 additions & 0 deletions vector/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func KindOf(v Any) Kind {
return KindBytes
case *String:
return KindString
case *TypeValue:
return KindType
case *Dict:
return KindOf(v.Any)
case *View:
Expand All @@ -63,6 +65,8 @@ func KindFromString(v string) Kind {
return KindBytes
case "String":
return KindString
case "TypeValue":
return KindType
default:
return KindInvalid
}
Expand Down

0 comments on commit e090566

Please sign in to comment.