aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/fef/featureoverride/featureoverride_test.cpp
blob: 158899e22da2ffee7e1c8c866085a4da5dc5d606 (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.
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/searchlib/fef/fef.h>

#include <vespa/searchlib/fef/test/indexenvironment.h>
#include <vespa/searchlib/fef/test/queryenvironment.h>
#include <vespa/searchlib/fef/test/plugin/double.h>
#include <vespa/searchlib/fef/test/plugin/sum.h>
#include <vespa/searchlib/features/valuefeature.h>
#include <vespa/searchlib/features/rankingexpressionfeature.h>
#include <vespa/searchlib/fef/test/test_features.h>
#include <vespa/eval/eval/tensor_spec.h>
#include <vespa/eval/eval/fast_value.h>
#include <vespa/eval/eval/value_codec.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/util/issue.h>

using namespace search::features;
using namespace search::fef::test;
using namespace search::fef;
using search::feature_t;
using vespalib::Issue;
using vespalib::eval::TensorSpec;
using vespalib::eval::FastValueBuilderFactory;
using vespalib::make_string_short::fmt;


using BPSP = Blueprint::SP;

struct Fixture
{
    MatchDataLayout mdl;
    vespalib::Stash stash;
    std::vector<FeatureExecutor *> executors;
    MatchData::UP md;
    Fixture() : mdl(), stash(), executors(), md() {}
    Fixture &add(FeatureExecutor *executor, size_t outCnt) {
        executor->bind_outputs(stash.create_array<NumberOrObject>(outCnt));
        executors.push_back(executor);
        return *this;
    }
    Fixture &run() {
        md = mdl.createMatchData();
        for (const auto &executor : executors) {
            executor->bind_match_data(*md);
            executor->lazy_execute(1);
        }
        return *this;
    }
    FeatureExecutor &createValueExecutor() {
        std::vector<feature_t> values;
        values.push_back(1.0);
        values.push_back(2.0);
        values.push_back(3.0);
        return stash.create<ValueExecutor>(values);
    }
};

TEST_F("test decorator - single override", Fixture)
{
    FeatureExecutor *fe = &f.createValueExecutor();
    vespalib::Stash &stash = f.stash;
    fe = &stash.create<FeatureOverrider>(*fe, 1, 50.0, nullptr);
    f.add(fe, 3).run();
    EXPECT_EQUAL(fe->outputs().size(), 3u);

    EXPECT_EQUAL(fe->outputs().get_number(0), 1.0);
    EXPECT_EQUAL(fe->outputs().get_number(1), 50.0);
    EXPECT_EQUAL(fe->outputs().get_number(2), 3.0);
}

TEST_F("test decorator - multiple overrides", Fixture)
{
    FeatureExecutor *fe = &f.createValueExecutor();
    vespalib::Stash &stash = f.stash;
    fe = &stash.create<FeatureOverrider>(*fe, 0, 50.0, nullptr);
    fe = &stash.create<FeatureOverrider>(*fe, 2, 100.0, nullptr);
    f.add(fe, 3).run();
    EXPECT_EQUAL(fe->outputs().size(), 3u);

    EXPECT_EQUAL(fe->outputs().get_number(0), 50.0);
    EXPECT_EQUAL(fe->outputs().get_number(1), 2.0);
    EXPECT_EQUAL(fe->outputs().get_number(2), 100.0);
}

TEST_F("test decorator - non-existing override", Fixture)
{
    FeatureExecutor *fe = &f.createValueExecutor();
    vespalib::Stash &stash = f.stash;
    fe = &stash.create<FeatureOverrider>(*fe, 1000, 50.0, nullptr);
    f.add(fe, 3).run();
    EXPECT_EQUAL(fe->outputs().size(), 3u);

    EXPECT_EQUAL(fe->outputs().get_number(0), 1.0);
    EXPECT_EQUAL(fe->outputs().get_number(1), 2.0);
    EXPECT_EQUAL(fe->outputs().get_number(2), 3.0);
}

TEST_F("test decorator - transitive override", Fixture)
{
    FeatureExecutor *fe = &f.createValueExecutor();
    vespalib::Stash &stash = f.stash;
    fe = &stash.create<FeatureOverrider>(*fe, 1, 50.0, nullptr);
    f.add(fe, 3);
    EXPECT_EQUAL(fe->outputs().size(), 3u);

    FeatureExecutor *fe2 = &stash.create<DoubleExecutor>(3);
    fe2 = &stash.create<FeatureOverrider>(*fe2, 2, 10.0, nullptr);
    auto inputs = stash.create_array<LazyValue>(3, nullptr);
    inputs[0] = LazyValue(fe->outputs().get_raw(0), fe);
    inputs[1] = LazyValue(fe->outputs().get_raw(1), fe);
    inputs[2] = LazyValue(fe->outputs().get_raw(2), fe);
    fe2->bind_inputs(inputs);
    f.add(fe2, 3).run();
    EXPECT_EQUAL(fe2->outputs().size(), 3u);

    EXPECT_EQUAL(fe->outputs().get_number(0), 1.0);
    EXPECT_EQUAL(fe->outputs().get_number(1), 50.0);
    EXPECT_EQUAL(fe->outputs().get_number(2), 3.0);
    EXPECT_EQUAL(fe2->outputs().get_number(0), 2.0);
    EXPECT_EQUAL(fe2->outputs().get_number(1), 100.0);
    EXPECT_EQUAL(fe2->outputs().get_number(2), 10.0);
}

TEST("test overrides")
{
    BlueprintFactory bf;
    bf.addPrototype(BPSP(new ValueBlueprint()));
    bf.addPrototype(BPSP(new DoubleBlueprint()));
    bf.addPrototype(BPSP(new SumBlueprint()));

    IndexEnvironment idxEnv;
    RankSetup        rs(bf, idxEnv);

    rs.addDumpFeature("value(1,2,3)");
    rs.addDumpFeature("double(value(1))");
    rs.addDumpFeature("double(value(2))");
    rs.addDumpFeature("double(value(3))");
    rs.addDumpFeature("mysum(value(2),value(2))");
    rs.addDumpFeature("mysum(value(1),value(2),value(3))");
    EXPECT_TRUE(rs.compile());

    RankProgram::UP rankProgram = rs.create_dump_program();

    MatchDataLayout         mdl;
    QueryEnvironment        queryEnv;
    Properties              overrides;

    overrides.add("value(2)",       "20.0");
    overrides.add("value(1,2,3).1",  "4.0");
    overrides.add("value(1,2,3).2",  "6.0");
    overrides.add("bogus(feature)", "10.0");

    MatchData::UP match_data = mdl.createMatchData();
    rankProgram->setup(*match_data, queryEnv, overrides);

    std::map<vespalib::string, feature_t> res = Utils::getAllFeatures(*rankProgram, 2);

    EXPECT_EQUAL(res.size(), 20u);
    EXPECT_APPROX(res["value(1)"],                               1.0, 1e-6);
    EXPECT_APPROX(res["value(1).0"],                             1.0, 1e-6);
    EXPECT_APPROX(res["value(2)"],                              20.0, 1e-6);
    EXPECT_APPROX(res["value(2).0"],                            20.0, 1e-6);
    EXPECT_APPROX(res["value(3)"],                               3.0, 1e-6);
    EXPECT_APPROX(res["value(3).0"],                             3.0, 1e-6);
    EXPECT_APPROX(res["value(1,2,3)"],                           1.0, 1e-6);
    EXPECT_APPROX(res["value(1,2,3).0"],                         1.0, 1e-6);
    EXPECT_APPROX(res["value(1,2,3).1"],                         4.0, 1e-6);
    EXPECT_APPROX(res["value(1,2,3).2"],                         6.0, 1e-6);
    EXPECT_APPROX(res["mysum(value(2),value(2))"],              40.0, 1e-6);
    EXPECT_APPROX(res["mysum(value(2),value(2)).out"],          40.0, 1e-6);
    EXPECT_APPROX(res["mysum(value(1),value(2),value(3))"],     24.0, 1e-6);
    EXPECT_APPROX(res["mysum(value(1),value(2),value(3)).out"], 24.0, 1e-6);
    EXPECT_APPROX(res["double(value(1))"],                       2.0, 1e-6);
    EXPECT_APPROX(res["double(value(1)).0"],                     2.0, 1e-6);
    EXPECT_APPROX(res["double(value(2))"],                      40.0, 1e-6);
    EXPECT_APPROX(res["double(value(2)).0"],                    40.0, 1e-6);
    EXPECT_APPROX(res["double(value(3))"],                       6.0, 1e-6);
    EXPECT_APPROX(res["double(value(3)).0"],                     6.0, 1e-6);
}

//-----------------------------------------------------------------------------

struct SimpleRankFixture {
    BlueprintFactory factory;
    IndexEnvironment indexEnv;
    BlueprintResolver::SP resolver;
    Properties overrides;
    MatchData::UP match_data;
    RankProgram program;
    static vespalib::string expr_feature(const vespalib::string &name) {
        return fmt("rankingExpression(%s)", name.c_str());
    }
    SimpleRankFixture()
      : factory(), indexEnv(), resolver(new BlueprintResolver(factory, indexEnv)),
        overrides(), match_data(), program(resolver)
    {
        factory.addPrototype(std::make_shared<DocidBlueprint>());
        factory.addPrototype(std::make_shared<RankingExpressionBlueprint>());
    }
    ~SimpleRankFixture();
    void add_expr(const vespalib::string &name, const vespalib::string &expr) {
        vespalib::string feature_name = expr_feature(name);
        vespalib::string expr_name = feature_name + ".rankingScript";
        indexEnv.getProperties().add(expr_name, expr);
    }
    void add_override(const vespalib::string &name, const TensorSpec &spec) {
        vespalib::nbostream data;
        auto tensor = vespalib::eval::value_from_spec(spec, FastValueBuilderFactory::get());
        vespalib::eval::encode_value(*tensor, data);
        overrides.add(name, vespalib::stringref(data.peek(), data.size()));
    }
    void add_override(const vespalib::string &name, const vespalib::string &str) {
        overrides.add(name, str);
    }
    bool try_compile(const vespalib::string &seed) {
        resolver->addSeed(seed);
        if (!resolver->compile()) {
            return false;
        }
        MatchDataLayout mdl;
        QueryEnvironment queryEnv(&indexEnv);
        match_data = mdl.createMatchData();
        program.setup(*match_data, queryEnv, overrides);
        return true;
    }
    void compile(const vespalib::string &seed) {
        ASSERT_TRUE(try_compile(seed));
    }
    TensorSpec get(uint32_t docid) {
        auto result = program.get_seeds(false);
        ASSERT_EQUAL(1u, result.num_features());
        return TensorSpec::from_value(result.resolve(0).as_object(docid));
    }
};
SimpleRankFixture::~SimpleRankFixture() = default;

TensorSpec from_expr(const vespalib::string &expr) {
    auto result = TensorSpec::from_expr(expr);
    ASSERT_TRUE(result.type() != "error");
    return result;
}

struct MyIssues : Issue::Handler {
    std::vector<vespalib::string> list;
    Issue::Binding capture;
    MyIssues() : list(), capture(Issue::listen(*this)) {}
    ~MyIssues() override;
    void handle(const Issue &issue) override { list.push_back(issue.message()); }
};

MyIssues::~MyIssues() = default;

//-----------------------------------------------------------------------------

TEST_F("require expression without override works", SimpleRankFixture) {
    auto expect = from_expr("tensor<float>(x[3]):[1,2,3]");
    f1.add_expr("foo", "tensor<float>(x[3]):[1,2,3]");
    f1.compile(f1.expr_feature("foo"));
    EXPECT_EQUAL(f1.get(1), expect);
}

TEST_F("require that const binary override works", SimpleRankFixture) {
    auto expect = from_expr("tensor<float>(x[3]):[5,6,7]");
    f1.add_expr("foo", "tensor<float>(x[3]):[1,2,3]");
    f1.add_override(f1.expr_feature("foo"), expect);
    f1.compile(f1.expr_feature("foo"));
    EXPECT_EQUAL(f1.get(1), expect);
}

TEST_F("require that non-const binary override works", SimpleRankFixture) {
    auto expect = from_expr("tensor<float>(x[3]):[5,6,7]");
    f1.add_expr("foo", "tensor<float>(x[3]):[docid,2,3]");
    f1.add_override(f1.expr_feature("foo"), expect);
    f1.compile(f1.expr_feature("foo"));
    EXPECT_EQUAL(f1.get(1), expect);
}

TEST_F("require that wrong type binary override is ignored", SimpleRankFixture) {
    MyIssues issues;
    auto expect = from_expr("tensor<float>(x[3]):[1,2,3]");
    auto other = from_expr("tensor(x[3]):[5,6,7]");
    f1.add_expr("foo", "tensor<float>(x[3]):[1,2,3]");
    f1.add_override(f1.expr_feature("foo"), other);
    f1.compile(f1.expr_feature("foo"));
    ASSERT_EQUAL(issues.list.size(), 1u);
    EXPECT_LESS(issues.list[0].find("has invalid type"), issues.list[0].size());
    fprintf(stderr, "issue: %s\n", issues.list[0].c_str());
}

TEST_F("require that bad format binary override is ignored", SimpleRankFixture) {
    MyIssues issues;
    auto expect = from_expr("tensor<float>(x[3]):[1,2,3]");
    f1.add_expr("foo", "tensor<float>(x[3]):[1,2,3]");
    f1.add_override(f1.expr_feature("foo"), vespalib::string("bad format"));
    f1.compile(f1.expr_feature("foo"));
    ASSERT_EQUAL(issues.list.size(), 1u);
    EXPECT_LESS(issues.list[0].find("has invalid format"), issues.list[0].size());
    fprintf(stderr, "issue: %s\n", issues.list[0].c_str());
}

//-----------------------------------------------------------------------------

TEST_MAIN() { TEST_RUN_ALL(); }