summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/expression.cpp
diff options
context:
space:
mode:
authorCharlie Swanson <charlie.swanson@mongodb.com>2019-05-01 16:36:19 -0400
committerCharlie Swanson <charlie.swanson@mongodb.com>2019-05-07 09:40:17 -0400
commit96e96c3af633f856dc0bb695c3fe0691a16459e0 (patch)
tree032f30fcb89821ad8b47d58885f71c75f5ef29bc /src/mongo/db/pipeline/expression.cpp
parenta14a727bf1ee93c1ac93769df1a234fd2e55624c (diff)
downloadmongo-96e96c3af633f856dc0bb695c3fe0691a16459e0.tar.gz
SERVER-40055 Consolidate $round behavior with and without a default
Diffstat (limited to 'src/mongo/db/pipeline/expression.cpp')
-rw-r--r--src/mongo/db/pipeline/expression.cpp40
1 files changed, 16 insertions, 24 deletions
diff --git a/src/mongo/db/pipeline/expression.cpp b/src/mongo/db/pipeline/expression.cpp
index cd9dad85e5b..d8ab5bce397 100644
--- a/src/mongo/db/pipeline/expression.cpp
+++ b/src/mongo/db/pipeline/expression.cpp
@@ -4843,32 +4843,24 @@ static Value evaluateRoundOrTrunc(const Document& root,
str::stream() << opName << " only supports numeric types, not "
<< typeName(numericArg.getType()),
numericArg.numeric());
- if (children.size() == 1) {
- switch (numericArg.getType()) {
- case BSONType::NumberDecimal:
- return Value(
- numericArg.getDecimal().quantize(Decimal128::kNormalizedZero, roundingMode));
- case BSONType::NumberDouble:
- return Value(doubleOp(numericArg.getDouble()));
- // There's no point to round/trunc integers or longs without precision argument, it will
- // have no effect.
- default:
- return numericArg;
+
+ long long precisionValue = 0;
+ if (children.size() > 1) {
+ auto precisionArg = Value(children[1]->evaluate(root));
+ if (precisionArg.nullish()) {
+ return Value(BSONNULL);
}
+ precisionValue = precisionArg.coerceToLong();
+ uassert(51082,
+ str::stream() << "precision argument to " << opName << " must be a integral value",
+ precisionArg.integral());
+ uassert(51083,
+ str::stream() << "cannot apply " << opName << " with precision value "
+ << precisionValue
+ << " value must be in [-20, 100]",
+ minPrecision <= precisionValue && precisionValue <= maxPrecision);
}
- // Else, if precision is specified, round to the specified precision.
- auto precisionArg = Value(children[1]->evaluate(root));
- if (precisionArg.nullish()) {
- return Value(BSONNULL);
- }
- uassert(51082,
- str::stream() << "precision argument to " << opName << " must be a integral value",
- precisionArg.integral());
- auto precisionValue = precisionArg.coerceToLong();
- uassert(51083,
- str::stream() << "cannot apply " << opName << " with precision value " << precisionValue
- << " value must be in [-20, 100]",
- minPrecision <= precisionValue && precisionValue <= maxPrecision);
+
// Construct 10^-precisionValue, which will be used as the quantize reference.
auto quantum = Decimal128(0LL, Decimal128::kExponentBias - precisionValue, 0LL, 1LL);
switch (numericArg.getType()) {