Merge branch 'improvesetmatches' into regexpopti

This commit is contained in:
Cyril Tovena 2021-10-07 14:05:14 +02:00
commit 54b3371060
No known key found for this signature in database
GPG key ID: FD8F768F9D633FB6
4 changed files with 14 additions and 3 deletions

View file

@ -111,6 +111,13 @@ func (m *Matcher) Inverse() (*Matcher, error) {
panic("labels.Matcher.Matches: invalid match type")
}
func (m *Matcher) GetRegexString() string {
if m.re == nil {
return ""
}
return m.re.GetRegexString()
}
// SetMatches returns a set of equality matchers for the current regex matchers if possible.
// For examples the regexp `a(b|f)` will returns "ab" and "af".
// Returns nil if we can't replace the regexp by only equality matchers.

View file

@ -61,6 +61,7 @@ func findSetMatches(re *syntax.Regexp, base string) []string {
if isCaseInsensitive(re) {
return nil
}
clearBeginEndText(re)
switch re.Op {
case syntax.OpLiteral:
return []string{base + string(re.Rune)}
@ -108,7 +109,6 @@ func findSetMatchesFromConcat(re *syntax.Regexp, base string) []string {
if len(re.Sub) == 0 {
return nil
}
clearBeginEndText(re)
clearCapture(re.Sub...)
matches := []string{base}
@ -209,6 +209,10 @@ func (m *FastRegexMatcher) SetMatches() []string {
return m.setMatches
}
func (m *FastRegexMatcher) GetRegexString() string {
return m.re.String()
}
// optimizeConcatRegex returns literal prefix/suffix text that can be safely
// checked against the label value before running the regexp matcher.
func optimizeConcatRegex(r *syntax.Regexp) (prefix, suffix, contains string) {

View file

@ -126,7 +126,7 @@ func TestFindSetMatches(t *testing.T) {
{"^foo$", []string{"foo"}},
// Simple sets alternates.
{"foo|bar|zz", []string{"foo", "bar", "zz"}},
// Simple sets alternate and concat (bar|baz is parsed as ba(r|z)).
// Simple sets alternate and concat (bar|baz is parsed as "ba[rz]").
{"foo|bar|baz", []string{"foo", "bar", "baz"}},
// Simple sets alternate and concat and capture
{"foo|bar|baz|(zz)", []string{"foo", "bar", "baz", "zz"}},

View file

@ -2042,7 +2042,7 @@ func TestPostingsForMatcher(t *testing.T) {
{
// Test case for double quoted regex matcher
matcher: labels.MustNewMatcher(labels.MatchRegexp, "test", "^(?:a|b)$"),
hasError: true,
hasError: false,
},
}