-
Notifications
You must be signed in to change notification settings - Fork 11
perf: improve wildcard query perf with predicate and contains-check pushdown #397
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
Open
cheb0
wants to merge
6
commits into
main
Choose a base branch
from
0-wildcard-predicate-pushdown
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0fa1d0e
pushdown predicate for wildcards
cheb0 cee0a60
lint fixes
cheb0 c1219b9
review fixes
cheb0 432b592
Merge branch 'main' into 0-wildcard-predicate-pushdown
cheb0 29ff016
remove lastTID, firstTID from FindContains params
cheb0 739974d
fix pattern_test.go
cheb0 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ import ( | |
|
|
||
| type tokenProvider interface { | ||
| GetToken(uint32) []byte | ||
| FindContains(firstTID uint32, lastTID uint32, needle []byte) ([]uint32, error) | ||
| FindToken(searcher Searcher) ([]uint32, error) | ||
| FirstTID() uint32 | ||
| LastTID() uint32 | ||
| Ordered() bool | ||
|
|
@@ -27,11 +29,11 @@ type baseSearch struct { | |
| last int | ||
| } | ||
|
|
||
| func (s *baseSearch) firstTID() uint32 { | ||
| func (s *baseSearch) FirstTID() uint32 { | ||
| return uint32(s.first) | ||
| } | ||
|
|
||
| func (s *baseSearch) lastTID() uint32 { | ||
| func (s *baseSearch) LastTID() uint32 { | ||
| return uint32(s.last) | ||
| } | ||
|
|
||
|
|
@@ -67,7 +69,7 @@ func (s *literalSearch) Narrow(tp tokenProvider) { | |
| s.last = s.first - 1 // begin > end: will be considered empty | ||
| } | ||
|
|
||
| func (s *literalSearch) check(val []byte) (bool, error) { | ||
| func (s *literalSearch) Check(val []byte) (bool, error) { | ||
| if s.narrowed { | ||
| return len(s.value) == len(val), nil | ||
| } | ||
|
|
@@ -165,7 +167,7 @@ func findSequence(haystack []byte, needles [][]byte) int { | |
| return len(needles) | ||
| } | ||
|
|
||
| func (s *wildcardSearch) check(val []byte) (bool, error) { | ||
| func (s *wildcardSearch) Check(val []byte) (bool, error) { | ||
| return s.checkPrefix(val) && s.checkSuffix(val) && s.checkMiddle(val), nil | ||
| } | ||
|
|
||
|
|
@@ -181,7 +183,7 @@ func newRangeTextSearch(base baseSearch, token *parser.Range) *rangeTextSearch { | |
| } | ||
| } | ||
|
|
||
| func (s *rangeTextSearch) check(val []byte) (bool, error) { | ||
| func (s *rangeTextSearch) Check(val []byte) (bool, error) { | ||
| valStr := string(val) | ||
| if s.token.From.Kind != parser.TermSymbol { | ||
| if s.token.IncludeFrom { | ||
|
|
@@ -244,7 +246,7 @@ func newRangeNumberSearch(base baseSearch, token *parser.Range) *rangeNumberSear | |
| return s | ||
| } | ||
|
|
||
| func (s *rangeNumberSearch) check(rawVal []byte) (bool, error) { | ||
| func (s *rangeNumberSearch) Check(rawVal []byte) (bool, error) { | ||
| val, err := strconv.ParseFloat(string(rawVal), 64) | ||
| if err != nil || isNaNOrInf(val) { | ||
| return false, nil | ||
|
|
@@ -301,7 +303,7 @@ func newRangeIPSearch(base baseSearch, token *parser.IPRange) *rangeIpSearch { | |
| return s | ||
| } | ||
|
|
||
| func (s *rangeIpSearch) check(rawVal []byte) (bool, error) { | ||
| func (s *rangeIpSearch) Check(rawVal []byte) (bool, error) { | ||
| val, err := netip.ParseAddr(string(rawVal)) | ||
| if err != nil { | ||
| return false, nil | ||
|
|
@@ -324,7 +326,7 @@ func newReSearch(base baseSearch, token *parser.Re) *reSearch { | |
| return &reSearch{baseSearch: base, r: token.CompiledExpression} | ||
| } | ||
|
|
||
| func (s *reSearch) check(rawVal []byte) (bool, error) { | ||
| func (s *reSearch) Check(rawVal []byte) (bool, error) { | ||
| if config.MaxRegexTokensCheck > 0 && s.checked >= config.MaxRegexTokensCheck { | ||
| return false, errors.New( | ||
| "'re' filter exceeded token limit: " + | ||
|
|
@@ -335,13 +337,13 @@ func (s *reSearch) check(rawVal []byte) (bool, error) { | |
| return s.r.Match(rawVal), nil | ||
| } | ||
|
|
||
| type searcher interface { | ||
| firstTID() uint32 | ||
| lastTID() uint32 | ||
| check(val []byte) (bool, error) | ||
| type Searcher interface { | ||
| FirstTID() uint32 | ||
| LastTID() uint32 | ||
| Check(val []byte) (bool, error) | ||
| } | ||
|
|
||
| func newSearcher(token parser.Token, tp tokenProvider) searcher { | ||
| func newSearcher(token parser.Token, tp tokenProvider) Searcher { | ||
| base := baseSearch{ | ||
| first: int(tp.FirstTID()), | ||
| last: int(tp.LastTID()), | ||
|
|
@@ -390,22 +392,25 @@ func isNaNOrInf(f float64) bool { | |
| return math.IsNaN(f) || math.IsInf(f, 0) | ||
| } | ||
|
|
||
| func Search(ctx context.Context, t parser.Token, tp tokenProvider) ([]uint32, error) { | ||
| tids := []uint32{} | ||
| s := newSearcher(t, tp) | ||
| for tid := s.firstTID(); tid <= s.lastTID(); tid++ { | ||
| if tid&1023 == 0 && util.IsCancelled(ctx) { | ||
| return nil, ctx.Err() | ||
| } | ||
|
|
||
| match, err := s.check(tp.GetToken(tid)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| // isSimpleWildcardContains checks if this AST token is simple wildcard like 'foo:*bar*' | ||
| func isSimpleWildcardContains(token parser.Token) (needle []byte, ok bool) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a comment with an example of an expression that fits/matches this check |
||
| lit, ok := token.(*parser.Literal) | ||
| if !ok || len(lit.Terms) != 3 { | ||
| return nil, false | ||
| } | ||
| if !lit.Terms[0].IsWildcard() || lit.Terms[1].Kind != parser.TermText || !lit.Terms[2].IsWildcard() { | ||
| return nil, false | ||
| } | ||
| return []byte(lit.Terms[1].Data), true | ||
| } | ||
|
|
||
| if match { | ||
| tids = append(tids, tid) | ||
| } | ||
| func Search(ctx context.Context, t parser.Token, tp tokenProvider) ([]uint32, error) { | ||
| if util.IsCancelled(ctx) { | ||
| return nil, ctx.Err() | ||
| } | ||
| if needle, ok := isSimpleWildcardContains(t); ok { | ||
| return tp.FindContains(tp.FirstTID(), tp.LastTID(), needle) | ||
| } | ||
| return tids, nil | ||
| s := newSearcher(t, tp) | ||
| return tp.FindToken(s) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Why did you decide to make
firstTIDandlastTIDa part of an API?Seems like for this specific case (e.g. query
foo:'*bar*') we cannot narrow the TID search boundaries.And now we always pass the first and last TID in this method:
https://github.com/ozontech/seq-db/blob/0-wildcard-predicate-pushdown/pattern/pattern.go#L411
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.
For
foo:'*bar*'we narrow down to all tokens offoofield, i.e.foo:?tokens. If there are 50 such tokens only, we will check only a single block, andfirstTIDmight be like 1000 andlastTID1050Uh oh!
There was an error while loading. Please reload this page.
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.
Yeah, I see. But for specific case of
FindContains()argumentsfirstLID,lastLIDshould not be a part of an API. Here, take a look, we already have all necessary information inProvider: