aboutsummaryrefslogtreecommitdiffstats
path: root/predicate-search/src/test/java/com/yahoo/search/predicate/index/PredicateSearchTest.java
blob: 62dde7ac585d5ea0ef111a6bd955cd2b4aefe29b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.predicate.index;

import com.yahoo.search.predicate.Hit;
import com.yahoo.search.predicate.SubqueryBitmap;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * @author <a href="mailto:magnarn@yahoo-inc.com">Magnar Nedland</a>
 * @author bjorncs
 */
public class PredicateSearchTest {

    @Test
    void requireThatNoStreamsReturnNoResults() {
        PredicateSearch search = new PredicateSearch(new ArrayList<>(), new byte[0], new byte[0], new short[0], 1);
        assertEquals(0, search.stream().count());
    }

    @Test
    void requireThatSingleStreamFiltersOnConstructedCompleteIntervals() {
        PredicateSearch search = createPredicateSearch(
                new byte[]{1, 1, 1},
                postingList(
                        SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x000100ff),
                        entry(1, 0x00010001, 0x000200ff),
                        entry(2, 0x00010042)));
        assertEquals(Arrays.asList(new Hit(0), new Hit(1)).toString(), search.stream().toList().toString());
    }

    @Test
    void requireThatMinFeatureIsUsedToPruneResults() {
        PredicateSearch search = createPredicateSearch(
                new byte[]{3, 1},
                postingList(
                        SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x000100ff),
                        entry(1, 0x000100ff)));
        assertEquals(Arrays.asList(new Hit(1)).toString(), search.stream().toList().toString());
    }

    @Test
    void requireThatAHighKCanYieldResults() {
        PredicateSearch search = createPredicateSearch(
                new byte[]{2},
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x00010001)),
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x000200ff)));
        assertEquals(Arrays.asList(new Hit(0)).toString(), search.stream().toList().toString());
    }

    @Test
    void requireThatPostingListsAreSortedAfterAdvancing() {
        PredicateSearch search = createPredicateSearch(
                new byte[]{2, 1, 1, 1},
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x000100ff),
                        entry(3, 0x000100ff)),
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(1, 0x000100ff),
                        entry(2, 0x000100ff)));
        assertEquals(Arrays.asList(new Hit(1), new Hit(2), new Hit(3)).toString(), search.stream().toList().toString());
    }

    @Test
    void requireThatEmptyPostingListsWork() {
        PredicateSearch search = createPredicateSearch(
                new byte[0],
                postingList(SubqueryBitmap.ALL_SUBQUERIES));
        assertEquals(Arrays.asList().toString(), search.stream().toList().toString());
    }

    @Test
    void requireThatShorterPostingListEndingIsOk() {
        PredicateSearch search = createPredicateSearch(
                new byte[]{1, 1, 1},
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x000100ff),
                        entry(1, 0x000100ff)),
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(2, 0x000100ff)));
        assertEquals(Arrays.asList(new Hit(0), new Hit(1), new Hit(2)).toString(), search.stream().toList().toString());
    }

    @Test
    void requireThatSortingWorksForManyPostingLists() {
        PredicateSearch search = createPredicateSearch(
                new byte[]{1, 5, 2, 2},
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x000100ff),
                        entry(1, 0x000100ff)),
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(1, 0x000100ff),
                        entry(2, 0x000100ff)),
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(1, 0x000100ff),
                        entry(3, 0x000100ff)),
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(1, 0x000100ff),
                        entry(2, 0x000100ff)),
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(1, 0x000100ff),
                        entry(3, 0x000100ff)));
        assertEquals(
                Arrays.asList(new Hit(0), new Hit(1), new Hit(2), new Hit(3)).toString(),
                search.stream().toList().toString());
    }

    @Test
    void requireThatInsufficientIntervalCoveragePreventsMatch() {
        PredicateSearch search = createPredicateSearch(
                new byte[]{1, 1},
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x00010001),
                        entry(1, 0x000200ff)));
        assertEquals(Arrays.asList().toString(), search.stream().toList().toString());
    }

    @Test
    void requireThatIntervalsAreSorted() {
        PredicateSearch search = createPredicateSearch(
                new byte[]{1},
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x00010001)),
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x000300ff)),
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x00020002)));
        assertEquals(Arrays.asList(new Hit(0)).toString(), search.stream().toList().toString());
    }

    @Test
    void requireThatThereCanBeManyIntervals() {
        PredicateSearch search = createPredicateSearch(
                new byte[]{1},
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x00010001, 0x00020002, 0x00030003, 0x000100ff, 0x00040004, 0x00050005, 0x00060006)));
        assertEquals(Arrays.asList(new Hit(0)).toString(), search.stream().toList().toString());
    }

    @Test
    void requireThatNotIsSupported_NoMatch() {
        PredicateSearch search = createPredicateSearch(
                new byte[]{1},
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x00010001)),
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x00010000, 0x00ff0001)));
        assertEquals(Arrays.asList().toString(), search.stream().toList().toString());
    }

    @Test
    void requireThatNotIsSupported_Match() {
        PredicateSearch search = createPredicateSearch(
                new byte[]{1},
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x00010000, 0x00ff0001)));
        assertEquals(Arrays.asList(new Hit(0)).toString(), search.stream().toList().toString());
    }

    @Test
    void requireThatNotIsSupported_NoMatchBecauseOfPreviousTerm() {
        PredicateSearch search = createPredicateSearch(
                new byte[]{1},
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x00020001, 0x00ff0001)));
        assertEquals(Arrays.asList().toString(), search.stream().toList().toString());
    }

    @Test
    void requireThatIntervalSortingWorksAsUnsigned() {
        PredicateSearch search = createPredicateSearch(
                new byte[]{1},
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x00010001)),
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x00fe0001, 0x00ff00fe)));
        assertEquals(Arrays.asList(new Hit(0)).toString(), search.stream().toList().toString());
    }

    @Test
    void requireThatMatchCanRequireMultiplePostingLists() {
        PredicateSearch search = createPredicateSearch(
                new byte[]{6},
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x00010001)),
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x0002000b, 0x00030003)),
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x00040003)),
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x00050004)),
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x00010008, 0x00060006)),
                postingList(SubqueryBitmap.ALL_SUBQUERIES,
                        entry(0, 0x00020002, 0x000700ff)));
        assertEquals(Arrays.asList(new Hit(0)).toString(), search.stream().toList().toString());
    }

    private static PredicateSearch createPredicateSearch(byte[] minFeatures, PostingList... postingLists) {
        byte[] nPostingListsForDocument = new byte[minFeatures.length];
        short[] intervalEnds = new short[minFeatures.length];
        Arrays.fill(intervalEnds, (short) 0xFF);
        List<PostingList> list = Arrays.asList(postingLists);
        for (PostingList postingList : postingLists) {
            for (int id : postingList.getDocIds()) {
                nPostingListsForDocument[id]++;
            }
        }
        return new PredicateSearch(list, nPostingListsForDocument, minFeatures, intervalEnds, 0xFF);
    }

    private static class SimplePostingList implements PostingList {
        private final long subquery;
        private final Entry[] entries;
        private int[] currentIntervals;
        private int currentIntervalIndex;
        private int currentDocId;
        private int currentIndex;

        public SimplePostingList(long subquery, Entry... entries) {
            this.subquery = subquery;
            this.entries = entries;
            this.currentIndex = 0;
            this.currentDocId = -1;
        }

        @Override
        public boolean nextDocument(int docId) {
            while (currentIndex < entries.length && entries[currentIndex].docId <= docId) {
                ++currentIndex;
            }
            if (currentIndex == entries.length) {
                return false;
            }
            Entry entry = entries[currentIndex];
            currentDocId = entry.docId;
            currentIntervals = entry.intervals;
            currentIntervalIndex = 0;
            return true;
        }

        @Override
        public boolean prepareIntervals() {
            return true;
        }

        @Override
        public boolean nextInterval() {
            return ++currentIntervalIndex < currentIntervals.length;
        }

        @Override
        public int getDocId() {
            return currentDocId;
        }

        @Override
        public int size() {
            return entries.length;
        }

        @Override
        public int getInterval() {
            return currentIntervals[currentIntervalIndex];
        }

        @Override
        public long getSubquery() {
            return subquery;
        }

        @Override
        public int[] getDocIds() {
            return Arrays.stream(entries).mapToInt(e -> e.docId).toArray();
        }

        public static class Entry {
            public final int docId;
            public final int[] intervals;

            public Entry(int docId, int... intervals) {
                this.docId = docId;
                this.intervals = intervals;
            }
        }
    }

    private static SimplePostingList postingList(long subquery, SimplePostingList.Entry... entries) {
        return new SimplePostingList(subquery, entries);
    }

    private static SimplePostingList.Entry entry(int docId, int... intervals) {
        return new SimplePostingList.Entry(docId, intervals);
    }
}