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

Remove empty match within concat of regex #6026

Merged
merged 2 commits into from
Apr 26, 2022
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
10 changes: 10 additions & 0 deletions pkg/logql/log/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,16 @@ func simplifyAlternate(reg *syntax.Regexp) (Filterer, bool) {
// Anything else is rejected.
func simplifyConcat(reg *syntax.Regexp, baseLiteral []byte) (Filterer, bool) {
clearCapture(reg.Sub...)
// remove empty match as we don't need them for filtering
i := 0
for _, r := range reg.Sub {
if r.Op == syntax.OpEmptyMatch {
continue
}
reg.Sub[i] = r
i++
}
reg.Sub = reg.Sub[:i]
// we support only simplication of concat operation with 3 sub expressions.
// for instance .*foo.*bar contains 4 subs (.*+foo+.*+bar) and can't be simplified.
if len(reg.Sub) > 3 {
Expand Down
2 changes: 2 additions & 0 deletions pkg/logql/log/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func Test_SimplifiedRegex(t *testing.T) {
{"(?i)foo", true, newContainsFilter([]byte("foo"), true), true},
{"(?i)界", true, newContainsFilter([]byte("界"), true), true},
{"(?i)ïB", true, newContainsFilter([]byte("ïB"), true), true},
{"(?:)foo|fatal|exception", true, newOrFilter(newOrFilter(newContainsFilter([]byte("foo"), false), newContainsFilter([]byte("fatal"), false)), newContainsFilter([]byte("exception"), false)), true},

// regex we are not supporting.
{"[a-z]+foo", true, nil, false},
Expand Down Expand Up @@ -160,6 +161,7 @@ func Benchmark_LineFilter(b *testing.B) {
{"(node:24) buzz*"},
{"(HTTP/.*\\\"|HEAD|GET) (2..|5..)"},
{"\"@l\":\"(Warning|Error|Fatal)\""},
{"(?:)foo|fatal|exception"},
} {
benchmarkRegex(b, test.re, logline, true)
benchmarkRegex(b, test.re, logline, false)
Expand Down