aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/expression/attributenode.cpp
blob: b05f5a9149128d3dfce4778078708a1f8ab76afd (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "attributenode.h"
#include "resultvector.h"
#include "enumattributeresult.h"
#include <vespa/searchcommon/attribute/iattributecontext.h>
#include <cassert>

namespace search::expression {

using namespace vespalib;
using search::attribute::IAttributeContext;
using search::attribute::IAttributeVector;
using search::attribute::BasicType;

IMPLEMENT_EXPRESSIONNODE(AttributeNode, FunctionNode);

template <typename V>
class AttributeNode::IntegerHandler : public AttributeNode::Handler
{
public:
    IntegerHandler(ResultNode & result) noexcept
        : Handler(),
          _vector(((V &)result).getVector()),
          _wVector()
    { }
    void handle(const AttributeResult & r) override;
private:
    typename V::Vector & _vector;
    std::vector<search::attribute::IAttributeVector::WeightedInt> _wVector;
};

class AttributeNode::FloatHandler : public AttributeNode::Handler
{
public:
    FloatHandler(ResultNode & result) noexcept
        : Handler(),
          _vector(((FloatResultNodeVector &)result).getVector()),
          _wVector()
    { }
    void handle(const AttributeResult & r) override;
private:
    FloatResultNodeVector::Vector & _vector;
    std::vector<search::attribute::IAttributeVector::WeightedFloat> _wVector;
};

class AttributeNode::StringHandler : public AttributeNode::Handler
{
public:
    StringHandler(ResultNode & result) noexcept
        : Handler(),
          _vector(((StringResultNodeVector &)result).getVector()),
          _wVector()
    { }
    void handle(const AttributeResult & r) override;
private:
    StringResultNodeVector::Vector & _vector;
    std::vector<search::attribute::IAttributeVector::WeightedConstChar> _wVector;
};

class AttributeNode::EnumHandler : public AttributeNode::Handler
{
public:
    EnumHandler(ResultNode & result) noexcept
        : Handler(),
          _vector(((EnumResultNodeVector &)result).getVector()),
          _wVector()
    { }
    void handle(const AttributeResult & r) override;
private:
    EnumResultNodeVector::Vector &_vector;
    std::vector<search::attribute::IAttributeVector::WeightedEnum> _wVector;
};

namespace {

std::unique_ptr<AttributeResult>
createResult(const IAttributeVector * attribute)
{
    IAttributeVector::EnumRefs enumRefs = attribute->make_enum_read_view();
    if (enumRefs.empty()) {
        if (attribute->isIntegerType()) return std::make_unique<IntegerAttributeResult>(attribute, 0);
        if (attribute->isFloatingPointType()) return std::make_unique<FloatAttributeResult>(attribute, 0);
        return std::make_unique<AttributeResult>(attribute, 0);
    }
    return std::make_unique<EnumAttributeResult>(enumRefs, attribute, 0);
}

template<typename T>
std::pair<std::unique_ptr<ResultNode>, std::unique_ptr<AttributeNode::Handler>>
createSingle() {
    return { std::make_unique<T>(), std::unique_ptr<AttributeNode::Handler>()};
}

template<typename T, typename H>
std::pair<std::unique_ptr<ResultNode>, std::unique_ptr<AttributeNode::Handler>>
createMulti() {
    auto result = std::make_unique<T>();
    auto handler = std::make_unique<H>(*result);
    return { std::move(result), std::move(handler)};
}

}

AttributeNode::AttributeNode() :
    FunctionNode(),
    _scratchResult(std::make_unique<AttributeResult>()),
    _index(nullptr),
    _keepAliveForIndexLookups(),
    _hasMultiValue(false),
    _useEnumOptimization(false),
    _needExecute(true),
    _handler(),
    _attributeName()
{}

AttributeNode::~AttributeNode() = default;

AttributeNode::AttributeNode(vespalib::stringref name)
    : FunctionNode(),
      _scratchResult(std::make_unique<AttributeResult>()),
      _index(nullptr),
      _keepAliveForIndexLookups(),
      _hasMultiValue(false),
      _useEnumOptimization(false),
      _needExecute(true),
      _handler(),
      _attributeName(name)
{}

AttributeNode::AttributeNode(const IAttributeVector & attribute)
    : FunctionNode(),
      _scratchResult(createResult(&attribute)),
      _index(nullptr),
      _keepAliveForIndexLookups(),
      _hasMultiValue(attribute.hasMultiValue()),
      _useEnumOptimization(false),
      _needExecute(true),
      _handler(),
      _attributeName(attribute.getName())
{}

AttributeNode::AttributeNode(const AttributeNode & attribute)
    : FunctionNode(attribute),
      _scratchResult(attribute._scratchResult->clone()),
      _index(nullptr),
      _keepAliveForIndexLookups(),
      _hasMultiValue(attribute._hasMultiValue),
      _useEnumOptimization(attribute._useEnumOptimization),
      _needExecute(true),
      _handler(),
      _attributeName(attribute._attributeName)
{
    _scratchResult->setDocId(0);
}

AttributeNode &
AttributeNode::operator = (const AttributeNode & attr)
{
    if (this != &attr) {
        FunctionNode::operator = (attr);
        _attributeName = attr._attributeName;
        _hasMultiValue = attr._hasMultiValue;
        _useEnumOptimization = attr._useEnumOptimization;
        _scratchResult.reset(attr._scratchResult->clone());
        _scratchResult->setDocId(0);
        _handler.reset();
        _keepAliveForIndexLookups.reset();
        _needExecute = true;
    }
    return *this;
}

std::pair<std::unique_ptr<ResultNode>, std::unique_ptr<AttributeNode::Handler>>
AttributeNode::createResultHandler(bool preserveAccurateTypes, const attribute::IAttributeVector & attribute) const {
    BasicType::Type basicType = attribute.getBasicType();
    if (attribute.isIntegerType()) {
        if (_hasMultiValue) {
            if (basicType == BasicType::BOOL) {
                return createMulti<BoolResultNodeVector, IntegerHandler<BoolResultNodeVector>>();
            } else if (preserveAccurateTypes) {
                switch (basicType) {
                    case BasicType::INT8:
                        return createMulti<Int8ResultNodeVector, IntegerHandler<Int8ResultNodeVector>>();
                    case BasicType::INT16:
                        return createMulti<Int16ResultNodeVector, IntegerHandler<Int16ResultNodeVector>>();
                    case BasicType::INT32:
                        return createMulti<Int32ResultNodeVector, IntegerHandler<Int32ResultNodeVector>>();
                    case BasicType::INT64:
                        return createMulti<Int64ResultNodeVector, IntegerHandler<Int64ResultNodeVector>>();
                    default:
                        throw std::runtime_error("This is no valid integer attribute " + attribute.getName());
                }
            } else {
                return createMulti<IntegerResultNodeVector, IntegerHandler<IntegerResultNodeVector>>();
            }
        } else {
            if (basicType == BasicType::BOOL) {
                return createSingle<BoolResultNode>();
            } else if (preserveAccurateTypes) {
                switch (basicType) {
                    case BasicType::INT8:
                        return createSingle<Int8ResultNode>();
                    case BasicType::INT16:
                        return createSingle<Int16ResultNode>();
                    case BasicType::INT32:
                        return createSingle<Int32ResultNode>();
                    case BasicType::INT64:
                        return createSingle<Int64ResultNode>();
                    default:
                        throw std::runtime_error("This is no valid integer attribute " + attribute.getName());
                }
            } else {
                return createSingle<Int64ResultNode>();
            }
        }
    } else if (attribute.isFloatingPointType()) {
        return (_hasMultiValue)
                ? createMulti<FloatResultNodeVector, FloatHandler>()
                : createSingle<FloatResultNode>();
    } else if (attribute.isStringType()) {
        if (_hasMultiValue) {
            return (_useEnumOptimization)
                    ? createMulti<EnumResultNodeVector, EnumHandler>()
                    : createMulti<StringResultNodeVector, StringHandler>();
        } else {
            return (_useEnumOptimization)
                    ? createSingle<EnumResultNode>()
                    : createSingle<StringResultNode>();
        }
    } else if (attribute.is_raw_type()) {
        if (_hasMultiValue) {
            throw std::runtime_error(make_string("Does not support multivalue raw attribute vector '%s'",
                                                 attribute.getName().c_str()));
        } else {
            return createSingle<RawResultNode>();
        }
    } else {
        throw std::runtime_error(make_string("Can not deduce correct resultclass for attribute vector '%s'",
                                             attribute.getName().c_str()));
    }
}

void
AttributeNode::onPrepare(bool preserveAccurateTypes)
{
    const IAttributeVector * attribute = getAttribute();
    if (attribute != nullptr) {
        auto[result, handler] = createResultHandler(preserveAccurateTypes, *attribute);
        _handler = std::move(handler);
        if (_index == nullptr) {
            setResultType(std::move(result));
        } else {
            assert(_hasMultiValue);
            assert(_handler);
            setResultType(result->createBaseType());
            assert(result->inherits(ResultNodeVector::classId));
            _keepAliveForIndexLookups.reset(dynamic_cast<ResultNodeVector *>(result.release()));
        }
    }
}

template <typename V>
void
AttributeNode::IntegerHandler<V>::handle(const AttributeResult & r)
{
    size_t numValues = r.getAttribute()->getValueCount(r.getDocId());
    _vector.resize(numValues);
    _wVector.resize(numValues);
    r.getAttribute()->get(r.getDocId(), _wVector.data(), _wVector.size());
    for(size_t i(0); i < numValues; i++) {
        _vector[i] = _wVector[i].getValue();
    }
}

void
AttributeNode::FloatHandler::handle(const AttributeResult & r)
{
    size_t numValues = r.getAttribute()->getValueCount(r.getDocId());
    _vector.resize(numValues);
    _wVector.resize(numValues);
    r.getAttribute()->get(r.getDocId(), _wVector.data(), _wVector.size());
    for(size_t i(0); i < numValues; i++) {
        _vector[i] = _wVector[i].getValue();
    }
}

void
AttributeNode::StringHandler::handle(const AttributeResult & r)
{
    size_t numValues = r.getAttribute()->getValueCount(r.getDocId());
    _vector.resize(numValues);
    _wVector.resize(numValues);
    r.getAttribute()->get(r.getDocId(), _wVector.data(), _wVector.size());
    for(size_t i(0); i < numValues; i++) {
        _vector[i] = _wVector[i].getValue();
    }
}

void
AttributeNode::EnumHandler::handle(const AttributeResult & r)
{
    size_t numValues = r.getAttribute()->getValueCount(r.getDocId());
    _vector.resize(numValues);
    _wVector.resize(numValues);
    r.getAttribute()->get(r.getDocId(), _wVector.data(), _wVector.size());
    for(size_t i(0); i < numValues; i++) {
        _vector[i] = _wVector[i].getValue();
    }
}

void
AttributeNode::setDocId(DocId docId) {
    _scratchResult->setDocId(docId);
    _needExecute = true;
}

bool
AttributeNode::onExecute() const
{
    if (_handler) {
        if (_needExecute) {
            _handler->handle(*_scratchResult);
            _needExecute = false;
        }
        if ((_index != nullptr) && !_keepAliveForIndexLookups->empty()) {
            assert(_hasMultiValue);
            size_t idx = std::min(size_t(_index->get()), _keepAliveForIndexLookups->size() - 1);
            updateResult().set(_keepAliveForIndexLookups->get(idx));
        }
    } else {
        updateResult().set(*_scratchResult);
    }
    return true;
}

void
AttributeNode::wireAttributes(const IAttributeContext & attrCtx)
{
    const IAttributeVector * attribute(_scratchResult ? _scratchResult->getAttribute() : nullptr);
    if (attribute == nullptr) {
        if (_useEnumOptimization) {
            attribute = attrCtx.getAttributeStableEnum(_attributeName);
        } else {
            attribute = attrCtx.getAttribute(_attributeName);
        }
        if (attribute == nullptr) {
            throw std::runtime_error(make_string("Failed locating attribute vector '%s'", _attributeName.c_str()));
        }
        _hasMultiValue = attribute->hasMultiValue();
        _scratchResult = createResult(attribute);
    }
}

void
AttributeNode::cleanup()
{
    _scratchResult.reset();
}

Serializer &
AttributeNode::onSerialize(Serializer & os) const
{
    FunctionNode::onSerialize(os);
    return os << _attributeName;
}

Deserializer &
AttributeNode::onDeserialize(Deserializer & is)
{
    FunctionNode::onDeserialize(is);

    return is >> _attributeName;
}

void
AttributeNode::visitMembers(vespalib::ObjectVisitor &visitor) const
{
    visit(visitor, "attributeName", _attributeName);
}

}

// this function was added by ../../forcelink.sh
void forcelink_file_searchlib_expression_attributenode() {}