summaryrefslogtreecommitdiff
path: root/src/mongo/db/catalog/index_signature_test.cpp
blob: b7f50fe72bc15038514d695eea20abe3b6197da2 (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
222
223
224
225
226
227
228
229
230
231
/**
 *    Copyright (C) 2020-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.
 */

#include "mongo/platform/basic.h"

#include "mongo/db/catalog/catalog_test_fixture.h"
#include "mongo/db/catalog/collection.h"
#include "mongo/db/catalog/index_catalog_entry_impl.h"
#include "mongo/db/catalog_raii.h"
#include "mongo/db/index/index_descriptor.h"
#include "mongo/db/query/collection_query_info.h"

namespace mongo {
namespace {

class IndexSignatureTest : public CatalogTestFixture {
public:
    IndexSignatureTest() : CatalogTestFixture() {}

    StatusWith<const IndexCatalogEntry*> createIndex(BSONObj spec) {
        // Get the index catalog associated with the test collection.
        auto* indexCatalog = coll()->getIndexCatalog();
        // Build the specified index on the collection.
        WriteUnitOfWork wuow(opCtx());
        auto status = indexCatalog->createIndexOnEmptyCollection(opCtx(), spec);
        if (!status.isOK()) {
            return status.getStatus();
        }
        wuow.commit();
        // Find the index entry and return it.
        return indexCatalog->getEntry(indexCatalog->findIndexByName(
            opCtx(), spec.getStringField(IndexDescriptor::kIndexNameFieldName)));
    }

    std::unique_ptr<IndexDescriptor> makeIndexDescriptor(BSONObj spec) {
        auto keyPattern = spec.getObjectField(IndexDescriptor::kKeyPatternFieldName);
        return std::make_unique<IndexDescriptor>(
            coll(), IndexNames::findPluginName(keyPattern), spec);
    }

    const NamespaceString& nss() const {
        return _nss;
    }

    Collection* coll() const {
        return _coll->getCollection();
    }

    OperationContext* opCtx() {
        return operationContext();
    }

protected:
    void setUp() override {
        CatalogTestFixture::setUp();
        ASSERT_OK(storageInterface()->createCollection(opCtx(), _nss, {}));
        _coll.emplace(opCtx(), _nss, MODE_X);
    }

    void tearDown() override {
        _coll.reset();
        CatalogTestFixture::tearDown();
    }

private:
    boost::optional<AutoGetCollection> _coll;
    NamespaceString _nss{"fooDB.barColl"};
};

TEST_F(IndexSignatureTest, CanCreateMultipleIndexesOnSameKeyPatternWithDifferentCollations) {
    // Create an index on {a: 1} with an 'en_US' collation.
    auto indexSpec = fromjson("{v: 2, name: 'a_1', key: {a: 1}, collation: {locale: 'en_US'}}");
    auto* basicIndex = unittest::assertGet(createIndex(indexSpec));

    // Create an index descriptor on the same keyPattern whose collation has an explicit strength.
    // The two indexes compare as kIdentical, because the collation comparison is performed using
    // the parsed collator rather than the static collation object from the BSON index specs.
    auto collationDesc = makeIndexDescriptor(
        indexSpec.addFields(fromjson("{collation: {locale: 'en_US', strength: 3}}")));
    ASSERT(collationDesc->compareIndexOptions(opCtx(), basicIndex) ==
           IndexDescriptor::Comparison::kIdentical);

    // Confirm that attempting to build this index will result in ErrorCodes::IndexAlreadyExists.
    ASSERT_EQ(createIndex(collationDesc->infoObj()), ErrorCodes::IndexAlreadyExists);

    // Now set the unique field. The indexes now compare kEquivalent, but not kIdentical. This means
    // that all signature fields which uniquely identify the index match, but other fields differ.
    auto collationUniqueDesc =
        makeIndexDescriptor(collationDesc->infoObj().addFields(fromjson("{unique: true}")));
    ASSERT(collationUniqueDesc->compareIndexOptions(opCtx(), basicIndex) ==
           IndexDescriptor::Comparison::kEquivalent);

    // Attempting to build the index, whether with the same name or a different name, now throws
    // IndexOptionsConflict. The error message returned with the exception specifies whether the
    // name matches an existing index.
    ASSERT_EQ(createIndex(collationUniqueDesc->infoObj()), ErrorCodes::IndexOptionsConflict);
    ASSERT_EQ(createIndex(
                  collationUniqueDesc->infoObj().addFields(fromjson("{name: 'collationUnique'}"))),
              ErrorCodes::IndexOptionsConflict);

    // Now create an index spec with an entirely different collation. The two indexes compare as
    // being kDifferent; this means that both of these indexes can co-exist together.
    auto differentCollationDesc = makeIndexDescriptor(
        collationDesc->infoObj().addFields(fromjson("{collation: {locale: 'fr'}}")));
    ASSERT(differentCollationDesc->compareIndexOptions(opCtx(), basicIndex) ==
           IndexDescriptor::Comparison::kDifferent);

    // Verify that we can build this index alongside the existing indexes.
    ASSERT_OK(createIndex(
        differentCollationDesc->infoObj().addFields(fromjson("{name: 'differentCollation'}"))));
}

TEST_F(IndexSignatureTest,
       CanCreateMultipleIndexesOnSameKeyPatternWithDifferentPartialFilterExpressions) {
    // Create a basic index on {a: 1} with no partialFilterExpression.
    auto indexSpec = fromjson("{v: 2, name: 'a_1', key: {a: 1}}");
    auto* basicIndex = unittest::assertGet(createIndex(indexSpec));

    // Create an index descriptor on the same keyPattern with a partialFilterExpression. The two
    // indexes compare as kDifferent, because the partialFilterExpression is one of the fields in
    // the index's signature.
    auto partialFilterDesc = makeIndexDescriptor(indexSpec.addFields(
        fromjson("{partialFilterExpression: {a: {$gt: 5, $lt: 10}, b: 'blah'}}")));
    ASSERT(partialFilterDesc->compareIndexOptions(opCtx(), basicIndex) ==
           IndexDescriptor::Comparison::kDifferent);

    // Verify that we can build an index with this spec alongside the original index.
    auto* partialFilterIndex = unittest::assertGet(
        createIndex(partialFilterDesc->infoObj().addFields(fromjson("{name: 'partialFilter'}"))));

    // Verify that partialFilterExpressions are normalized before being compared. Here, the filter
    // is expressed differently than in the existing index, but the two still compare as kIdentical.
    auto partialFilterDupeDesc = makeIndexDescriptor(indexSpec.addFields(
        fromjson("{name: 'partialFilter', partialFilterExpression: {$and: [{b: 'blah'}, "
                 "{a: {$lt: 10}}, {a: {$gt: 5}}]}}")));
    ASSERT(partialFilterDupeDesc->compareIndexOptions(opCtx(), partialFilterIndex) ==
           IndexDescriptor::Comparison::kIdentical);

    // Confirm that attempting to build this index will result in ErrorCodes::IndexAlreadyExists.
    ASSERT_EQ(createIndex(partialFilterDupeDesc->infoObj()), ErrorCodes::IndexAlreadyExists);

    // Now set the unique field. The indexes now compare kEquivalent, but not kIdentical. This means
    // that all signature fields which uniquely identify the index match, but other fields differ.
    auto partialFilterUniqueDesc =
        makeIndexDescriptor(partialFilterDesc->infoObj().addFields(fromjson("{unique: true}")));
    ASSERT(partialFilterUniqueDesc->compareIndexOptions(opCtx(), partialFilterIndex) ==
           IndexDescriptor::Comparison::kEquivalent);

    // Attempting to build the index, whether with the same name or a different name, now throws
    // IndexOptionsConflict. The error message returned with the exception specifies whether the
    // name matches an existing index.
    ASSERT_EQ(createIndex(partialFilterUniqueDesc->infoObj().addFields(
                  fromjson("{name: 'partialFilterExpression'}"))),
              ErrorCodes::IndexOptionsConflict);
    ASSERT_EQ(createIndex(partialFilterUniqueDesc->infoObj().addFields(
                  fromjson("{name: 'partialFilterUnique'}"))),
              ErrorCodes::IndexOptionsConflict);

    // Now create an index spec with an entirely different partialFilterExpression. The two indexes
    // compare as kDifferent; this means that both of these indexes can co-exist together.
    auto differentPartialFilterDesc = makeIndexDescriptor(partialFilterDesc->infoObj().addFields(
        fromjson("{partialFilterExpression: {a: {$gt: 0, $lt: 10}, b: 'blah'}}")));
    ASSERT(differentPartialFilterDesc->compareIndexOptions(opCtx(), partialFilterIndex) ==
           IndexDescriptor::Comparison::kDifferent);

    // Verify that we can build this index alongside the existing indexes.
    ASSERT_OK(createIndex(differentPartialFilterDesc->infoObj().addFields(
        fromjson("{name: 'differentPartialFilter'}"))));
}

TEST_F(IndexSignatureTest, CannotCreateMultipleIndexesOnSameKeyPatternIfNonSignatureFieldsDiffer) {
    // Create a basic index on {a: 1} with all other options set to their default values.
    auto indexSpec = fromjson("{v: 2, name: 'a_1', key: {a: 1}}");
    auto* basicIndex = unittest::assertGet(createIndex(indexSpec));

    // TODO SERVER-47657: unique and sparse should be part of the signature.
    std::vector<BSONObj> nonSigOptions = {
        BSON(IndexDescriptor::kUniqueFieldName << true),
        BSON(IndexDescriptor::kSparseFieldName << true),
        BSON(IndexDescriptor::kExpireAfterSecondsFieldName << 10)};

    // Verify that changing each of the non-signature fields does not distinguish this index from
    // the existing index. The two are considered equivalent, and we cannot build the new index.
    for (auto&& nonSigOpt : nonSigOptions) {
        auto nonSigDesc = makeIndexDescriptor(indexSpec.addFields(nonSigOpt));
        ASSERT(nonSigDesc->compareIndexOptions(opCtx(), basicIndex) ==
               IndexDescriptor::Comparison::kEquivalent);
        ASSERT_EQ(createIndex(nonSigDesc->infoObj()), ErrorCodes::IndexOptionsConflict);
    }

    // Build a wildcard index and confirm that 'wildcardProjection' is a non-signature field.
    // TODO SERVER-47659: wildcardProjection should be part of the signature.
    auto* wildcardIndex =
        unittest::assertGet(createIndex(fromjson("{v: 2, name: '$**_1', key: {'$**': 1}}")));
    auto nonSigWildcardDesc = makeIndexDescriptor(
        wildcardIndex->descriptor()->infoObj().addFields(fromjson("{wildcardProjection: {a: 1}}")));
    ASSERT(nonSigWildcardDesc->compareIndexOptions(opCtx(), wildcardIndex) ==
           IndexDescriptor::Comparison::kEquivalent);
    ASSERT_EQ(
        createIndex(nonSigWildcardDesc->infoObj().addFields(fromjson("{name: 'nonSigWildcard'}"))),
        ErrorCodes::IndexOptionsConflict);
}

}  // namespace
}  // namespace mongo