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 cidr_match() function #5592

Merged
merged 3 commits into from
Feb 5, 2025
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
12 changes: 10 additions & 2 deletions runtime/sam/expr/function/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,16 @@ var errMatch = errors.New("match")

func (c *CIDRMatch) Call(_ super.Allocator, args []super.Value) super.Value {
maskVal := args[0]
if maskVal.Type().ID() != super.IDNet {
return c.zctx.WrapError("cidr_match: not a net", maskVal)
if id := maskVal.Type().ID(); id != super.IDNet && id != super.IDNull {
val := c.zctx.WrapError("cidr_match: not a net", maskVal)
if maskVal.IsNull() {
val = super.NewValue(val.Type(), nil)
}
return val

}
if maskVal.IsNull() || args[1].IsNull() {
return super.NewValue(super.TypeBool, nil)
}
prefix := super.DecodeNet(maskVal.Bytes())
err := args[1].Walk(func(typ super.Type, body zcode.Bytes) error {
Expand Down
48 changes: 48 additions & 0 deletions runtime/vam/expr/function/cidrmatch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package function

import (
"github.com/brimdata/super"
"github.com/brimdata/super/runtime/vam/expr"
"github.com/brimdata/super/vector"
)

type CIDRMatch struct {
zctx *super.Context
pw *expr.PredicateWalk
}

func NewCIDRMatch(zctx *super.Context) *CIDRMatch {
return &CIDRMatch{zctx, expr.NewPredicateWalk(cidrMatch)}
}

func (c *CIDRMatch) Call(args ...vector.Any) vector.Any {
if id := args[0].Type().ID(); id != super.IDNet && id != super.IDNull {
out := vector.NewWrappedError(c.zctx, "cidr_match: not a net", args[0])
out.Nulls = vector.Or(vector.NullsOf(args[0]), vector.NullsOf(args[1]))
return out
}
return c.pw.Eval(args...)
}

func cidrMatch(vec ...vector.Any) vector.Any {
netVec, valVec := vec[0], vec[1]
nulls := vector.Or(vector.NullsOf(netVec), vector.NullsOf(valVec))
if id := valVec.Type().ID(); id != super.IDIP {
return vector.NewConst(super.False, valVec.Len(), nulls)
}
out := vector.NewBoolEmpty(valVec.Len(), nulls)
for i := range netVec.Len() {
net, null := vector.NetValue(netVec, i)
if null {
continue
}
ip, null := vector.IPValue(valVec, i)
if null {
continue
}
if net.Contains(ip) {
out.Set(i)
}
}
return out
}
4 changes: 4 additions & 0 deletions runtime/vam/expr/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func New(zctx *super.Context, name string, narg int) (expr.Function, field.Path,
f = &Bucket{zctx: zctx, name: name}
case "ceil":
f = &Ceil{zctx}
case "cidr_match":
argmin = 2
argmax = 2
f = NewCIDRMatch(zctx)
case "coalesce":
argmax = -1
f = &Coalesce{}
Expand Down
36 changes: 22 additions & 14 deletions runtime/vam/expr/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ type In struct {
zctx *super.Context
lhs Evaluator
rhs Evaluator
eq *Compare
pw *PredicateWalk
}

func NewIn(zctx *super.Context, lhs, rhs Evaluator) *In {
return &In{zctx, lhs, rhs, NewCompare(zctx, nil, nil, "==")}
return &In{zctx, lhs, rhs, NewPredicateWalk(NewCompare(zctx, nil, nil, "==").eval)}
}

func (i *In) Eval(this vector.Any) vector.Any {
Expand All @@ -229,10 +229,18 @@ func (i *In) eval(vecs ...vector.Any) vector.Any {
if rhs.Type().Kind() == super.ErrorKind {
return rhs
}
return i.evalResursive(lhs, rhs)
return i.pw.Eval(lhs, rhs)
}

func (i *In) evalResursive(vecs ...vector.Any) vector.Any {
type PredicateWalk struct {
pred func(...vector.Any) vector.Any
}

func NewPredicateWalk(pred func(...vector.Any) vector.Any) *PredicateWalk {
return &PredicateWalk{pred}
}

func (p *PredicateWalk) Eval(vecs ...vector.Any) vector.Any {
lhs, rhs := vecs[0], vecs[1]
rhs = vector.Under(rhs)
rhsOrig := rhs
Expand All @@ -248,32 +256,32 @@ func (i *In) evalResursive(vecs ...vector.Any) vector.Any {
if index != nil {
f = vector.NewView(f, index)
}
out = vector.Or(out, toBool(i.evalResursive(lhs, f)))
out = vector.Or(out, toBool(p.Eval(lhs, f)))
}
return out
case *vector.Array:
return i.evalForList(lhs, rhs.Values, rhs.Offsets, index)
return p.evalForList(lhs, rhs.Values, rhs.Offsets, index)
case *vector.Set:
return i.evalForList(lhs, rhs.Values, rhs.Offsets, index)
return p.evalForList(lhs, rhs.Values, rhs.Offsets, index)
case *vector.Map:
return vector.Or(i.evalForList(lhs, rhs.Keys, rhs.Offsets, index),
i.evalForList(lhs, rhs.Values, rhs.Offsets, index))
return vector.Or(p.evalForList(lhs, rhs.Keys, rhs.Offsets, index),
p.evalForList(lhs, rhs.Values, rhs.Offsets, index))
case *vector.Union:
if index != nil {
panic("vector.Union unexpected in vector.View")
}
return vector.Apply(true, i.evalResursive, lhs, rhs)
return vector.Apply(true, p.Eval, lhs, rhs)
case *vector.Error:
if index != nil {
panic("vector.Error unexpected in vector.View")
}
return i.evalResursive(lhs, rhs.Vals)
return p.Eval(lhs, rhs.Vals)
default:
return i.eq.eval(lhs, rhsOrig)
return p.pred(lhs, rhsOrig)
}
}

func (i *In) evalForList(lhs, rhs vector.Any, offsets, index []uint32) *vector.Bool {
func (p *PredicateWalk) evalForList(lhs, rhs vector.Any, offsets, index []uint32) *vector.Bool {
out := vector.NewBoolEmpty(lhs.Len(), nil)
var lhsIndex, rhsIndex []uint32
for j := range lhs.Len() {
Expand All @@ -293,7 +301,7 @@ func (i *In) evalForList(lhs, rhs vector.Any, offsets, index []uint32) *vector.B
}
lhsView := vector.NewView(lhs, lhsIndex)
rhsView := vector.NewView(rhs, rhsIndex)
if toBool(i.evalResursive(lhsView, rhsView)).TrueCount() > 0 {
if toBool(p.Eval(lhsView, rhsView)).TrueCount() > 0 {
out.Set(j)
}
}
Expand Down
38 changes: 38 additions & 0 deletions runtime/ztests/expr/function/cidr_match.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
zed: |
yield cidr_match(this[0], this[1])

vector: true

input: |
[1.1.0.0/16, 1.1.1.1]
[1.1.0.0/16, {a:1.1.1.1,b:2.2.2.2}]
[1.1.0.0/16, [2.2.2.2,1.1.1.1]]
[1.1.0.0/16, |[2.2.2.2,1.1.1.1]|]
[1.1.0.0/16, 2.2.2.2]
[1.1.0.0/16, {a:2.2.2.2,b:3.3.3.3}]
[1.1.0.0/16, [2.2.2.2,3.3.3.3]]
[1.1.0.0/16, null(ip)]
[1.1.0.0/16, null]
[1.1.0.0/16, null(string)]
[null(net), 1.1.1.1]
[null, null]
[null(string), 1.1.1.1]
[1.1.0.0/16, "s"]
["s", 1.1.1.1]

output: |
true
true
true
true
false
false
false
null(bool)
null(bool)
null(bool)
null(bool)
null(bool)
null(error({message:string,on:string}))
false
error({message:"cidr_match: not a net",on:"s"})