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>
pull/15475/head
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
1 changed files with 14 additions and 1 deletions

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