Skip to content

Commit

Permalink
execution/filter: allow multiple matchers with same name (#320)
Browse files Browse the repository at this point in the history
It is entirely valid to have multiple matchers with the same name e.g.
`phase!="Running", phase!="Succeeded`.

See #312.

Signed-off-by: Giedrius Statkevičius <[email protected]>
  • Loading branch information
GiedriusS authored Oct 13, 2023
1 parent 8605b6a commit 4517c0d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
15 changes: 9 additions & 6 deletions execution/storage/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ func (n nopFilter) Matches(storage.Series) bool { return true }

type filter struct {
matchers []*labels.Matcher
matcherSet map[string]*labels.Matcher
matcherSet map[string][]*labels.Matcher
}

func NewFilter(matchers []*labels.Matcher) Filter {
if len(matchers) == 0 {
return &nopFilter{}
}

matcherSet := make(map[string]*labels.Matcher)
matcherSet := make(map[string][]*labels.Matcher)
for _, m := range matchers {
matcherSet[m.Name] = m
matcherSet[m.Name] = append(matcherSet[m.Name], m)
}
return &filter{
matchers: matchers,
Expand All @@ -46,10 +46,13 @@ func (f filter) Matches(series storage.Series) bool {
return true
}

for name, m := range f.matcherSet {
for name, matchers := range f.matcherSet {
label := series.Labels().Get(name)
if !m.Matches(label) {
return false

for _, m := range matchers {
if !m.Matches(label) {
return false
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions execution/storage/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@ import (
"github.com/prometheus/prometheus/model/labels"
promstg "github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/tsdb/chunkenc"
"github.com/stretchr/testify/require"

"github.com/thanos-io/promql-engine/execution/storage"
)

func TestFilter_MultipleMatcherWithSameName(t *testing.T) {
f := storage.NewFilter([]*labels.Matcher{
labels.MustNewMatcher(labels.MatchNotEqual, "phase", "Running"),
labels.MustNewMatcher(labels.MatchNotEqual, "phase", "Succeeded"),
})

require.Equal(t, false, f.Matches(&mockLabelSeries{labels: labels.FromStrings("phase", "Running")}))
}

func TestFilter_Matches(t *testing.T) {
testCases := []struct {
name string
Expand Down

0 comments on commit 4517c0d

Please sign in to comment.