aboutsummaryrefslogtreecommitdiffstats
path: root/predicate-search-core/src/test/java/com/yahoo/search/predicate/optimization/OrSimplifierTest.java
blob: e51e3d29ac20cc97b3292828a14ca62f09d39652 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.predicate.optimization;

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

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.assertEquals;

public class OrSimplifierTest {

    @Test
    void require_that_or_with_feature_sets_of_same_key_is_simplified_to_single_feature_set() {
        Predicate p =
                or(
                        feature("key1").inSet("value1", "value4"),
                        feature("key1").inSet("value2", "value3"),
                        feature("key1").inSet("value1", "value4"));
        Predicate expected = feature("key1").inSet("value1", "value2", "value3", "value4");
        assertConvertedPredicateEquals(expected, p);
    }

    @Test
    void require_that_or_with_feature_sets_of_different_keys_is_simplified() {
        Predicate p =
                or(
                        feature("key1").inSet("value1", "value3"),
                        feature("key1").inSet("value2"),
                        feature("key2").inSet("value1"),
                        feature("key2").inSet("value2", "value3"));
        Predicate expected =
                or(
                        feature("key1").inSet("value1", "value2", "value3"),
                        feature("key2").inSet("value1", "value2", "value3"));
        assertConvertedPredicateEquals(expected, p);
    }

    @Test
    void require_that_conversion_is_recursive_and_cascades() {
        Predicate p =
                or(
                        feature("key1").inSet("value1", "value4"),
                        feature("key1").inSet("value2", "value3"),
                        or(
                                feature("key1").inSet("value1"),
                                feature("key1").inSet("value4")));
        Predicate expected = feature("key1").inSet("value1", "value2", "value3", "value4");
        assertConvertedPredicateEquals(expected, p);
    }

    @Test
    void require_that_or_below_and_is_converted() {
        Predicate p =
                and(
                        or(
                                feature("key1").inSet("value1"),
                                feature("key1").inSet("value2")),
                        feature("key2").inSet("value2"));
        Predicate expected =
                and(
                        feature("key1").inSet("value1", "value2"),
                        feature("key2").inSet("value2"));
        assertConvertedPredicateEquals(expected, p);
    }

    @Test
    void require_that_or_below_not_is_converted() {
        Predicate p =
                not(
                        or(
                                feature("key1").inSet("value1"),
                                feature("key1").inSet("value2")));
        Predicate expected = not(feature("key1").inSet("value1", "value2"));
        assertConvertedPredicateEquals(expected, p);
    }

    @Test
    void require_that_non_feature_set_nodes_are_left_untouched() {
        Predicate p =
                or(
                        feature("key1").inSet("value1"),
                        feature("key1").inSet("value2"),
                        not(feature("key1").inSet("value3")));
        Predicate expected =
                or(
                        not(feature("key1").inSet("value3")),
                        feature("key1").inSet("value1", "value2"));
        assertConvertedPredicateEquals(expected, p);
    }

    private static void assertConvertedPredicateEquals(Predicate expected, Predicate predicate) {
        OrSimplifier simplifier = new OrSimplifier();
        assertEquals(expected, simplifier.simplifyTree(predicate));
    }
}