From fdbf5b579591a8ce5926626523c955cd567f6fb4 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 24 Aug 2022 17:10:30 +0300 Subject: [PATCH] lib/storage: ignore `start text` and `end text` anchors in getOrValues(regexp) function This is OK, since the anchors are implicitly applied to the whole regexp. This optimization should improve the speed for regexp series filters with explicit $ and ^ anchors. For example, `{label="^(foo|bar)$"}` --- lib/storage/tag_filters.go | 2 ++ lib/storage/tag_filters_test.go | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/lib/storage/tag_filters.go b/lib/storage/tag_filters.go index b9e3ce592..e4f40165b 100644 --- a/lib/storage/tag_filters.go +++ b/lib/storage/tag_filters.go @@ -811,6 +811,8 @@ func getOrValuesExt(sre *syntax.Regexp) []string { return []string{string(sre.Rune)} case syntax.OpEmptyMatch: return []string{""} + case syntax.OpBeginText, syntax.OpEndText: + return []string{""} case syntax.OpAlternate: a := make([]string, 0, len(sre.Sub)) for _, reSub := range sre.Sub { diff --git a/lib/storage/tag_filters_test.go b/lib/storage/tag_filters_test.go index 71476e5a2..803df8570 100644 --- a/lib/storage/tag_filters_test.go +++ b/lib/storage/tag_filters_test.go @@ -1176,6 +1176,13 @@ func TestGetOrValues(t *testing.T) { f("(a|b|c)(d|e|f)(g|h|k)", nil) f("(?i)foo", nil) f("(?i)(foo|bar)", nil) + f("^foo|bar$", []string{"bar", "foo"}) + f("^(foo|bar)$", []string{"bar", "foo"}) + f("^a(foo|b(?:a|r))$", []string{"aba", "abr", "afoo"}) + // This is incorrect conversion, because the regexp matches nothing. + // It is OK for now, since such regexps are uncommon in practice. + // TODO: properly handle this case. + f("^a(^foo|bar$)z$", []string{"abarz", "afooz"}) } func TestGetRegexpPrefix(t *testing.T) {