aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/schema/processing/RankingExpressionWithTensorTestCase.java
blob: c5bd082100766137a71754579450a168e6d49083 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.processing;

import com.yahoo.schema.parser.ParseException;
import org.junit.jupiter.api.Test;

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

/**
 * @author geirst
 */
public class RankingExpressionWithTensorTestCase {

    @Test
    void requireThatSingleLineConstantMappedTensorCanBeParsed() throws ParseException {
        RankProfileSearchFixture f = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                        "    first-phase {\n" +
                        "      expression: sum(my_tensor)\n" +
                        "    }\n" +
                        "    constants {\n" +
                        "      my_tensor tensor(x{},y{}):{ {x:1,y:2}:1, {x:2,y:1}:2 }\n" +
                        "    }\n" +
                        "  }");
        f.compileRankProfile("my_profile");
        f.assertFirstPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile");
        f.assertRankProperty("tensor(x{},y{}):{{x:1,y:2}:1.0, {x:2,y:1}:2.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{},y{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    void requireThatSingleLineConstantIndexedTensorCanBeParsed() throws ParseException {
        RankProfileSearchFixture f = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                        "    first-phase {\n" +
                        "      expression: sum(my_tensor)\n" +
                        "    }\n" +
                        "    constants {\n" +
                        "      my_tensor tensor(x[3]):{ {x:0}:1, {x:1}:2, {x:2}:3 }\n" +
                        "    }\n" +
                        "  }");
        f.compileRankProfile("my_profile");
        f.assertFirstPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile");
        f.assertRankProperty("tensor(x[3]):[1.0, 2.0, 3.0]", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x[3])", "constant(my_tensor).type", "my_profile");
    }

    @Test
    void requireThatSingleLineConstantIndexedTensorShortFormCanBeParsed() throws ParseException {
        RankProfileSearchFixture f = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                        "    first-phase {\n" +
                        "      expression: sum(my_tensor)\n" +
                        "    }\n" +
                        "    constants {\n" +
                        "      my_tensor tensor(x[3]):[1, 2, 3]\n" +
                        "    }\n" +
                        "  }");
        f.compileRankProfile("my_profile");
        f.assertFirstPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile");
        f.assertRankProperty("tensor(x[3]):[1.0, 2.0, 3.0]", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x[3])", "constant(my_tensor).type", "my_profile");
    }

    @Test
    void requireConstantTensorCanBeReferredViaConstantFeature() throws ParseException {
        RankProfileSearchFixture f = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                        "    first-phase {\n" +
                        "      expression: sum(constant(my_tensor))\n" +
                        "    }\n" +
                        "    constants {\n" +
                        "      my_tensor tensor(x{},y{}):{{x:1,y:2}:1, {x:2,y:1}:2}\n" +
                        "    }\n" +
                        "  }");
        f.compileRankProfile("my_profile");
        f.assertFirstPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile");
        f.assertRankProperty("tensor(x{},y{}):{{x:1,y:2}:1.0, {x:2,y:1}:2.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{},y{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    void requireThatMultiLineConstantTensorAndTypeCanBeParsed() throws ParseException {
        RankProfileSearchFixture f = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                        "    first-phase {\n" +
                        "      expression: sum(my_tensor)\n" +
                        "    }\n" +
                        "    constants {\n" +
                        "      my_tensor tensor(x{},y{}):\n" +
                        "          { {x:1,y:2}:1,\n" +
                        "            {x:2,y:1}:2 }\n" +
                        "    }\n" +
                        "  }");
        f.compileRankProfile("my_profile");
        f.assertFirstPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile");
        f.assertRankProperty("tensor(x{},y{}):{{x:1,y:2}:1.0, {x:2,y:1}:2.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{},y{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    void requireThatConstantTensorsCanBeUsedInSecondPhaseExpression() throws ParseException {
        RankProfileSearchFixture f = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                        "    second-phase {\n" +
                        "      expression: sum(my_tensor)\n" +
                        "    }\n" +
                        "    constants {\n" +
                        "      my_tensor tensor(x{}):{ {x:1}:1 }\n" +
                        "    }\n" +
                        "  }");
        f.compileRankProfile("my_profile");
        f.assertSecondPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile");
        f.assertRankProperty("tensor(x{}):{1:1.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    void requireThatConstantTensorsCanBeUsedInInheritedRankProfile() throws ParseException {
        RankProfileSearchFixture f = new RankProfileSearchFixture(
                "  rank-profile parent {\n" +
                        "    constants {\n" +
                        "      my_tensor {\n" +
                        "        value: { {x:1}:1 }\n" +
                        "      }\n" +
                        "    }\n" +
                        "  }\n" +
                        "  rank-profile my_profile inherits parent {\n" +
                        "    first-phase {\n" +
                        "      expression: sum(my_tensor)\n" +
                        "    }\n" +
                        "  }");
        f.compileRankProfile("my_profile");
        f.assertFirstPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile");
        f.assertRankProperty("tensor(x{}):{1:1.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    void requireThatConstantTensorsCanBeUsedInFunction() throws ParseException {
        RankProfileSearchFixture f = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                        "    function my_macro() {\n" +
                        "      expression: sum(my_tensor)\n" +
                        "    }\n" +
                        "    first-phase {\n" +
                        "      expression: 5.0 + my_macro\n" +
                        "    }\n" +
                        "    constants {\n" +
                        "      my_tensor tensor(x{}):{ {x:1}:1 }\n" +
                        "    }\n" +
                        "  }");
        f.compileRankProfile("my_profile");
        f.assertFirstPhaseExpression("5.0 + my_macro", "my_profile");
        f.assertFunction("reduce(constant(my_tensor), sum)", "my_macro", "my_profile");
        f.assertRankProperty("tensor(x{}):{1:1.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    void requireThatCombinationOfConstantTensorsAndConstantValuesCanBeUsed() throws ParseException {
        RankProfileSearchFixture f = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                        "    first-phase {\n" +
                        "      expression: my_number_1 + sum(my_tensor) + my_number_2\n" +
                        "    }\n" +
                        "    constants {\n" +
                        "      my_number_1 double: 3.0\n" +
                        "      my_tensor tensor(x{}):{ {x:1}:1 }\n" +
                        "      my_number_2 double: 5.0\n" +
                        "    }\n" +
                        "  }");
        f.compileRankProfile("my_profile");
        f.assertFirstPhaseExpression("3.0 + reduce(constant(my_tensor), sum) + 5.0", "my_profile");
        f.assertRankProperty("tensor(x{}):{1:1.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    void requireThatInvalidTensorTypeSpecThrowsException() throws ParseException {
        try {
            RankProfileSearchFixture f = new RankProfileSearchFixture(
                    "  rank-profile my_profile {\n" +
                            "    constants {\n" +
                            "      my_tensor tensor(x):{ {x:1}:1 }\n" +
                            "    }\n" +
                            "  }");
            f.compileRankProfile("my_profile");
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) {
            assertStartsWith("Type of constant(my_tensor): Illegal tensor type spec: A tensor type spec must be on the form",
                    e.getMessage());
        }
    }

    private void assertStartsWith(String prefix, String string) {
        assertEquals(prefix, string.substring(0, Math.min(prefix.length(), string.length())));
    }

}