Optimize l=~".+" matcher (#15474)

Since dot is matching newline now, `l=~".+"` is "any non empty label
value", and #14144 added a specific method in the index for that so we
don't need to run the matcher on each one of the label values.

Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>
This commit is contained in:
Oleg Zaytsev 2024-11-27 12:33:20 +01:00 committed by GitHub
parent 4dacd7572a
commit 9ad93ba8df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -256,10 +256,23 @@ func PostingsForMatchers(ctx context.Context, ix IndexReader, ms ...*labels.Matc
// We already handled the case at the top of the function,
// and it is unexpected to get all postings again here.
return nil, errors.New("unexpected all postings")
case m.Type == labels.MatchRegexp && m.Value == ".*":
// .* regexp matches any string: do nothing.
case m.Type == labels.MatchNotRegexp && m.Value == ".*":
return index.EmptyPostings(), nil
case m.Type == labels.MatchRegexp && m.Value == ".+":
// .+ regexp matches any non-empty string: get postings for all label values.
it := ix.PostingsForAllLabelValues(ctx, m.Name)
if index.IsEmptyPostingsType(it) {
return index.EmptyPostings(), nil
}
its = append(its, it)
case m.Type == labels.MatchNotRegexp && m.Value == ".+":
// .+ regexp matches any non-empty string: get postings for all label values and remove them.
its = append(notIts, ix.PostingsForAllLabelValues(ctx, m.Name))
case labelMustBeSet[m.Name]:
// If this matcher must be non-empty, we can be smarter.
matchesEmpty := m.Matches("")
@ -294,7 +307,7 @@ func PostingsForMatchers(ctx context.Context, ix IndexReader, ms ...*labels.Matc
return index.EmptyPostings(), nil
}
its = append(its, it)
default: // l="a"
default: // l="a", l=~"a|b", l=~"a.b", etc.
// Non-Not matcher, use normal postingsForMatcher.
it, err := postingsForMatcher(ctx, ix, m)
if err != nil {