From 2976ec89b839f40922a02b32a007ccb7dfc70c8b Mon Sep 17 00:00:00 2001
From: Aliaksandr Valialkin <valyala@gmail.com>
Date: Wed, 3 Feb 2021 18:45:42 +0200
Subject: [PATCH] lib/storage: fix a bug, which breaks searching by Graphite
 wildcard filters

---
 docs/CHANGELOG.md      | 2 ++
 lib/storage/storage.go | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index d1aa1345de..a9f079f869 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -2,6 +2,8 @@
 
 # tip
 
+* BUGFIX: vmselect: fix the bug peventing from proper searching by Graphite filter with wildcards such as `{__graphite__="foo.*.bar"}`.
+
 
 # [v1.53.0](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.53.0)
 
diff --git a/lib/storage/storage.go b/lib/storage/storage.go
index 477c87a34c..35bbdb5a18 100644
--- a/lib/storage/storage.go
+++ b/lib/storage/storage.go
@@ -1095,12 +1095,12 @@ func (s *Storage) SearchGraphitePaths(accountID, projectID uint32, tr TimeRange,
 		return nil, fmt.Errorf("more than maxPaths=%d suffixes found", maxPaths)
 	}
 	qPrefixStr := queryStr[:n]
-	qNode := queryStr[n:]
 	qTail := ""
+	qNode := queryStr[n:]
 	mustMatchLeafs := true
 	if m := strings.IndexByte(qNode, '.'); m >= 0 {
-		qNode = qNode[:m+1]
 		qTail = qNode[m+1:]
+		qNode = qNode[:m+1]
 		mustMatchLeafs = false
 	}
 	re, err := getRegexpForGraphiteNodeQuery(qNode)