aboutsummaryrefslogtreecommitdiffstats
path: root/predicate-search/src/test/java/com/yahoo/search/predicate/annotator/PredicateTreeAnalyzerTest.java
blob: 401c006b273bed5e7e67893a96c94c6080a1f877 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.predicate.annotator;

import com.yahoo.document.predicate.FeatureConjunction;
import com.yahoo.document.predicate.Predicate;
import com.yahoo.document.predicate.PredicateOperator;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static com.yahoo.document.predicate.Predicates.and;
import static com.yahoo.document.predicate.Predicates.feature;
import static com.yahoo.document.predicate.Predicates.not;
import static com.yahoo.document.predicate.Predicates.or;
import static org.junit.jupiter.api.Assertions.*;

public class PredicateTreeAnalyzerTest {

    @Test
    void require_that_minfeature_is_1_for_simple_term() {
        Predicate p = feature("foo").inSet("bar");
        PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
        assertEquals(1, r.minFeature);
        assertEquals(1, r.treeSize);
        assertTrue(r.sizeMap.isEmpty());
    }

    @Test
    void require_that_minfeature_is_1_for_simple_negative_term() {
        Predicate p = not(feature("foo").inSet("bar"));
        PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
        assertEquals(1, r.minFeature);
    }

    @Test
    void require_that_minfeature_is_sum_for_and() {
        Predicate p =
                and(
                        feature("foo").inSet("bar"),
                        feature("baz").inSet("qux"),
                        feature("quux").inSet("corge"));
        PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
        assertEquals(3, r.minFeature);
        assertEquals(3, r.treeSize);
        assertEquals(3, r.sizeMap.size());
        assertSizeMapContains(r, pred(p).child(0), 1);
        assertSizeMapContains(r, pred(p).child(1), 1);
        assertSizeMapContains(r, pred(p).child(2), 1);
    }

    @Test
    void require_that_minfeature_is_min_for_or() {
        Predicate p =
                or(
                        and(
                                feature("foo").inSet("bar"),
                                feature("baz").inSet("qux"),
                                feature("quux").inSet("corge")),
                        and(
                                feature("grault").inSet("garply"),
                                feature("waldo").inSet("fred")));
        PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
        assertEquals(2, r.minFeature);
        assertEquals(5, r.treeSize);
        assertEquals(5, r.sizeMap.size());
        assertSizeMapContains(r, pred(p).child(0).child(0), 1);
        assertSizeMapContains(r, pred(p).child(0).child(1), 1);
        assertSizeMapContains(r, pred(p).child(0).child(2), 1);
        assertSizeMapContains(r, pred(p).child(1).child(0), 1);
        assertSizeMapContains(r, pred(p).child(1).child(1), 1);
    }

    @Test
    void require_that_minfeature_rounds_up() {
        Predicate p =
                or(
                        feature("foo").inSet("bar"),
                        feature("foo").inSet("bar"),
                        feature("foo").inSet("bar"));
        PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
        assertEquals(1, r.minFeature);
        assertEquals(3, r.treeSize);
    }

    @Test
    void require_that_minvalue_feature_set_considers_all_values() {
        {
            Predicate p =
                    and(
                            feature("foo").inSet("A", "B"),
                            feature("foo").inSet("B"));
            PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
            assertEquals(1, r.minFeature);
            assertEquals(2, r.treeSize);
        }
        {
            Predicate p =
                    and(
                            feature("foo").inSet("A", "B"),
                            feature("foo").inSet("C"));
            PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
            assertEquals(2, r.minFeature);
            assertEquals(2, r.treeSize);
        }
    }

    @Test
    void require_that_not_features_dont_count_towards_minfeature_calculation() {
        Predicate p =
                and(
                        feature("foo").inSet("A"),
                        not(feature("foo").inSet("A")),
                        not(feature("foo").inSet("B")),
                        feature("foo").inSet("B"));
        PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
        assertEquals(3, r.minFeature);
        assertEquals(6, r.treeSize);
    }

    @Test
    void require_that_multilevel_and_stores_size() {
        Predicate p =
                and(
                        and(
                                feature("foo").inSet("bar"),
                                feature("baz").inSet("qux"),
                                feature("quux").inSet("corge")),
                        and(
                                feature("grault").inSet("garply"),
                                feature("waldo").inSet("fred")));
        PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
        assertEquals(5, r.minFeature);
        assertEquals(5, r.treeSize);
        assertEquals(7, r.sizeMap.size());
        assertSizeMapContains(r, pred(p).child(0), 3);
        assertSizeMapContains(r, pred(p).child(1), 2);
        assertSizeMapContains(r, pred(p).child(0).child(0), 1);
        assertSizeMapContains(r, pred(p).child(0).child(1), 1);
        assertSizeMapContains(r, pred(p).child(0).child(2), 1);
        assertSizeMapContains(r, pred(p).child(1).child(0), 1);
        assertSizeMapContains(r, pred(p).child(1).child(1), 1);
    }

    @Test
    void require_that_not_ranges_dont_count_towards_minfeature_calculation() {
        Predicate p =
                and(
                        feature("foo").inRange(0, 10),
                        not(feature("foo").inRange(0, 10)),
                        feature("bar").inRange(0, 10),
                        not(feature("bar").inRange(0, 10)));
        PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
        assertEquals(3, r.minFeature);
        assertEquals(6, r.treeSize);
    }

    @Test
    void require_that_featureconjunctions_contribute_as_one_feature() {
        Predicate p =
                conj(
                        feature("foo").inSet("bar"),
                        feature("baz").inSet("qux"));
        PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
        assertEquals(1, r.minFeature);
        assertEquals(1, r.treeSize);
    }

    @Test
    void require_that_featureconjunctions_count_as_leaf_in_subtree_calculation() {
        Predicate p =
                and(
                        and(
                                feature("grault").inRange(0, 10),
                                feature("waldo").inRange(0, 10)),
                        conj(
                                feature("foo").inSet("bar"),
                                feature("baz").inSet("qux"),
                                feature("quux").inSet("corge")));
        PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
        assertEquals(3, r.minFeature);
        assertEquals(3, r.treeSize);
        assertEquals(4, r.sizeMap.size());
        assertSizeMapContains(r, pred(p).child(0), 2);
        assertSizeMapContains(r, pred(p).child(0).child(0), 1);
        assertSizeMapContains(r, pred(p).child(0).child(1), 1);
        assertSizeMapContains(r, pred(p).child(1), 1);
    }

    @Test
    void require_that_multiple_indentical_feature_conjunctions_does_not_contribute_more_than_one() {
        Predicate p =
                and(
                        or(
                                conj(
                                        feature("a").inSet("b"),
                                        feature("c").inSet("d")
                                ),
                                feature("x").inSet("y")),
                        or(
                                conj(
                                        feature("a").inSet("b"),
                                        feature("c").inSet("d")
                                ),
                                feature("z").inSet("w")));
        PredicateTreeAnalyzerResult r = PredicateTreeAnalyzer.analyzePredicateTree(p);
        assertEquals(1, r.minFeature);
        assertEquals(4, r.treeSize);
    }

    private static FeatureConjunction conj(Predicate... operands) {
        return new FeatureConjunction(Arrays.asList(operands));
    }

    private static void assertSizeMapContains(PredicateTreeAnalyzerResult r, PredicateSelector selector, int expectedValue) {
        Integer actualValue = r.sizeMap.get(selector.predicate);
        assertNotNull(actualValue);
        assertEquals(expectedValue, actualValue.intValue());
    }

    private static class PredicateSelector {
        public final Predicate predicate;

        public PredicateSelector(Predicate predicate) {
            this.predicate = predicate;
        }

        public PredicateSelector child(int index) {
            PredicateOperator op = (PredicateOperator) predicate;
            return new PredicateSelector(op.getOperands().get(index));
        }
    }

    private static PredicateSelector pred(Predicate p) {
        return new PredicateSelector(p);
    }
}