-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement vector switch operator (#5538)
- Loading branch information
Showing
13 changed files
with
232 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package op | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/brimdata/super" | ||
"github.com/brimdata/super/runtime/vam/expr" | ||
"github.com/brimdata/super/vector" | ||
"github.com/brimdata/super/zcode" | ||
) | ||
|
||
type ExprSwitch struct { | ||
expr expr.Evaluator | ||
router *router | ||
|
||
builder zcode.Builder | ||
cases map[string]*route | ||
caseIndexes map[*route][]uint32 | ||
defaultRoute *route | ||
} | ||
|
||
func NewExprSwitch(ctx context.Context, parent vector.Puller, e expr.Evaluator) *ExprSwitch { | ||
s := &ExprSwitch{expr: e, cases: map[string]*route{}, caseIndexes: map[*route][]uint32{}} | ||
s.router = newRouter(ctx, s, parent) | ||
return s | ||
} | ||
|
||
func (s *ExprSwitch) AddCase(val *super.Value) vector.Puller { | ||
r := s.router.addRoute() | ||
if val == nil { | ||
s.defaultRoute = r | ||
} else { | ||
s.cases[string(val.Bytes())] = r | ||
} | ||
return r | ||
} | ||
|
||
func (s *ExprSwitch) forward(vec vector.Any) bool { | ||
defer clear(s.caseIndexes) | ||
exprVec := s.expr.Eval(vec) | ||
for i := range exprVec.Len() { | ||
s.builder.Truncate() | ||
exprVec.Serialize(&s.builder, i) | ||
route, ok := s.cases[string(s.builder.Bytes().Body())] | ||
if !ok { | ||
route = s.defaultRoute | ||
} | ||
if route != nil { | ||
s.caseIndexes[route] = append(s.caseIndexes[route], i) | ||
} | ||
} | ||
for route, index := range s.caseIndexes { | ||
view := vector.NewView(vec, index) | ||
if !route.send(view, nil) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package op | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/RoaringBitmap/roaring" | ||
"github.com/brimdata/super" | ||
"github.com/brimdata/super/runtime/vam/expr" | ||
"github.com/brimdata/super/vector" | ||
) | ||
|
||
type Switch struct { | ||
router *router | ||
cases []expr.Evaluator | ||
} | ||
|
||
func NewSwitch(ctx context.Context, parent vector.Puller) *Switch { | ||
s := &Switch{} | ||
s.router = newRouter(ctx, s, parent) | ||
return s | ||
} | ||
|
||
func (s *Switch) AddCase(e expr.Evaluator) vector.Puller { | ||
s.cases = append(s.cases, e) | ||
return s.router.addRoute() | ||
} | ||
|
||
func (s *Switch) forward(vec vector.Any) bool { | ||
doneMap := roaring.New() | ||
for i, c := range s.cases { | ||
maskVec := c.Eval(vec) | ||
boolMap, errMap := expr.BoolMask(maskVec) | ||
boolMap.AndNot(doneMap) | ||
errMap.AndNot(doneMap) | ||
doneMap.Or(boolMap) | ||
if !errMap.IsEmpty() { | ||
// Clone because iteration results are undefined if the bitmap is modified. | ||
for it := errMap.Clone().Iterator(); it.HasNext(); { | ||
i := it.Next() | ||
if isErrorMissing(maskVec, i) { | ||
errMap.Remove(i) | ||
} | ||
} | ||
} | ||
var vec2 vector.Any | ||
if errMap.IsEmpty() { | ||
if boolMap.IsEmpty() { | ||
continue | ||
} | ||
vec2 = vector.NewView(vec, boolMap.ToArray()) | ||
} else if boolMap.IsEmpty() { | ||
vec2 = vector.NewView(maskVec, errMap.ToArray()) | ||
} else { | ||
valIndex := boolMap.ToArray() | ||
errIndex := errMap.ToArray() | ||
tags := make([]uint32, 0, len(valIndex)+len(errIndex)) | ||
for len(valIndex) > 0 && len(errIndex) > 0 { | ||
if valIndex[0] < errIndex[0] { | ||
valIndex = valIndex[1:] | ||
tags = append(tags, 0) | ||
} else { | ||
errIndex = errIndex[1:] | ||
tags = append(tags, 1) | ||
} | ||
} | ||
tags = append(tags, valIndex...) | ||
tags = append(tags, errIndex...) | ||
valVec := vector.NewView(vec, valIndex) | ||
errVec := vector.NewView(maskVec, errIndex) | ||
vec2 = vector.NewDynamic(tags, []vector.Any{valVec, errVec}) | ||
} | ||
if !s.router.routes[i].send(vec2, nil) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func isErrorMissing(vec vector.Any, i uint32) bool { | ||
vec = vector.Under(vec) | ||
if dynVec, ok := vec.(*vector.Dynamic); ok { | ||
vec = dynVec.Values[dynVec.Tags[i]] | ||
i = dynVec.TagMap.Forward[i] | ||
} | ||
errVec, ok := vec.(*vector.Error) | ||
if !ok { | ||
return false | ||
} | ||
if errVec.Vals.Type().ID() != super.IDString { | ||
return false | ||
} | ||
s, null := vector.StringValue(errVec.Vals, i) | ||
return !null && s == string(super.Missing) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ zed: | | |
case this==3 => yield 4 | ||
) | ||
vector: true | ||
|
||
input: | | ||
1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ zed: | | |
default => pass | ||
) |> sort b | ||
vector: true | ||
|
||
input: | | ||
{a:1,b:1} | ||
{a:2,b:2} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ zed: | | |
case 3 => ? null | ||
) |> sort a | ||
vector: true | ||
|
||
input: | | ||
{a:1(int32),s:"a"} | ||
{a:2(int32),s:"B"} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters