Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing problem on FilteredSelector when applying filter in series without filtered labels #163

Merged
merged 6 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ include .bingo/Variables.mk
FILES_TO_FMT ?= $(shell find . -path ./vendor -prune -o -name '*.go' -print)
MDOX_VALIDATE_CONFIG ?= .mdox.validate.yaml

# if macos, use gsed
SED ?= $(shell which gsed 2>/dev/null || which sed)


define require_clean_work_tree
@git update-index -q --ignore-submodules --refresh

Expand Down
28 changes: 28 additions & 0 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,34 @@ func TestQueriesAgainstOldEngine(t *testing.T) {
+ on() group_left()
sum(http_requests_total{ns="nginx"})`,
},
{
name: "binop with positive matcher using regex, only one side has data",
load: `load 30s
metric{} 1+2x5
metric{} 1+2x20`,
query: `sum(rate(metric{err=~".+"}[5m])) / sum(rate(metric{}[5m]))`,
},
{
name: "binop with positive matcher using regex, both sides have data",
load: `load 30s
metric{} 1+2x5
metric{err="FooBarKey"} 1+2x20`,
query: `sum(rate(metric{err=~".+"}[5m])) / sum(rate(metric{}[5m]))`,
},
{
name: "binop with negative matcher using regex, only one side has data",
load: `load 30s
metric{} 1+2x5
metric{} 1+2x20`,
query: `sum(rate(metric{err!~".+"}[5m])) / sum(rate(metric{}[5m]))`,
},
{
name: "binop with negative matcher using regex, both sides have data",
load: `load 30s
metric{} 1+2x5
metric{err="FooBarKey"} 1+2x20`,
query: `sum(rate(metric{err!~".+"}[5m])) / sum(rate(metric{}[5m]))`,
},
{
name: "scalar func with NaN",
load: `load 30s
Expand Down
16 changes: 7 additions & 9 deletions execution/storage/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@ func (f filter) Matches(series storage.Series) bool {
if len(f.matcherSet) == 0 {
return true
}

for _, l := range series.Labels() {
m, ok := f.matcherSet[l.Name]
if !ok {
continue
}
if !m.Matches(l.Value) {
return false
hasMatch := true
for name, m := range f.matcherSet {
label := series.Labels().Get(name)
if !m.Matches(label) {
hasMatch = false
break
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can return false immediately here.

}
}

return true
return hasMatch
}
88 changes: 88 additions & 0 deletions execution/storage/filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) The Thanos Community Authors.
// Licensed under the Apache License 2.0.

package storage_test

import (
"testing"

"github.com/prometheus/prometheus/model/labels"
promstg "github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/tsdb/chunkenc"

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

func TestFilter_Matches(t *testing.T) {
testCases := []struct {
name string
matchers []*labels.Matcher
series promstg.Series
expected bool
}{
{
name: "empty matchers",
matchers: []*labels.Matcher{},
series: &mockLabelSeries{labels: labels.FromStrings("foo", "bar")},
expected: true,
},
{
name: "no match",
matchers: []*labels.Matcher{labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")},
series: &mockLabelSeries{labels: labels.FromStrings("foo", "baz")},
},
{
name: "regex match",
matchers: []*labels.Matcher{labels.MustNewMatcher(labels.MatchRegexp, "foo", "ba.")},
series: &mockLabelSeries{labels: labels.FromStrings("foo", "bar")},
expected: true,
},
{
name: "regex no match",
matchers: []*labels.Matcher{labels.MustNewMatcher(labels.MatchRegexp, "foo", "ba.")},
series: &mockLabelSeries{labels: labels.FromStrings("foo", "nope")},
},
{
name: "multiple matchers",
matchers: []*labels.Matcher{labels.MustNewMatcher(labels.MatchEqual, "foo", "bar"), labels.MustNewMatcher(labels.MatchEqual, "baz", "qux")},
series: &mockLabelSeries{labels: labels.FromStrings("foo", "bar", "baz", "qux")},
expected: true,
},
{
name: "single regex matcher, with label name not present",
matchers: []*labels.Matcher{labels.MustNewMatcher(labels.MatchRegexp, "foo", ".*")},
series: &mockLabelSeries{labels: labels.FromStrings("bar", "baz")},
expected: true,
},
{
name: "single regex matcher, with label name not present, negative regex",
matchers: []*labels.Matcher{labels.MustNewMatcher(labels.MatchNotRegexp, "foo", ".*")},
series: &mockLabelSeries{labels: labels.FromStrings("bar", "baz")},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
f := storage.NewFilter(tc.matchers)
if got := f.Matches(tc.series); got != tc.expected {
if tc.expected {
t.Errorf("expected %s to match %s, but it did not.", tc.series.Labels().String(), tc.matchers)
} else {
t.Errorf("expected %s to not match %s, but it did.", tc.series.Labels().String(), tc.matchers)
}
}
})
}
}

type mockLabelSeries struct {
labels labels.Labels
}

func (s *mockLabelSeries) Labels() labels.Labels {
return s.labels
}

func (s *mockLabelSeries) Iterator() chunkenc.Iterator {
return nil
}