aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/queryeval/predicate_blueprint.h
blob: 977dd4ddb4f4016073173382b1c8abf645edce50 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "blueprint.h"
#include "predicate_search.h"
#include <vespa/searchlib/common/bitvectorcache.h>
#include <vespa/searchlib/predicate/simple_index.h>
#include <vespa/searchlib/attribute/attributeguard.h>
#include <vespa/searchlib/attribute/predicate_attribute.h>

namespace search::query { class PredicateQuery; }

namespace search::queryeval {
/**
 * Blueprint for building predicate searches. It builds search
 * iterators based on PredicateSearch.
 */
class PredicateBlueprint : public ComplexLeafBlueprint {
public:
    struct IntervalEntry {
        vespalib::datastore::EntryRef entry_ref;
        uint64_t subquery;
        size_t   size;
        uint64_t feature;
    };
    struct BoundsEntry {
        vespalib::datastore::EntryRef entry_ref;
        uint32_t value_diff;
        uint64_t subquery;
        size_t   size;
        uint64_t feature;
    };
    template<typename I>
    struct IntervalIteratorEntry {
        I iterator;
        const IntervalEntry &entry;
    };
    template<typename I>
    struct BoundsIteratorEntry {
        I iterator;
        const BoundsEntry &entry;
    };

    PredicateBlueprint(const FieldSpecBase &field,
                       const PredicateAttribute & attribute,
                       const query::PredicateQuery &query);

    ~PredicateBlueprint();
    void fetchPostings(const ExecuteInfo &execInfo) override;

    void sort(InFlow in_flow) override;
    FlowStats calculate_flow_stats(uint32_t) const override {
        return default_flow_stats(_interval_btree_iterators.size() + _interval_vector_iterators.size() +
                                  _bounds_btree_iterators.size() + _bounds_vector_iterators.size() + 2);
    }

    SearchIterator::UP
    createLeafSearch(const fef::TermFieldMatchDataArray &tfmda) const override;
    SearchIteratorUP createFilterSearch(FilterConstraint constraint) const override {
        return create_default_filter(constraint);
    }

    // Exposed for testing
    const BitVectorCache::CountVector &  getKV() const { return _kV; }
    const BitVectorCache::KeySet &       getCachedFeatures() const { return _cachedFeatures; }
private:
    using BTreeIterator = predicate::SimpleIndex<vespalib::datastore::EntryRef>::BTreeIterator;
    using VectorIterator = predicate::SimpleIndex<vespalib::datastore::EntryRef>::VectorIterator;
    template <typename T>
    using optional = std::optional<T>;
    using Alloc = vespalib::alloc::Alloc;

    const PredicateAttribute & predicate_attribute() const {
        return _attribute;
    }
    PredicateAttribute & predicate_attribute() {
        return const_cast<PredicateAttribute &>(_attribute);
    }
    void addBoundsPostingToK(uint64_t feature);
    void addPostingToK(uint64_t feature);
    void addZeroConstraintToK();
    std::vector<predicate::PredicatePostingList::UP> createPostingLists() const;

    const PredicateAttribute        & _attribute;
    const predicate::PredicateIndex &_index;
    Alloc                            _kVBacking;
    BitVectorCache::CountVector      _kV;
    BitVectorCache::KeySet           _cachedFeatures;

    std::vector<IntervalEntry>       _interval_dict_entries;
    std::vector<BoundsEntry>         _bounds_dict_entries;
    vespalib::datastore::EntryRef    _zstar_dict_entry;

    std::vector<IntervalIteratorEntry<BTreeIterator>>  _interval_btree_iterators;
    std::vector<IntervalIteratorEntry<VectorIterator>> _interval_vector_iterators;
    std::vector<BoundsIteratorEntry<BTreeIterator>>    _bounds_btree_iterators;
    std::vector<BoundsIteratorEntry<VectorIterator>>   _bounds_vector_iterators;
    // The zstar iterator is either a vector or a btree iterator.
    optional<BTreeIterator>  _zstar_btree_iterator;
    optional<VectorIterator> _zstar_vector_iterator;
    bool                     _fetch_postings_done;
};

}