Merge pull request #578 from grafana/logiraptor/fix-set-matches-for-regexp-literal

Fix regexp set matches for literal matchers
This commit is contained in:
Patrick Oyarzun 2024-01-03 05:59:24 -06:00 committed by GitHub
commit ae280910a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 1 deletions

View file

@ -366,7 +366,7 @@ func optimizeAlternatingLiterals(s string) (StringMatcher, []string) {
// If there are no alternates, check if the string is a literal // If there are no alternates, check if the string is a literal
if estimatedAlternates == 1 { if estimatedAlternates == 1 {
if regexp.QuoteMeta(s) == s { if regexp.QuoteMeta(s) == s {
return &equalStringMatcher{s: s, caseSensitive: true}, nil return &equalStringMatcher{s: s, caseSensitive: true}, []string{s}
} }
return nil, nil return nil, nil
} }

View file

@ -342,6 +342,14 @@ func TestFindSetMatches(t *testing.T) {
matches, actualCaseSensitive := findSetMatches(parsed) matches, actualCaseSensitive := findSetMatches(parsed)
require.Equal(t, c.expMatches, matches) require.Equal(t, c.expMatches, matches)
require.Equal(t, c.expCaseSensitive, actualCaseSensitive) require.Equal(t, c.expCaseSensitive, actualCaseSensitive)
if c.expCaseSensitive {
// When the regexp is case sensitive, we want to ensure that the
// set matches are maintained in the final matcher.
r, err := newFastRegexMatcherWithoutCache(c.pattern)
require.NoError(t, err)
require.Equal(t, c.expMatches, r.SetMatches())
}
}) })
} }
} }

View file

@ -125,6 +125,7 @@ func benchmarkPostingsForMatchers(b *testing.B, ir IndexReader) {
iNotAlternate := labels.MustNewMatcher(labels.MatchNotRegexp, "i", "(1|2|3|4|5|6|20|55)") iNotAlternate := labels.MustNewMatcher(labels.MatchNotRegexp, "i", "(1|2|3|4|5|6|20|55)")
iXYZ := labels.MustNewMatcher(labels.MatchRegexp, "i", "X|Y|Z") iXYZ := labels.MustNewMatcher(labels.MatchRegexp, "i", "X|Y|Z")
iNotXYZ := labels.MustNewMatcher(labels.MatchNotRegexp, "i", "X|Y|Z") iNotXYZ := labels.MustNewMatcher(labels.MatchNotRegexp, "i", "X|Y|Z")
literalRegexp := labels.MustNewMatcher(labels.MatchRegexp, "i_times_n", "0")
cases := []struct { cases := []struct {
name string name string
matchers []*labels.Matcher matchers []*labels.Matcher
@ -168,6 +169,7 @@ func benchmarkPostingsForMatchers(b *testing.B, ir IndexReader) {
{`n="1",i=~".+",i!~"2.*",j="foo"`, []*labels.Matcher{n1, iPlus, iNot2Star, jFoo}}, {`n="1",i=~".+",i!~"2.*",j="foo"`, []*labels.Matcher{n1, iPlus, iNot2Star, jFoo}},
{`n="1",i=~".+",i!~".*2.*",j="foo"`, []*labels.Matcher{n1, iPlus, iNotStar2Star, jFoo}}, {`n="1",i=~".+",i!~".*2.*",j="foo"`, []*labels.Matcher{n1, iPlus, iNotStar2Star, jFoo}},
{`n="X",i=~".+",i!~".*2.*",j="foo"`, []*labels.Matcher{nX, iPlus, iNotStar2Star, jFoo}}, {`n="X",i=~".+",i!~".*2.*",j="foo"`, []*labels.Matcher{nX, iPlus, iNotStar2Star, jFoo}},
{`i_times_n=~"0"`, []*labels.Matcher{literalRegexp}},
} }
for _, c := range cases { for _, c := range cases {