aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/features/constant_feature.cpp
blob: 6c07cf8f008261b2f0f84141461950701f4b19c1 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "constant_feature.h"
#include "valuefeature.h"
#include <vespa/searchlib/fef/featureexecutor.h>
#include <vespa/searchlib/fef/properties.h>
#include <vespa/eval/eval/function.h>
#include <vespa/eval/eval/value_cache/constant_value.h>
#include <vespa/vespalib/util/stash.h>

#include <vespa/log/log.h>
LOG_SETUP(".features.constant_feature");

using namespace search::fef;

using vespalib::eval::ValueType;
using vespalib::eval::Function;
using vespalib::eval::SimpleConstantValue;

namespace search::features {

/**
 * Feature executor that returns a constant value.
 */
class ConstantFeatureExecutor : public fef::FeatureExecutor
{
private:
    const vespalib::eval::Value &_value;

public:
    ConstantFeatureExecutor(const vespalib::eval::Value &value)
        : _value(value)
    {}
    bool isPure() override { return true; }
    void execute(uint32_t) override {
        outputs().set_object(0, _value);
    }
    static FeatureExecutor &create(const vespalib::eval::Value &value, vespalib::Stash &stash) {
        return stash.create<ConstantFeatureExecutor>(value);
    }
};

ConstantBlueprint::ConstantBlueprint()
    : Blueprint("constant"),
      _key(),
      _value()
{
}

ConstantBlueprint::~ConstantBlueprint() = default;

void
ConstantBlueprint::visitDumpFeatures(const IIndexEnvironment &,
                                     IDumpFeatureVisitor &) const
{
}

Blueprint::UP
ConstantBlueprint::createInstance() const
{
    return std::make_unique<ConstantBlueprint>();
}

bool
ConstantBlueprint::setup(const IIndexEnvironment &env,
                         const ParameterList &params)
{
    _key = params[0].getValue();
    _value = env.getConstantValue(_key);
    if (!_value) {
        auto type_prop = env.getProperties().lookup(getName(), "type");
        auto value_prop = env.getProperties().lookup(getName(), "value");
        if ((type_prop.size() == 1) && (value_prop.size() == 1)) {
            auto type = ValueType::from_spec(type_prop.get());
            auto value = Function::parse(value_prop.get())->root().get_const_value();
            if (!type.is_error() && value && (value->type() == type)) {
                _value = std::make_unique<SimpleConstantValue>(std::move(value));
            } else {
                fail("Constant '%s' has invalid spec: type='%s', value='%s'",
                     _key.c_str(), type_prop.get().c_str(), value_prop.get().c_str());
            }
        } else {
            fail("Constant '%s' not found", _key.c_str());
        }
    } else if (_value->type().is_error()) {
        fail("Constant '%s' has invalid type", _key.c_str());
    }
    FeatureType output_type = _value
        ? FeatureType::object(_value->type())
        : FeatureType::number();
    describeOutput("out", "The constant looked up in index environment using the given key.",
                   output_type);
    return (_value && !_value->type().is_error());
}

FeatureExecutor &
ConstantBlueprint::createExecutor(const IQueryEnvironment &env, vespalib::Stash &stash) const
{
    (void) env;
    if (_value) {
        return ConstantFeatureExecutor::create(_value->value(), stash);
    } else {
        // Note: Should not happen, setup() has already failed
        return stash.create<SingleZeroValueExecutor>();
    }
}

}