summaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/features/internal_max_reduce_prod_join_feature.cpp
blob: b096e6e8ff5542d997e3330408ab19f0acca896c (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "internal_max_reduce_prod_join_feature.h"
#include "valuefeature.h"
#include "weighted_set_parser.h"
#include "dotproductfeature.h"

#include <vespa/searchlib/attribute/attribute.h>
#include <vespa/searchlib/attribute/imported_attribute_vector_read_guard.h>
#include <vespa/searchlib/attribute/multinumericattribute.h>
#include <vespa/searchlib/fef/properties.h>
#include <vespa/searchlib/fef/featureexecutor.h>
#include <vespa/searchcommon/common/datatype.h>

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

using namespace search::attribute;
using namespace search::fef;

using search::features::dotproduct::wset::IntegerVector;

namespace search::features {

/**
 * Executor used when array can be accessed directly
 */
template <typename BaseType>
class RawExecutor : public FeatureExecutor {
protected:
    const IAttributeVector *_attribute;
    IntegerVector _queryVector;

public:
    RawExecutor(const IAttributeVector *attribute, const IntegerVector &queryVector);
    void execute(uint32_t docId) override;
};

template <typename BaseType>
RawExecutor<BaseType>::RawExecutor(const IAttributeVector *attribute, const IntegerVector &queryVector) :
        FeatureExecutor(),
        _attribute(attribute),
        _queryVector(queryVector)
{
    _queryVector.syncMap();
}

template <typename A, typename V>
feature_t maxProduct(const A &array, size_t count, const V &query)
{
    feature_t val = -std::numeric_limits<double>::max();
    for (size_t i = 0; i < count; ++i) {
        auto itr = query.getDimMap().find(array[i].value());
        if (itr != query.getDimMap().end()) {
            feature_t v = itr->second; // weight from attribute is assumed to be 1.0
            if (v > val) {
                val = v;
            }
        }
    }
    return val == -std::numeric_limits<double>::max() ? 0.0 : val;
}

template <typename BaseType>
void
RawExecutor<BaseType>::execute(uint32_t docId)
{
    using A = IntegerAttributeTemplate<BaseType>;
    const multivalue::Value<BaseType> *values(nullptr);
    const A *iattr = dynamic_cast<const A *>(_attribute);
    size_t count = iattr->getRawValues(docId, values);
    outputs().set_number(0, maxProduct(values, count, _queryVector));
}

/**
 * Executor when array can't be accessed directly
 */
template <typename BaseType>
class BufferedExecutor : public RawExecutor<BaseType> {
private:
    WeightedIntegerContent _buffer;

public:
    BufferedExecutor(const IAttributeVector *attribute, const IntegerVector &queryVector);
    void execute(uint32_t docId) override;
};

template <typename BaseType>
BufferedExecutor<BaseType>::BufferedExecutor(const IAttributeVector *attribute, const IntegerVector &queryVector) :
    RawExecutor<BaseType>(attribute, queryVector),
    _buffer()
{
}


template <typename BaseType>
void
BufferedExecutor<BaseType>::execute(uint32_t docId)
{
    _buffer.fill(*(this->_attribute), docId);
    this->outputs().set_number(0, maxProduct(_buffer, _buffer.size(), this->_queryVector));
}

/**
 * Blueprint
 */
InternalMaxReduceProdJoinBlueprint::InternalMaxReduceProdJoinBlueprint() :
        Blueprint("internalMaxReduceProdJoin")
{
}

InternalMaxReduceProdJoinBlueprint::~InternalMaxReduceProdJoinBlueprint()
{
}

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

Blueprint::UP
InternalMaxReduceProdJoinBlueprint::createInstance() const
{
    return Blueprint::UP(new InternalMaxReduceProdJoinBlueprint());
}

ParameterDescriptions
InternalMaxReduceProdJoinBlueprint::getDescriptions() const
{
    return ParameterDescriptions().desc().attribute(ParameterDataTypeSet::int32OrInt64TypeSet(), ParameterCollection::ARRAY).string();
}

bool
InternalMaxReduceProdJoinBlueprint::setup(const IIndexEnvironment &env, const ParameterList &params)
{
    _attribute = params[0].getValue();
    _query = params[1].getValue();
    describeOutput("scalar", "Internal executor for optimized execution of reduce(join(A,Q,f(x,y)(x*y)),max)");
    env.hintAttributeAccess(_attribute);
    return true;
}

template<typename A>
bool supportsGetRawValues(const A &attr) noexcept {
    try {
        const multivalue::Value<typename A::BaseType> *tmp = nullptr;
        attr.getRawValues(0, tmp); // Throws if unsupported
        return true;
    } catch (const std::runtime_error &e) {
        (void) e;
        return false;
    }
}

template <typename BaseType>
FeatureExecutor &
selectTypedExecutor(const IAttributeVector *attribute, const IntegerVector &vector, vespalib::Stash &stash)
{
    if (!attribute->isImported()) {
        using A = IntegerAttributeTemplate<BaseType>;
        using VT = multivalue::Value<BaseType>;
        using ExactA = MultiValueNumericAttribute<A, VT>;

        const A *iattr = dynamic_cast<const A *>(attribute);
        if (supportsGetRawValues(*iattr)) {
            const ExactA *exactA = dynamic_cast<const ExactA *>(iattr);
            if (exactA != nullptr) {
                return stash.create<RawExecutor<BaseType>>(attribute, vector);
            }
        }
    }
    return stash.create<BufferedExecutor<BaseType>>(attribute, vector);
}

FeatureExecutor &
selectExecutor(const IAttributeVector *attribute, const IntegerVector &vector, vespalib::Stash &stash)
{
    if (attribute->getCollectionType() == CollectionType::ARRAY) {
        switch (attribute->getBasicType()) {
            case BasicType::INT32:
                return selectTypedExecutor<int32_t>(attribute, vector, stash);
            case BasicType::INT64:
                return selectTypedExecutor<int64_t>(attribute, vector, stash);
            default:
                break;
        }
    }
    LOG(warning, "The attribute vector '%s' is not of type "
            "array<int/long>, returning executor with default value.", attribute->getName().c_str());
    return stash.create<SingleZeroValueExecutor>();
}


FeatureExecutor &
InternalMaxReduceProdJoinBlueprint::createExecutor(const IQueryEnvironment &env, vespalib::Stash &stash) const
{
    const IAttributeVector *attribute = env.getAttributeContext().getAttribute(_attribute);
    if (attribute == nullptr) {
        LOG(warning, "The attribute vector '%s' was not found in the attribute manager, "
                "returning executor with default value.",
            _attribute.c_str());
        return stash.create<SingleZeroValueExecutor>();
    }
    Property prop = env.getProperties().lookup(_query);
    if (prop.found() && !prop.get().empty()) {
        IntegerVector vector;
        WeightedSetParser::parse(prop.get(), vector);
        if (!vector.getVector().empty()) {
            return selectExecutor(attribute, vector, stash);
        }
    }
    return stash.create<SingleZeroValueExecutor>();
}

}