From f7f80a3abfbd525e26429d1cdc8681ba1ba069cf Mon Sep 17 00:00:00 2001 From: Cyril Tovena Date: Wed, 6 Oct 2021 16:44:26 +0200 Subject: [PATCH 1/3] Update pkg/labels/regexp_test.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Peter Štibraný --- pkg/labels/regexp_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/labels/regexp_test.go b/pkg/labels/regexp_test.go index 9eca3afb0..1b0dc8fd3 100644 --- a/pkg/labels/regexp_test.go +++ b/pkg/labels/regexp_test.go @@ -109,7 +109,7 @@ func TestFindSetMatches(t *testing.T) { {"^foo$", []string{"foo"}}, // Simple sets alternates. {"foo|bar|zz", []string{"foo", "bar", "zz"}}, - // Simple sets alternate and concat (bar|baz is parsed as ba(r|z)). + // Simple sets alternate and concat (bar|baz is parsed as "ba[rz]"). {"foo|bar|baz", []string{"foo", "bar", "baz"}}, // Simple sets alternate and concat and capture {"foo|bar|baz|(zz)", []string{"foo", "bar", "baz", "zz"}}, From 2d879c65fc23526a3d729b63066498bca305b4df Mon Sep 17 00:00:00 2001 From: Cyril Tovena Date: Thu, 7 Oct 2021 14:00:16 +0200 Subject: [PATCH 2/3] Avoid API break Signed-off-by: Cyril Tovena --- pkg/labels/matcher.go | 7 +++++++ pkg/labels/regexp.go | 6 +++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/labels/matcher.go b/pkg/labels/matcher.go index 1b40f77fa..c0d186b48 100644 --- a/pkg/labels/matcher.go +++ b/pkg/labels/matcher.go @@ -111,6 +111,13 @@ func (m *Matcher) Inverse() (*Matcher, error) { panic("labels.Matcher.Matches: invalid match type") } +func (m *Matcher) GetRegexString() string { + if m.re == nil { + return "" + } + return m.re.GetRegexString() +} + // SetMatches returns a set of equality matchers for the current regex matchers if possible. // For examples the regexp `a(b|f)` will returns "ab" and "af". // Returns nil if we can't replace the regexp by only equality matchers. diff --git a/pkg/labels/regexp.go b/pkg/labels/regexp.go index 351e88043..f3664db2a 100644 --- a/pkg/labels/regexp.go +++ b/pkg/labels/regexp.go @@ -61,6 +61,7 @@ func findSetMatches(re *syntax.Regexp, base string) []string { if isCaseInsensitive(re) { return nil } + clearBeginEndText(re) switch re.Op { case syntax.OpLiteral: return []string{base + string(re.Rune)} @@ -108,7 +109,6 @@ func findSetMatchesFromConcat(re *syntax.Regexp, base string) []string { if len(re.Sub) == 0 { return nil } - clearBeginEndText(re) clearCapture(re.Sub...) matches := []string{base} @@ -209,6 +209,10 @@ func (m *FastRegexMatcher) SetMatches() []string { return m.setMatches } +func (m *FastRegexMatcher) GetRegexString() string { + return m.re.String() +} + // optimizeConcatRegex returns literal prefix/suffix text that can be safely // checked against the label value before running the regexp matcher. func optimizeConcatRegex(r *syntax.Regexp) (prefix, suffix, contains string) { From ce289124394eaae864ab71f513ef7f31785c30ed Mon Sep 17 00:00:00 2001 From: Cyril Tovena Date: Thu, 7 Oct 2021 14:04:52 +0200 Subject: [PATCH 3/3] Fixes a test now that we support more setMatches. Signed-off-by: Cyril Tovena --- tsdb/querier_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsdb/querier_test.go b/tsdb/querier_test.go index 050838790..224835578 100644 --- a/tsdb/querier_test.go +++ b/tsdb/querier_test.go @@ -2042,7 +2042,7 @@ func TestPostingsForMatcher(t *testing.T) { { // Test case for double quoted regex matcher matcher: labels.MustNewMatcher(labels.MatchRegexp, "test", "^(?:a|b)$"), - hasError: true, + hasError: false, }, }