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

Optimize empty filter queries. #1810

Merged
merged 3 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion pkg/logql/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ func (e *filterExpr) Filter() (LineFilter, error) {
if err != nil {
return nil, err
}
return newAndFilter(nextFilter, f), nil
f = newAndFilter(nextFilter, f)
}
if f == TrueFilter {
return nil, nil
}
return f, nil
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/logql/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ func Test_logSelectorExpr_String(t *testing.T) {
{`{foo="bar", bar!="baz"}`, false},
{`{foo="bar", bar!="baz"} != "bip" !~ ".+bop"`, true},
{`{foo="bar"} |= "baz" |~ "blip" != "flip" !~ "flap"`, true},
{`{foo="bar", bar!="baz"} |= ""`, false},
{`{foo="bar", bar!="baz"} |~ ""`, false},
{`{foo="bar", bar!="baz"} |~ ".*"`, false},
{`{foo="bar", bar!="baz"} |= "" |= ""`, false},
{`{foo="bar", bar!="baz"} |~ "" |= "" |~ ".*"`, false},
}

for _, tt := range tests {
Expand Down
31 changes: 28 additions & 3 deletions pkg/logql/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ func (f LineFilterFunc) Filter(line []byte) bool {
return f(line)
}

type trueFilter struct{}

func (trueFilter) Filter(_ []byte) bool { return true }

// TrueFilter is a filter that returns and matches all log lines whatever their content.
var TrueFilter = trueFilter{}

type notFilter struct {
LineFilter
}
Expand All @@ -46,6 +53,9 @@ type andFilter struct {

// newAndFilter creates a new filter which matches only if left and right matches.
func newAndFilter(left LineFilter, right LineFilter) LineFilter {
if (right == TrueFilter || right == nil) && (left == TrueFilter || left == nil) {
cyriltovena marked this conversation as resolved.
Show resolved Hide resolved
return TrueFilter
}
return andFilter{
left: left,
right: right,
Expand All @@ -63,6 +73,9 @@ type orFilter struct {

// newOrFilter creates a new filter which matches only if left or right matches.
func newOrFilter(left LineFilter, right LineFilter) LineFilter {
if (right == TrueFilter || right == nil) && (left == TrueFilter || left == nil) {
return TrueFilter
}
return orFilter{
left: left,
right: right,
Expand Down Expand Up @@ -113,6 +126,13 @@ func (l containsFilter) String() string {
return string(l)
}

func newContainsFilter(match string) LineFilter {
if match == "" {
return TrueFilter
}
return containsFilter(match)
}

// newFilter creates a new line filter from a match string and type.
func newFilter(match string, mt labels.MatchType) (LineFilter, error) {
switch mt {
Expand All @@ -121,10 +141,9 @@ func newFilter(match string, mt labels.MatchType) (LineFilter, error) {
case labels.MatchNotRegexp:
return parseRegexpFilter(match, false)
case labels.MatchEqual:
return containsFilter(match), nil
return newContainsFilter(match), nil
case labels.MatchNotEqual:
return newNotFilter(containsFilter(match)), nil

return newNotFilter(newContainsFilter(match)), nil
default:
return nil, fmt.Errorf("unknown matcher: %v", match)
}
Expand Down Expand Up @@ -163,6 +182,12 @@ func simplify(reg *syntax.Regexp) (LineFilter, bool) {
return simplify(reg)
case syntax.OpLiteral:
return containsFilter(string(reg.Rune)), true
case syntax.OpStar:
if reg.Sub[0].Op == syntax.OpAnyCharNotNL {
return TrueFilter, true
}
case syntax.OpEmptyMatch:
return TrueFilter, true
}
return nil, false
}
Expand Down
37 changes: 36 additions & 1 deletion pkg/logql/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ func Test_SimplifiedRegex(t *testing.T) {
{"f.*|foobar.*|.*buzz", true, newOrFilter(newOrFilter(containsFilter("f"), containsFilter("foobar")), containsFilter("buzz")), true},
// parsed as ((f(?-s:.)*)|foobar(?-s:.)*)|(?-s:.)*buzz
{"((f.*)|foobar.*)|.*buzz", true, newOrFilter(newOrFilter(containsFilter("f"), containsFilter("foobar")), containsFilter("buzz")), true},
{".*", true, TrueFilter, true},
{".*|.*", true, TrueFilter, true},
{".*||||", true, TrueFilter, true},
{"", true, TrueFilter, true},

// regex we are not supporting.
{"[a-z]+foo", false, nil, false},
{".+foo", false, nil, false},
{".*fo.*o", false, nil, false},
{".*", false, nil, false},
{`\d`, false, nil, false},
{`\sfoo`, false, nil, false},
{`foo?`, false, nil, false},
Expand Down Expand Up @@ -89,6 +92,38 @@ func Test_SimplifiedRegex(t *testing.T) {
}
}

func Test_TrueFilter(t *testing.T) {
for _, test := range []struct {
name string
f LineFilter
expectTrue bool
}{
{"empty match", newContainsFilter(""), true},
{"not empty match", newNotFilter(newContainsFilter("")), false},
{"match", newContainsFilter("foo"), false},
{"empty match and", newAndFilter(newContainsFilter(""), newContainsFilter("")), true},
{"empty match or", newOrFilter(newContainsFilter(""), newContainsFilter("")), true},
{"nil right and", newAndFilter(newContainsFilter(""), nil), true},
{"nil left or", newOrFilter(nil, newContainsFilter("")), true},
{"nil right and not empty", newAndFilter(newContainsFilter("foo"), nil), false},
{"nil left or not empty", newOrFilter(nil, newContainsFilter("foo")), false},
{"nil both and", newAndFilter(nil, nil), true},
{"nil both or", newOrFilter(nil, nil), true},
{"empty match and chained", newAndFilter(newContainsFilter(""), newAndFilter(newContainsFilter(""), newAndFilter(newContainsFilter(""), newContainsFilter("")))), true},
{"empty match or chained", newOrFilter(newContainsFilter(""), newOrFilter(newContainsFilter(""), newOrFilter(newContainsFilter(""), newContainsFilter("")))), true},
{"empty match and", newNotFilter(newAndFilter(newContainsFilter(""), newContainsFilter(""))), false},
{"empty match or", newNotFilter(newOrFilter(newContainsFilter(""), newContainsFilter(""))), false},
} {
t.Run(test.name, func(t *testing.T) {
if test.expectTrue {
require.Equal(t, TrueFilter, test.f)
} else {
require.NotEqual(t, TrueFilter, test.f)
}
})
}
}

func Benchmark_LineFilter(b *testing.B) {
b.ReportAllocs()
logline := `level=bar ts=2020-02-22T14:57:59.398312973Z caller=logging.go:44 traceID=2107b6b551458908 msg="GET /buzz (200) 4.599635ms`
Expand Down