Skip to content

Commit

Permalink
Support _id aggregation operators for $group stage (#3096)
Browse files Browse the repository at this point in the history
  • Loading branch information
noisersup authored Jul 26, 2023
1 parent 19e1cdb commit 777222a
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 112 deletions.
18 changes: 14 additions & 4 deletions integration/aggregate_documents_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,25 +618,35 @@ func TestAggregateCompatGroup(t *testing.T) {
},
"IDType": {
pipeline: bson.A{bson.D{{"$group", bson.D{
{"_id", bson.D{{"$type", "_id"}}},
{"_id", bson.D{{"$type", "$v"}}},
{"count", bson.D{{"$count", bson.D{}}}},
}}}},
skip: "https://github.com/FerretDB/FerretDB/issues/2679",
},
"IDSum": {
pipeline: bson.A{
bson.D{{"$sort", bson.D{{"_id", 1}}}},
bson.D{{"$group", bson.D{{"_id", bson.D{{"$sum", "$v"}}}}}},
bson.D{{"$sort", bson.D{{"_id", 1}}}},
},
skip: "https://github.com/FerretDB/FerretDB/issues/2694",
},
"IDSumNonExistentField": {
pipeline: bson.A{
bson.D{{"$sort", bson.D{{"_id", 1}}}},
bson.D{{"$group", bson.D{{"_id", bson.D{{"$sum", "$non-existent"}}}}}},
bson.D{{"$sort", bson.D{{"_id", 1}}}},
},
skip: "https://github.com/FerretDB/FerretDB/issues/2694",
},
"IDSumInvalid": {
pipeline: bson.A{
bson.D{{"$group", bson.D{{"_id", bson.D{{"$sum", "$"}}}}}},
},
resultType: emptyResult,
},
"IDSumRecursiveInvalid": {
pipeline: bson.A{
bson.D{{"$group", bson.D{{"_id", bson.D{{"$sum", bson.D{{"$sum", "$"}}}}}}}},
},
resultType: emptyResult,
},
}

Expand Down
89 changes: 88 additions & 1 deletion integration/aggregate_documents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestAggregateGroupErrors(t *testing.T) {
altMessage string // optional, alternative error message for FerretDB, ignored if empty
skip string // optional, skip test with a specified reason
}{
"StageGroupUnaryOperatorSum": {
"UnaryOperatorSum": {
pipeline: bson.A{
bson.D{{"$group", bson.D{{"sum", bson.D{{"$sum", bson.A{}}}}}}},
},
Expand All @@ -105,6 +105,93 @@ func TestAggregateGroupErrors(t *testing.T) {
},
altMessage: "The $sum accumulator is a unary operator",
},
"TypeEmpty": {
pipeline: bson.A{
bson.D{{"$group", bson.D{{"v", bson.D{}}}}},
},
err: &mongo.CommandError{
Code: 40234,
Name: "Location40234",
Message: "The field 'v' must be an accumulator object",
},
},
"TwoOperators": {
pipeline: bson.A{
bson.D{{"$group", bson.D{{"_id", bson.D{{"$type", int32(42)}, {"$op", int32(42)}}}}}},
},
err: &mongo.CommandError{
Code: 15983,
Name: "Location15983",
Message: "An object representing an expression must have exactly one field: { $type: 42, $op: 42 }",
},
altMessage: "An object representing an expression must have exactly one field",
},
"TypeInvalidLen": {
pipeline: bson.A{
bson.D{{"$group", bson.D{{"_id", bson.D{{"$type", bson.A{"foo", "bar"}}}}}}},
},
err: &mongo.CommandError{
Code: 16020,
Name: "Location16020",
Message: "Expression $type takes exactly 1 arguments. 2 were passed in.",
},
},
"NonExistentOperator": {
pipeline: bson.A{
bson.D{{"$group", bson.D{{"_id", bson.D{{"$non-existent", "foo"}}}}}},
},
err: &mongo.CommandError{
Code: 168,
Name: "InvalidPipelineOperator",
Message: "Unrecognized expression '$non-existent'",
},
},
"SumEmptyExpression": {
pipeline: bson.A{
bson.D{{"$group", bson.D{
{"_id", bson.D{{"$sum", "$"}}},
}}},
},
err: &mongo.CommandError{
Code: 16872,
Name: "Location16872",
Message: "'$' by itself is not a valid FieldPath",
},
},
"SumEmptyVariable": {
pipeline: bson.A{
bson.D{{"$group", bson.D{
{"_id", bson.D{{"$sum", "$$"}}},
}}},
},
err: &mongo.CommandError{
Code: 9,
Name: "FailedToParse",
Message: "empty variable names are not allowed",
},
},
"SumDollarVariable": {
pipeline: bson.A{
bson.D{{"$group", bson.D{
{"_id", bson.D{{"$sum", "$$$"}}},
}}},
},
err: &mongo.CommandError{
Code: 9,
Name: "FailedToParse",
Message: "'$' starts with an invalid character for a user variable name",
},
},
"RecursiveNonExistentOperator": {
pipeline: bson.A{
bson.D{{"$group", bson.D{{"_id", bson.D{{"$type", bson.D{{"$non-existent", "foo"}}}}}}}},
},
err: &mongo.CommandError{
Code: 168,
Name: "InvalidPipelineOperator",
Message: "Unrecognized expression '$non-existent'",
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
Expand Down
Loading

0 comments on commit 777222a

Please sign in to comment.