From e5868b9c29f65c0a097ecdbe72bec2470d6810b2 Mon Sep 17 00:00:00 2001 From: jduncan0000 Date: Fri, 18 Mar 2022 05:58:22 -0500 Subject: [PATCH] Fix for issue #2255 - matchTagFilters for positive empty-match filters (#2304) * fix for issue 2255 - matchTagFilters for positive empty-match filters * add example to comments * formatting * add test for positive empty match * formatting --- lib/storage/index_db.go | 10 ++++++++-- lib/storage/index_db_test.go | 13 +++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/storage/index_db.go b/lib/storage/index_db.go index 77e1c27821..b470db59ac 100644 --- a/lib/storage/index_db.go +++ b/lib/storage/index_db.go @@ -2152,14 +2152,20 @@ func matchTagFilters(mn *MetricName, tfs []*tagFilter, kb *bytesutil.ByteBuffer) tagMatched = true break } - if !tagSeen && tf.isNegative && !tf.isEmptyMatch { + if !tagSeen && (!tf.isNegative && tf.isEmptyMatch || tf.isNegative && !tf.isEmptyMatch) { + // tf contains positive empty-match filter for non-existing tag key, i.e. + // {non_existing_tag_key=~"foobar|"} + // + // OR + // // tf contains negative filter for non-exsisting tag key // and this filter doesn't match empty string, i.e. {non_existing_tag_key!="foobar"} // Such filter matches anything. // // Note that the filter `{non_existing_tag_key!~"|foobar"}` shouldn't match anything, // since it is expected that it matches non-empty `non_existing_tag_key`. - // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/546 for details. + // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/546 and + // https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2255 for details. continue } if tagMatched { diff --git a/lib/storage/index_db_test.go b/lib/storage/index_db_test.go index 1f79360f04..72ecec2fab 100644 --- a/lib/storage/index_db_test.go +++ b/lib/storage/index_db_test.go @@ -1182,6 +1182,19 @@ func TestMatchTagFilters(t *testing.T) { t.Fatalf("Should match") } + // Positive empty match by non-existing tag + tfs.Reset() + if err := tfs.Add([]byte("non-existing-tag"), []byte("foobar|"), false, true); err != nil { + t.Fatalf("cannot add regexp, positive filter: %s", err) + } + ok, err = matchTagFilters(&mn, toTFPointers(tfs.tfs), &bb) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + if !ok { + t.Fatalf("Should match") + } + // Negative match by non-existing tag tfs.Reset() if err := tfs.Add([]byte("non-existing-tag"), []byte("foobar"), false, false); err != nil {