-
Notifications
You must be signed in to change notification settings - Fork 67
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 switch operator #5538
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need to truncate
caseIndexes
each time you get a new vector.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch. Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in afa044f and e8bc7ef.