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

#include "query_value.h"
#include "iindexenvironment.h"
#include "indexproperties.h"
#include "iqueryenvironment.h"
#include <vespa/searchlib/features/utils.h>
#include <vespa/document/datatype/tensor_data_type.h>
#include <vespa/eval/eval/fast_value.h>
#include <vespa/eval/eval/interpreted_function.h>
#include <vespa/eval/eval/value.h>
#include <vespa/eval/eval/value_codec.h>
#include <vespa/vespalib/locale/c.h>
#include <vespa/vespalib/util/issue.h>
#include <cerrno>

using document::TensorDataType;
using vespalib::Issue;
using vespalib::eval::DoubleValue;
using vespalib::eval::Function;
using vespalib::eval::InterpretedFunction;
using vespalib::eval::NodeTypes;
using vespalib::eval::SimpleObjectParams;
using vespalib::eval::TensorSpec;
using vespalib::eval::Value;
using vespalib::eval::ValueType;

using namespace search::fef::indexproperties;

namespace search::fef {

using ValueWrapper = AnyWrapper<Value::UP>;

InvalidValueTypeException::InvalidValueTypeException(const vespalib::string& query_key, const vespalib::string& type_str_in)
    : vespalib::Exception("Invalid type '" + type_str_in + "' for query value '" + query_key + "'"),
      _type_str(type_str_in)
{
}

InvalidTensorValueException::InvalidTensorValueException(const vespalib::eval::ValueType& type, const vespalib::string& expr_in)
    : vespalib::Exception("Could not create tensor value of type '" + type.to_spec() + "' from the expression '" + expr_in + "'"),
      _expr(expr_in)
{
}

namespace {

/**
 * Convert a string to a feature value using special quoting mechanics;
 * a string that can be converted directly into a feature
 * (numeric value) will be converted. If the string cannot be
 * converted directly, it will be hashed, after stripping the leading
 * "'" if it exists.
 */
feature_t
as_feature(const vespalib::string& str)
{
    char *end;
    errno = 0;
    double val = vespalib::locale::c::strtod(str.c_str(), &end);
    if (errno != 0 || *end != '\0') { // not happy
        if ( ! str.empty() && str[0] == '\'') {
            val = features::util::getAsFeature(vespalib::stringref(str.substr(1)));
        } else {
            val = features::util::getAsFeature(vespalib::stringref(str));
        }
    }
    return val;
}

// Create an empty tensor of the given type.
std::unique_ptr<Value>
empty_tensor(const ValueType& type)
{
    const auto& factory = vespalib::eval::FastValueBuilderFactory::get();
    return vespalib::eval::value_from_spec(TensorSpec(type.to_spec()), factory);
}

// Create a tensor value by evaluating a self-contained expression.
std::unique_ptr<Value>
as_tensor(const vespalib::string& expr, const ValueType& wanted_type)
{
    const auto& factory = vespalib::eval::FastValueBuilderFactory::get();
    auto fun = Function::parse(expr);
    if (!fun->has_error() && (fun->num_params() == 0)) {
        NodeTypes types = NodeTypes(*fun, {});
        const ValueType & res_type = types.get_type(fun->root());
        if (res_type == wanted_type) {
            SimpleObjectParams params({});
            InterpretedFunction ifun(factory, *fun, types);
            InterpretedFunction::Context ctx(ifun);
            return factory.copy(ifun.eval(ctx, params));
        }
    }
    return {};
}

std::unique_ptr<Value>
decode_tensor_value(Property prop, const ValueType& value_type)
{
    if (prop.found() && !prop.get().empty()) {
        const vespalib::string& value = prop.get();
        vespalib::nbostream stream(value.data(), value.size());
        try {
            auto tensor = vespalib::eval::decode_value(stream, vespalib::eval::FastValueBuilderFactory::get());
            if (TensorDataType::isAssignableType(value_type, tensor->type())) {
                return tensor;
            } else {
                Issue::report("Query value type is '%s' but decoded tensor type is '%s'",
                              value_type.to_spec().c_str(), tensor->type().to_spec().c_str());
            }
        } catch (const vespalib::eval::DecodeValueException& e) {
            Issue::report("Query value has invalid binary format: %s", e.what());
        }
    }
    return {};
}

}

Property
QueryValue::config_lookup(const IIndexEnvironment& env) const
{
    const auto& props = env.getProperties();
    auto res = props.lookup(_name); // query(foo)
    if (!res.found()) {
        res = props.lookup(_old_key); // $foo
    }
    return res;
}

Property
QueryValue::request_lookup(const IQueryEnvironment& env) const
{
    const auto& props = env.getProperties();
    auto res = props.lookup(_name); // query(foo)
    if (!res.found()) {
        res = props.lookup(_key); // foo
    }
    if (!res.found()) {
        res = props.lookup(_old_key); // $foo
    }
    return res;
}

QueryValue::QueryValue()
    : _key(),
      _name(),
      _old_key(),
      _stored_value_key(),
      _type(ValueType::double_type())
{
}

QueryValue::QueryValue(const vespalib::string& key, vespalib::eval::ValueType type)
    : _key(key),
      _name("query(" + key + ")"),
      _old_key("$" + key),
      _stored_value_key("query.value." + key),
      _type(std::move(type))
{
}

QueryValue::~QueryValue() = default;

QueryValue
QueryValue::from_config(const vespalib::string& key, const IIndexEnvironment& env)
{
    vespalib::string type_str = type::QueryFeature::lookup(env.getProperties(), key);
    ValueType type = type_str.empty() ? ValueType::double_type() : ValueType::from_spec(type_str);
    if (type.is_error()) {
        throw InvalidValueTypeException(key, type_str);
    }
    return {key, std::move(type)};
}

std::unique_ptr<Value>
QueryValue::make_default_value(const IIndexEnvironment& env) const
{
    Property p = config_lookup(env);
    if (_type.is_double()) {
        if (p.found()) {
            return std::make_unique<DoubleValue>(as_feature(p.get()));
        } else {
            return std::make_unique<DoubleValue>(0);
        }
    } else {
        if (p.found()) {
            auto tensor = as_tensor(p.get(), _type);
            if ( ! tensor) {
                throw InvalidTensorValueException(_type, p.get().c_str());
            }
            return tensor;
        } else {
            return empty_tensor(_type);
        }
    }
}

void
QueryValue::prepare_shared_state(const fef::IQueryEnvironment& env, fef::IObjectStore& store) const
{
    if (!_stored_value_key.empty() && _type.has_dimensions() && (store.get(_stored_value_key) == nullptr)) {
        if (auto value = decode_tensor_value(request_lookup(env), _type)) {
            store.add(_stored_value_key, std::make_unique<ValueWrapper>(std::move(value)));
        }
    }
}

const Value*
QueryValue::lookup_value(const fef::IObjectStore& store) const
{
    if (const Anything* wrapped_value = store.get(_stored_value_key)) {
        return ValueWrapper::getValue(*wrapped_value).get();
    }
    return nullptr;
}

double
QueryValue::lookup_number(const fef::IQueryEnvironment& env, double default_value) const
{
    assert(!_type.has_dimensions());
    auto p = request_lookup(env);
    if (p.found()) {
        return as_feature(p.get());
    }
    return default_value;
}

}