Skip to content

Commit

Permalink
Remove empty match within concat of regex (#6026)
Browse files Browse the repository at this point in the history
* Remove empty match within concat of regex

* add back tests
  • Loading branch information
cyriltovena committed Apr 26, 2022
1 parent 0cc3fe7 commit 986fe5c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
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

0 comments on commit 986fe5c

Please sign in to comment.