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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/**
* Copyright (C) 2011 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/>.
*/
#include "pch.h"
#include "db/pipeline/document_source.h"
#include "db/jsobj.h"
#include "db/pipeline/accumulator.h"
#include "db/pipeline/document.h"
#include "db/pipeline/expression.h"
#include "db/pipeline/expression_context.h"
#include "db/pipeline/value.h"
namespace mongo {
const char DocumentSourceGroup::groupName[] = "$group";
DocumentSourceGroup::~DocumentSourceGroup() {
}
bool DocumentSourceGroup::eof() {
if (!populated)
populate();
return (groupsIterator == groups.end());
}
bool DocumentSourceGroup::advance() {
if (!populated)
populate();
assert(groupsIterator != groups.end());
++groupsIterator;
if (groupsIterator == groups.end()) {
pCurrent.reset();
return false;
}
pCurrent = makeDocument(groupsIterator);
return true;
}
intrusive_ptr<Document> DocumentSourceGroup::getCurrent() {
if (!populated)
populate();
return pCurrent;
}
void DocumentSourceGroup::sourceToBson(BSONObjBuilder *pBuilder) const {
BSONObjBuilder insides;
/* add the _id */
pIdExpression->addToBsonObj(&insides, Document::idName.c_str(), 0);
/* add the remaining fields */
const size_t n = vFieldName.size();
for(size_t i = 0; i < n; ++i) {
intrusive_ptr<Accumulator> pA((*vpAccumulatorFactory[i])(pCtx));
pA->addOperand(vpExpression[i]);
pA->addToBsonObj(&insides, vFieldName[i], 0);
}
pBuilder->append(groupName, insides.done());
}
intrusive_ptr<DocumentSourceGroup> DocumentSourceGroup::create(
const intrusive_ptr<ExpressionContext> &pCtx) {
intrusive_ptr<DocumentSourceGroup> pSource(
new DocumentSourceGroup(pCtx));
return pSource;
}
DocumentSourceGroup::DocumentSourceGroup(
const intrusive_ptr<ExpressionContext> &pTheCtx):
populated(false),
pIdExpression(),
groups(),
vFieldName(),
vpAccumulatorFactory(),
vpExpression(),
pCtx(pTheCtx) {
}
void DocumentSourceGroup::addAccumulator(
string fieldName,
intrusive_ptr<Accumulator> (*pAccumulatorFactory)(
const intrusive_ptr<ExpressionContext> &),
const intrusive_ptr<Expression> &pExpression) {
vFieldName.push_back(fieldName);
vpAccumulatorFactory.push_back(pAccumulatorFactory);
vpExpression.push_back(pExpression);
}
struct GroupOpDesc {
const char *pName;
intrusive_ptr<Accumulator> (*pFactory)(
const intrusive_ptr<ExpressionContext> &);
};
static int GroupOpDescCmp(const void *pL, const void *pR) {
return strcmp(((const GroupOpDesc *)pL)->pName,
((const GroupOpDesc *)pR)->pName);
}
/*
Keep these sorted alphabetically so we can bsearch() them using
GroupOpDescCmp() above.
*/
static const GroupOpDesc GroupOpTable[] = {
{"$addToSet", AccumulatorAddToSet::create},
{"$avg", AccumulatorAvg::create},
{"$first", AccumulatorFirst::create},
{"$last", AccumulatorLast::create},
{"$max", AccumulatorMinMax::createMax},
{"$min", AccumulatorMinMax::createMin},
{"$push", AccumulatorPush::create},
{"$sum", AccumulatorSum::create},
};
static const size_t NGroupOp = sizeof(GroupOpTable)/sizeof(GroupOpTable[0]);
intrusive_ptr<DocumentSource> DocumentSourceGroup::createFromBson(
BSONElement *pBsonElement,
const intrusive_ptr<ExpressionContext> &pCtx) {
uassert(15947, "a group's fields must be specified in an object",
pBsonElement->type() == Object);
intrusive_ptr<DocumentSourceGroup> pGroup(
DocumentSourceGroup::create(pCtx));
bool idSet = false;
BSONObj groupObj(pBsonElement->Obj());
BSONObjIterator groupIterator(groupObj);
while(groupIterator.more()) {
BSONElement groupField(groupIterator.next());
const char *pFieldName = groupField.fieldName();
if (strcmp(pFieldName, Document::idName.c_str()) == 0) {
uassert(15948, "a group's _id may only be specified once",
!idSet);
BSONType groupType = groupField.type();
if (groupType == Object) {
/*
Use the projection-like set of field paths to create the
group-by key.
*/
Expression::ObjectCtx oCtx(
Expression::ObjectCtx::DOCUMENT_OK);
intrusive_ptr<Expression> pId(
Expression::parseObject(&groupField, &oCtx));
pGroup->setIdExpression(pId);
idSet = true;
}
else if (groupType == String) {
string groupString(groupField.String());
const char *pGroupString = groupString.c_str();
if ((groupString.length() == 0) ||
(pGroupString[0] != '$'))
goto StringConstantId;
string pathString(
Expression::removeFieldPrefix(groupString));
intrusive_ptr<ExpressionFieldPath> pFieldPath(
ExpressionFieldPath::create(pathString));
pGroup->setIdExpression(pFieldPath);
idSet = true;
}
else {
/* pick out the constant types that are allowed */
switch(groupType) {
case NumberDouble:
case String:
case Object:
case Array:
case jstOID:
case Bool:
case Date:
case NumberInt:
case Timestamp:
case NumberLong:
case jstNULL:
StringConstantId: // from string case above
{
intrusive_ptr<const Value> pValue(
Value::createFromBsonElement(&groupField));
intrusive_ptr<ExpressionConstant> pConstant(
ExpressionConstant::create(pValue));
pGroup->setIdExpression(pConstant);
idSet = true;
break;
}
default:
uassert(15949, str::stream() <<
"a group's _id may not include fields of BSON type " << groupType,
false);
}
}
}
else {
/*
Treat as a projection field with the additional ability to
add aggregation operators.
*/
uassert(15950, str::stream() <<
"the group aggregate field name " <<
*pFieldName << " cannot be an operator name",
*pFieldName != '$');
uassert(15951, str::stream() <<
"the group aggregate field " << *pFieldName <<
"must be defined as an expression inside an object",
groupField.type() == Object);
BSONObj subField(groupField.Obj());
BSONObjIterator subIterator(subField);
size_t subCount = 0;
for(; subIterator.more(); ++subCount) {
BSONElement subElement(subIterator.next());
/* look for the specified operator */
GroupOpDesc key;
key.pName = subElement.fieldName();
const GroupOpDesc *pOp =
(const GroupOpDesc *)bsearch(
&key, GroupOpTable, NGroupOp, sizeof(GroupOpDesc),
GroupOpDescCmp);
uassert(15952, str::stream() <<
"unknown group operator \"" <<
key.pName << "\"",
pOp);
intrusive_ptr<Expression> pGroupExpr;
BSONType elementType = subElement.type();
if (elementType == Object) {
Expression::ObjectCtx oCtx(
Expression::ObjectCtx::DOCUMENT_OK);
pGroupExpr = Expression::parseObject(
&subElement, &oCtx);
}
else if (elementType == Array) {
uassert(15953, str::stream() <<
"aggregating group operators are unary (" <<
key.pName << ")", false);
}
else { /* assume its an atomic single operand */
pGroupExpr = Expression::parseOperand(&subElement);
}
pGroup->addAccumulator(
pFieldName, pOp->pFactory, pGroupExpr);
}
uassert(15954, str::stream() <<
"the computed aggregate \"" <<
pFieldName << "\" must specify exactly one operator",
subCount == 1);
}
}
uassert(15955, "a group specification must include an _id", idSet);
return pGroup;
}
void DocumentSourceGroup::populate() {
for(bool hasNext = !pSource->eof(); hasNext;
hasNext = pSource->advance()) {
intrusive_ptr<Document> pDocument(pSource->getCurrent());
/* get the _id document */
intrusive_ptr<const Value> pId(pIdExpression->evaluate(pDocument));
uassert(15956, "the _id field for a group must not be undefined",
pId->getType() != Undefined);
/*
Look for the _id value in the map; if it's not there, add a
new entry with a blank accumulator.
*/
vector<intrusive_ptr<Accumulator> > *pGroup;
GroupsType::iterator it(groups.find(pId));
if (it != groups.end()) {
/* point at the existing accumulators */
pGroup = &it->second;
}
else {
/* insert a new group into the map */
groups.insert(it,
pair<intrusive_ptr<const Value>,
vector<intrusive_ptr<Accumulator> > >(
pId, vector<intrusive_ptr<Accumulator> >()));
/* find the accumulator vector (the map value) */
it = groups.find(pId);
pGroup = &it->second;
/* add the accumulators */
const size_t n = vpAccumulatorFactory.size();
pGroup->reserve(n);
for(size_t i = 0; i < n; ++i) {
intrusive_ptr<Accumulator> pAccumulator(
(*vpAccumulatorFactory[i])(pCtx));
pAccumulator->addOperand(vpExpression[i]);
pGroup->push_back(pAccumulator);
}
}
/* point at the existing key */
// unneeded atm // pId = it.first;
/* tickle all the accumulators for the group we found */
const size_t n = pGroup->size();
for(size_t i = 0; i < n; ++i)
(*pGroup)[i]->evaluate(pDocument);
}
/* start the group iterator */
groupsIterator = groups.begin();
if (groupsIterator != groups.end())
pCurrent = makeDocument(groupsIterator);
populated = true;
}
intrusive_ptr<Document> DocumentSourceGroup::makeDocument(
const GroupsType::iterator &rIter) {
vector<intrusive_ptr<Accumulator> > *pGroup = &rIter->second;
const size_t n = vFieldName.size();
intrusive_ptr<Document> pResult(Document::create(1 + n));
/* add the _id field */
pResult->addField(Document::idName, rIter->first);
/* add the rest of the fields */
for(size_t i = 0; i < n; ++i) {
intrusive_ptr<const Value> pValue((*pGroup)[i]->getValue());
if (pValue->getType() != Undefined)
pResult->addField(vFieldName[i], pValue);
}
return pResult;
}
intrusive_ptr<DocumentSource> DocumentSourceGroup::createMerger() {
intrusive_ptr<DocumentSourceGroup> pMerger(
DocumentSourceGroup::create(pCtx));
/* the merger will use the same grouping key */
pMerger->setIdExpression(ExpressionFieldPath::create(
Document::idName.c_str()));
const size_t n = vFieldName.size();
for(size_t i = 0; i < n; ++i) {
/*
The merger's output field names will be the same, as will the
accumulator factories. However, for some accumulators, the
expression to be accumulated will be different. The original
accumulator may be collecting an expression based on a field
expression or constant. Here, we accumulate the output of the
same name from the prior group.
*/
pMerger->addAccumulator(
vFieldName[i], vpAccumulatorFactory[i],
ExpressionFieldPath::create(vFieldName[i]));
}
return pMerger;
}
}
|