-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SERVER-10689 Aggregation now supports the switch expression.
- Loading branch information
1 parent
63d021f
commit 77aaa54
Showing
8 changed files
with
375 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// In SERVER-10689, the $switch expression was introduced. In this file, we test the functionality | ||
// of the expression. | ||
|
||
(function() { | ||
"use strict"; | ||
|
||
var coll = db.switch; | ||
coll.drop(); | ||
|
||
// Insert an empty document so that something can flow through the pipeline. | ||
coll.insert({}); | ||
|
||
// Ensure that a branch is correctly evaluated. | ||
var pipeline = { | ||
"$project": { | ||
"_id": 0, | ||
"output": { | ||
"$switch": { | ||
"branches": [{"case": {"$eq": [1, 1]}, "then": "one is equal to one!"}], | ||
} | ||
} | ||
} | ||
}; | ||
var res = coll.aggregate(pipeline).toArray(); | ||
|
||
assert.eq(res.length, 1); | ||
assert.eq(res[0], {"output": "one is equal to one!"}); | ||
|
||
// Ensure that the first branch which matches is chosen. | ||
pipeline = { | ||
"$project": { | ||
"_id": 0, | ||
"output": { | ||
"$switch": { | ||
"branches": [ | ||
{"case": {"$eq": [1, 1]}, "then": "one is equal to one!"}, | ||
{"case": {"$eq": [2, 2]}, "then": "two is equal to two!"} | ||
], | ||
} | ||
} | ||
} | ||
}; | ||
res = coll.aggregate(pipeline).toArray(); | ||
|
||
assert.eq(res.length, 1); | ||
assert.eq(res[0], {"output": "one is equal to one!"}); | ||
|
||
// Ensure that the default is chosen if no case matches. | ||
pipeline = { | ||
"$project": { | ||
"_id": 0, | ||
"output": { | ||
"$switch": { | ||
"branches": [{"case": {"$eq": [1, 2]}, "then": "one is equal to two!"}], | ||
"default": "no case matched." | ||
} | ||
} | ||
} | ||
}; | ||
res = coll.aggregate(pipeline).toArray(); | ||
|
||
assert.eq(res.length, 1); | ||
assert.eq(res[0], {"output": "no case matched."}); | ||
|
||
// Ensure that nullish values are treated as false when they are a "case", and are null | ||
// otherwise. | ||
pipeline = { | ||
"$project": { | ||
"_id": 0, | ||
"output": { | ||
"$switch": { | ||
"branches": [{"case": null, "then": "Null was true!"}], | ||
"default": "No case matched." | ||
} | ||
} | ||
} | ||
}; | ||
res = coll.aggregate(pipeline).toArray(); | ||
|
||
assert.eq(res.length, 1); | ||
assert.eq(res[0], {"output": "No case matched."}); | ||
|
||
pipeline = { | ||
"$project": { | ||
"_id": 0, | ||
"output": { | ||
"$switch": { | ||
"branches": [{"case": "$missingField", "then": "Null was true!"}], | ||
"default": "No case matched." | ||
} | ||
} | ||
} | ||
}; | ||
res = coll.aggregate(pipeline).toArray(); | ||
|
||
assert.eq(res.length, 1); | ||
assert.eq(res[0], {"output": "No case matched."}); | ||
|
||
pipeline = { | ||
"$project": { | ||
"_id": 0, | ||
"output": | ||
{"$switch": {"branches": [{"case": true, "then": null}], "default": false}} | ||
} | ||
}; | ||
res = coll.aggregate(pipeline).toArray(); | ||
|
||
assert.eq(res.length, 1); | ||
assert.eq(res[0], {"output": null}); | ||
|
||
pipeline = { | ||
"$project": { | ||
"_id": 0, | ||
"output": { | ||
"$switch": | ||
{"branches": [{"case": true, "then": "$missingField"}], "default": false} | ||
} | ||
} | ||
}; | ||
res = coll.aggregate(pipeline).toArray(); | ||
|
||
assert.eq(res.length, 1); | ||
assert.eq(res[0], {}); | ||
|
||
pipeline = { | ||
"$project": { | ||
"_id": 0, | ||
"output": | ||
{"$switch": {"branches": [{"case": null, "then": false}], "default": null}} | ||
} | ||
}; | ||
res = coll.aggregate(pipeline).toArray(); | ||
|
||
assert.eq(res.length, 1); | ||
assert.eq(res[0], {"output": null}); | ||
|
||
pipeline = { | ||
"$project": { | ||
"_id": 0, | ||
"output": { | ||
"$switch": | ||
{"branches": [{"case": null, "then": false}], "default": "$missingField"} | ||
} | ||
} | ||
}; | ||
res = coll.aggregate(pipeline).toArray(); | ||
|
||
assert.eq(res.length, 1); | ||
assert.eq(res[0], {}); | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// SERVER-10689 introduced the $switch expression. In this file, we test the error cases of the | ||
// expression. | ||
load("jstests/aggregation/extras/utils.js"); // For assertErrorCode. | ||
|
||
(function() { | ||
"use strict"; | ||
|
||
var coll = db.switch; | ||
coll.drop(); | ||
|
||
var pipeline = { | ||
"$project": {"output": {"$switch": "not an object"}} | ||
}; | ||
assertErrorCode(coll, pipeline, 40060, "$switch requires an object as an argument."); | ||
|
||
pipeline = { | ||
"$project": {"output": {"$switch": {"branches": "not an array"}}} | ||
}; | ||
assertErrorCode(coll, pipeline, 40061, "$switch requires 'branches' to be an array."); | ||
|
||
pipeline = { | ||
"$project": {"output": {"$switch": {"branches": ["not an object"]}}} | ||
}; | ||
assertErrorCode(coll, pipeline, 40062, "$switch requires each branch to be an object."); | ||
|
||
pipeline = { | ||
"$project": {"output": {"$switch": {"branches": [{}]}}} | ||
}; | ||
assertErrorCode(coll, pipeline, 40064, "$switch requires each branch have a 'case'."); | ||
|
||
pipeline = { | ||
"$project": { | ||
"output": { | ||
"$switch": { | ||
"branches": [{ | ||
"case": 1, | ||
}] | ||
} | ||
} | ||
} | ||
}; | ||
assertErrorCode(coll, pipeline, 40065, "$switch requires each branch have a 'then'."); | ||
|
||
pipeline = { | ||
"$project": | ||
{"output": {"$switch": {"branches": [{"case": true, "then": false, "badKey": 1}]}}} | ||
}; | ||
assertErrorCode(coll, pipeline, 40063, "$switch found a branch with an unknown argument"); | ||
|
||
pipeline = { | ||
"$project": {"output": {"$switch": {"notAnArgument": 1}}} | ||
}; | ||
assertErrorCode(coll, pipeline, 40067, "$switch found an unknown argument"); | ||
|
||
pipeline = { | ||
"$project": {"output": {"$switch": {"branches": []}}} | ||
}; | ||
assertErrorCode(coll, pipeline, 40068, "$switch requires at least one branch"); | ||
|
||
pipeline = { | ||
"$project": {"output": {"$switch": {}}} | ||
}; | ||
assertErrorCode(coll, pipeline, 40068, "$switch requires at least one branch"); | ||
|
||
coll.insert({x: 1}); | ||
pipeline = { | ||
"$project": | ||
{"output": {"$switch": {"branches": [{"case": {"$eq": ["$x", 0]}, "then": 1}]}}} | ||
}; | ||
assertErrorCode(coll, pipeline, 40066, "$switch has no default and an input matched no case"); | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.