-
Notifications
You must be signed in to change notification settings - Fork 385
/
group.go
471 lines (398 loc) · 12.4 KB
/
group.go
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package stages
import (
"context"
"errors"
"fmt"
"time"
"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/common/aggregations"
"github.com/FerretDB/FerretDB/internal/handlers/common/aggregations/operators"
"github.com/FerretDB/FerretDB/internal/handlers/common/aggregations/operators/accumulators"
"github.com/FerretDB/FerretDB/internal/handlers/commonerrors"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/iterator"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/must"
)
// group represents $group stage.
//
// { $group: {
// _id: <groupExpression>,
// <groupBy[0].outputField>: {accumulator0: expression0},
// ...
// <groupBy[N].outputField>: {accumulatorN: expressionN},
// }}
//
// $group uses group expression to group documents that have the same evaluated expression.
// The evaluated expression becomes the _id for that group of documents.
// For each group of documents, accumulators are applied.
type group struct {
groupExpression any
groupBy []groupBy
}
// groupBy represents accumulation to apply on the group.
type groupBy struct {
accumulator accumulators.Accumulator
outputField string
}
// newGroup creates a new $group stage.
func newGroup(stage *types.Document) (aggregations.Stage, error) {
fields, err := common.GetRequiredParam[*types.Document](stage, "$group")
if err != nil {
return nil, commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrStageGroupInvalidFields,
"a group's fields must be specified in an object",
"$group (stage)",
)
}
var groupKey any
var groups []groupBy
iter := fields.Iterator()
defer iter.Close()
for {
field, v, err := iter.Next()
if errors.Is(err, iterator.ErrIteratorDone) {
break
}
if err != nil {
return nil, lazyerrors.Error(err)
}
if field == "_id" {
if err = validateGroupKey(v); err != nil {
return nil, err
}
groupKey = v
continue
}
accumulator, err := accumulators.NewAccumulator("$group", field, v)
if err != nil {
return nil, processGroupStageError(err)
}
groups = append(groups, groupBy{
outputField: field,
accumulator: accumulator,
})
}
if groupKey == nil {
return nil, commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrStageGroupMissingID,
"a group specification must include an _id",
"$group (stage)",
)
}
return &group{
groupExpression: groupKey,
groupBy: groups,
}, nil
}
// Process implements Stage interface.
func (g *group) Process(ctx context.Context, iter types.DocumentsIterator, closer *iterator.MultiCloser) (types.DocumentsIterator, error) { //nolint:lll // for readability
groupedDocuments, err := g.groupDocuments(iter)
if err != nil {
return nil, err
}
var res []*types.Document
for _, groupedDocument := range groupedDocuments {
doc := must.NotFail(types.NewDocument("_id", groupedDocument.groupID))
groupIter := iterator.Values(iterator.ForSlice(groupedDocument.documents))
defer groupIter.Close()
for _, accumulation := range g.groupBy {
out, err := accumulation.accumulator.Accumulate(groupIter)
if err != nil {
// existing accumulators do not return error
return nil, processGroupStageError(err)
}
if doc.Has(accumulation.outputField) {
// document has duplicate key
return nil, commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrDuplicateField,
fmt.Sprintf("duplicate field: %s", accumulation.outputField),
"$group (stage)",
)
}
doc.Set(accumulation.outputField, out)
}
res = append(res, doc)
}
iter = iterator.Values(iterator.ForSlice(res))
closer.Add(iter)
return iter, nil
}
// validateGroupKey returns error on invalid group key.
// If group key is a document, it recursively validates operator and expression.
func validateGroupKey(groupKey any) error {
doc, ok := groupKey.(*types.Document)
if !ok {
return nil
}
if operators.IsOperator(doc) {
op, err := operators.NewOperator(doc)
if err != nil {
return processGroupStageError(err)
}
_, err = op.Process(nil)
if err != nil {
// TODO https://github.com/FerretDB/FerretDB/issues/3129
return processGroupStageError(err)
}
}
iter := doc.Iterator()
defer iter.Close()
fields := make(map[string]struct{}, doc.Len())
for {
k, v, err := iter.Next()
if errors.Is(err, iterator.ErrIteratorDone) {
break
}
if err != nil {
return lazyerrors.Error(err)
}
if _, ok := fields[k]; ok {
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrGroupDuplicateFieldName,
fmt.Sprintf("duplicate field name specified in object literal: %s", types.FormatAnyValue(doc)),
"$group (stage)",
)
}
fields[k] = struct{}{}
switch v := v.(type) {
case *types.Document:
return validateGroupKey(v)
case string:
_, err := aggregations.NewExpression(v, nil)
var exprErr *aggregations.ExpressionError
if errors.As(err, &exprErr) && exprErr.Code() == aggregations.ErrNotExpression {
err = nil
}
if err != nil {
return processGroupStageError(err)
}
}
}
return nil
}
// groupDocuments groups documents into groups using group key. If group key contains expressions
// or operators, they are evaluated before using it as the group key of documents.
func (g *group) groupDocuments(iter types.DocumentsIterator) ([]groupedDocuments, error) {
var m groupMap
for {
_, doc, err := iter.Next()
if errors.Is(err, iterator.ErrIteratorDone) {
break
}
if err != nil {
return nil, lazyerrors.Error(err)
}
switch groupKey := g.groupExpression.(type) {
case *types.Document:
val, err := evaluateDocument(groupKey, doc, false)
if err != nil {
// operator and expression errors are validated in newGroup
return nil, lazyerrors.Error(err)
}
m.addOrAppend(val, doc)
case *types.Array, float64, types.Binary, types.ObjectID, bool, time.Time, types.NullType,
types.Regex, int32, types.Timestamp, int64:
m.addOrAppend(groupKey, doc)
case string:
expression, err := aggregations.NewExpression(groupKey, nil)
if err != nil {
var exprErr *aggregations.ExpressionError
if errors.As(err, &exprErr) {
if exprErr.Code() == aggregations.ErrNotExpression {
m.addOrAppend(groupKey, doc)
continue
}
return nil, processGroupStageError(err)
}
return nil, lazyerrors.Error(err)
}
val, err := expression.Evaluate(doc)
if err != nil {
// $group treats non-existent fields as nulls
val = types.Null
}
m.addOrAppend(val, doc)
default:
panic(fmt.Sprintf("unexpected type %[1]T (%#[1]v)", groupKey))
}
}
return m.docs, nil
}
// evaluateDocument recursively evaluates document's field expressions and operators.
func evaluateDocument(expr, doc *types.Document, nestedField bool) (any, error) {
if operators.IsOperator(expr) {
op, err := operators.NewOperator(expr)
if err != nil {
// operator error was validated in newGroup
return nil, processGroupStageError(err)
}
v, err := op.Process(doc)
if err != nil {
// operator and expression errors are validated in newGroup
return nil, processGroupStageError(err)
}
return v, nil
}
iter := expr.Iterator()
defer iter.Close()
evaluatedDocument := new(types.Document)
for {
k, exprVal, err := iter.Next()
if errors.Is(err, iterator.ErrIteratorDone) {
break
}
if err != nil {
return nil, lazyerrors.Error(err)
}
switch exprVal := exprVal.(type) {
case *types.Document:
v, err := evaluateDocument(exprVal, doc, true)
if err != nil {
return nil, lazyerrors.Error(err)
}
evaluatedDocument.Set(k, v)
case string:
expression, err := aggregations.NewExpression(exprVal, nil)
var exprErr *aggregations.ExpressionError
if errors.As(err, &exprErr) && exprErr.Code() == aggregations.ErrNotExpression {
evaluatedDocument.Set(k, exprVal)
continue
}
if err != nil {
// expression error was validated in newGroup.
return nil, lazyerrors.Error(err)
}
v, err := expression.Evaluate(doc)
if err != nil {
if expr.Len() == 1 && !nestedField {
// non-existent path is set to null if expression contains single field and not a nested document
evaluatedDocument.Set(k, types.Null)
}
continue
}
evaluatedDocument.Set(k, v)
default:
evaluatedDocument.Set(k, exprVal)
}
}
return evaluatedDocument, nil
}
// groupedDocuments contains group key and the documents for that group.
type groupedDocuments struct {
groupID any
documents []*types.Document
}
// groupMap holds groups of documents.
type groupMap struct {
docs []groupedDocuments
}
// addOrAppend adds a groupID documents pair if the groupID does not exist,
// if the groupID exists it appends the documents to the slice.
func (m *groupMap) addOrAppend(groupKey any, docs ...*types.Document) {
for i, g := range m.docs {
// groupID is a distinct key and can be any BSON type including array and Binary,
// so we cannot use structure like map.
// Compare is used to check if groupID exists in groupMap, because
// numbers are grouped for the same value regardless of their number type.
if types.CompareForAggregation(groupKey, g.groupID) == types.Equal {
m.docs[i].documents = append(m.docs[i].documents, docs...)
return
}
}
m.docs = append(m.docs, groupedDocuments{
groupID: groupKey,
documents: docs,
})
}
// processGroupError takes internal error related to operator evaluation and
// expression evaluation and returns CommandError that can be returned by $group
// aggregation stage.
func processGroupStageError(err error) error {
var opErr operators.OperatorError
var exErr *aggregations.ExpressionError
switch {
case errors.As(err, &opErr):
switch opErr.Code() {
case operators.ErrTooManyFields:
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrExpressionWrongLenOfFields,
"An object representing an expression must have exactly one field",
"$group (stage)",
)
case operators.ErrNotImplemented:
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrNotImplemented,
"Invalid $group :: caused by :: "+opErr.Error(),
"$group (stage)",
)
case operators.ErrArgsInvalidLen:
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrOperatorWrongLenOfArgs,
opErr.Error(),
"$group (stage)",
)
case operators.ErrInvalidExpression:
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrInvalidPipelineOperator,
opErr.Error(),
"$group (stage)",
)
case operators.ErrInvalidNestedExpression:
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrInvalidPipelineOperator,
opErr.Error(),
"$group (stage)",
)
}
case errors.As(err, &exErr):
switch exErr.Code() {
case aggregations.ErrNotExpression:
// handled by upstream and this should not be reachable for existing expression implementation
fallthrough
case aggregations.ErrInvalidExpression:
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrFailedToParse,
"'$' starts with an invalid character for a user variable name",
"$group (stage)",
)
case aggregations.ErrEmptyFieldPath:
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrGroupInvalidFieldPath,
"'$' by itself is not a valid FieldPath",
"$group (stage)",
)
case aggregations.ErrUndefinedVariable:
// TODO https://github.com/FerretDB/FerretDB/issues/2275
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrNotImplemented,
"Aggregation expression variables are not implemented yet",
"$group (stage)",
)
case aggregations.ErrEmptyVariable:
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrFailedToParse,
"empty variable names are not allowed",
"$group (stage)",
)
}
}
return err
}
// check interfaces
var (
_ aggregations.Stage = (*group)(nil)
)