From 2935529165bc798d3778054ebdb005503c8f7715 Mon Sep 17 00:00:00 2001 From: Michail Safronov Date: Thu, 9 Nov 2023 15:39:39 +0500 Subject: [PATCH] fix(finder): fix seriesByTag arg t=~^q (#256) --- finder/tagged_test.go | 4 ++++ pkg/where/match.go | 6 +++--- pkg/where/where.go | 7 +------ pkg/where/where_test.go | 1 + 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/finder/tagged_test.go b/finder/tagged_test.go index 006de8758..5c1b68f93 100644 --- a/finder/tagged_test.go +++ b/finder/tagged_test.go @@ -50,6 +50,10 @@ func TestTaggedWhere(t *testing.T) { {"seriesByTag('name=~^cpu|mem')", 0, "Tag1 LIKE '\\\\_\\\\_name\\\\_\\\\_=%' AND match(Tag1, '^__name__=cpu|mem')", "Tag1 LIKE '\\\\_\\\\_name\\\\_\\\\_=%' AND match(Tag1, '^__name__=cpu|mem')", false}, {"seriesByTag('name=~^cpu|mem$')", 0, "Tag1 LIKE '\\\\_\\\\_name\\\\_\\\\_=%' AND match(Tag1, '^__name__=cpu|mem$')", "Tag1 LIKE '\\\\_\\\\_name\\\\_\\\\_=%' AND match(Tag1, '^__name__=cpu|mem$')", false}, {"seriesByTag('name=rps', 'key=~value')", 0, "(Tag1='__name__=rps') AND (arrayExists((x) -> x LIKE 'key=%' AND match(x, '^key=.*value'), Tags))", "", false}, + // test for issue #244 + {"seriesByTag('name=rps', 'key=~^value')", 0, "(Tag1='__name__=rps') AND (arrayExists((x) -> x LIKE 'key=value%' AND match(x, '^key=value'), Tags))", "", false}, + {"seriesByTag('name=rps', 'key=~^value.*')", 0, "(Tag1='__name__=rps') AND (arrayExists((x) -> x LIKE 'key=value%' AND match(x, '^key=value.*'), Tags))", "", false}, + {"seriesByTag('name=rps', 'key=~^valu[a-e]')", 0, "(Tag1='__name__=rps') AND (arrayExists((x) -> x LIKE 'key=valu%' AND match(x, '^key=valu[a-e]'), Tags))", "", false}, {"seriesByTag('name=rps', 'key=~^value$')", 0, "(Tag1='__name__=rps') AND (arrayExists((x) -> x='key=value', Tags))", "", false}, {"seriesByTag('name=rps', 'key=~hello.world')", 0, "(Tag1='__name__=rps') AND (arrayExists((x) -> x LIKE 'key=%' AND match(x, '^key=.*hello.world'), Tags))", "", false}, {`seriesByTag('cpu=cpu-total','host=~Vladimirs-MacBook-Pro\.local')`, 0, `(Tag1='cpu=cpu-total') AND (arrayExists((x) -> x LIKE 'host=%' AND match(x, '^host=.*Vladimirs-MacBook-Pro\\.local'), Tags))`, "", false}, diff --git a/pkg/where/match.go b/pkg/where/match.go index 4f9e3c952..9c002e41b 100644 --- a/pkg/where/match.go +++ b/pkg/where/match.go @@ -144,10 +144,10 @@ func TreeGlob(field string, query string) string { func ConcatMatchKV(key, value string) string { startLine := value[0] == '^' endLine := value[len(value)-1] == '$' - if startLine { + if startLine && endLine { return key + opEq + value[1:] - } else if endLine { - return key + opEq + value + "\\\\%" + } else if startLine { + return key + opEq + value[1:] + "\\\\%" } return key + opEq + "\\\\%" + value } diff --git a/pkg/where/where.go b/pkg/where/where.go index 2efdb0067..af5a90956 100644 --- a/pkg/where/where.go +++ b/pkg/where/where.go @@ -124,13 +124,8 @@ func quote(value interface{}) string { func quoteRegex(key, value string) string { startLine := value[0] == '^' - endLine := value[len(value)-1] == '$' - if startLine && endLine { + if startLine { return fmt.Sprintf("'^%s%s%s'", key, opEq, escape(value[1:])) - } else if startLine { - return fmt.Sprintf("'^%s%s%s'", key, opEq, escape(value[1:])) - } else if endLine { - return fmt.Sprintf("'^%s%s.*%s'", key, opEq, escape(value[0:])) } return fmt.Sprintf("'^%s%s.*%s'", key, opEq, escape(value)) } diff --git a/pkg/where/where_test.go b/pkg/where/where_test.go index 72c79aca0..545f805fb 100644 --- a/pkg/where/where_test.go +++ b/pkg/where/where_test.go @@ -63,6 +63,7 @@ func TestNonRegexpPrefix(t *testing.T) { {`__name__=cpu.load`, `__name__=cpu`}, {`__name__=~(cpu|mem)`, `__name__=~`}, {`__name__=~cpu|mem`, `__name__=~`}, + {`__name__=~^host`, `__name__=~`}, } for _, test := range table {