summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/pipeline')
-rw-r--r--src/mongo/db/pipeline/accumulator_multi.cpp20
-rw-r--r--src/mongo/db/pipeline/accumulator_multi.h13
-rw-r--r--src/mongo/db/pipeline/expression_test.cpp40
3 files changed, 39 insertions, 34 deletions
diff --git a/src/mongo/db/pipeline/accumulator_multi.cpp b/src/mongo/db/pipeline/accumulator_multi.cpp
index 3430f527637..6cef4cd12c7 100644
--- a/src/mongo/db/pipeline/accumulator_multi.cpp
+++ b/src/mongo/db/pipeline/accumulator_multi.cpp
@@ -199,11 +199,11 @@ AccumulatorN::parseArgs(ExpressionContext* const expCtx,
const BSONObj& args,
VariablesParseState vps) {
boost::intrusive_ptr<Expression> n;
- boost::intrusive_ptr<Expression> output;
+ boost::intrusive_ptr<Expression> input;
for (auto&& element : args) {
auto fieldName = element.fieldNameStringData();
- if (fieldName == kFieldNameOutput) {
- output = Expression::parseOperand(expCtx, element, vps);
+ if (fieldName == kFieldNameInput) {
+ input = Expression::parseOperand(expCtx, element, vps);
} else if (fieldName == kFieldNameN) {
n = Expression::parseOperand(expCtx, element, vps);
} else {
@@ -211,8 +211,8 @@ AccumulatorN::parseArgs(ExpressionContext* const expCtx,
}
}
uassert(5787906, str::stream() << "Missing value for '" << kFieldNameN << "'", n);
- uassert(5787907, str::stream() << "Missing value for '" << kFieldNameOutput << "'", output);
- return std::make_tuple(n, output);
+ uassert(5787907, str::stream() << "Missing value for '" << kFieldNameInput << "'", input);
+ return std::make_tuple(n, input);
}
void AccumulatorN::serializeHelper(const boost::intrusive_ptr<Expression>& initializer,
@@ -220,7 +220,7 @@ void AccumulatorN::serializeHelper(const boost::intrusive_ptr<Expression>& initi
bool explain,
MutableDocument& md) {
md.addField(kFieldNameN, Value(initializer->serialize(explain)));
- md.addField(kFieldNameOutput, Value(argument->serialize(explain)));
+ md.addField(kFieldNameInput, Value(argument->serialize(explain)));
}
template <MinMaxSense s>
@@ -241,7 +241,7 @@ AccumulationExpression AccumulatorMinMaxN::parseMinMaxN(ExpressionContext* const
elem.type() == BSONType::Object);
BSONObj obj = elem.embeddedObject();
- auto [n, output] = AccumulatorN::parseArgs(expCtx, obj, vps);
+ auto [n, input] = AccumulatorN::parseArgs(expCtx, obj, vps);
auto factory = [expCtx] {
if constexpr (s == MinMaxSense::kMin) {
@@ -251,7 +251,7 @@ AccumulationExpression AccumulatorMinMaxN::parseMinMaxN(ExpressionContext* const
}
};
- return {std::move(n), std::move(output), std::move(factory), name};
+ return {std::move(n), std::move(input), std::move(factory), name};
}
void AccumulatorMinMaxN::processValue(const Value& val) {
@@ -332,7 +332,7 @@ AccumulationExpression AccumulatorFirstLastN::parseFirstLastN(ExpressionContext*
elem.type() == BSONType::Object);
auto obj = elem.embeddedObject();
- auto [n, output] = AccumulatorN::parseArgs(expCtx, obj, vps);
+ auto [n, input] = AccumulatorN::parseArgs(expCtx, obj, vps);
auto factory = [expCtx] {
if constexpr (v == Sense::kFirst) {
@@ -342,7 +342,7 @@ AccumulationExpression AccumulatorFirstLastN::parseFirstLastN(ExpressionContext*
}
};
- return {std::move(n), std::move(output), std::move(factory), name};
+ return {std::move(n), std::move(input), std::move(factory), name};
}
void AccumulatorFirstLastN::processValue(const Value& val) {
diff --git a/src/mongo/db/pipeline/accumulator_multi.h b/src/mongo/db/pipeline/accumulator_multi.h
index 17d534f2313..01fb33a53f2 100644
--- a/src/mongo/db/pipeline/accumulator_multi.h
+++ b/src/mongo/db/pipeline/accumulator_multi.h
@@ -42,14 +42,19 @@ namespace mongo {
* has different criteria for how to pick values and order the final array, but any common behavior
* shared by derived classes is implemented in this class. In particular:
* - Initializing 'n' during 'startNewGroup'.
- * - Parsing the expressions for 'n' and 'output'.
+ * - Parsing the expressions for 'n' and 'input'.
*/
class AccumulatorN : public AccumulatorState {
public:
static constexpr auto kFieldNameN = "n"_sd;
- static constexpr auto kFieldNameOutput = "output"_sd;
+ static constexpr auto kFieldNameInput = "input"_sd;
// Field names related to top/bottom/topN/bottomN.
+
+ // Whereas other 'n' accumulators accept an 'input' parameter, top/bottom/topN/bottomN accept
+ // 'output'. This is done in order to disambiguate the expression that will be used to
+ // compute the output from the 'sortBy' expression, which will be used to order the output.
+ static constexpr auto kFieldNameOutput = "output"_sd;
// Sort specification given by user.
static constexpr auto kFieldNameSortBy = "sortBy"_sd;
// Array containing only the fields needed to generate a sortKey from the input document.
@@ -72,7 +77,7 @@ public:
void startNewGroup(const Value& input) final;
/**
- * Helper which appends the 'n' and 'output' fields to 'md'.
+ * Helper which appends the 'n' and 'input' fields to 'md'.
*/
static void serializeHelper(const boost::intrusive_ptr<Expression>& initializer,
const boost::intrusive_ptr<Expression>& argument,
@@ -80,7 +85,7 @@ public:
MutableDocument& md);
protected:
- // Parses 'args' for the 'n' and 'output' arguments that are common to the 'N' family of
+ // Parses 'args' for the 'n' and 'input' arguments that are common to the 'N' family of
// accumulators.
static std::tuple<boost::intrusive_ptr<Expression>, boost::intrusive_ptr<Expression>> parseArgs(
ExpressionContext* expCtx, const BSONObj& args, VariablesParseState vps);
diff --git a/src/mongo/db/pipeline/expression_test.cpp b/src/mongo/db/pipeline/expression_test.cpp
index 1b8c76c3eb1..3f6b6c73438 100644
--- a/src/mongo/db/pipeline/expression_test.cpp
+++ b/src/mongo/db/pipeline/expression_test.cpp
@@ -754,14 +754,14 @@ TEST(ExpressionFromAccumulators, FirstNLastN) {
spec.firstElement(),
Value(expected));
};
- firstNFn(fromjson("{$firstN: {n: 3, output: [19, 7, 28, 3, 5]}}"),
+ firstNFn(fromjson("{$firstN: {n: 3, input: [19, 7, 28, 3, 5]}}"),
BSONArray(fromjson("[19, 7, 28]")));
- firstNFn(fromjson("{$firstN: {n: 6, output: [19, 7, 28, 3, 5]}}"),
+ firstNFn(fromjson("{$firstN: {n: 6, input: [19, 7, 28, 3, 5]}}"),
BSONArray(fromjson("[19, 7, 28, 3, 5]")));
- firstNFn(fromjson("{$firstN: {n: 3, output: [1,2,3,4,5,6]}}"), BSONArray(fromjson("[1,2,3]")));
- firstNFn(fromjson("{$firstN: {n: 3, output: [1,2,null,null]}}"),
+ firstNFn(fromjson("{$firstN: {n: 3, input: [1,2,3,4,5,6]}}"), BSONArray(fromjson("[1,2,3]")));
+ firstNFn(fromjson("{$firstN: {n: 3, input: [1,2,null,null]}}"),
BSONArray(fromjson("[1,2,null]")));
- firstNFn(fromjson("{$firstN: {n: 3, output: [1.1, 2.713, 3, 3.4]}}"),
+ firstNFn(fromjson("{$firstN: {n: 3, input: [1.1, 2.713, 3, 3.4]}}"),
BSONArray(fromjson("[1.1, 2.713, 3]")));
// $lastN
@@ -770,13 +770,13 @@ TEST(ExpressionFromAccumulators, FirstNLastN) {
spec.firstElement(),
Value(expected));
};
- lastNFn(fromjson("{$lastN: {n: 3, output: [19, 7, 28, 3, 5]}}"),
+ lastNFn(fromjson("{$lastN: {n: 3, input: [19, 7, 28, 3, 5]}}"),
BSONArray(fromjson("[28,3,5]")));
- lastNFn(fromjson("{$lastN: {n: 6, output: [19, 7, 28, 3, 5]}}"),
+ lastNFn(fromjson("{$lastN: {n: 6, input: [19, 7, 28, 3, 5]}}"),
BSONArray(fromjson("[19, 7, 28, 3, 5]")));
- lastNFn(fromjson("{$lastN: {n: 3, output: [3,2,1,4,5,6]}}"), BSONArray(fromjson("[4,5,6]")));
- lastNFn(fromjson("{$lastN: {n: 3, output: [1,2,null,3]}}"), BSONArray(fromjson("[2,null,3]")));
- lastNFn(fromjson("{$lastN: {n: 3, output: [3, 2.713, 1.1, 2.7]}}"),
+ lastNFn(fromjson("{$lastN: {n: 3, input: [3,2,1,4,5,6]}}"), BSONArray(fromjson("[4,5,6]")));
+ lastNFn(fromjson("{$lastN: {n: 3, input: [1,2,null,3]}}"), BSONArray(fromjson("[2,null,3]")));
+ lastNFn(fromjson("{$lastN: {n: 3, input: [3, 2.713, 1.1, 2.7]}}"),
BSONArray(fromjson("[2.713, 1.1, 2.7]")));
}
@@ -811,13 +811,13 @@ TEST(ExpressionFromAccumulators, MinNMaxN) {
};
// $maxN
- maxNFn(fromjson("{$maxN: {n: 3, output: [19, 7, 28, 3, 5]}}"),
+ maxNFn(fromjson("{$maxN: {n: 3, input: [19, 7, 28, 3, 5]}}"),
BSONArray(fromjson("[28, 19, 7]")));
- maxNFn(fromjson("{$maxN: {n: 6, output: [19, 7, 28, 3, 5]}}"),
+ maxNFn(fromjson("{$maxN: {n: 6, input: [19, 7, 28, 3, 5]}}"),
BSONArray(fromjson("[28, 19, 7, 5, 3]")));
- maxNFn(fromjson("{$maxN: {n: 3, output: [1,2,3]}}"), BSONArray(fromjson("[3,2,1]")));
- maxNFn(fromjson("{$maxN: {n: 3, output: [1,2,null]}}"), BSONArray(fromjson("[2,1]")));
- maxNFn(fromjson("{$maxN: {n: 3, output: [1.1, 2.713, 3]}}"),
+ maxNFn(fromjson("{$maxN: {n: 3, input: [1,2,3]}}"), BSONArray(fromjson("[3,2,1]")));
+ maxNFn(fromjson("{$maxN: {n: 3, input: [1,2,null]}}"), BSONArray(fromjson("[2,1]")));
+ maxNFn(fromjson("{$maxN: {n: 3, input: [1.1, 2.713, 3]}}"),
BSONArray(fromjson("[3, 2.713, 1.1]")));
auto minNFn = [&](const BSONObj& spec, const BSONArray& expected) {
@@ -826,12 +826,12 @@ TEST(ExpressionFromAccumulators, MinNMaxN) {
};
// $minN
- minNFn(fromjson("{$minN: {n: 3, output: [19, 7, 28, 3, 5]}}"), BSONArray(fromjson("[3,5,7]")));
- minNFn(fromjson("{$minN: {n: 6, output: [19, 7, 28, 3, 5]}}"),
+ minNFn(fromjson("{$minN: {n: 3, input: [19, 7, 28, 3, 5]}}"), BSONArray(fromjson("[3,5,7]")));
+ minNFn(fromjson("{$minN: {n: 6, input: [19, 7, 28, 3, 5]}}"),
BSONArray(fromjson("[3,5,7,19, 28]")));
- minNFn(fromjson("{$minN: {n: 3, output: [3,2,1]}}"), BSONArray(fromjson("[1,2,3]")));
- minNFn(fromjson("{$minN: {n: 3, output: [1,2,null]}}"), BSONArray(fromjson("[1,2]")));
- minNFn(fromjson("{$minN: {n: 3, output: [3, 2.713, 1.1]}}"),
+ minNFn(fromjson("{$minN: {n: 3, input: [3,2,1]}}"), BSONArray(fromjson("[1,2,3]")));
+ minNFn(fromjson("{$minN: {n: 3, input: [1,2,null]}}"), BSONArray(fromjson("[1,2]")));
+ minNFn(fromjson("{$minN: {n: 3, input: [3, 2.713, 1.1]}}"),
BSONArray(fromjson("[1.1, 2.713, 3]")));
}