Repository: prometheus/prometheus
File: tsdb/querier.go (Go)

Code before:
		its = append(its, allPostings)
	}

	// Sort matchers to have the intersecting matchers first.
	// This way the base for subtraction is smaller and
	// there is no chance that the set we subtract from
	// contains postings of series that didn't exist when
	// we constructed the set we subtract by.
	slices.SortStableFunc(ms, func(i, j *labels.Matcher) int {
		if !isSubtractingMatcher(i) && isSubtractingMatcher(j) {
			return -1
		}

		return +1
	})

	for _, m := range ms {
		if ctx.Err() != nil {
			return nil, ctx.Err()
		}
		switch {
		case m.Name == "" && m.Value == "":
			// 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 labelMustBeSet[m.Name]:
			// If this matcher must be non-empty, we can be smarter.
			matchesEmpty := m.Matches("")
			isNot := m.Type == labels.MatchNotEqual || m.Type == labels.MatchNotRegexp
			switch {
			case isNot && matchesEmpty: // l!="foo"
				// If the label can't be empty and is a Not and the inner matcher
				// doesn't match empty, then subtract it out at the end.
				inverse, err := m.Inverse()
				if err != nil {
					return nil, err
				}

				it, err := postingsForMatcher(ctx, ix, inverse)
				if err != nil {
					return nil, err
				}
				notIts = append(notIts, it)
			case isNot && !matchesEmpty: // l!=""
				// If the label can't be empty and is a Not, but the inner matcher can
				// be empty we need to use inversePostingsForMatcher.
				inverse, err := m.Inverse()

Review comment (suggestion):
Just a nit, but I'd prefer staying consistent and not adding blank lines between the `switch` branches.

Code after:
		its = append(its, allPostings)
	}

	// Sort matchers to have the intersecting matchers first.
	// This way the base for subtraction is smaller and
	// there is no chance that the set we subtract from
	// contains postings of series that didn't exist when
	// we constructed the set we subtract by.
	slices.SortStableFunc(ms, func(i, j *labels.Matcher) int {
		if !isSubtractingMatcher(i) && isSubtractingMatcher(j) {
			return -1
		}

		return +1
	})

	for _, m := range ms {
		if ctx.Err() != nil {
			return nil, ctx.Err()
		}
		switch {
		case m.Name == "" && m.Value == "":
			// 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("")
			isNot := m.Type == labels.MatchNotEqual || m.Type == labels.MatchNotRegexp
			switch {
			case isNot && matchesEmpty: // l!="foo"
				// If the label can't be empty and is a Not and the inner matcher
				// doesn't match empty, then subtract it out at the end.
				inverse, err := m.Inverse()