@@ -1013,19 +1013,119 @@ func (pr mockPostingsReader) Postings(ctx context.Context, name string, values .
10131013}
10141014
10151015func TestMemPostings_PostingsForMatcher (t * testing.T ) {
1016+ series := []labels.Labels {
1017+ labels .FromStrings ("n" , "1" ),
1018+ labels .FromStrings ("n" , "1" , "i" , "a" ),
1019+ labels .FromStrings ("n" , "1" , "i" , "b" ),
1020+ labels .FromStrings ("n" , "2" ),
1021+ labels .FromStrings ("n" , "2.5" ),
1022+ }
1023+
10161024 p := NewMemPostings ()
1017- p . Add ( 1 , labels . FromStrings ( "lbl1" , "a" ))
1018- p .Add (2 , labels . FromStrings ( "lbl1" , "b" ) )
1019- p . Add ( 3 , labels . FromStrings ( "lbl2" , "a" ))
1025+ for i , lbls := range series {
1026+ p .Add (storage . SeriesRef ( i + 1 ), lbls )
1027+ }
10201028 pr := mockPostingsReader {
10211029 mp : p ,
10221030 }
10231031
1024- it := p .PostingsForMatcher (context .Background (), pr , labels .MustNewMatcher (labels .MatchRegexp , "lbl1" , "[a,b]" ))
1025- postings , err := ExpandPostings (it )
1026- require .NoError (t , err )
1032+ testCases := []struct {
1033+ matcher * labels.Matcher
1034+ exp []storage.SeriesRef
1035+ }{
1036+ // Simple equals.
1037+ {
1038+ matcher : labels .MustNewMatcher (labels .MatchEqual , "n" , "1" ),
1039+ exp : []storage.SeriesRef {1 , 2 , 3 },
1040+ },
1041+ {
1042+ // PostingsForMatcher will only return postings for the matcher's label.
1043+ matcher : labels .MustNewMatcher (labels .MatchEqual , "missing" , "" ),
1044+ exp : []storage.SeriesRef {},
1045+ },
1046+ // Not equals.
1047+ {
1048+ matcher : labels .MustNewMatcher (labels .MatchNotEqual , "n" , "1" ),
1049+ exp : []storage.SeriesRef {4 , 5 },
1050+ },
1051+ {
1052+ matcher : labels .MustNewMatcher (labels .MatchNotEqual , "i" , "" ),
1053+ exp : []storage.SeriesRef {2 , 3 },
1054+ },
1055+ {
1056+ matcher : labels .MustNewMatcher (labels .MatchNotEqual , "missing" , "" ),
1057+ exp : []storage.SeriesRef {},
1058+ },
1059+ // Regexp.
1060+ {
1061+ matcher : labels .MustNewMatcher (labels .MatchRegexp , "n" , "^1$" ),
1062+ exp : []storage.SeriesRef {1 , 2 , 3 },
1063+ },
1064+ {
1065+ // PostingsForMatcher will only return postings for the matcher's label.
1066+ matcher : labels .MustNewMatcher (labels .MatchRegexp , "i" , "^$" ),
1067+ exp : []storage.SeriesRef {},
1068+ },
1069+ // Not regexp.
1070+ {
1071+ matcher : labels .MustNewMatcher (labels .MatchNotRegexp , "n" , "^1$" ),
1072+ exp : []storage.SeriesRef {4 , 5 },
1073+ },
1074+ {
1075+ matcher : labels .MustNewMatcher (labels .MatchNotRegexp , "n" , "1" ),
1076+ exp : []storage.SeriesRef {4 , 5 },
1077+ },
1078+ {
1079+ matcher : labels .MustNewMatcher (labels .MatchNotRegexp , "n" , "1|2.5" ),
1080+ exp : []storage.SeriesRef {4 },
1081+ },
1082+ {
1083+ matcher : labels .MustNewMatcher (labels .MatchNotRegexp , "n" , "(1|2.5)" ),
1084+ exp : []storage.SeriesRef {4 },
1085+ },
1086+ // Set optimization for regexp.
1087+ // Refer to https://github.com/prometheus/prometheus/issues/2651.
1088+ {
1089+ matcher : labels .MustNewMatcher (labels .MatchRegexp , "n" , "1|2" ),
1090+ exp : []storage.SeriesRef {1 , 2 , 3 , 4 },
1091+ },
1092+ {
1093+ matcher : labels .MustNewMatcher (labels .MatchRegexp , "i" , "a|b" ),
1094+ exp : []storage.SeriesRef {2 , 3 },
1095+ },
1096+ {
1097+ matcher : labels .MustNewMatcher (labels .MatchRegexp , "i" , "(a|b)" ),
1098+ exp : []storage.SeriesRef {2 , 3 },
1099+ },
1100+ {
1101+ matcher : labels .MustNewMatcher (labels .MatchRegexp , "n" , "x1|2" ),
1102+ exp : []storage.SeriesRef {4 },
1103+ },
1104+ {
1105+ matcher : labels .MustNewMatcher (labels .MatchRegexp , "n" , "2|2\\ .5" ),
1106+ exp : []storage.SeriesRef {4 , 5 },
1107+ },
1108+ // Empty value.
1109+ {
1110+ // PostingsForMatcher will only return postings having a matching label.
1111+ matcher : labels .MustNewMatcher (labels .MatchRegexp , "i" , "c||d" ),
1112+ exp : []storage.SeriesRef {},
1113+ },
1114+ {
1115+ // PostingsForMatcher will only return postings having a matching label.
1116+ matcher : labels .MustNewMatcher (labels .MatchRegexp , "i" , "(c||d)" ),
1117+ exp : []storage.SeriesRef {},
1118+ },
1119+ }
1120+ for _ , tc := range testCases {
1121+ t .Run (tc .matcher .String (), func (t * testing.T ) {
1122+ it := p .PostingsForMatcher (context .Background (), pr , tc .matcher )
1123+ srs , err := ExpandPostings (it )
1124+ require .NoError (t , err )
10271125
1028- require .Equal (t , []storage.SeriesRef {1 , 2 }, postings )
1126+ require .ElementsMatch (t , tc .exp , srs )
1127+ })
1128+ }
10291129}
10301130
10311131func TestPostingsCloner (t * testing.T ) {
0 commit comments