summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/index_bounds_builder_test.h
blob: c8c036f2d83e4672bc9179bcb857d27cc048cdf0 (plain)
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
/**
 *    Copyright (C) 2019-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#pragma once

#include "mongo/db/matcher/expression_parser.h"
#include "mongo/db/pipeline/expression_context_for_test.h"
#include "mongo/db/query/index_bounds_builder.h"
#include "mongo/db/query/interval_evaluation_tree.h"
#include "mongo/unittest/unittest.h"

namespace mongo {

class IndexBoundsBuilderTest : public unittest::Test {
public:
    /**
     * Utility function to create MatchExpression
     */
    std::pair<std::unique_ptr<MatchExpression>, std::vector<const MatchExpression*>>
    parseMatchExpression(const BSONObj& obj, bool shouldNormalize = true) {
        boost::intrusive_ptr<ExpressionContextForTest> expCtx(new ExpressionContextForTest());
        StatusWithMatchExpression status = MatchExpressionParser::parse(obj, std::move(expCtx));
        ASSERT_OK(status.getStatus()) << obj;
        auto expr = std::move(status.getValue());
        if (shouldNormalize) {
            expr = MatchExpression::normalize(std::move(expr));
        }
        auto inputParamIdMap = MatchExpression::parameterize(expr.get());
        return {std::move(expr), inputParamIdMap};
    }

    /**
     * Make a minimal IndexEntry from just an optional key pattern. A dummy name will be added. An
     * empty key pattern will be used if none is provided.
     */
    IndexEntry buildSimpleIndexEntry(const BSONObj& kp = BSONObj()) {
        return {kp,
                IndexNames::nameToType(IndexNames::findPluginName(kp)),
                IndexDescriptor::kLatestIndexVersion,
                false,
                {},
                {},
                false,
                false,
                CoreIndexInfo::Identifier("test_foo"),
                nullptr,
                {},
                nullptr,
                nullptr};
    }

    /**
     * Make a multikey IndexEntry with the provided key pattern and multikey paths.
     */
    IndexEntry buildMultikeyIndexEntry(const BSONObj& kp, const MultikeyPaths& mkp) {
        return {kp,
                IndexNames::nameToType(IndexNames::findPluginName(kp)),
                IndexDescriptor::kLatestIndexVersion,
                true,  // multikey
                mkp,   // multikey paths
                {},
                false,
                false,
                IndexEntry::Identifier{"test_multikey"},
                nullptr,
                {},
                nullptr,
                nullptr};
    }

    /**
     * Make a Wildcard IndexEntry with the provided key pattern and multikey paths.
     */
    IndexEntry buildWildcardIndexEntry(const BSONObj& kp,
                                       const MultikeyPaths& mkp,
                                       const size_t wildcardPos = 0) {
        bool isMultikey = false;
        for (const auto& val : mkp) {
            if (!val.empty()) {
                isMultikey = true;
                break;
            }
        }
        return {kp,
                IndexType::INDEX_WILDCARD,
                IndexDescriptor::kLatestIndexVersion,
                isMultikey,  // multikey
                mkp,         // multikey paths
                {},
                false,
                false,
                IndexEntry::Identifier{"test_wildcard"},
                nullptr,
                {},
                nullptr,
                nullptr,
                wildcardPos};
    }

    /**
     * Given a list of queries in 'toUnion', translate into index bounds and return
     * the union of these bounds in the out-parameter 'oilOut'.
     */
    void testTranslateAndUnion(const std::vector<BSONObj>& toUnion,
                               OrderedIntervalList* oilOut,
                               IndexBoundsBuilder::BoundsTightness* tightnessOut,
                               bool shouldNormalize = true) {
        auto testIndex = buildSimpleIndexEntry();
        auto obj = BSON("$or" << toUnion);
        auto [expr, inputParamIdMap] = parseMatchExpression(obj, shouldNormalize);
        BSONElement elt = toUnion[0].firstElement();
        interval_evaluation_tree::Builder ietBuilder{};

        ASSERT_EQ(MatchExpression::OR, expr->matchType()) << expr->debugString();
        for (size_t childIndex = 0; childIndex < expr->numChildren(); ++childIndex) {
            auto child = expr->getChild(childIndex);
            if (childIndex == 0) {
                IndexBoundsBuilder::translate(
                    child, elt, testIndex, oilOut, tightnessOut, &ietBuilder);
            } else {
                IndexBoundsBuilder::translateAndUnion(
                    child, elt, testIndex, oilOut, tightnessOut, &ietBuilder);
            }
        }

        assertIET(inputParamIdMap, ietBuilder, elt, testIndex, *oilOut);
    }

    /**
     * Given a list of queries in 'toUnion', translate into index bounds and return
     * the intersection of these bounds in the out-parameter 'oilOut'.
     */
    void testTranslateAndIntersect(const std::vector<BSONObj>& toIntersect,
                                   OrderedIntervalList* oilOut,
                                   IndexBoundsBuilder::BoundsTightness* tightnessOut) {
        auto testIndex = buildSimpleIndexEntry();
        auto obj = BSON("$and" << toIntersect);
        auto [expr, inputParamIdMap] = parseMatchExpression(obj);
        BSONElement elt = toIntersect[0].firstElement();
        interval_evaluation_tree::Builder ietBuilder{};

        ASSERT_EQ(MatchExpression::AND, expr->matchType());
        for (size_t childIndex = 0; childIndex < expr->numChildren(); ++childIndex) {
            auto child = expr->getChild(childIndex);
            if (childIndex == 0) {
                IndexBoundsBuilder::translate(
                    child, elt, testIndex, oilOut, tightnessOut, &ietBuilder);
            } else {
                IndexBoundsBuilder::translateAndIntersect(
                    child, elt, testIndex, oilOut, tightnessOut, &ietBuilder);
            }
        }

        assertIET(inputParamIdMap, ietBuilder, elt, testIndex, *oilOut);
    }

    /**
     * Get a BSONObj which represents the interval from MinKey to 'end'.
     */
    BSONObj minKeyIntObj(int end) {
        BSONObjBuilder bob;
        bob.appendMinKey("");
        bob.appendNumber("", end);
        return bob.obj();
    }

    /**
     * Get a BSONObj which represents the interval from 'start' to MaxKey.
     */
    BSONObj maxKeyIntObj(int start) {
        BSONObjBuilder bob;
        bob.appendNumber("", start);
        bob.appendMaxKey("");
        return bob.obj();
    }

    /**
     * Evaluates index index intervals from the given Interval Evaluation Trees (IET) and asserts
     * the the result is equal to the given OrderedIntervalList.
     */
    static void assertIET(const std::vector<const MatchExpression*>& inputParamIdMap,
                          const interval_evaluation_tree::Builder& ietBuilder,
                          const BSONElement& elt,
                          const IndexEntry& index,
                          const OrderedIntervalList& oil) {
        auto iet = ietBuilder.done();
        ASSERT(iet);

        auto restoredOil =
            interval_evaluation_tree::evaluateIntervals(*iet, inputParamIdMap, elt, index);
        ASSERT(oil == restoredOil);
    }
};

}  // namespace mongo