summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/parsed_projection_test.cpp
blob: f850221b5fe6722110ee14a9fdd104a084749522 (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
232
233
234
235
236
237
238
/**
 *    Copyright (C) 2013 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    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
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    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 GNU Affero General 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/db/query/parsed_projection.h"

#include <memory>
#include "mongo/db/json.h"
#include "mongo/db/matcher/expression_parser.h"
#include "mongo/unittest/unittest.h"

namespace {

using std::unique_ptr;
using std::string;
using std::vector;

using namespace mongo;

//
// creation function
//

unique_ptr<ParsedProjection> createParsedProjection(const BSONObj& query, const BSONObj& projObj) {
    StatusWithMatchExpression statusWithMatcher = MatchExpressionParser::parse(query);
    ASSERT(statusWithMatcher.isOK());
    std::unique_ptr<MatchExpression> queryMatchExpr = std::move(statusWithMatcher.getValue());
    ParsedProjection* out = NULL;
    Status status = ParsedProjection::make(projObj, queryMatchExpr.get(), &out);
    if (!status.isOK()) {
        FAIL(mongoutils::str::stream() << "failed to parse projection " << projObj
                                       << " (query: " << query << "): " << status.toString());
    }
    ASSERT(out);
    return unique_ptr<ParsedProjection>(out);
}

unique_ptr<ParsedProjection> createParsedProjection(const char* queryStr, const char* projStr) {
    BSONObj query = fromjson(queryStr);
    BSONObj projObj = fromjson(projStr);
    return createParsedProjection(query, projObj);
}

//
// Failure to create a parsed projection is expected
//

void assertInvalidProjection(const char* queryStr, const char* projStr) {
    BSONObj query = fromjson(queryStr);
    BSONObj projObj = fromjson(projStr);
    StatusWithMatchExpression statusWithMatcher = MatchExpressionParser::parse(query);
    ASSERT(statusWithMatcher.isOK());
    std::unique_ptr<MatchExpression> queryMatchExpr = std::move(statusWithMatcher.getValue());
    ParsedProjection* out = NULL;
    Status status = ParsedProjection::make(projObj, queryMatchExpr.get(), &out);
    std::unique_ptr<ParsedProjection> destroy(out);
    ASSERT(!status.isOK());
}

// canonical_query.cpp will invoke ParsedProjection::make only when
// the projection spec is non-empty. This test case is included for
// completeness and do not reflect actual usage.
TEST(ParsedProjectionTest, MakeId) {
    unique_ptr<ParsedProjection> parsedProj(createParsedProjection("{}", "{}"));
    ASSERT(parsedProj->requiresDocument());
}

TEST(ParsedProjectionTest, MakeEmpty) {
    unique_ptr<ParsedProjection> parsedProj(createParsedProjection("{}", "{_id: 0}"));
    ASSERT(parsedProj->requiresDocument());
}

TEST(ParsedProjectionTest, MakeSingleField) {
    unique_ptr<ParsedProjection> parsedProj(createParsedProjection("{}", "{a: 1}"));
    ASSERT(!parsedProj->requiresDocument());
    const vector<string>& fields = parsedProj->getRequiredFields();
    ASSERT_EQUALS(fields.size(), 2U);
    ASSERT_EQUALS(fields[0], "_id");
    ASSERT_EQUALS(fields[1], "a");
}

TEST(ParsedProjectionTest, MakeSingleFieldCovered) {
    unique_ptr<ParsedProjection> parsedProj(createParsedProjection("{}", "{_id: 0, a: 1}"));
    ASSERT(!parsedProj->requiresDocument());
    const vector<string>& fields = parsedProj->getRequiredFields();
    ASSERT_EQUALS(fields.size(), 1U);
    ASSERT_EQUALS(fields[0], "a");
}

TEST(ParsedProjectionTest, MakeSingleFieldIDCovered) {
    unique_ptr<ParsedProjection> parsedProj(createParsedProjection("{}", "{_id: 1}"));
    ASSERT(!parsedProj->requiresDocument());
    const vector<string>& fields = parsedProj->getRequiredFields();
    ASSERT_EQUALS(fields.size(), 1U);
    ASSERT_EQUALS(fields[0], "_id");
}

// boolean support is undocumented
TEST(ParsedProjectionTest, MakeSingleFieldCoveredBoolean) {
    unique_ptr<ParsedProjection> parsedProj(createParsedProjection("{}", "{_id: 0, a: true}"));
    ASSERT(!parsedProj->requiresDocument());
    const vector<string>& fields = parsedProj->getRequiredFields();
    ASSERT_EQUALS(fields.size(), 1U);
    ASSERT_EQUALS(fields[0], "a");
}

// boolean support is undocumented
TEST(ParsedProjectionTest, MakeSingleFieldCoveredIdBoolean) {
    unique_ptr<ParsedProjection> parsedProj(createParsedProjection("{}", "{_id: false, a: 1}"));
    ASSERT(!parsedProj->requiresDocument());
    const vector<string>& fields = parsedProj->getRequiredFields();
    ASSERT_EQUALS(fields.size(), 1U);
    ASSERT_EQUALS(fields[0], "a");
}

//
// Positional operator validation
//

TEST(ParsedProjectionTest, InvalidPositionalOperatorProjections) {
    assertInvalidProjection("{}", "{'a.$': 1}");
    assertInvalidProjection("{a: 1}", "{'b.$': 1}");
    assertInvalidProjection("{a: 1}", "{'a.$': 0}");
    assertInvalidProjection("{a: 1}", "{'a.$.d.$': 1}");
    assertInvalidProjection("{a: 1}", "{'a.$.$': 1}");
    assertInvalidProjection("{a: 1}", "{'a.$.$': 1}");
    assertInvalidProjection("{a: 1, b: 1, c: 1}", "{'abc.$': 1}");
    assertInvalidProjection("{$or: [{a: 1}, {$or: [{b: 1}, {c: 1}]}]}", "{'d.$': 1}");
    assertInvalidProjection("{a: [1, 2, 3]}", "{'.$': 1}");
}

TEST(ParsedProjectionTest, ValidPositionalOperatorProjections) {
    createParsedProjection("{a: 1}", "{'a.$': 1}");
    createParsedProjection("{a: 1}", "{'a.foo.bar.$': 1}");
    createParsedProjection("{a: 1}", "{'a.foo.bar.$.x.y': 1}");
    createParsedProjection("{'a.b.c': 1}", "{'a.b.c.$': 1}");
    createParsedProjection("{'a.b.c': 1}", "{'a.e.f.$': 1}");
    createParsedProjection("{a: {b: 1}}", "{'a.$': 1}");
    createParsedProjection("{a: 1, b: 1}}", "{'a.$': 1}");
    createParsedProjection("{a: 1, b: 1}}", "{'b.$': 1}");
    createParsedProjection("{$and: [{a: 1}, {b: 1}]}", "{'a.$': 1}");
    createParsedProjection("{$and: [{a: 1}, {b: 1}]}", "{'b.$': 1}");
    createParsedProjection("{$or: [{a: 1}, {b: 1}]}", "{'a.$': 1}");
    createParsedProjection("{$or: [{a: 1}, {b: 1}]}", "{'b.$': 1}");
    createParsedProjection("{$and: [{$or: [{a: 1}, {$and: [{b: 1}, {c: 1}]}]}]}", "{'c.d.f.$': 1}");
    // Fields with empty name can be projected using the positional $ operator.
    createParsedProjection("{'': [1, 2, 3]}", "{'.$': 1}");
}

// Some match expressions (eg. $where) do not override MatchExpression::path()
// In this test case, we use an internal match expression implementation ALWAYS_FALSE
// to achieve the same effect.
// Projection parser should handle this the same way as an empty path.
TEST(ParsedProjectionTest, InvalidPositionalProjectionDefaultPathMatchExpression) {
    unique_ptr<MatchExpression> queryMatchExpr(new FalseMatchExpression());
    ASSERT(NULL == queryMatchExpr->path().rawData());

    ParsedProjection* out = NULL;
    BSONObj projObj = fromjson("{'a.$': 1}");
    Status status = ParsedProjection::make(projObj, queryMatchExpr.get(), &out);
    ASSERT(!status.isOK());
    std::unique_ptr<ParsedProjection> destroy(out);

    // Projecting onto empty field should fail.
    BSONObj emptyFieldProjObj = fromjson("{'.$': 1}");
    status = ParsedProjection::make(emptyFieldProjObj, queryMatchExpr.get(), &out);
    ASSERT(!status.isOK());
}

TEST(ParsedProjectionTest, ParsedProjectionDefaults) {
    auto parsedProjection = createParsedProjection("{}", "{}");

    ASSERT_FALSE(parsedProjection->wantSortKey());
    ASSERT_TRUE(parsedProjection->requiresDocument());
    ASSERT_FALSE(parsedProjection->requiresMatchDetails());
    ASSERT_FALSE(parsedProjection->wantGeoNearDistance());
    ASSERT_FALSE(parsedProjection->wantGeoNearPoint());
    ASSERT_FALSE(parsedProjection->wantIndexKey());
}

TEST(ParsedProjectionTest, SortKeyMetaProjection) {
    auto parsedProjection = createParsedProjection("{}", "{foo: {$meta: 'sortKey'}}");

    ASSERT_EQ(parsedProjection->getProjObj(), fromjson("{foo: {$meta: 'sortKey'}}"));
    ASSERT_TRUE(parsedProjection->wantSortKey());
    ASSERT_TRUE(parsedProjection->requiresDocument());

    ASSERT_FALSE(parsedProjection->requiresMatchDetails());
    ASSERT_FALSE(parsedProjection->wantGeoNearDistance());
    ASSERT_FALSE(parsedProjection->wantGeoNearPoint());
    ASSERT_FALSE(parsedProjection->wantIndexKey());
}

//
// DBRef projections
//

TEST(ParsedProjectionTest, DBRefProjections) {
    // non-dotted
    createParsedProjection(BSONObj(), BSON("$ref" << 1));
    createParsedProjection(BSONObj(), BSON("$id" << 1));
    createParsedProjection(BSONObj(), BSON("$ref" << 1));
    // dotted before
    createParsedProjection("{}", "{'a.$ref': 1}");
    createParsedProjection("{}", "{'a.$id': 1}");
    createParsedProjection("{}", "{'a.$db': 1}");
    // dotted after
    createParsedProjection("{}", "{'$id.a': 1}");
    // position operator on $id
    // $ref and $db hold the collection and database names respectively,
    // so these fields cannot be arrays.
    createParsedProjection("{'a.$id': {$elemMatch: {x: 1}}}", "{'a.$id.$': 1}");
}
}  // unnamed namespace