/** * Copyright (C) 2018-present MongoDB, Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the Server Side Public License, version 1, * as published by MongoDB, Inc. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * Server Side Public License for more details. * * You should have received a copy of the Server Side Public License * along with this program. If not, see * . * * As a special exception, the copyright holders give permission to link the * code of portions of this program with the OpenSSL library under certain * conditions as described in each individual source file and distribute * linked combinations including the program with the OpenSSL library. You * must comply with the Server Side Public License in all respects for * all of the code used other than as permitted herein. If you modify file(s) * with this exception, you may extend this exception to your version of the * file(s), but you are not obligated to do so. If you do not wish to do so, * delete this exception statement from your version. If you delete this * exception statement from all source files in the program, then also delete * it in the license file. */ #include "mongo/platform/basic.h" #include "mongo/bson/bsonmisc.h" #include "mongo/config.h" #include "mongo/db/jsobj.h" #include "mongo/db/json.h" #include "mongo/db/pipeline/accumulator.h" #include "mongo/db/pipeline/document.h" #include "mongo/db/pipeline/document_value_test_util.h" #include "mongo/db/pipeline/expression.h" #include "mongo/db/pipeline/expression_context_for_test.h" #include "mongo/db/pipeline/value_comparator.h" #include "mongo/db/query/collation/collator_interface_mock.h" #include "mongo/dbtests/dbtests.h" #include "mongo/unittest/unittest.h" namespace ExpressionTests { using boost::intrusive_ptr; using std::initializer_list; using std::list; using std::numeric_limits; using std::pair; using std::set; using std::sort; using std::string; using std::vector; /** * Creates an expression given by 'expressionName' and evaluates it using * 'operands' as inputs, returning the result. */ static Value evaluateExpression(const string& expressionName, const vector& operands) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; const BSONObj obj = BSON(expressionName << ImplicitValue::convertToValue(operands)); auto expression = Expression::parseExpression(expCtx, obj, vps); Value result = expression->evaluate({}, &expCtx->variables); return result; } /** * Creates an expression which parses named arguments via an object specification, then evaluates it * and returns the result. */ static Value evaluateNamedArgExpression(const string& expressionName, const Document& operand) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; const BSONObj obj = BSON(expressionName << operand); auto expression = Expression::parseExpression(expCtx, obj, vps); Value result = expression->evaluate({}, &expCtx->variables); return result; } /** * Takes the name of an expression as its first argument and a list of pairs of arguments and * expected results as its second argument, and asserts that for the given expression the arguments * evaluate to the expected results. */ static void assertExpectedResults( const string& expression, initializer_list, ImplicitValue>> operations) { for (auto&& op : operations) { try { Value result = evaluateExpression(expression, op.first); ASSERT_VALUE_EQ(op.second, result); ASSERT_EQUALS(op.second.getType(), result.getType()); } catch (...) { log() << "failed with arguments: " << ImplicitValue::convertToValue(op.first); throw; } } } /** Convert BSONObj to a BSONObj with our $const wrappings. */ static BSONObj constify(const BSONObj& obj, bool parentIsArray = false) { BSONObjBuilder bob; for (BSONObjIterator itr(obj); itr.more(); itr.next()) { BSONElement elem = *itr; if (elem.type() == Object) { bob << elem.fieldName() << constify(elem.Obj(), false); } else if (elem.type() == Array && !parentIsArray) { // arrays within arrays are treated as constant values by the real // parser bob << elem.fieldName() << BSONArray(constify(elem.Obj(), true)); } else if (elem.fieldNameStringData() == "$const" || (elem.type() == mongo::String && elem.valueStringDataSafe().startsWith("$"))) { bob.append(elem); } else { bob.append(elem.fieldName(), BSON("$const" << elem)); } } return bob.obj(); } /** Check binary equality, ensuring use of the same numeric types. */ static void assertBinaryEqual(const BSONObj& expected, const BSONObj& actual) { ASSERT_BSONOBJ_EQ(expected, actual); ASSERT(expected.binaryEqual(actual)); } /** Convert Value to a wrapped BSONObj with an empty string field name. */ static BSONObj toBson(const Value& value) { BSONObjBuilder bob; value.addToBsonObj(&bob, ""); return bob.obj(); } /** Convert Expression to BSON. */ static BSONObj expressionToBson(const intrusive_ptr& expression) { return BSON("" << expression->serialize(false)).firstElement().embeddedObject().getOwned(); } /** Convert Document to BSON. */ static BSONObj toBson(const Document& document) { return document.toBson(); } /** Create a Document from a BSONObj. */ Document fromBson(BSONObj obj) { return Document(obj); } /** Create a Value from a BSONObj. */ Value valueFromBson(BSONObj obj) { BSONElement element = obj.firstElement(); return Value(element); } template intrusive_ptr makeConstant(T&& val) { intrusive_ptr expCtx(new ExpressionContextForTest()); return ExpressionConstant::create(expCtx, Value(std::forward(val))); } class ExpressionBaseTest : public unittest::Test { public: void addOperand(intrusive_ptr expr, Value arg) { intrusive_ptr expCtx(new ExpressionContextForTest()); expr->addOperand(ExpressionConstant::create(expCtx, arg)); } }; class ExpressionNaryTestOneArg : public ExpressionBaseTest { public: virtual void assertEvaluates(Value input, Value output) { addOperand(_expr, input); ASSERT_VALUE_EQ(output, _expr->evaluate({}, &_expr->getExpressionContext()->variables)); ASSERT_EQUALS(output.getType(), _expr->evaluate({}, &_expr->getExpressionContext()->variables).getType()); } intrusive_ptr _expr; }; class ExpressionNaryTestTwoArg : public ExpressionBaseTest { public: virtual void assertEvaluates(Value input1, Value input2, Value output) { addOperand(_expr, input1); addOperand(_expr, input2); ASSERT_VALUE_EQ(output, _expr->evaluate({}, &_expr->getExpressionContext()->variables)); ASSERT_EQUALS(output.getType(), _expr->evaluate({}, &_expr->getExpressionContext()->variables).getType()); } intrusive_ptr _expr; }; /* ------------------------- NaryExpression -------------------------- */ /** A dummy child of ExpressionNary used for testing. */ class Testable : public ExpressionNary { public: virtual Value evaluate(const Document& root, Variables* variables) const { // Just put all the values in a list. // By default, this is not associative/commutative so the results will change if // instantiated as commutative or associative and operations are reordered. vector values; for (auto&& child : _children) values.push_back(child->evaluate(root, variables)); return Value(values); } virtual const char* getOpName() const { return "$testable"; } virtual bool isAssociative() const { return _isAssociative; } virtual bool isCommutative() const { return _isCommutative; } void acceptVisitor(ExpressionVisitor* visitor) final { return visitor->visit(this); } static intrusive_ptr create(bool associative, bool commutative) { return new Testable(associative, commutative); } private: Testable(bool isAssociative, bool isCommutative) : ExpressionNary( boost::intrusive_ptr(new ExpressionContextForTest())), _isAssociative(isAssociative), _isCommutative(isCommutative) {} bool _isAssociative; bool _isCommutative; }; class ExpressionNaryTest : public unittest::Test { public: virtual void setUp() override { _notAssociativeNorCommutative = Testable::create(false, false); _associativeOnly = Testable::create(true, false); _associativeAndCommutative = Testable::create(true, true); } protected: void assertDependencies(const intrusive_ptr& expr, const BSONArray& expectedDependencies) { DepsTracker dependencies; expr->addDependencies(&dependencies); BSONArrayBuilder dependenciesBson; for (set::const_iterator i = dependencies.fields.begin(); i != dependencies.fields.end(); ++i) { dependenciesBson << *i; } ASSERT_BSONOBJ_EQ(expectedDependencies, dependenciesBson.arr()); ASSERT_EQUALS(false, dependencies.needWholeDocument); ASSERT_EQUALS(false, dependencies.getNeedsMetadata(DepsTracker::MetadataType::TEXT_SCORE)); } void assertContents(const intrusive_ptr& expr, const BSONArray& expectedContents) { ASSERT_BSONOBJ_EQ(constify(BSON("$testable" << expectedContents)), expressionToBson(expr)); } void addOperandArrayToExpr(const intrusive_ptr& expr, const BSONArray& operands) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; BSONObjIterator i(operands); while (i.more()) { BSONElement element = i.next(); expr->addOperand(Expression::parseOperand(expCtx, element, vps)); } } intrusive_ptr _notAssociativeNorCommutative; intrusive_ptr _associativeOnly; intrusive_ptr _associativeAndCommutative; }; TEST_F(ExpressionNaryTest, AddedConstantOperandIsSerialized) { intrusive_ptr expCtx(new ExpressionContextForTest()); _notAssociativeNorCommutative->addOperand(ExpressionConstant::create(expCtx, Value(9))); assertContents(_notAssociativeNorCommutative, BSON_ARRAY(9)); } TEST_F(ExpressionNaryTest, AddedFieldPathOperandIsSerialized) { intrusive_ptr expCtx(new ExpressionContextForTest()); _notAssociativeNorCommutative->addOperand(ExpressionFieldPath::create(expCtx, "ab.c")); assertContents(_notAssociativeNorCommutative, BSON_ARRAY("$ab.c")); } TEST_F(ExpressionNaryTest, ValidateEmptyDependencies) { assertDependencies(_notAssociativeNorCommutative, BSONArray()); } TEST_F(ExpressionNaryTest, ValidateConstantExpressionDependency) { intrusive_ptr expCtx(new ExpressionContextForTest()); _notAssociativeNorCommutative->addOperand(ExpressionConstant::create(expCtx, Value(1))); assertDependencies(_notAssociativeNorCommutative, BSONArray()); } TEST_F(ExpressionNaryTest, ValidateFieldPathExpressionDependency) { intrusive_ptr expCtx(new ExpressionContextForTest()); _notAssociativeNorCommutative->addOperand(ExpressionFieldPath::create(expCtx, "ab.c")); assertDependencies(_notAssociativeNorCommutative, BSON_ARRAY("ab.c")); } TEST_F(ExpressionNaryTest, ValidateObjectExpressionDependency) { BSONObj spec = BSON("" << BSON("a" << "$x" << "q" << "$r")); intrusive_ptr expCtx(new ExpressionContextForTest()); BSONElement specElement = spec.firstElement(); VariablesParseState vps = expCtx->variablesParseState; _notAssociativeNorCommutative->addOperand( Expression::parseObject(expCtx, specElement.Obj(), vps)); assertDependencies(_notAssociativeNorCommutative, BSON_ARRAY("r" << "x")); } TEST_F(ExpressionNaryTest, SerializationToBsonObj) { intrusive_ptr expCtx(new ExpressionContextForTest()); _notAssociativeNorCommutative->addOperand(ExpressionConstant::create(expCtx, Value(5))); ASSERT_BSONOBJ_EQ(BSON("foo" << BSON("$testable" << BSON_ARRAY(BSON("$const" << 5)))), BSON("foo" << _notAssociativeNorCommutative->serialize(false))); } TEST_F(ExpressionNaryTest, SerializationToBsonArr) { intrusive_ptr expCtx(new ExpressionContextForTest()); _notAssociativeNorCommutative->addOperand(ExpressionConstant::create(expCtx, Value(5))); ASSERT_BSONOBJ_EQ(constify(BSON_ARRAY(BSON("$testable" << BSON_ARRAY(5)))), BSON_ARRAY(_notAssociativeNorCommutative->serialize(false))); } // Verify that the internal operands are optimized TEST_F(ExpressionNaryTest, InternalOperandOptimizationIsDone) { BSONArray spec = BSON_ARRAY(BSON("$and" << BSONArray()) << "$abc"); addOperandArrayToExpr(_notAssociativeNorCommutative, spec); assertContents(_notAssociativeNorCommutative, spec); ASSERT(_notAssociativeNorCommutative == _notAssociativeNorCommutative->optimize()); assertContents(_notAssociativeNorCommutative, BSON_ARRAY(true << "$abc")); } // Verify that if all the operands are constants, the expression is replaced // by a constant value equivalent to the expression applied to the operands. TEST_F(ExpressionNaryTest, AllConstantOperandOptimization) { BSONArray spec = BSON_ARRAY(1 << 2); addOperandArrayToExpr(_notAssociativeNorCommutative, spec); assertContents(_notAssociativeNorCommutative, spec); intrusive_ptr optimized = _notAssociativeNorCommutative->optimize(); ASSERT(_notAssociativeNorCommutative != optimized); ASSERT_BSONOBJ_EQ(BSON("$const" << BSON_ARRAY(1 << 2)), expressionToBson(optimized)); } // Verify that the optimization of grouping constant and non-constant operands // and then applying the expression to the constant operands to reduce them to // one constant operand is only applied if the expression is associative and // commutative. TEST_F(ExpressionNaryTest, GroupingOptimizationOnNotCommutativeNorAssociative) { BSONArray spec = BSON_ARRAY(55 << 66 << "$path"); addOperandArrayToExpr(_notAssociativeNorCommutative, spec); assertContents(_notAssociativeNorCommutative, spec); intrusive_ptr optimized = _notAssociativeNorCommutative->optimize(); ASSERT(_notAssociativeNorCommutative == optimized); assertContents(_notAssociativeNorCommutative, spec); } TEST_F(ExpressionNaryTest, GroupingOptimizationOnAssociativeOnlyFrontOperands) { BSONArray spec = BSON_ARRAY(55 << 66 << "$path"); addOperandArrayToExpr(_associativeOnly, spec); assertContents(_associativeOnly, spec); intrusive_ptr optimized = _associativeOnly->optimize(); ASSERT(_associativeOnly == optimized); assertContents(_associativeOnly, BSON_ARRAY(BSON_ARRAY(55 << 66) << "$path")); } TEST_F(ExpressionNaryTest, GroupingOptimizationOnAssociativeOnlyMiddleOperands) { BSONArray spec = BSON_ARRAY("$path1" << 55 << 66 << "$path"); addOperandArrayToExpr(_associativeOnly, spec); assertContents(_associativeOnly, spec); intrusive_ptr optimized = _associativeOnly->optimize(); ASSERT(_associativeOnly == optimized); assertContents(_associativeOnly, BSON_ARRAY("$path1" << BSON_ARRAY(55 << 66) << "$path")); } TEST_F(ExpressionNaryTest, GroupingOptimizationOnAssociativeOnlyBackOperands) { BSONArray spec = BSON_ARRAY("$path" << 55 << 66); addOperandArrayToExpr(_associativeOnly, spec); assertContents(_associativeOnly, spec); intrusive_ptr optimized = _associativeOnly->optimize(); ASSERT(_associativeOnly == optimized); assertContents(_associativeOnly, BSON_ARRAY("$path" << BSON_ARRAY(55 << 66))); } TEST_F(ExpressionNaryTest, GroupingOptimizationOnAssociativeOnlyNotExecuteOnSingleConstantsFront) { BSONArray spec = BSON_ARRAY(55 << "$path"); addOperandArrayToExpr(_associativeOnly, spec); assertContents(_associativeOnly, spec); intrusive_ptr optimized = _associativeOnly->optimize(); ASSERT(_associativeOnly == optimized); assertContents(_associativeOnly, BSON_ARRAY(55 << "$path")); } TEST_F(ExpressionNaryTest, GroupingOptimizationOnAssociativeOnlyNotExecuteOnSingleConstantsMiddle) { BSONArray spec = BSON_ARRAY("$path1" << 55 << "$path2"); addOperandArrayToExpr(_associativeOnly, spec); assertContents(_associativeOnly, spec); intrusive_ptr optimized = _associativeOnly->optimize(); ASSERT(_associativeOnly == optimized); assertContents(_associativeOnly, BSON_ARRAY("$path1" << 55 << "$path2")); } TEST_F(ExpressionNaryTest, GroupingOptimizationOnAssociativeOnlyNotExecuteOnSingleConstantsBack) { BSONArray spec = BSON_ARRAY("$path" << 55); addOperandArrayToExpr(_associativeOnly, spec); assertContents(_associativeOnly, spec); intrusive_ptr optimized = _associativeOnly->optimize(); ASSERT(_associativeOnly == optimized); assertContents(_associativeOnly, BSON_ARRAY("$path" << 55)); } TEST_F(ExpressionNaryTest, GroupingOptimizationOnCommutativeAndAssociative) { BSONArray spec = BSON_ARRAY(55 << 66 << "$path"); addOperandArrayToExpr(_associativeAndCommutative, spec); assertContents(_associativeAndCommutative, spec); intrusive_ptr optimized = _associativeAndCommutative->optimize(); ASSERT(_associativeAndCommutative == optimized); assertContents(_associativeAndCommutative, BSON_ARRAY("$path" << BSON_ARRAY(55 << 66))); } TEST_F(ExpressionNaryTest, FlattenOptimizationNotDoneOnOtherExpressionsForAssociativeExpressions) { BSONArray spec = BSON_ARRAY(66 << "$path" << BSON("$sum" << BSON_ARRAY("$path" << 2))); addOperandArrayToExpr(_associativeOnly, spec); assertContents(_associativeOnly, spec); intrusive_ptr optimized = _associativeOnly->optimize(); ASSERT(_associativeOnly == optimized); assertContents(_associativeOnly, spec); } TEST_F(ExpressionNaryTest, FlattenOptimizationNotDoneOnSameButNotAssociativeExpression) { BSONArrayBuilder specBuilder; intrusive_ptr innerOperand = Testable::create(false, false); addOperandArrayToExpr(innerOperand, BSON_ARRAY(100 << "$path1" << 101)); specBuilder.append(expressionToBson(innerOperand)); _associativeOnly->addOperand(innerOperand); addOperandArrayToExpr(_associativeOnly, BSON_ARRAY(99 << "$path2")); specBuilder << 99 << "$path2"; BSONArray spec = specBuilder.arr(); assertContents(_associativeOnly, spec); intrusive_ptr optimized = _associativeOnly->optimize(); ASSERT(_associativeOnly == optimized); assertContents(_associativeOnly, spec); } // Test that if there is an expression of the same type in a non-commutative nor associative // expression, the inner expression is not expanded. // {"$testable" : [ { "$testable" : [ 100, "$path1"] }, 99, "$path2"] } is optimized to: // {"$testable" : [ { "$testable" : [ 100, "$path1"] }, 99, "$path2"] } TEST_F(ExpressionNaryTest, FlattenInnerOperandsOptimizationOnNotCommutativeNorAssociative) { BSONArrayBuilder specBuilder; intrusive_ptr innerOperand = Testable::create(false, false); addOperandArrayToExpr(innerOperand, BSON_ARRAY(100 << "$path1")); specBuilder.append(expressionToBson(innerOperand)); _notAssociativeNorCommutative->addOperand(innerOperand); addOperandArrayToExpr(_notAssociativeNorCommutative, BSON_ARRAY(99 << "$path2")); specBuilder << 99 << "$path2"; BSONArray spec = specBuilder.arr(); assertContents(_notAssociativeNorCommutative, spec); intrusive_ptr optimized = _notAssociativeNorCommutative->optimize(); ASSERT(_notAssociativeNorCommutative == optimized); assertContents(_notAssociativeNorCommutative, spec); } // Test that if there is an expression of the same type as the first operand // in a non-commutative but associative expression, the inner expression is expanded. // Also, there shouldn't be any grouping of the operands. // {"$testable" : [ { "$testable" : [ 100, "$path1"] }, 99, "$path2"] } is optimized to: // {"$testable" : [ 100, "$path1", 99, "$path2"] } TEST_F(ExpressionNaryTest, FlattenInnerOperandsOptimizationOnAssociativeOnlyFrontOperandNoGroup) { BSONArrayBuilder specBuilder; intrusive_ptr innerOperand = Testable::create(true, false); addOperandArrayToExpr(innerOperand, BSON_ARRAY(100 << "$path1")); specBuilder.append(expressionToBson(innerOperand)); _associativeOnly->addOperand(innerOperand); addOperandArrayToExpr(_associativeOnly, BSON_ARRAY(99 << "$path2")); specBuilder << 99 << "$path2"; BSONArray spec = specBuilder.arr(); assertContents(_associativeOnly, spec); intrusive_ptr optimized = _associativeOnly->optimize(); ASSERT(_associativeOnly == optimized); BSONArray expectedContent = BSON_ARRAY(100 << "$path1" << 99 << "$path2"); assertContents(_associativeOnly, expectedContent); } // Test that if there is an expression of the same type as the first operand // in a non-commutative but associative expression, the inner expression is expanded. // Partial collapsing optimization should be applied to the operands. // {"$testable" : [ { "$testable" : [ 100, "$path1", 101] }, 99, "$path2"] } is optimized to: // {"$testable" : [ 100, "$path1", [101, 99], "$path2"] } TEST_F(ExpressionNaryTest, FlattenInnerOperandsOptimizationOnAssociativeOnlyFrontOperandAndGroup) { BSONArrayBuilder specBuilder; intrusive_ptr innerOperand = Testable::create(true, false); addOperandArrayToExpr(innerOperand, BSON_ARRAY(100 << "$path1" << 101)); specBuilder.append(expressionToBson(innerOperand)); _associativeOnly->addOperand(innerOperand); addOperandArrayToExpr(_associativeOnly, BSON_ARRAY(99 << "$path2")); specBuilder << 99 << "$path2"; BSONArray spec = specBuilder.arr(); assertContents(_associativeOnly, spec); intrusive_ptr optimized = _associativeOnly->optimize(); ASSERT(_associativeOnly == optimized); BSONArray expectedContent = BSON_ARRAY(100 << "$path1" << BSON_ARRAY(101 << 99) << "$path2"); assertContents(_associativeOnly, expectedContent); } // Test that if there is an expression of the same type in the middle of the operands // in a non-commutative but associative expression, the inner expression is expanded. // Partial collapsing optimization should not be applied to the operands. // {"$testable" : [ 200, "$path3", { "$testable" : [ 100, "$path1"] }, 99, "$path2"] } is // optimized to: {"$testable" : [ 200, "$path3", 100, "$path1", 99, "$path2"] } TEST_F(ExpressionNaryTest, FlattenInnerOperandsOptimizationOnAssociativeOnlyMiddleOperandNoGroup) { BSONArrayBuilder specBuilder; addOperandArrayToExpr(_associativeOnly, BSON_ARRAY(200 << "$path3")); specBuilder << 200 << "$path3"; intrusive_ptr innerOperand = Testable::create(true, false); addOperandArrayToExpr(innerOperand, BSON_ARRAY(100 << "$path1")); specBuilder.append(expressionToBson(innerOperand)); _associativeOnly->addOperand(innerOperand); addOperandArrayToExpr(_associativeOnly, BSON_ARRAY(99 << "$path2")); specBuilder << 99 << "$path2"; BSONArray spec = specBuilder.arr(); assertContents(_associativeOnly, spec); intrusive_ptr optimized = _associativeOnly->optimize(); ASSERT(_associativeOnly == optimized); BSONArray expectedContent = BSON_ARRAY(200 << "$path3" << 100 << "$path1" << 99 << "$path2"); assertContents(_associativeOnly, expectedContent); } // Test that if there is an expression of the same type in the middle of the operands // in a non-commutative but associative expression, the inner expression is expanded. // Partial collapsing optimization should be applied to the operands. // {"$testable" : [ 200, "$path3", 201 { "$testable" : [ 100, "$path1", 101] }, 99, "$path2"] } is // optimized to: {"$testable" : [ 200, "$path3", [201, 100], "$path1", [101, 99], "$path2"] } TEST_F(ExpressionNaryTest, FlattenInnerOperandsOptimizationOnAssociativeOnlyMiddleOperandAndGroup) { BSONArrayBuilder specBuilder; addOperandArrayToExpr(_associativeOnly, BSON_ARRAY(200 << "$path3" << 201)); specBuilder << 200 << "$path3" << 201; intrusive_ptr innerOperand = Testable::create(true, false); addOperandArrayToExpr(innerOperand, BSON_ARRAY(100 << "$path1" << 101)); specBuilder.append(expressionToBson(innerOperand)); _associativeOnly->addOperand(innerOperand); addOperandArrayToExpr(_associativeOnly, BSON_ARRAY(99 << "$path2")); specBuilder << 99 << "$path2"; BSONArray spec = specBuilder.arr(); assertContents(_associativeOnly, spec); intrusive_ptr optimized = _associativeOnly->optimize(); ASSERT(_associativeOnly == optimized); BSONArray expectedContent = BSON_ARRAY(200 << "$path3" << BSON_ARRAY(201 << 100) << "$path1" << BSON_ARRAY(101 << 99) << "$path2"); assertContents(_associativeOnly, expectedContent); } // Test that if there is an expression of the same type in the back of the operands in a // non-commutative but associative expression, the inner expression is expanded. // Partial collapsing optimization should not be applied to the operands. // {"$testable" : [ 200, "$path3", { "$testable" : [ 100, "$path1"] }] } is // optimized to: {"$testable" : [ 200, "$path3", 100, "$path1"] } TEST_F(ExpressionNaryTest, FlattenInnerOperandsOptimizationOnAssociativeOnlyBackOperandNoGroup) { BSONArrayBuilder specBuilder; addOperandArrayToExpr(_associativeOnly, BSON_ARRAY(200 << "$path3")); specBuilder << 200 << "$path3"; intrusive_ptr innerOperand = Testable::create(true, false); addOperandArrayToExpr(innerOperand, BSON_ARRAY(100 << "$path1")); specBuilder.append(expressionToBson(innerOperand)); _associativeOnly->addOperand(innerOperand); BSONArray spec = specBuilder.arr(); assertContents(_associativeOnly, spec); intrusive_ptr optimized = _associativeOnly->optimize(); ASSERT(_associativeOnly == optimized); BSONArray expectedContent = BSON_ARRAY(200 << "$path3" << 100 << "$path1"); assertContents(_associativeOnly, expectedContent); } // Test that if there is an expression of the same type in the back of the operands in a // non-commutative but associative expression, the inner expression is expanded. // Partial collapsing optimization should be applied to the operands. // {"$testable" : [ 200, "$path3", 201, { "$testable" : [ 100, "$path1", 101] }] } is // optimized to: {"$testable" : [ 200, "$path3", [201, 100], "$path1", 101] } TEST_F(ExpressionNaryTest, FlattenInnerOperandsOptimizationOnAssociativeOnlyBackOperandAndGroup) { BSONArrayBuilder specBuilder; addOperandArrayToExpr(_associativeOnly, BSON_ARRAY(200 << "$path3" << 201)); specBuilder << 200 << "$path3" << 201; intrusive_ptr innerOperand = Testable::create(true, false); addOperandArrayToExpr(innerOperand, BSON_ARRAY(100 << "$path1" << 101)); specBuilder.append(expressionToBson(innerOperand)); _associativeOnly->addOperand(innerOperand); BSONArray spec = specBuilder.arr(); assertContents(_associativeOnly, spec); intrusive_ptr optimized = _associativeOnly->optimize(); ASSERT(_associativeOnly == optimized); BSONArray expectedContent = BSON_ARRAY(200 << "$path3" << BSON_ARRAY(201 << 100) << "$path1" << 101); assertContents(_associativeOnly, expectedContent); } // Test that if there are two consecutive inner expressions of the same type in a non-commutative // but associative expression, both expressions are correctly flattened. // Partial collapsing optimization should not be applied to the operands. // {"$testable" : [ { "$testable" : [ 100, "$path1"] }, { "$testable" : [ 200, "$path2"] }] } is // optimized to: {"$testable" : [ 100, "$path1", 200, "$path2"] } TEST_F(ExpressionNaryTest, FlattenConsecutiveInnerOperandsOptimizationOnAssociativeOnlyNoGroup) { BSONArrayBuilder specBuilder; intrusive_ptr innerOperand = Testable::create(true, false); addOperandArrayToExpr(innerOperand, BSON_ARRAY(100 << "$path1")); specBuilder.append(expressionToBson(innerOperand)); _associativeOnly->addOperand(innerOperand); intrusive_ptr innerOperand2 = Testable::create(true, false); addOperandArrayToExpr(innerOperand2, BSON_ARRAY(200 << "$path2")); specBuilder.append(expressionToBson(innerOperand2)); _associativeOnly->addOperand(innerOperand2); BSONArray spec = specBuilder.arr(); assertContents(_associativeOnly, spec); intrusive_ptr optimized = _associativeOnly->optimize(); ASSERT(_associativeOnly == optimized); BSONArray expectedContent = BSON_ARRAY(100 << "$path1" << 200 << "$path2"); assertContents(_associativeOnly, expectedContent); } // Test that if there are two consecutive inner expressions of the same type in a non-commutative // but associative expression, both expressions are correctly flattened. // Partial collapsing optimization should be applied to the operands. // {"$testable" : [ { "$testable" : [ 100, "$path1", 101] }, { "$testable" : [ 200, "$path2"] }] } // is optimized to: {"$testable" : [ 100, "$path1", [ 101, 200], "$path2"] } TEST_F(ExpressionNaryTest, FlattenConsecutiveInnerOperandsOptimizationOnAssociativeAndGroup) { BSONArrayBuilder specBuilder; intrusive_ptr innerOperand = Testable::create(true, false); addOperandArrayToExpr(innerOperand, BSON_ARRAY(100 << "$path1" << 101)); specBuilder.append(expressionToBson(innerOperand)); _associativeOnly->addOperand(innerOperand); intrusive_ptr innerOperand2 = Testable::create(true, false); addOperandArrayToExpr(innerOperand2, BSON_ARRAY(200 << "$path2")); specBuilder.append(expressionToBson(innerOperand2)); _associativeOnly->addOperand(innerOperand2); BSONArray spec = specBuilder.arr(); assertContents(_associativeOnly, spec); intrusive_ptr optimized = _associativeOnly->optimize(); ASSERT(_associativeOnly == optimized); BSONArray expectedContent = BSON_ARRAY(100 << "$path1" << BSON_ARRAY(101 << 200) << "$path2"); assertContents(_associativeOnly, expectedContent); } // Test that inner expressions are correctly flattened and constant operands re-arranged and // collapsed when using a commutative and associative expression. // {"$testable" : [ 200, "$path3", 201, { "$testable" : [ 100, "$path1", 101] }, 99, "$path2"] } is // optimized to: {"$testable" : [ "$path3", "$path1", "$path2", [200, 201, [ 100, 101], 99] ] } TEST_F(ExpressionNaryTest, FlattenInnerOperandsOptimizationOnCommutativeAndAssociative) { BSONArrayBuilder specBuilder; addOperandArrayToExpr(_associativeAndCommutative, BSON_ARRAY(200 << "$path3" << 201)); specBuilder << 200 << "$path3" << 201; intrusive_ptr innerOperand = Testable::create(true, true); addOperandArrayToExpr(innerOperand, BSON_ARRAY(100 << "$path1" << 101)); specBuilder.append(expressionToBson(innerOperand)); _associativeAndCommutative->addOperand(innerOperand); addOperandArrayToExpr(_associativeAndCommutative, BSON_ARRAY(99 << "$path2")); specBuilder << 99 << "$path2"; BSONArray spec = specBuilder.arr(); assertContents(_associativeAndCommutative, spec); intrusive_ptr optimized = _associativeAndCommutative->optimize(); ASSERT(_associativeAndCommutative == optimized); BSONArray expectedContent = BSON_ARRAY("$path3" << "$path1" << "$path2" << BSON_ARRAY(200 << 201 << BSON_ARRAY(100 << 101) << 99)); assertContents(_associativeAndCommutative, expectedContent); } /* ------------------------- ExpressionArrayToObject -------------------------- */ TEST(ExpressionArrayToObjectTest, KVFormatSimple) { assertExpectedResults("$arrayToObject", {{{Value(BSON_ARRAY(BSON("k" << "key1" << "v" << 2) << BSON("k" << "key2" << "v" << 3)))}, {Value(BSON("key1" << 2 << "key2" << 3))}}}); } TEST(ExpressionArrayToObjectTest, KVFormatWithDuplicates) { assertExpectedResults("$arrayToObject", {{{Value(BSON_ARRAY(BSON("k" << "hi" << "v" << 2) << BSON("k" << "hi" << "v" << 3)))}, {Value(BSON("hi" << 3))}}}); } TEST(ExpressionArrayToObjectTest, ListFormatSimple) { assertExpectedResults("$arrayToObject", {{{Value(BSON_ARRAY(BSON_ARRAY("key1" << 2) << BSON_ARRAY("key2" << 3)))}, {Value(BSON("key1" << 2 << "key2" << 3))}}}); } TEST(ExpressionArrayToObjectTest, ListFormWithDuplicates) { assertExpectedResults("$arrayToObject", {{{Value(BSON_ARRAY(BSON_ARRAY("key1" << 2) << BSON_ARRAY("key1" << 3)))}, {Value(BSON("key1" << 3))}}}); } /* ------------------------- ExpressionCeil -------------------------- */ class ExpressionCeilTest : public ExpressionNaryTestOneArg { public: virtual void assertEvaluates(Value input, Value output) override { intrusive_ptr expCtx(new ExpressionContextForTest()); _expr = new ExpressionCeil(expCtx); ExpressionNaryTestOneArg::assertEvaluates(input, output); } }; TEST_F(ExpressionCeilTest, IntArg) { assertEvaluates(Value(0), Value(0)); assertEvaluates(Value(numeric_limits::min()), Value(numeric_limits::min())); assertEvaluates(Value(numeric_limits::max()), Value(numeric_limits::max())); } TEST_F(ExpressionCeilTest, LongArg) { assertEvaluates(Value(0LL), Value(0LL)); assertEvaluates(Value(numeric_limits::min()), Value(numeric_limits::min())); assertEvaluates(Value(numeric_limits::max()), Value(numeric_limits::max())); } TEST_F(ExpressionCeilTest, DoubleArg) { assertEvaluates(Value(2.0), Value(2.0)); assertEvaluates(Value(-2.0), Value(-2.0)); assertEvaluates(Value(0.9), Value(1.0)); assertEvaluates(Value(0.1), Value(1.0)); assertEvaluates(Value(-1.2), Value(-1.0)); assertEvaluates(Value(-1.7), Value(-1.0)); // Outside the range of long longs (there isn't enough precision for decimals in this range, so // ceil should just preserve the number). double largerThanLong = numeric_limits::max() * 2.0; assertEvaluates(Value(largerThanLong), Value(largerThanLong)); double smallerThanLong = numeric_limits::min() * 2.0; assertEvaluates(Value(smallerThanLong), Value(smallerThanLong)); } TEST_F(ExpressionCeilTest, DecimalArg) { assertEvaluates(Value(Decimal128("2")), Value(Decimal128("2.0"))); assertEvaluates(Value(Decimal128("-2")), Value(Decimal128("-2.0"))); assertEvaluates(Value(Decimal128("0.9")), Value(Decimal128("1.0"))); assertEvaluates(Value(Decimal128("0.1")), Value(Decimal128("1.0"))); assertEvaluates(Value(Decimal128("-1.2")), Value(Decimal128("-1.0"))); assertEvaluates(Value(Decimal128("-1.7")), Value(Decimal128("-1.0"))); assertEvaluates(Value(Decimal128("1234567889.000000000000000000000001")), Value(Decimal128("1234567890"))); assertEvaluates(Value(Decimal128("-99999999999999999999999999999.99")), Value(Decimal128("-99999999999999999999999999999.00"))); assertEvaluates(Value(Decimal128("3.4E-6000")), Value(Decimal128("1"))); } TEST_F(ExpressionCeilTest, NullArg) { assertEvaluates(Value(BSONNULL), Value(BSONNULL)); } /* ------------------------- ExpressionFloor -------------------------- */ class ExpressionFloorTest : public ExpressionNaryTestOneArg { public: virtual void assertEvaluates(Value input, Value output) override { intrusive_ptr expCtx(new ExpressionContextForTest()); _expr = new ExpressionFloor(expCtx); ExpressionNaryTestOneArg::assertEvaluates(input, output); } }; TEST_F(ExpressionFloorTest, IntArg) { assertEvaluates(Value(0), Value(0)); assertEvaluates(Value(numeric_limits::min()), Value(numeric_limits::min())); assertEvaluates(Value(numeric_limits::max()), Value(numeric_limits::max())); } TEST_F(ExpressionFloorTest, LongArg) { assertEvaluates(Value(0LL), Value(0LL)); assertEvaluates(Value(numeric_limits::min()), Value(numeric_limits::min())); assertEvaluates(Value(numeric_limits::max()), Value(numeric_limits::max())); } TEST_F(ExpressionFloorTest, DoubleArg) { assertEvaluates(Value(2.0), Value(2.0)); assertEvaluates(Value(-2.0), Value(-2.0)); assertEvaluates(Value(0.9), Value(0.0)); assertEvaluates(Value(0.1), Value(0.0)); assertEvaluates(Value(-1.2), Value(-2.0)); assertEvaluates(Value(-1.7), Value(-2.0)); // Outside the range of long longs (there isn't enough precision for decimals in this range, so // floor should just preserve the number). double largerThanLong = numeric_limits::max() * 2.0; assertEvaluates(Value(largerThanLong), Value(largerThanLong)); double smallerThanLong = numeric_limits::min() * 2.0; assertEvaluates(Value(smallerThanLong), Value(smallerThanLong)); } TEST_F(ExpressionFloorTest, DecimalArg) { assertEvaluates(Value(Decimal128("2")), Value(Decimal128("2.0"))); assertEvaluates(Value(Decimal128("-2")), Value(Decimal128("-2.0"))); assertEvaluates(Value(Decimal128("0.9")), Value(Decimal128("0.0"))); assertEvaluates(Value(Decimal128("0.1")), Value(Decimal128("0.0"))); assertEvaluates(Value(Decimal128("-1.2")), Value(Decimal128("-2.0"))); assertEvaluates(Value(Decimal128("-1.7")), Value(Decimal128("-2.0"))); assertEvaluates(Value(Decimal128("1234567890.000000000000000000000001")), Value(Decimal128("1234567890"))); assertEvaluates(Value(Decimal128("-99999999999999999999999999999.99")), Value(Decimal128("-100000000000000000000000000000"))); assertEvaluates(Value(Decimal128("3.4E-6000")), Value(Decimal128("0"))); } TEST_F(ExpressionFloorTest, NullArg) { assertEvaluates(Value(BSONNULL), Value(BSONNULL)); } /* ------------------------ ExpressionRange --------------------------- */ TEST(ExpressionRangeTest, ComputesStandardRange) { assertExpectedResults("$range", {{{Value(0), Value(3)}, Value(BSON_ARRAY(0 << 1 << 2))}}); } TEST(ExpressionRangeTest, ComputesRangeWithStep) { assertExpectedResults("$range", {{{Value(0), Value(6), Value(2)}, Value(BSON_ARRAY(0 << 2 << 4))}}); } TEST(ExpressionRangeTest, ComputesReverseRange) { assertExpectedResults("$range", {{{Value(0), Value(-3), Value(-1)}, Value(BSON_ARRAY(0 << -1 << -2))}}); } TEST(ExpressionRangeTest, ComputesRangeWithPositiveAndNegative) { assertExpectedResults("$range", {{{Value(-2), Value(3)}, Value(BSON_ARRAY(-2 << -1 << 0 << 1 << 2))}}); } TEST(ExpressionRangeTest, ComputesEmptyRange) { assertExpectedResults("$range", {{{Value(-2), Value(3), Value(-1)}, Value(std::vector())}}); } TEST(ExpressionRangeTest, ComputesRangeWithSameStartAndEnd) { assertExpectedResults("$range", {{{Value(20), Value(20)}, Value(std::vector())}}); } TEST(ExpressionRangeTest, ComputesRangeWithLargeNegativeStep) { assertExpectedResults("$range", {{{Value(3), Value(-5), Value(-3)}, Value(BSON_ARRAY(3 << 0 << -3))}}); } /* ------------------------ ExpressionReverseArray -------------------- */ TEST(ExpressionReverseArrayTest, ReversesNormalArray) { assertExpectedResults("$reverseArray", {{{Value(BSON_ARRAY(1 << 2 << 3))}, Value(BSON_ARRAY(3 << 2 << 1))}}); } TEST(ExpressionReverseArrayTest, ReversesEmptyArray) { assertExpectedResults("$reverseArray", {{{Value(std::vector())}, Value(std::vector())}}); } TEST(ExpressionReverseArrayTest, ReversesOneElementArray) { assertExpectedResults("$reverseArray", {{{Value(BSON_ARRAY(1))}, Value(BSON_ARRAY(1))}}); } TEST(ExpressionReverseArrayTest, ReturnsNullWithNullishInput) { assertExpectedResults( "$reverseArray", {{{Value(BSONNULL)}, Value(BSONNULL)}, {{Value(BSONUndefined)}, Value(BSONNULL)}}); } /* ------------------------- ExpressionRound -------------------------- */ class ExpressionRoundOneArgTest : public ExpressionNaryTestOneArg { public: void assertEval(ImplicitValue input, ImplicitValue output) { intrusive_ptr expCtx(new ExpressionContextForTest()); _expr = new ExpressionRound(expCtx); ExpressionNaryTestOneArg::assertEvaluates(input, output); } }; class ExpressionRoundTwoArgTest : public ExpressionNaryTestTwoArg { public: void assertEval(ImplicitValue input1, ImplicitValue input2, ImplicitValue output) { intrusive_ptr expCtx(new ExpressionContextForTest()); _expr = new ExpressionRound(expCtx); ExpressionNaryTestTwoArg::assertEvaluates(input1, input2, output); } }; TEST_F(ExpressionRoundOneArgTest, IntArg1) { assertEval(0, 0); assertEval(numeric_limits::min(), numeric_limits::min()); assertEval(numeric_limits::max(), numeric_limits::max()); } TEST_F(ExpressionRoundTwoArgTest, IntArg2) { assertEval(0, 0, 0); assertEval(2, -1, 0); assertEval(29, -1, 30); assertEval(numeric_limits::min(), 10, numeric_limits::min()); assertEval(numeric_limits::max(), 42, numeric_limits::max()); } TEST_F(ExpressionRoundOneArgTest, LongArg1) { assertEval(0LL, 0LL); assertEval(numeric_limits::min(), numeric_limits::min()); assertEval(numeric_limits::max(), numeric_limits::max()); } TEST_F(ExpressionRoundTwoArgTest, LongArg2) { assertEval(0LL, 0LL, 0LL); assertEval(2LL, -1LL, 0LL); assertEval(29LL, -1LL, 30LL); assertEval(numeric_limits::min(), 10LL, numeric_limits::min()); assertEval(numeric_limits::max(), 42LL, numeric_limits::max()); } TEST_F(ExpressionRoundOneArgTest, DoubleArg1) { assertEval(2.0, 2.0); assertEval(-2.0, -2.0); assertEval(0.9, 1.0); assertEval(0.1, 0.0); assertEval(1.5, 2.0); assertEval(2.5, 2.0); assertEval(3.5, 4.0); assertEval(-1.2, -1.0); assertEval(-1.7, -2.0); assertEval(-1.5, -2.0); assertEval(-2.5, -2.0); // Outside the range of long longs (there isn't enough precision for decimals in this range, so // should just preserve the number). double largerThanLong = numeric_limits::max() * 2.0; assertEval(largerThanLong, largerThanLong); double smallerThanLong = numeric_limits::min() * 2.0; assertEval(smallerThanLong, smallerThanLong); } TEST_F(ExpressionRoundTwoArgTest, DoubleArg2) { assertEval(2.0, 1.0, 2.0); assertEval(-2.0, 2.0, -2.0); assertEval(0.9, 0, 1.0); assertEval(0.1, 0, 0.0); assertEval(1.5, 0, 2.0); assertEval(2.5, 0, 2.0); assertEval(3.5, 0, 4.0); assertEval(-1.2, 0, -1.0); assertEval(-1.7, 0, -2.0); assertEval(-1.5, 0, -2.0); assertEval(-2.5, 0, -2.0); assertEval(-3.14159265, 0, -3.0); assertEval(-3.14159265, 1, -3.1); assertEval(-3.14159265, 2, -3.14); assertEval(-3.14159265, 3, -3.142); assertEval(-3.14159265, 4, -3.1416); assertEval(-3.14159265, 5, -3.14159); assertEval(-3.14159265, 6, -3.141593); assertEval(-3.14159265, 7, -3.1415927); assertEval(-3.14159265, 100, -3.14159265); assertEval(3.14159265, 0, 3.0); assertEval(3.14159265, 1, 3.1); assertEval(3.14159265, 2, 3.14); assertEval(3.14159265, 3, 3.142); assertEval(3.14159265, 4, 3.1416); assertEval(3.14159265, 5, 3.14159); assertEval(3.14159265, 6, 3.141593); assertEval(3.14159265, 7, 3.1415927); assertEval(3.14159265, 100, 3.14159265); assertEval(3.14159265, -1, 0.0); assertEval(335.14159265, -1, 340.0); assertEval(333.14159265, -2, 300.0); } TEST_F(ExpressionRoundOneArgTest, DecimalArg1) { assertEval(Decimal128("2"), Decimal128("2.0")); assertEval(Decimal128("-2"), Decimal128("-2.0")); assertEval(Decimal128("0.9"), Decimal128("1.0")); assertEval(Decimal128("0.1"), Decimal128("0.0")); assertEval(Decimal128("0.5"), Decimal128("0.0")); assertEval(Decimal128("1.5"), Decimal128("2.0")); assertEval(Decimal128("2.5"), Decimal128("2.0")); assertEval(Decimal128("-1.2"), Decimal128("-1.0")); assertEval(Decimal128("-1.7"), Decimal128("-2.0")); assertEval(Decimal128("123456789.9999999999999999999999999"), Decimal128("123456790")); assertEval(Decimal128("-99999999999999999999999999999.99"), Decimal128("-100000000000000000000000000000")); assertEval(Decimal128("3.4E-6000"), Decimal128("0")); } TEST_F(ExpressionRoundTwoArgTest, DecimalArg2) { assertEval(Decimal128("2"), 0, Decimal128("2.0")); assertEval(Decimal128("-2"), 0, Decimal128("-2.0")); assertEval(Decimal128("0.9"), 0, Decimal128("1.0")); assertEval(Decimal128("0.1"), 0, Decimal128("0.0")); assertEval(Decimal128("0.5"), 0, Decimal128("0.0")); assertEval(Decimal128("1.5"), 0, Decimal128("2.0")); assertEval(Decimal128("2.5"), 0, Decimal128("2.0")); assertEval(Decimal128("-1.2"), 0, Decimal128("-1.0")); assertEval(Decimal128("-1.7"), 0, Decimal128("-2.0")); assertEval(Decimal128("123456789.9999999999999999999999999"), 0, Decimal128("123456790")); assertEval(Decimal128("-99999999999999999999999999999.99"), 0, Decimal128("-100000000000000000000000000000")); assertEval(Decimal128("3.4E-6000"), 0, Decimal128("0")); assertEval(Decimal128("-3.14159265"), 0, Decimal128("-3.0")); assertEval(Decimal128("-3.14159265"), 1, Decimal128("-3.1")); assertEval(Decimal128("-3.14159265"), 2, Decimal128("-3.14")); assertEval(Decimal128("-3.14159265"), 3, Decimal128("-3.142")); assertEval(Decimal128("-3.14159265"), 4, Decimal128("-3.1416")); assertEval(Decimal128("-3.14159265"), 5, Decimal128("-3.14159")); assertEval(Decimal128("-3.14159265"), 6, Decimal128("-3.141593")); assertEval(Decimal128("-3.14159265"), 7, Decimal128("-3.1415926")); assertEval(Decimal128("-3.14159265"), 100, Decimal128("-3.14159265")); assertEval(Decimal128("3.14159265"), 0, Decimal128("3.0")); assertEval(Decimal128("3.14159265"), 1, Decimal128("3.1")); assertEval(Decimal128("3.14159265"), 2, Decimal128("3.14")); assertEval(Decimal128("3.14159265"), Decimal128("3"), Decimal128("3.142")); assertEval(Decimal128("3.14159265"), 4, Decimal128("3.1416")); assertEval(Decimal128("3.14159265"), Decimal128("5"), Decimal128("3.14159")); assertEval(Decimal128("3.14159265"), 6, Decimal128("3.141593")); assertEval(Decimal128("3.14159265"), 7, Decimal128("3.1415926")); assertEval(Decimal128("3.14159265"), 100, Decimal128("3.14159265")); assertEval(Decimal128("3.14159265"), Decimal128("-1"), Decimal128("0")); assertEval(Decimal128("335.14159265"), -1, Decimal128("340")); assertEval(Decimal128("333.14159265"), -2, Decimal128("300")); } TEST_F(ExpressionRoundOneArgTest, NullArg1) { assertEval(BSONNULL, BSONNULL); } TEST_F(ExpressionRoundTwoArgTest, NullArg2) { assertEval(BSONNULL, BSONNULL, BSONNULL); assertEval(1, BSONNULL, BSONNULL); assertEval(BSONNULL, 1, BSONNULL); } /* ------------------------- ExpressionTrunc -------------------------- */ class ExpressionTruncOneArgTest : public ExpressionNaryTestOneArg { public: void assertEval(ImplicitValue input, ImplicitValue output) { intrusive_ptr expCtx(new ExpressionContextForTest()); _expr = new ExpressionTrunc(expCtx); ExpressionNaryTestOneArg::assertEvaluates(input, output); } }; class ExpressionTruncTwoArgTest : public ExpressionNaryTestTwoArg { public: void assertEval(ImplicitValue input1, ImplicitValue input2, ImplicitValue output) { intrusive_ptr expCtx(new ExpressionContextForTest()); _expr = new ExpressionTrunc(expCtx); ExpressionNaryTestTwoArg::assertEvaluates(input1, input2, output); } }; TEST_F(ExpressionTruncOneArgTest, IntArg1) { assertEval(0, 0); assertEval(0, 0); assertEval(numeric_limits::min(), numeric_limits::min()); assertEval(numeric_limits::max(), numeric_limits::max()); } TEST_F(ExpressionTruncTwoArgTest, IntArg2) { assertEval(0, 0, 0); assertEval(2, -1, 0); assertEval(29, -1, 20); assertEval(numeric_limits::min(), 10, numeric_limits::min()); assertEval(numeric_limits::max(), 42, numeric_limits::max()); } TEST_F(ExpressionTruncOneArgTest, LongArg1) { assertEval(0LL, 0LL); assertEval(numeric_limits::min(), numeric_limits::min()); assertEval(numeric_limits::max(), numeric_limits::max()); } TEST_F(ExpressionTruncTwoArgTest, LongArg2) { assertEval(0LL, 0LL, 0LL); assertEval(2LL, -1LL, 0LL); assertEval(29LL, -1LL, 20LL); assertEval(numeric_limits::min(), 10LL, numeric_limits::min()); assertEval(numeric_limits::max(), 42LL, numeric_limits::max()); } TEST_F(ExpressionTruncOneArgTest, DoubleArg1) { assertEval(2.0, 2.0); assertEval(-2.0, -2.0); assertEval(0.9, 0.0); assertEval(0.1, 0.0); assertEval(0.5, 0.0); assertEval(1.5, 1.0); assertEval(2.5, 2.0); assertEval(-1.2, -1.0); assertEval(-1.7, -1.0); assertEval(-0.5, -0.0); assertEval(-1.5, -1.0); assertEval(-2.5, -2.0); // Outside the range of long longs (there isn't enough precision for decimals in this range, so // should just preserve the number). double largerThanLong = numeric_limits::max() * 2.0; assertEval(largerThanLong, largerThanLong); double smallerThanLong = numeric_limits::min() * 2.0; assertEval(smallerThanLong, smallerThanLong); } TEST_F(ExpressionTruncTwoArgTest, DoubleArg2) { assertEval(2.0, 1.0, 2.0); assertEval(-2.0, 2.0, -2.0); assertEval(0.9, 0, 0.0); assertEval(0.1, 0, 0.0); assertEval(0.5, 0, 0.0); assertEval(1.5, 0, 1.0); assertEval(2.5, 0, 2.0); assertEval(-1.2, 0, -1.0); assertEval(-1.7, 0, -1.0); assertEval(-0.5, 0, -0.0); assertEval(-1.5, 0, -1.0); assertEval(-2.5, 0, -2.0); assertEval(-3.14159265, 0, -3.0); assertEval(-3.14159265, 1, -3.1); assertEval(-3.14159265, 2, -3.14); assertEval(-3.14159265, 3, -3.141); assertEval(-3.14159265, 4, -3.1415); assertEval(-3.14159265, 5, -3.14159); assertEval(-3.14159265, 6, -3.141592); assertEval(-3.14159265, 7, -3.1415926); assertEval(-3.14159265, 100, -3.14159265); assertEval(3.14159265, 0, 3.0); assertEval(3.14159265, 1, 3.1); assertEval(3.14159265, 2, 3.14); assertEval(3.14159265, 3, 3.141); assertEval(3.14159265, 4, 3.1415); assertEval(3.14159265, 5, 3.14159); assertEval(3.14159265, 6, 3.141592); assertEval(3.14159265, 7, 3.1415926); assertEval(3.14159265, 100, 3.14159265); assertEval(3.14159265, -1, 0.0); assertEval(335.14159265, -1, 330.0); assertEval(333.14159265, -2, 300.0); } TEST_F(ExpressionTruncOneArgTest, DecimalArg1) { assertEval(Decimal128("2"), Decimal128("2.0")); assertEval(Decimal128("-2"), Decimal128("-2.0")); assertEval(Decimal128("0.9"), Decimal128("0.0")); assertEval(Decimal128("0.1"), Decimal128("0.0")); assertEval(Decimal128("-1.2"), Decimal128("-1.0")); assertEval(Decimal128("-1.7"), Decimal128("-1.0")); assertEval(Decimal128("123456789.9999999999999999999999999"), Decimal128("123456789")); assertEval(Decimal128("-99999999999999999999999999999.99"), Decimal128("-99999999999999999999999999999.00")); assertEval(Decimal128("3.4E-6000"), Decimal128("0")); } TEST_F(ExpressionTruncTwoArgTest, DecimalArg2) { assertEval(Decimal128("2"), 0, Decimal128("2.0")); assertEval(Decimal128("-2"), 0, Decimal128("-2.0")); assertEval(Decimal128("0.9"), 0, Decimal128("0.0")); assertEval(Decimal128("0.1"), 0, Decimal128("0.0")); assertEval(Decimal128("-1.2"), 0, Decimal128("-1.0")); assertEval(Decimal128("-1.7"), 0, Decimal128("-1.0")); assertEval(Decimal128("123456789.9999999999999999999999999"), 0, Decimal128("123456789")); assertEval(Decimal128("-99999999999999999999999999999.99"), 0, Decimal128("-99999999999999999999999999999.00")); assertEval(Decimal128("3.4E-6000"), 0, Decimal128("0")); assertEval(Decimal128("-3.14159265"), 0, Decimal128("-3.0")); assertEval(Decimal128("-3.14159265"), 1, Decimal128("-3.1")); assertEval(Decimal128("-3.14159265"), 2, Decimal128("-3.14")); assertEval(Decimal128("-3.14159265"), 3, Decimal128("-3.141")); assertEval(Decimal128("-3.14159265"), 4, Decimal128("-3.1415")); assertEval(Decimal128("-3.14159265"), 5, Decimal128("-3.14159")); assertEval(Decimal128("-3.14159265"), 6, Decimal128("-3.141592")); assertEval(Decimal128("-3.14159265"), 7, Decimal128("-3.1415926")); assertEval(Decimal128("-3.14159265"), 100, Decimal128("-3.14159265")); assertEval(Decimal128("3.14159265"), 0, Decimal128("3.0")); assertEval(Decimal128("3.14159265"), 1, Decimal128("3.1")); assertEval(Decimal128("3.14159265"), 2, Decimal128("3.14")); assertEval(Decimal128("3.14159265"), Decimal128("3"), Decimal128("3.141")); assertEval(Decimal128("3.14159265"), 4, Decimal128("3.1415")); assertEval(Decimal128("3.14159265"), Decimal128("5"), Decimal128("3.14159")); assertEval(Decimal128("3.14159265"), 6, Decimal128("3.141592")); assertEval(Decimal128("3.14159265"), 7, Decimal128("3.1415926")); assertEval(Decimal128("3.14159265"), 100, Decimal128("3.14159265")); assertEval(Decimal128("3.14159265"), Decimal128("-1"), Decimal128("0")); assertEval(Decimal128("335.14159265"), -1, Decimal128("330")); assertEval(Decimal128("333.14159265"), -2, Decimal128("300")); } TEST_F(ExpressionTruncOneArgTest, NullArg1) { assertEval((BSONNULL), (BSONNULL)); } TEST_F(ExpressionTruncTwoArgTest, NullArg2) { assertEval((BSONNULL), (BSONNULL), (BSONNULL)); assertEval((1), (BSONNULL), (BSONNULL)); assertEval((BSONNULL), (1), (BSONNULL)); } /* ------------------------- ExpressionSqrt -------------------------- */ class ExpressionSqrtTest : public ExpressionNaryTestOneArg { public: virtual void assertEvaluates(Value input, Value output) override { intrusive_ptr expCtx(new ExpressionContextForTest()); _expr = new ExpressionSqrt(expCtx); ExpressionNaryTestOneArg::assertEvaluates(input, output); } }; TEST_F(ExpressionSqrtTest, SqrtIntArg) { assertEvaluates(Value(0), Value(0.0)); assertEvaluates(Value(1), Value(1.0)); assertEvaluates(Value(25), Value(5.0)); } TEST_F(ExpressionSqrtTest, SqrtLongArg) { assertEvaluates(Value(0LL), Value(0.0)); assertEvaluates(Value(1LL), Value(1.0)); assertEvaluates(Value(25LL), Value(5.0)); assertEvaluates(Value(40000000000LL), Value(200000.0)); } TEST_F(ExpressionSqrtTest, SqrtDoubleArg) { assertEvaluates(Value(0.0), Value(0.0)); assertEvaluates(Value(1.0), Value(1.0)); assertEvaluates(Value(25.0), Value(5.0)); } TEST_F(ExpressionSqrtTest, SqrtDecimalArg) { assertEvaluates(Value(Decimal128("0")), Value(Decimal128("0"))); assertEvaluates(Value(Decimal128("1")), Value(Decimal128("1"))); assertEvaluates(Value(Decimal128("25")), Value(Decimal128("5"))); assertEvaluates(Value(Decimal128("30.25")), Value(Decimal128("5.5"))); } TEST_F(ExpressionSqrtTest, SqrtNullArg) { assertEvaluates(Value(BSONNULL), Value(BSONNULL)); } TEST_F(ExpressionSqrtTest, SqrtNaNArg) { assertEvaluates(Value(std::numeric_limits::quiet_NaN()), Value(std::numeric_limits::quiet_NaN())); } /* ------------------------- ExpressionExp -------------------------- */ class ExpressionExpTest : public ExpressionNaryTestOneArg { public: virtual void assertEvaluates(Value input, Value output) override { intrusive_ptr expCtx(new ExpressionContextForTest()); _expr = new ExpressionExp(expCtx); ExpressionNaryTestOneArg::assertEvaluates(input, output); } const Decimal128 decimalE = Decimal128("2.718281828459045235360287471352662"); }; TEST_F(ExpressionExpTest, ExpIntArg) { assertEvaluates(Value(0), Value(1.0)); assertEvaluates(Value(1), Value(exp(1.0))); } TEST_F(ExpressionExpTest, ExpLongArg) { assertEvaluates(Value(0LL), Value(1.0)); assertEvaluates(Value(1LL), Value(exp(1.0))); } TEST_F(ExpressionExpTest, ExpDoubleArg) { assertEvaluates(Value(0.0), Value(1.0)); assertEvaluates(Value(1.0), Value(exp(1.0))); } TEST_F(ExpressionExpTest, ExpDecimalArg) { assertEvaluates(Value(Decimal128("0")), Value(Decimal128("1"))); assertEvaluates(Value(Decimal128("1")), Value(decimalE)); } TEST_F(ExpressionExpTest, ExpNullArg) { assertEvaluates(Value(BSONNULL), Value(BSONNULL)); } TEST_F(ExpressionExpTest, ExpNaNArg) { assertEvaluates(Value(std::numeric_limits::quiet_NaN()), Value(std::numeric_limits::quiet_NaN())); } /* ------------------------- Old-style tests -------------------------- */ namespace Add { class ExpectedResultBase { public: virtual ~ExpectedResultBase() {} void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = new ExpressionAdd(expCtx); populateOperands(expression); ASSERT_BSONOBJ_EQ(expectedResult(), toBson(expression->evaluate({}, &expCtx->variables))); } protected: virtual void populateOperands(intrusive_ptr& expression) = 0; virtual BSONObj expectedResult() = 0; }; /** $add with a NULL Document pointer, as called by ExpressionNary::optimize(). */ class NullDocument { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = new ExpressionAdd(expCtx); expression->addOperand(ExpressionConstant::create(expCtx, Value(2))); ASSERT_BSONOBJ_EQ(BSON("" << 2), toBson(expression->evaluate({}, &expCtx->variables))); } }; /** $add without operands. */ class NoOperands : public ExpectedResultBase { void populateOperands(intrusive_ptr& expression) {} virtual BSONObj expectedResult() { return BSON("" << 0); } }; /** String type unsupported. */ class String { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = new ExpressionAdd(expCtx); expression->addOperand(ExpressionConstant::create(expCtx, Value("a"_sd))); ASSERT_THROWS(expression->evaluate({}, &expCtx->variables), AssertionException); } }; /** Bool type unsupported. */ class Bool { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = new ExpressionAdd(expCtx); expression->addOperand(ExpressionConstant::create(expCtx, Value(true))); ASSERT_THROWS(expression->evaluate({}, &expCtx->variables), AssertionException); } }; class SingleOperandBase : public ExpectedResultBase { void populateOperands(intrusive_ptr& expression) { intrusive_ptr expCtx(new ExpressionContextForTest()); expression->addOperand(ExpressionConstant::create(expCtx, valueFromBson(operand()))); } BSONObj expectedResult() { return operand(); } protected: virtual BSONObj operand() = 0; }; /** Single int argument. */ class Int : public SingleOperandBase { BSONObj operand() { return BSON("" << 1); } }; /** Single long argument. */ class Long : public SingleOperandBase { BSONObj operand() { return BSON("" << 5555LL); } }; /** Single double argument. */ class Double : public SingleOperandBase { BSONObj operand() { return BSON("" << 99.99); } }; /** Single Date argument. */ class Date : public SingleOperandBase { BSONObj operand() { return BSON("" << Date_t::fromMillisSinceEpoch(12345)); } }; /** Single null argument. */ class Null : public SingleOperandBase { BSONObj operand() { return BSON("" << BSONNULL); } BSONObj expectedResult() { return BSON("" << BSONNULL); } }; /** Single undefined argument. */ class Undefined : public SingleOperandBase { BSONObj operand() { return fromjson("{'':undefined}"); } BSONObj expectedResult() { return BSON("" << BSONNULL); } }; class TwoOperandBase : public ExpectedResultBase { public: TwoOperandBase() : _reverse() {} void run() { ExpectedResultBase::run(); // Now add the operands in the reverse direction. _reverse = true; ExpectedResultBase::run(); } protected: void populateOperands(intrusive_ptr& expression) { intrusive_ptr expCtx(new ExpressionContextForTest()); expression->addOperand( ExpressionConstant::create(expCtx, valueFromBson(_reverse ? operand2() : operand1()))); expression->addOperand( ExpressionConstant::create(expCtx, valueFromBson(_reverse ? operand1() : operand2()))); } virtual BSONObj operand1() = 0; virtual BSONObj operand2() = 0; private: bool _reverse; }; /** Add two ints. */ class IntInt : public TwoOperandBase { BSONObj operand1() { return BSON("" << 1); } BSONObj operand2() { return BSON("" << 5); } BSONObj expectedResult() { return BSON("" << 6); } }; /** Adding two large ints produces a long, not an overflowed int. */ class IntIntNoOverflow : public TwoOperandBase { BSONObj operand1() { return BSON("" << numeric_limits::max()); } BSONObj operand2() { return BSON("" << numeric_limits::max()); } BSONObj expectedResult() { return BSON("" << ((long long)(numeric_limits::max()) + numeric_limits::max())); } }; /** Adding an int and a long produces a long. */ class IntLong : public TwoOperandBase { BSONObj operand1() { return BSON("" << 1); } BSONObj operand2() { return BSON("" << 9LL); } BSONObj expectedResult() { return BSON("" << 10LL); } }; /** Adding an int and a long produces a double. */ class IntLongOverflowToDouble : public TwoOperandBase { BSONObj operand1() { return BSON("" << numeric_limits::max()); } BSONObj operand2() { return BSON("" << numeric_limits::max()); } BSONObj expectedResult() { // When the result cannot be represented in a NumberLong, a NumberDouble is returned. const auto im = numeric_limits::max(); const auto llm = numeric_limits::max(); double result = static_cast(im) + static_cast(llm); return BSON("" << result); } }; /** Adding an int and a double produces a double. */ class IntDouble : public TwoOperandBase { BSONObj operand1() { return BSON("" << 9); } BSONObj operand2() { return BSON("" << 1.1); } BSONObj expectedResult() { return BSON("" << 10.1); } }; /** Adding an int and a Date produces a Date. */ class IntDate : public TwoOperandBase { BSONObj operand1() { return BSON("" << 6); } BSONObj operand2() { return BSON("" << Date_t::fromMillisSinceEpoch(123450)); } BSONObj expectedResult() { return BSON("" << Date_t::fromMillisSinceEpoch(123456)); } }; /** Adding a long and a double produces a double. */ class LongDouble : public TwoOperandBase { BSONObj operand1() { return BSON("" << 9LL); } BSONObj operand2() { return BSON("" << 1.1); } BSONObj expectedResult() { return BSON("" << 10.1); } }; /** Adding a long and a double does not overflow. */ class LongDoubleNoOverflow : public TwoOperandBase { BSONObj operand1() { return BSON("" << numeric_limits::max()); } BSONObj operand2() { return BSON("" << double(numeric_limits::max())); } BSONObj expectedResult() { return BSON("" << numeric_limits::max() + double(numeric_limits::max())); } }; /** Adding an int and null. */ class IntNull : public TwoOperandBase { BSONObj operand1() { return BSON("" << 1); } BSONObj operand2() { return BSON("" << BSONNULL); } BSONObj expectedResult() { return BSON("" << BSONNULL); } }; /** Adding a long and undefined. */ class LongUndefined : public TwoOperandBase { BSONObj operand1() { return BSON("" << 5LL); } BSONObj operand2() { return fromjson("{'':undefined}"); } BSONObj expectedResult() { return BSON("" << BSONNULL); } }; } // namespace Add namespace And { class ExpectedResultBase { public: virtual ~ExpectedResultBase() {} void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONObj specObject = BSON("" << spec()); BSONElement specElement = specObject.firstElement(); VariablesParseState vps = expCtx->variablesParseState; intrusive_ptr expression = Expression::parseOperand(expCtx, specElement, vps); ASSERT_BSONOBJ_EQ(constify(spec()), expressionToBson(expression)); ASSERT_BSONOBJ_EQ( BSON("" << expectedResult()), toBson(expression->evaluate(fromBson(BSON("a" << 1)), &expCtx->variables))); intrusive_ptr optimized = expression->optimize(); ASSERT_BSONOBJ_EQ( BSON("" << expectedResult()), toBson(optimized->evaluate(fromBson(BSON("a" << 1)), &expCtx->variables))); } protected: virtual BSONObj spec() = 0; virtual bool expectedResult() = 0; }; class OptimizeBase { public: virtual ~OptimizeBase() {} void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONObj specObject = BSON("" << spec()); BSONElement specElement = specObject.firstElement(); VariablesParseState vps = expCtx->variablesParseState; intrusive_ptr expression = Expression::parseOperand(expCtx, specElement, vps); ASSERT_BSONOBJ_EQ(constify(spec()), expressionToBson(expression)); intrusive_ptr optimized = expression->optimize(); ASSERT_BSONOBJ_EQ(expectedOptimized(), expressionToBson(optimized)); } protected: virtual BSONObj spec() = 0; virtual BSONObj expectedOptimized() = 0; }; class NoOptimizeBase : public OptimizeBase { BSONObj expectedOptimized() { return constify(spec()); } }; /** $and without operands. */ class NoOperands : public ExpectedResultBase { BSONObj spec() { return BSON("$and" << BSONArray()); } bool expectedResult() { return true; } }; /** $and passed 'true'. */ class True : public ExpectedResultBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY(true)); } bool expectedResult() { return true; } }; /** $and passed 'false'. */ class False : public ExpectedResultBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY(false)); } bool expectedResult() { return false; } }; /** $and passed 'true', 'true'. */ class TrueTrue : public ExpectedResultBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY(true << true)); } bool expectedResult() { return true; } }; /** $and passed 'true', 'false'. */ class TrueFalse : public ExpectedResultBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY(true << false)); } bool expectedResult() { return false; } }; /** $and passed 'false', 'true'. */ class FalseTrue : public ExpectedResultBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY(false << true)); } bool expectedResult() { return false; } }; /** $and passed 'false', 'false'. */ class FalseFalse : public ExpectedResultBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY(false << false)); } bool expectedResult() { return false; } }; /** $and passed 'true', 'true', 'true'. */ class TrueTrueTrue : public ExpectedResultBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY(true << true << true)); } bool expectedResult() { return true; } }; /** $and passed 'true', 'true', 'false'. */ class TrueTrueFalse : public ExpectedResultBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY(true << true << false)); } bool expectedResult() { return false; } }; /** $and passed '0', '1'. */ class ZeroOne : public ExpectedResultBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY(0 << 1)); } bool expectedResult() { return false; } }; /** $and passed '1', '2'. */ class OneTwo : public ExpectedResultBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY(1 << 2)); } bool expectedResult() { return true; } }; /** $and passed a field path. */ class FieldPath : public ExpectedResultBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY("$a")); } bool expectedResult() { return true; } }; /** A constant expression is optimized to a constant. */ class OptimizeConstantExpression : public OptimizeBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY(1)); } BSONObj expectedOptimized() { return BSON("$const" << true); } }; /** A non constant expression is not optimized. */ class NonConstant : public NoOptimizeBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY("$a")); } }; /** An expression beginning with a single constant is optimized. */ class ConstantNonConstantTrue : public OptimizeBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY(1 << "$a")); } BSONObj expectedOptimized() { return BSON("$and" << BSON_ARRAY("$a")); } // note: using $and as serialization of ExpressionCoerceToBool rather than // ExpressionAnd }; class ConstantNonConstantFalse : public OptimizeBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY(0 << "$a")); } BSONObj expectedOptimized() { return BSON("$const" << false); } }; /** An expression with a field path and '1'. */ class NonConstantOne : public OptimizeBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY("$a" << 1)); } BSONObj expectedOptimized() { return BSON("$and" << BSON_ARRAY("$a")); } }; /** An expression with a field path and '0'. */ class NonConstantZero : public OptimizeBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY("$a" << 0)); } BSONObj expectedOptimized() { return BSON("$const" << false); } }; /** An expression with two field paths and '1'. */ class NonConstantNonConstantOne : public OptimizeBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY("$a" << "$b" << 1)); } BSONObj expectedOptimized() { return BSON("$and" << BSON_ARRAY("$a" << "$b")); } }; /** An expression with two field paths and '0'. */ class NonConstantNonConstantZero : public OptimizeBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY("$a" << "$b" << 0)); } BSONObj expectedOptimized() { return BSON("$const" << false); } }; /** An expression with '0', '1', and a field path. */ class ZeroOneNonConstant : public OptimizeBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY(0 << 1 << "$a")); } BSONObj expectedOptimized() { return BSON("$const" << false); } }; /** An expression with '1', '1', and a field path. */ class OneOneNonConstant : public OptimizeBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY(1 << 1 << "$a")); } BSONObj expectedOptimized() { return BSON("$and" << BSON_ARRAY("$a")); } }; /** Nested $and expressions. */ class Nested : public OptimizeBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY(1 << BSON("$and" << BSON_ARRAY(1)) << "$a" << "$b")); } BSONObj expectedOptimized() { return BSON("$and" << BSON_ARRAY("$a" << "$b")); } }; /** Nested $and expressions containing a nested value evaluating to false. */ class NestedZero : public OptimizeBase { BSONObj spec() { return BSON("$and" << BSON_ARRAY( 1 << BSON("$and" << BSON_ARRAY(BSON("$and" << BSON_ARRAY(0)))) << "$a" << "$b")); } BSONObj expectedOptimized() { return BSON("$const" << false); } }; } // namespace And namespace CoerceToBool { /** Nested expression coerced to true. */ class EvaluateTrue { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr nested = ExpressionConstant::create(expCtx, Value(5)); intrusive_ptr expression = ExpressionCoerceToBool::create(expCtx, nested); ASSERT(expression->evaluate({}, &expCtx->variables).getBool()); } }; /** Nested expression coerced to false. */ class EvaluateFalse { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr nested = ExpressionConstant::create(expCtx, Value(0)); intrusive_ptr expression = ExpressionCoerceToBool::create(expCtx, nested); ASSERT(!expression->evaluate({}, &expCtx->variables).getBool()); } }; /** Dependencies forwarded from nested expression. */ class Dependencies { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr nested = ExpressionFieldPath::create(expCtx, "a.b"); intrusive_ptr expression = ExpressionCoerceToBool::create(expCtx, nested); DepsTracker dependencies; expression->addDependencies(&dependencies); ASSERT_EQUALS(1U, dependencies.fields.size()); ASSERT_EQUALS(1U, dependencies.fields.count("a.b")); ASSERT_EQUALS(false, dependencies.needWholeDocument); ASSERT_EQUALS(false, dependencies.getNeedsMetadata(DepsTracker::MetadataType::TEXT_SCORE)); } }; /** Output to BSONObj. */ class AddToBsonObj { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionCoerceToBool::create(expCtx, ExpressionFieldPath::create(expCtx, "foo")); // serialized as $and because CoerceToBool isn't an ExpressionNary assertBinaryEqual(fromjson("{field:{$and:['$foo']}}"), toBsonObj(expression)); } private: static BSONObj toBsonObj(const intrusive_ptr& expression) { return BSON("field" << expression->serialize(false)); } }; /** Output to BSONArray. */ class AddToBsonArray { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionCoerceToBool::create(expCtx, ExpressionFieldPath::create(expCtx, "foo")); // serialized as $and because CoerceToBool isn't an ExpressionNary assertBinaryEqual(BSON_ARRAY(fromjson("{$and:['$foo']}")), toBsonArray(expression)); } private: static BSONArray toBsonArray(const intrusive_ptr& expression) { BSONArrayBuilder bab; bab << expression->serialize(false); return bab.arr(); } }; // TODO Test optimize(), difficult because a CoerceToBool cannot be output as // BSON. } // namespace CoerceToBool namespace Compare { class OptimizeBase { public: virtual ~OptimizeBase() {} void run() { BSONObj specObject = BSON("" << spec()); BSONElement specElement = specObject.firstElement(); intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; intrusive_ptr expression = Expression::parseOperand(expCtx, specElement, vps); intrusive_ptr optimized = expression->optimize(); ASSERT_BSONOBJ_EQ(constify(expectedOptimized()), expressionToBson(optimized)); } protected: virtual BSONObj spec() = 0; virtual BSONObj expectedOptimized() = 0; }; class FieldRangeOptimize : public OptimizeBase { BSONObj expectedOptimized() { return spec(); } }; class NoOptimize : public OptimizeBase { BSONObj expectedOptimized() { return spec(); } }; /** Check expected result for expressions depending on constants. */ class ExpectedResultBase : public OptimizeBase { public: void run() { OptimizeBase::run(); BSONObj specObject = BSON("" << spec()); BSONElement specElement = specObject.firstElement(); intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; intrusive_ptr expression = Expression::parseOperand(expCtx, specElement, vps); // Check expression spec round trip. ASSERT_BSONOBJ_EQ(constify(spec()), expressionToBson(expression)); // Check evaluation result. ASSERT_BSONOBJ_EQ(expectedResult(), toBson(expression->evaluate({}, &expCtx->variables))); // Check that the result is the same after optimizing. intrusive_ptr optimized = expression->optimize(); ASSERT_BSONOBJ_EQ(expectedResult(), toBson(optimized->evaluate({}, &expCtx->variables))); } protected: virtual BSONObj spec() = 0; virtual BSONObj expectedResult() = 0; private: virtual BSONObj expectedOptimized() { return BSON("$const" << expectedResult().firstElement()); } }; class ExpectedTrue : public ExpectedResultBase { BSONObj expectedResult() { return BSON("" << true); } }; class ExpectedFalse : public ExpectedResultBase { BSONObj expectedResult() { return BSON("" << false); } }; class ParseError { public: virtual ~ParseError() {} void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONObj specObject = BSON("" << spec()); BSONElement specElement = specObject.firstElement(); VariablesParseState vps = expCtx->variablesParseState; ASSERT_THROWS(Expression::parseOperand(expCtx, specElement, vps), AssertionException); } protected: virtual BSONObj spec() = 0; }; /** $eq with first < second. */ class EqLt : public ExpectedFalse { BSONObj spec() { return BSON("$eq" << BSON_ARRAY(1 << 2)); } }; /** $eq with first == second. */ class EqEq : public ExpectedTrue { BSONObj spec() { return BSON("$eq" << BSON_ARRAY(1 << 1)); } }; /** $eq with first > second. */ class EqGt : public ExpectedFalse { BSONObj spec() { return BSON("$eq" << BSON_ARRAY(1 << 0)); } }; /** $ne with first < second. */ class NeLt : public ExpectedTrue { BSONObj spec() { return BSON("$ne" << BSON_ARRAY(1 << 2)); } }; /** $ne with first == second. */ class NeEq : public ExpectedFalse { BSONObj spec() { return BSON("$ne" << BSON_ARRAY(1 << 1)); } }; /** $ne with first > second. */ class NeGt : public ExpectedTrue { BSONObj spec() { return BSON("$ne" << BSON_ARRAY(1 << 0)); } }; /** $gt with first < second. */ class GtLt : public ExpectedFalse { BSONObj spec() { return BSON("$gt" << BSON_ARRAY(1 << 2)); } }; /** $gt with first == second. */ class GtEq : public ExpectedFalse { BSONObj spec() { return BSON("$gt" << BSON_ARRAY(1 << 1)); } }; /** $gt with first > second. */ class GtGt : public ExpectedTrue { BSONObj spec() { return BSON("$gt" << BSON_ARRAY(1 << 0)); } }; /** $gte with first < second. */ class GteLt : public ExpectedFalse { BSONObj spec() { return BSON("$gte" << BSON_ARRAY(1 << 2)); } }; /** $gte with first == second. */ class GteEq : public ExpectedTrue { BSONObj spec() { return BSON("$gte" << BSON_ARRAY(1 << 1)); } }; /** $gte with first > second. */ class GteGt : public ExpectedTrue { BSONObj spec() { return BSON("$gte" << BSON_ARRAY(1 << 0)); } }; /** $lt with first < second. */ class LtLt : public ExpectedTrue { BSONObj spec() { return BSON("$lt" << BSON_ARRAY(1 << 2)); } }; /** $lt with first == second. */ class LtEq : public ExpectedFalse { BSONObj spec() { return BSON("$lt" << BSON_ARRAY(1 << 1)); } }; /** $lt with first > second. */ class LtGt : public ExpectedFalse { BSONObj spec() { return BSON("$lt" << BSON_ARRAY(1 << 0)); } }; /** $lte with first < second. */ class LteLt : public ExpectedTrue { BSONObj spec() { return BSON("$lte" << BSON_ARRAY(1 << 2)); } }; /** $lte with first == second. */ class LteEq : public ExpectedTrue { BSONObj spec() { return BSON("$lte" << BSON_ARRAY(1 << 1)); } }; /** $lte with first > second. */ class LteGt : public ExpectedFalse { BSONObj spec() { return BSON("$lte" << BSON_ARRAY(1 << 0)); } }; /** $cmp with first < second. */ class CmpLt : public ExpectedResultBase { BSONObj spec() { return BSON("$cmp" << BSON_ARRAY(1 << 2)); } BSONObj expectedResult() { return BSON("" << -1); } }; /** $cmp with first == second. */ class CmpEq : public ExpectedResultBase { BSONObj spec() { return BSON("$cmp" << BSON_ARRAY(1 << 1)); } BSONObj expectedResult() { return BSON("" << 0); } }; /** $cmp with first > second. */ class CmpGt : public ExpectedResultBase { BSONObj spec() { return BSON("$cmp" << BSON_ARRAY(1 << 0)); } BSONObj expectedResult() { return BSON("" << 1); } }; /** $cmp results are bracketed to an absolute value of 1. */ class CmpBracketed : public ExpectedResultBase { BSONObj spec() { return BSON("$cmp" << BSON_ARRAY("z" << "a")); } BSONObj expectedResult() { return BSON("" << 1); } }; /** Zero operands provided. */ class ZeroOperands : public ParseError { BSONObj spec() { return BSON("$ne" << BSONArray()); } }; /** One operand provided. */ class OneOperand : public ParseError { BSONObj spec() { return BSON("$eq" << BSON_ARRAY(1)); } }; /** Three operands provided. */ class ThreeOperands : public ParseError { BSONObj spec() { return BSON("$gt" << BSON_ARRAY(2 << 3 << 4)); } }; /** Incompatible types can be compared. */ class IncompatibleTypes { public: void run() { BSONObj specObject = BSON("" << BSON("$ne" << BSON_ARRAY("a" << 1))); BSONElement specElement = specObject.firstElement(); intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; intrusive_ptr expression = Expression::parseOperand(expCtx, specElement, vps); ASSERT_VALUE_EQ(expression->evaluate({}, &expCtx->variables), Value(true)); } }; /** * An expression depending on constants is optimized to a constant via * ExpressionNary::optimize(). */ class OptimizeConstants : public OptimizeBase { BSONObj spec() { return BSON("$eq" << BSON_ARRAY(1 << 1)); } BSONObj expectedOptimized() { return BSON("$const" << true); } }; /** $cmp is not optimized. */ class NoOptimizeCmp : public NoOptimize { BSONObj spec() { return BSON("$cmp" << BSON_ARRAY(1 << "$a")); } }; /** $ne is not optimized. */ class NoOptimizeNe : public NoOptimize { BSONObj spec() { return BSON("$ne" << BSON_ARRAY(1 << "$a")); } }; /** No optimization is performend without a constant. */ class NoOptimizeNoConstant : public NoOptimize { BSONObj spec() { return BSON("$ne" << BSON_ARRAY("$a" << "$b")); } }; /** No optimization is performend without an immediate field path. */ class NoOptimizeWithoutFieldPath : public NoOptimize { BSONObj spec() { return BSON("$eq" << BSON_ARRAY(BSON("$and" << BSON_ARRAY("$a")) << 1)); } }; /** No optimization is performend without an immediate field path. */ class NoOptimizeWithoutFieldPathReverse : public NoOptimize { BSONObj spec() { return BSON("$eq" << BSON_ARRAY(1 << BSON("$and" << BSON_ARRAY("$a")))); } }; /** An equality expression is optimized. */ class OptimizeEq : public FieldRangeOptimize { BSONObj spec() { return BSON("$eq" << BSON_ARRAY("$a" << 1)); } }; /** A reverse sense equality expression is optimized. */ class OptimizeEqReverse : public FieldRangeOptimize { BSONObj spec() { return BSON("$eq" << BSON_ARRAY(1 << "$a")); } }; /** A $lt expression is optimized. */ class OptimizeLt : public FieldRangeOptimize { BSONObj spec() { return BSON("$lt" << BSON_ARRAY("$a" << 1)); } }; /** A reverse sense $lt expression is optimized. */ class OptimizeLtReverse : public FieldRangeOptimize { BSONObj spec() { return BSON("$lt" << BSON_ARRAY(1 << "$a")); } }; /** A $lte expression is optimized. */ class OptimizeLte : public FieldRangeOptimize { BSONObj spec() { return BSON("$lte" << BSON_ARRAY("$b" << 2)); } }; /** A reverse sense $lte expression is optimized. */ class OptimizeLteReverse : public FieldRangeOptimize { BSONObj spec() { return BSON("$lte" << BSON_ARRAY(2 << "$b")); } }; /** A $gt expression is optimized. */ class OptimizeGt : public FieldRangeOptimize { BSONObj spec() { return BSON("$gt" << BSON_ARRAY("$b" << 2)); } }; /** A reverse sense $gt expression is optimized. */ class OptimizeGtReverse : public FieldRangeOptimize { BSONObj spec() { return BSON("$gt" << BSON_ARRAY(2 << "$b")); } }; /** A $gte expression is optimized. */ class OptimizeGte : public FieldRangeOptimize { BSONObj spec() { return BSON("$gte" << BSON_ARRAY("$b" << 2)); } }; /** A reverse sense $gte expression is optimized. */ class OptimizeGteReverse : public FieldRangeOptimize { BSONObj spec() { return BSON("$gte" << BSON_ARRAY(2 << "$b")); } }; } // namespace Compare namespace Constant { /** Create an ExpressionConstant from a Value. */ class Create { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionConstant::create(expCtx, Value(5)); assertBinaryEqual(BSON("" << 5), toBson(expression->evaluate({}, &expCtx->variables))); } }; /** Create an ExpressionConstant from a BsonElement. */ class CreateFromBsonElement { public: void run() { BSONObj spec = BSON("IGNORED_FIELD_NAME" << "foo"); intrusive_ptr expCtx(new ExpressionContextForTest()); BSONElement specElement = spec.firstElement(); VariablesParseState vps = expCtx->variablesParseState; intrusive_ptr expression = ExpressionConstant::parse(expCtx, specElement, vps); assertBinaryEqual(BSON("" << "foo"), toBson(expression->evaluate({}, &expCtx->variables))); } }; /** No optimization is performed. */ class Optimize { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionConstant::create(expCtx, Value(5)); // An attempt to optimize returns the Expression itself. ASSERT_EQUALS(expression, expression->optimize()); } }; /** No dependencies. */ class Dependencies { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionConstant::create(expCtx, Value(5)); DepsTracker dependencies; expression->addDependencies(&dependencies); ASSERT_EQUALS(0U, dependencies.fields.size()); ASSERT_EQUALS(false, dependencies.needWholeDocument); ASSERT_EQUALS(false, dependencies.getNeedsMetadata(DepsTracker::MetadataType::TEXT_SCORE)); } }; /** Output to BSONObj. */ class AddToBsonObj { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionConstant::create(expCtx, Value(5)); // The constant is replaced with a $ expression. assertBinaryEqual(BSON("field" << BSON("$const" << 5)), toBsonObj(expression)); } private: static BSONObj toBsonObj(const intrusive_ptr& expression) { return BSON("field" << expression->serialize(false)); } }; /** Output to BSONArray. */ class AddToBsonArray { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionConstant::create(expCtx, Value(5)); // The constant is copied out as is. assertBinaryEqual(constify(BSON_ARRAY(5)), toBsonArray(expression)); } private: static BSONObj toBsonArray(const intrusive_ptr& expression) { BSONArrayBuilder bab; bab << expression->serialize(false); return bab.obj(); } }; TEST(ExpressionConstantTest, ConstantOfValueMissingRemovesField) { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionConstant::create(expCtx, Value()); assertBinaryEqual( BSONObj(), toBson(expression->evaluate(Document{{"foo", Value("bar"_sd)}}, &expCtx->variables))); } TEST(ExpressionConstantTest, ConstantOfValueMissingSerializesToRemoveSystemVar) { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionConstant::create(expCtx, Value()); assertBinaryEqual(BSON("field" << "$$REMOVE"), BSON("field" << expression->serialize(false))); } } // namespace Constant TEST(ExpressionFromAccumulators, Avg) { assertExpectedResults("$avg", {// $avg ignores non-numeric inputs. {{Value("string"_sd), Value(BSONNULL), Value(), Value(3)}, Value(3.0)}, // $avg always returns a double. {{Value(10LL), Value(20LL)}, Value(15.0)}, // $avg returns null when no arguments are provided. {{}, Value(BSONNULL)}}); } TEST(ExpressionFromAccumulators, Max) { assertExpectedResults("$max", {// $max treats non-numeric inputs as valid arguments. {{Value(1), Value(BSONNULL), Value(), Value("a"_sd)}, Value("a"_sd)}, {{Value("a"_sd), Value("b"_sd)}, Value("b"_sd)}, // $max always preserves the type of the result. {{Value(10LL), Value(0.0), Value(5)}, Value(10LL)}, // $max returns null when no arguments are provided. {{}, Value(BSONNULL)}}); } TEST(ExpressionFromAccumulators, Min) { assertExpectedResults("$min", {// $min treats non-numeric inputs as valid arguments. {{Value("string"_sd)}, Value("string"_sd)}, {{Value(1), Value(BSONNULL), Value(), Value("a"_sd)}, Value(1)}, {{Value("a"_sd), Value("b"_sd)}, Value("a"_sd)}, // $min always preserves the type of the result. {{Value(0LL), Value(20.0), Value(10)}, Value(0LL)}, // $min returns null when no arguments are provided. {{}, Value(BSONNULL)}}); } TEST(ExpressionFromAccumulators, Sum) { assertExpectedResults( "$sum", {// $sum ignores non-numeric inputs. {{Value("string"_sd), Value(BSONNULL), Value(), Value(3)}, Value(3)}, // If any argument is a double, $sum returns a double {{Value(10LL), Value(10.0)}, Value(20.0)}, // If no arguments are doubles and an argument is a long, $sum returns a long {{Value(10LL), Value(10)}, Value(20LL)}, // $sum returns 0 when no arguments are provided. {{}, Value(0)}}); } TEST(ExpressionFromAccumulators, StdDevPop) { assertExpectedResults("$stdDevPop", {// $stdDevPop ignores non-numeric inputs. {{Value("string"_sd), Value(BSONNULL), Value(), Value(3)}, Value(0.0)}, // $stdDevPop always returns a double. {{Value(1LL), Value(3LL)}, Value(1.0)}, // $stdDevPop returns null when no arguments are provided. {{}, Value(BSONNULL)}}); } TEST(ExpressionFromAccumulators, StdDevSamp) { assertExpectedResults( "$stdDevSamp", {// $stdDevSamp ignores non-numeric inputs. {{Value("string"_sd), Value(BSONNULL), Value(), Value(3)}, Value(BSONNULL)}, // $stdDevSamp always returns a double. {{Value(1LL), Value(2LL), Value(3LL)}, Value(1.0)}, // $stdDevSamp returns null when no arguments are provided. {{}, Value(BSONNULL)}}); } TEST(ExpressionPowTest, LargeExponentValuesWithBaseOfZero) { assertExpectedResults( "$pow", { {{Value(0), Value(0)}, Value(1)}, {{Value(0LL), Value(0LL)}, Value(1LL)}, {{Value(0), Value(10)}, Value(0)}, {{Value(0), Value(10000)}, Value(0)}, {{Value(0LL), Value(10)}, Value(0LL)}, // $pow may sometimes use a loop to compute a^b, so it's important to check // that the loop doesn't hang if a large exponent is provided. {{Value(0LL), Value(std::numeric_limits::max())}, Value(0LL)}, }); } TEST(ExpressionPowTest, ThrowsWhenBaseZeroAndExpNegative) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; const auto expr = Expression::parseExpression(expCtx, BSON("$pow" << BSON_ARRAY(0 << -5)), vps); ASSERT_THROWS([&] { expr->evaluate({}, &expCtx->variables); }(), AssertionException); const auto exprWithLong = Expression::parseExpression(expCtx, BSON("$pow" << BSON_ARRAY(0LL << -5LL)), vps); ASSERT_THROWS([&] { expr->evaluate({}, &expCtx->variables); }(), AssertionException); } TEST(ExpressionPowTest, LargeExponentValuesWithBaseOfOne) { assertExpectedResults( "$pow", { {{Value(1), Value(10)}, Value(1)}, {{Value(1), Value(10LL)}, Value(1LL)}, {{Value(1), Value(10000LL)}, Value(1LL)}, {{Value(1LL), Value(10LL)}, Value(1LL)}, // $pow may sometimes use a loop to compute a^b, so it's important to check // that the loop doesn't hang if a large exponent is provided. {{Value(1LL), Value(std::numeric_limits::max())}, Value(1LL)}, {{Value(1LL), Value(std::numeric_limits::min())}, Value(1LL)}, }); } TEST(ExpressionPowTest, LargeExponentValuesWithBaseOfNegativeOne) { assertExpectedResults("$pow", { {{Value(-1), Value(-1)}, Value(-1)}, {{Value(-1), Value(-2)}, Value(1)}, {{Value(-1), Value(-3)}, Value(-1)}, {{Value(-1LL), Value(0LL)}, Value(1LL)}, {{Value(-1LL), Value(-1LL)}, Value(-1LL)}, {{Value(-1LL), Value(-2LL)}, Value(1LL)}, {{Value(-1LL), Value(-3LL)}, Value(-1LL)}, {{Value(-1LL), Value(-4LL)}, Value(1LL)}, {{Value(-1LL), Value(-5LL)}, Value(-1LL)}, {{Value(-1LL), Value(-61LL)}, Value(-1LL)}, {{Value(-1LL), Value(61LL)}, Value(-1LL)}, {{Value(-1LL), Value(-62LL)}, Value(1LL)}, {{Value(-1LL), Value(62LL)}, Value(1LL)}, {{Value(-1LL), Value(-101LL)}, Value(-1LL)}, {{Value(-1LL), Value(-102LL)}, Value(1LL)}, // Use a value large enough that will make the test hang for a // considerable amount of time if a loop is used to compute the // answer. {{Value(-1LL), Value(63234673905128LL)}, Value(1LL)}, {{Value(-1LL), Value(-63234673905128LL)}, Value(1LL)}, {{Value(-1LL), Value(63234673905127LL)}, Value(-1LL)}, {{Value(-1LL), Value(-63234673905127LL)}, Value(-1LL)}, }); } TEST(ExpressionPowTest, LargeBaseSmallPositiveExponent) { assertExpectedResults("$pow", { {{Value(4294967296LL), Value(1LL)}, Value(4294967296LL)}, {{Value(4294967296LL), Value(0)}, Value(1LL)}, }); } TEST(ExpressionArray, ExpressionArrayWithAllConstantValuesShouldOptimizeToExpressionConstant) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; // ExpressionArray of constant values should optimize to ExpressionConsant. BSONObj bsonarrayOfConstants = BSON("" << BSON_ARRAY(1 << 2 << 3 << 4)); BSONElement elementArray = bsonarrayOfConstants.firstElement(); auto expressionArr = ExpressionArray::parse(expCtx, elementArray, vps); auto optimizedToConstant = expressionArr->optimize(); auto exprConstant = dynamic_cast(optimizedToConstant.get()); ASSERT_TRUE(exprConstant); // ExpressionArray with not all constant values should not optimize to ExpressionConstant. BSONObj bsonarray = BSON("" << BSON_ARRAY(1 << "$x" << 3 << 4)); BSONElement elementArrayNotConstant = bsonarray.firstElement(); auto expressionArrNotConstant = ExpressionArray::parse(expCtx, elementArrayNotConstant, vps); auto notOptimized = expressionArrNotConstant->optimize(); auto notExprConstant = dynamic_cast(notOptimized.get()); ASSERT_FALSE(notExprConstant); } TEST(ExpressionArray, ExpressionArrayShouldOptimizeSubExpressionToExpressionConstant) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; // ExpressionArray with constant values and sub expression that evaluates to constant should // optimize to Expression constant. BSONObj bsonarrayWithSubExpression = BSON("" << BSON_ARRAY(1 << BSON("$add" << BSON_ARRAY(1 << 1)) << 3 << 4)); BSONElement elementArrayWithSubExpression = bsonarrayWithSubExpression.firstElement(); auto expressionArrWithSubExpression = ExpressionArray::parse(expCtx, elementArrayWithSubExpression, vps); auto optimizedToConstantWithSubExpression = expressionArrWithSubExpression->optimize(); auto constantExpression = dynamic_cast(optimizedToConstantWithSubExpression.get()); ASSERT_TRUE(constantExpression); } TEST(ExpressionIndexOfArray, ExpressionIndexOfArrayShouldOptimizeArguments) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto expIndexOfArray = Expression::parseExpression( expCtx, // 2, 1, 1 BSON("$indexOfArray" << BSON_ARRAY( BSON_ARRAY(BSON("$add" << BSON_ARRAY(1 << 1)) << 1 << 1 << 2) // Value we are searching for = 2. << BSON("$add" << BSON_ARRAY(1 << 1)) // Start index = 1. << BSON("$add" << BSON_ARRAY(0 << 1)) // End index = 4. << BSON("$add" << BSON_ARRAY(1 << 3)))), expCtx->variablesParseState); auto argsOptimizedToConstants = expIndexOfArray->optimize(); auto shouldBeIndexOfArray = dynamic_cast(argsOptimizedToConstants.get()); ASSERT_TRUE(shouldBeIndexOfArray); ASSERT_VALUE_EQ(Value(3), shouldBeIndexOfArray->getValue()); } TEST(ExpressionIndexOfArray, ExpressionIndexOfArrayShouldOptimizeNullishInputArrayToExpressionConstant) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; auto expIndex = Expression::parseExpression( expCtx, fromjson("{ $indexOfArray : [ undefined , 1, 1]}"), expCtx->variablesParseState); auto isExpIndexOfArray = dynamic_cast(expIndex.get()); ASSERT_TRUE(isExpIndexOfArray); auto nullishValueOptimizedToExpConstant = isExpIndexOfArray->optimize(); auto shouldBeExpressionConstant = dynamic_cast(nullishValueOptimizedToExpConstant.get()); ASSERT_TRUE(shouldBeExpressionConstant); // Nullish input array should become a Value(BSONNULL). ASSERT_VALUE_EQ(Value(BSONNULL), shouldBeExpressionConstant->getValue()); } TEST(ExpressionIndexOfArray, OptimizedExpressionIndexOfArrayWithConstantArgumentsShouldEvaluateProperly) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto expIndexOfArray = Expression::parseExpression( expCtx, // Search for $x. fromjson("{ $indexOfArray : [ [0, 1, 2, 3, 4, 5, 'val'] , '$x'] }"), expCtx->variablesParseState); auto optimizedIndexOfArray = expIndexOfArray->optimize(); ASSERT_VALUE_EQ(Value(0), optimizedIndexOfArray->evaluate(Document{{"x", 0}}, &expCtx->variables)); ASSERT_VALUE_EQ(Value(1), optimizedIndexOfArray->evaluate(Document{{"x", 1}}, &expCtx->variables)); ASSERT_VALUE_EQ(Value(2), optimizedIndexOfArray->evaluate(Document{{"x", 2}}, &expCtx->variables)); ASSERT_VALUE_EQ(Value(3), optimizedIndexOfArray->evaluate(Document{{"x", 3}}, &expCtx->variables)); ASSERT_VALUE_EQ(Value(4), optimizedIndexOfArray->evaluate(Document{{"x", 4}}, &expCtx->variables)); ASSERT_VALUE_EQ(Value(5), optimizedIndexOfArray->evaluate(Document{{"x", 5}}, &expCtx->variables)); ASSERT_VALUE_EQ( Value(6), optimizedIndexOfArray->evaluate(Document{{"x", string("val")}}, &expCtx->variables)); auto optimizedIndexNotFound = optimizedIndexOfArray->optimize(); // Should evaluate to -1 if not found. ASSERT_VALUE_EQ(Value(-1), optimizedIndexNotFound->evaluate(Document{{"x", 10}}, &expCtx->variables)); ASSERT_VALUE_EQ(Value(-1), optimizedIndexNotFound->evaluate(Document{{"x", 100}}, &expCtx->variables)); ASSERT_VALUE_EQ(Value(-1), optimizedIndexNotFound->evaluate(Document{{"x", 1000}}, &expCtx->variables)); ASSERT_VALUE_EQ( Value(-1), optimizedIndexNotFound->evaluate(Document{{"x", string("string")}}, &expCtx->variables)); ASSERT_VALUE_EQ(Value(-1), optimizedIndexNotFound->evaluate(Document{{"x", -1}}, &expCtx->variables)); } TEST(ExpressionIndexOfArray, OptimizedExpressionIndexOfArrayWithConstantArgumentsShouldEvaluateProperlyWithRange) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto expIndexOfArray = Expression::parseExpression( expCtx, // Search for 4 between 3 and 5. fromjson("{ $indexOfArray : [ [0, 1, 2, 3, 4, 5] , '$x', 3, 5] }"), expCtx->variablesParseState); auto optimizedIndexOfArray = expIndexOfArray->optimize(); ASSERT_VALUE_EQ(Value(4), optimizedIndexOfArray->evaluate(Document{{"x", 4}}, &expCtx->variables)); // Should evaluate to -1 if not found in range. ASSERT_VALUE_EQ(Value(-1), optimizedIndexOfArray->evaluate(Document{{"x", 0}}, &expCtx->variables)); } TEST(ExpressionIndexOfArray, OptimizedExpressionIndexOfArrayWithConstantArrayShouldEvaluateProperlyWithDuplicateValues) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto expIndexOfArrayWithDuplicateValues = Expression::parseExpression(expCtx, // Search for 4 between 3 and 5. fromjson("{ $indexOfArray : [ [0, 1, 2, 2, 3, 4, 5] , '$x'] }"), expCtx->variablesParseState); auto optimizedIndexOfArrayWithDuplicateValues = expIndexOfArrayWithDuplicateValues->optimize(); ASSERT_VALUE_EQ( Value(2), optimizedIndexOfArrayWithDuplicateValues->evaluate(Document{{"x", 2}}, &expCtx->variables)); // Duplicate Values in a range. auto expIndexInRangeWithhDuplicateValues = Expression::parseExpression( expCtx, // Search for 2 between 4 and 6. fromjson("{ $indexOfArray : [ [0, 1, 2, 2, 2, 2, 4, 5] , '$x', 4, 6] }"), expCtx->variablesParseState); auto optimizedIndexInRangeWithDuplcateValues = expIndexInRangeWithhDuplicateValues->optimize(); // Should evaluate to 4. ASSERT_VALUE_EQ( Value(4), optimizedIndexInRangeWithDuplcateValues->evaluate(Document{{"x", 2}}, &expCtx->variables)); } namespace FieldPath { /** The provided field path does not pass validation. */ class Invalid { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); ASSERT_THROWS(ExpressionFieldPath::create(expCtx, ""), AssertionException); } }; TEST(FieldPath, NoOptimizationForRootFieldPathWithDottedPath) { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::parse(expCtx, "$$ROOT.x.y", expCtx->variablesParseState); // An attempt to optimize returns the Expression itself. ASSERT_EQUALS(expression, expression->optimize()); } TEST(FieldPath, NoOptimizationForCurrentFieldPathWithDottedPath) { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::parse(expCtx, "$$CURRENT.x.y", expCtx->variablesParseState); // An attempt to optimize returns the Expression itself. ASSERT_EQUALS(expression, expression->optimize()); } TEST(FieldPath, RemoveOptimizesToMissingValue) { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::parse(expCtx, "$$REMOVE", expCtx->variablesParseState); auto optimizedExpr = expression->optimize(); ASSERT_VALUE_EQ( Value(), optimizedExpr->evaluate(Document(BSON("x" << BSON("y" << 123))), &expCtx->variables)); } TEST(FieldPath, NoOptimizationOnNormalPath) { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a"); // An attempt to optimize returns the Expression itself. ASSERT_EQUALS(expression, expression->optimize()); } TEST(FieldPath, OptimizeOnVariableWithConstantScalarValue) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto varId = expCtx->variablesParseState.defineVariable("userVar"); expCtx->variables.setConstantValue(varId, Value(123)); auto expr = ExpressionFieldPath::parse(expCtx, "$$userVar", expCtx->variablesParseState); ASSERT_TRUE(dynamic_cast(expr.get())); auto optimizedExpr = expr->optimize(); ASSERT_TRUE(dynamic_cast(optimizedExpr.get())); } TEST(FieldPath, OptimizeOnVariableWithConstantArrayValue) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto varId = expCtx->variablesParseState.defineVariable("userVar"); expCtx->variables.setConstantValue(varId, Value(BSON_ARRAY(1 << 2 << 3))); auto expr = ExpressionFieldPath::parse(expCtx, "$$userVar", expCtx->variablesParseState); ASSERT_TRUE(dynamic_cast(expr.get())); auto optimizedExpr = expr->optimize(); auto constantExpr = dynamic_cast(optimizedExpr.get()); ASSERT_TRUE(constantExpr); ASSERT_VALUE_EQ(Value(BSON_ARRAY(1 << 2 << 3)), constantExpr->getValue()); } TEST(FieldPath, OptimizeToEmptyArrayOnNumericalPathComponentAndConstantArrayValue) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto varId = expCtx->variablesParseState.defineVariable("userVar"); expCtx->variables.setConstantValue(varId, Value(BSON_ARRAY(1 << 2 << 3))); auto expr = ExpressionFieldPath::parse(expCtx, "$$userVar.1", expCtx->variablesParseState); ASSERT_TRUE(dynamic_cast(expr.get())); auto optimizedExpr = expr->optimize(); auto constantExpr = dynamic_cast(optimizedExpr.get()); ASSERT_TRUE(constantExpr); ASSERT_VALUE_EQ(Value(BSONArray()), constantExpr->getValue()); } TEST(FieldPath, OptimizeOnVariableWithConstantValueAndDottedPath) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto varId = expCtx->variablesParseState.defineVariable("userVar"); expCtx->variables.setConstantValue(varId, Value(Document{{"x", Document{{"y", 123}}}})); auto expr = ExpressionFieldPath::parse(expCtx, "$$userVar.x.y", expCtx->variablesParseState); ASSERT_TRUE(dynamic_cast(expr.get())); auto optimizedExpr = expr->optimize(); auto constantExpr = dynamic_cast(optimizedExpr.get()); ASSERT_TRUE(constantExpr); ASSERT_VALUE_EQ(Value(123), constantExpr->getValue()); } TEST(FieldPath, NoOptimizationOnVariableWithNoValue) { intrusive_ptr expCtx(new ExpressionContextForTest()); expCtx->variablesParseState.defineVariable("userVar"); auto expr = ExpressionFieldPath::parse(expCtx, "$$userVar", expCtx->variablesParseState); ASSERT_TRUE(dynamic_cast(expr.get())); auto optimizedExpr = expr->optimize(); ASSERT_FALSE(dynamic_cast(optimizedExpr.get())); } TEST(FieldPath, NoOptimizationOnVariableWithMissingValue) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto varId = expCtx->variablesParseState.defineVariable("userVar"); expCtx->variables.setValue(varId, Value()); auto expr = ExpressionFieldPath::parse(expCtx, "$$userVar", expCtx->variablesParseState); ASSERT_TRUE(dynamic_cast(expr.get())); auto optimizedExpr = expr->optimize(); ASSERT_FALSE(dynamic_cast(optimizedExpr.get())); } TEST(FieldPath, ScalarVariableWithDottedFieldPathOptimizesToConstantMissingValue) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto varId = expCtx->variablesParseState.defineVariable("userVar"); expCtx->variables.setConstantValue(varId, Value(123)); auto expr = ExpressionFieldPath::parse(expCtx, "$$userVar.x.y", expCtx->variablesParseState); ASSERT_TRUE(dynamic_cast(expr.get())); auto optimizedExpr = expr->optimize(); auto constantExpr = dynamic_cast(optimizedExpr.get()); ASSERT_TRUE(constantExpr); ASSERT_VALUE_EQ(Value(), constantExpr->getValue()); } /** The field path itself is a dependency. */ class Dependencies { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a.b"); DepsTracker dependencies; expression->addDependencies(&dependencies); ASSERT_EQUALS(1U, dependencies.fields.size()); ASSERT_EQUALS(1U, dependencies.fields.count("a.b")); ASSERT_EQUALS(false, dependencies.needWholeDocument); ASSERT_EQUALS(false, dependencies.getNeedsMetadata(DepsTracker::MetadataType::TEXT_SCORE)); } }; /** Field path target field is missing. */ class Missing { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a"); assertBinaryEqual(fromjson("{}"), toBson(expression->evaluate({}, &expCtx->variables))); } }; /** Simple case where the target field is present. */ class Present { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a"); assertBinaryEqual( fromjson("{'':123}"), toBson(expression->evaluate(fromBson(BSON("a" << 123)), &expCtx->variables))); } }; /** Target field parent is null. */ class NestedBelowNull { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a.b"); assertBinaryEqual( fromjson("{}"), toBson(expression->evaluate(fromBson(fromjson("{a:null}")), &expCtx->variables))); } }; /** Target field parent is undefined. */ class NestedBelowUndefined { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a.b"); assertBinaryEqual( fromjson("{}"), toBson(expression->evaluate(fromBson(fromjson("{a:undefined}")), &expCtx->variables))); } }; /** Target field parent is missing. */ class NestedBelowMissing { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a.b"); assertBinaryEqual( fromjson("{}"), toBson(expression->evaluate(fromBson(fromjson("{z:1}")), &expCtx->variables))); } }; /** Target field parent is an integer. */ class NestedBelowInt { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a.b"); assertBinaryEqual( fromjson("{}"), toBson(expression->evaluate(fromBson(BSON("a" << 2)), &expCtx->variables))); } }; /** A value in a nested object. */ class NestedValue { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a.b"); assertBinaryEqual(BSON("" << 55), toBson(expression->evaluate(fromBson(BSON("a" << BSON("b" << 55))), &expCtx->variables))); } }; /** Target field within an empty object. */ class NestedBelowEmptyObject { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a.b"); assertBinaryEqual( fromjson("{}"), toBson(expression->evaluate(fromBson(BSON("a" << BSONObj())), &expCtx->variables))); } }; /** Target field within an empty array. */ class NestedBelowEmptyArray { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a.b"); assertBinaryEqual( BSON("" << BSONArray()), toBson(expression->evaluate(fromBson(BSON("a" << BSONArray())), &expCtx->variables))); } }; /** Target field within an array containing null. */ class NestedBelowArrayWithNull { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a.b"); assertBinaryEqual( fromjson("{'':[]}"), toBson(expression->evaluate(fromBson(fromjson("{a:[null]}")), &expCtx->variables))); } }; /** Target field within an array containing undefined. */ class NestedBelowArrayWithUndefined { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a.b"); assertBinaryEqual(fromjson("{'':[]}"), toBson(expression->evaluate(fromBson(fromjson("{a:[undefined]}")), &expCtx->variables))); } }; /** Target field within an array containing an integer. */ class NestedBelowArrayWithInt { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a.b"); assertBinaryEqual( fromjson("{'':[]}"), toBson(expression->evaluate(fromBson(fromjson("{a:[1]}")), &expCtx->variables))); } }; /** Target field within an array. */ class NestedWithinArray { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a.b"); assertBinaryEqual( fromjson("{'':[9]}"), toBson(expression->evaluate(fromBson(fromjson("{a:[{b:9}]}")), &expCtx->variables))); } }; /** Multiple value types within an array. */ class MultipleArrayValues { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a.b"); assertBinaryEqual(fromjson("{'':[9,20]}"), toBson(expression->evaluate( fromBson(fromjson("{a:[{b:9},null,undefined,{g:4},{b:20},{}]}")), &expCtx->variables))); } }; /** Expanding values within nested arrays. */ class ExpandNestedArrays { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a.b.c"); assertBinaryEqual(fromjson("{'':[[1,2],3,[4],[[5]],[6,7]]}"), toBson(expression->evaluate(fromBson(fromjson("{a:[{b:[{c:1},{c:2}]}," "{b:{c:3}}," "{b:[{c:4}]}," "{b:[{c:[5]}]}," "{b:{c:[6,7]}}]}")), &expCtx->variables))); } }; /** Add to a BSONObj. */ class AddToBsonObj { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a.b.c"); assertBinaryEqual(BSON("foo" << "$a.b.c"), BSON("foo" << expression->serialize(false))); } }; /** Add to a BSONArray. */ class AddToBsonArray { public: void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); intrusive_ptr expression = ExpressionFieldPath::create(expCtx, "a.b.c"); BSONArrayBuilder bab; bab << expression->serialize(false); assertBinaryEqual(BSON_ARRAY("$a.b.c"), bab.arr()); } }; } // namespace FieldPath namespace Object { using mongo::ExpressionObject; template Document literal(T&& value) { return Document{{"$const", Value(std::forward(value))}}; } // // Parsing. // TEST(ExpressionObjectParse, ShouldAcceptEmptyObject) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; auto object = ExpressionObject::parse(expCtx, BSONObj(), vps); ASSERT_VALUE_EQ(Value(Document{}), object->serialize(false)); } TEST(ExpressionObjectParse, ShouldAcceptLiteralsAsValues) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; auto object = ExpressionObject::parse(expCtx, BSON("a" << 5 << "b" << "string" << "c" << BSONNULL), vps); auto expectedResult = Value(Document{{"a", literal(5)}, {"b", literal("string"_sd)}, {"c", literal(BSONNULL)}}); ASSERT_VALUE_EQ(expectedResult, object->serialize(false)); } TEST(ExpressionObjectParse, ShouldAccept_idAsFieldName) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; auto object = ExpressionObject::parse(expCtx, BSON("_id" << 5), vps); auto expectedResult = Value(Document{{"_id", literal(5)}}); ASSERT_VALUE_EQ(expectedResult, object->serialize(false)); } TEST(ExpressionObjectParse, ShouldAcceptFieldNameContainingDollar) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; auto object = ExpressionObject::parse(expCtx, BSON("a$b" << 5), vps); auto expectedResult = Value(Document{{"a$b", literal(5)}}); ASSERT_VALUE_EQ(expectedResult, object->serialize(false)); } TEST(ExpressionObjectParse, ShouldAcceptNestedObjects) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; auto object = ExpressionObject::parse(expCtx, fromjson("{a: {b: 1}, c: {d: {e: 1, f: 1}}}"), vps); auto expectedResult = Value(Document{{"a", Document{{"b", literal(1)}}}, {"c", Document{{"d", Document{{"e", literal(1)}, {"f", literal(1)}}}}}}); ASSERT_VALUE_EQ(expectedResult, object->serialize(false)); } TEST(ExpressionObjectParse, ShouldAcceptArrays) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; auto object = ExpressionObject::parse(expCtx, fromjson("{a: [1, 2]}"), vps); auto expectedResult = Value(Document{{"a", vector{Value(literal(1)), Value(literal(2))}}}); ASSERT_VALUE_EQ(expectedResult, object->serialize(false)); } TEST(ObjectParsing, ShouldAcceptExpressionAsValue) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; auto object = ExpressionObject::parse(expCtx, BSON("a" << BSON("$and" << BSONArray())), vps); ASSERT_VALUE_EQ(object->serialize(false), Value(Document{{"a", Document{{"$and", BSONArray()}}}})); } // // Error cases. // TEST(ExpressionObjectParse, ShouldRejectDottedFieldNames) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; ASSERT_THROWS(ExpressionObject::parse(expCtx, BSON("a.b" << 1), vps), AssertionException); ASSERT_THROWS(ExpressionObject::parse(expCtx, BSON("c" << 3 << "a.b" << 1), vps), AssertionException); ASSERT_THROWS(ExpressionObject::parse(expCtx, BSON("a.b" << 1 << "c" << 3), vps), AssertionException); } TEST(ExpressionObjectParse, ShouldRejectDuplicateFieldNames) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; ASSERT_THROWS(ExpressionObject::parse(expCtx, BSON("a" << 1 << "a" << 1), vps), AssertionException); ASSERT_THROWS(ExpressionObject::parse(expCtx, BSON("a" << 1 << "b" << 2 << "a" << 1), vps), AssertionException); ASSERT_THROWS( ExpressionObject::parse(expCtx, BSON("a" << BSON("c" << 1) << "b" << 2 << "a" << 1), vps), AssertionException); ASSERT_THROWS( ExpressionObject::parse(expCtx, BSON("a" << 1 << "b" << 2 << "a" << BSON("c" << 1)), vps), AssertionException); } TEST(ExpressionObjectParse, ShouldRejectInvalidFieldName) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; ASSERT_THROWS(ExpressionObject::parse(expCtx, BSON("$a" << 1), vps), AssertionException); ASSERT_THROWS(ExpressionObject::parse(expCtx, BSON("" << 1), vps), AssertionException); ASSERT_THROWS(ExpressionObject::parse(expCtx, BSON(std::string("a\0b", 3) << 1), vps), AssertionException); } TEST(ExpressionObjectParse, ShouldRejectInvalidFieldPathAsValue) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; ASSERT_THROWS(ExpressionObject::parse(expCtx, BSON("a" << "$field."), vps), AssertionException); } TEST(ParseObject, ShouldRejectExpressionAsTheSecondField) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; ASSERT_THROWS( ExpressionObject::parse( expCtx, BSON("a" << BSON("$and" << BSONArray()) << "$or" << BSONArray()), vps), AssertionException); } // // Evaluation. // namespace { /** * ExpressionObject builds two vectors within it's ::parse() method, one owning and one with names * and references to the former. Since the ::create() method bypasses this step, we have to mimic * the behavior here. */ auto expressionObjectCreateHelper( const boost::intrusive_ptr& expCtx, std::vector>>&& expressionsWithChildrenInPlace) { std::vector> children; std::vector&>> expressions; for (auto& [unused, expression] : expressionsWithChildrenInPlace) children.push_back(std::move(expression)); std::vector>::size_type index = 0; for (auto& [fieldName, unused] : expressionsWithChildrenInPlace) { expressions.emplace_back(fieldName, children[index]); ++index; } return ExpressionObject::create(expCtx, std::move(children), std::move(expressions)); } } // namespace TEST(ExpressionObjectEvaluate, EmptyObjectShouldEvaluateToEmptyDocument) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto object = expressionObjectCreateHelper(expCtx, {}); ASSERT_VALUE_EQ(Value(Document()), object->evaluate(Document(), &(expCtx->variables))); ASSERT_VALUE_EQ(Value(Document()), object->evaluate(Document{{"a", 1}}, &(expCtx->variables))); ASSERT_VALUE_EQ(Value(Document()), object->evaluate(Document{{"_id", "ID"_sd}}, &(expCtx->variables))); } TEST(ExpressionObjectEvaluate, ShouldEvaluateEachField) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto object = expressionObjectCreateHelper(expCtx, {{"a", makeConstant(1)}, {"b", makeConstant(5)}}); ASSERT_VALUE_EQ(Value(Document{{"a", 1}, {"b", 5}}), object->evaluate(Document(), &(expCtx->variables))); ASSERT_VALUE_EQ(Value(Document{{"a", 1}, {"b", 5}}), object->evaluate(Document{{"a", 1}}, &(expCtx->variables))); ASSERT_VALUE_EQ(Value(Document{{"a", 1}, {"b", 5}}), object->evaluate(Document{{"_id", "ID"_sd}}, &(expCtx->variables))); } TEST(ExpressionObjectEvaluate, OrderOfFieldsInOutputShouldMatchOrderInSpecification) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto object = expressionObjectCreateHelper(expCtx, {{"a", ExpressionFieldPath::create(expCtx, "a")}, {"b", ExpressionFieldPath::create(expCtx, "b")}, {"c", ExpressionFieldPath::create(expCtx, "c")}}); ASSERT_VALUE_EQ( Value(Document{{"a", "A"_sd}, {"b", "B"_sd}, {"c", "C"_sd}}), object->evaluate(Document{{"c", "C"_sd}, {"a", "A"_sd}, {"b", "B"_sd}, {"_id", "ID"_sd}}, &(expCtx->variables))); } TEST(ExpressionObjectEvaluate, ShouldRemoveFieldsThatHaveMissingValues) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto object = expressionObjectCreateHelper(expCtx, {{"a", ExpressionFieldPath::create(expCtx, "a.b")}, {"b", ExpressionFieldPath::create(expCtx, "missing")}}); ASSERT_VALUE_EQ(Value(Document{}), object->evaluate(Document(), &(expCtx->variables))); ASSERT_VALUE_EQ(Value(Document{}), object->evaluate(Document{{"a", 1}}, &(expCtx->variables))); } TEST(ExpressionObjectEvaluate, ShouldEvaluateFieldsWithinNestedObject) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto object = expressionObjectCreateHelper( expCtx, {{"a", expressionObjectCreateHelper( expCtx, {{"b", makeConstant(1)}, {"c", ExpressionFieldPath::create(expCtx, "_id")}})}}); ASSERT_VALUE_EQ(Value(Document{{"a", Document{{"b", 1}}}}), object->evaluate(Document(), &(expCtx->variables))); ASSERT_VALUE_EQ(Value(Document{{"a", Document{{"b", 1}, {"c", "ID"_sd}}}}), object->evaluate(Document{{"_id", "ID"_sd}}, &(expCtx->variables))); } TEST(ExpressionObjectEvaluate, ShouldEvaluateToEmptyDocumentIfAllFieldsAreMissing) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto object = expressionObjectCreateHelper( expCtx, {{"a", ExpressionFieldPath::create(expCtx, "missing")}}); ASSERT_VALUE_EQ(Value(Document{}), object->evaluate(Document(), &(expCtx->variables))); auto objectWithNestedObject = expressionObjectCreateHelper(expCtx, {{"nested", object}}); ASSERT_VALUE_EQ(Value(Document{{"nested", Document{}}}), objectWithNestedObject->evaluate(Document(), &(expCtx->variables))); } // // Dependencies. // TEST(ExpressionObjectDependencies, ConstantValuesShouldNotBeAddedToDependencies) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto object = expressionObjectCreateHelper(expCtx, {{"a", makeConstant(5)}}); DepsTracker deps; object->addDependencies(&deps); ASSERT_EQ(deps.fields.size(), 0UL); } TEST(ExpressionObjectDependencies, FieldPathsShouldBeAddedToDependencies) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto object = expressionObjectCreateHelper(expCtx, {{"x", ExpressionFieldPath::create(expCtx, "c.d")}}); DepsTracker deps; object->addDependencies(&deps); ASSERT_EQ(deps.fields.size(), 1UL); ASSERT_EQ(deps.fields.count("c.d"), 1UL); }; TEST(ExpressionObjectDependencies, VariablesShouldBeAddedToDependencies) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto varID = expCtx->variablesParseState.defineVariable("var1"); auto fieldPath = ExpressionFieldPath::parse(expCtx, "$$var1", expCtx->variablesParseState); DepsTracker deps; fieldPath->addDependencies(&deps); ASSERT_EQ(deps.vars.size(), 1UL); ASSERT_EQ(deps.vars.count(varID), 1UL); } TEST(ExpressionObjectDependencies, LocalLetVariablesShouldBeFilteredOutOfDependencies) { intrusive_ptr expCtx(new ExpressionContextForTest()); expCtx->variablesParseState.defineVariable("var1"); auto letSpec = BSON("$let" << BSON("vars" << BSON("var2" << "abc") << "in" << BSON("$multiply" << BSON_ARRAY("$$var1" << "$$var2")))); auto expressionLet = ExpressionLet::parse(expCtx, letSpec.firstElement(), expCtx->variablesParseState); DepsTracker deps; expressionLet->addDependencies(&deps); ASSERT_EQ(deps.vars.size(), 1UL); ASSERT_EQ(expCtx->variablesParseState.getVariable("var1"), *deps.vars.begin()); } TEST(ExpressionObjectDependencies, LocalMapVariablesShouldBeFilteredOutOfDependencies) { intrusive_ptr expCtx(new ExpressionContextForTest()); expCtx->variablesParseState.defineVariable("var1"); auto mapSpec = BSON("$map" << BSON("input" << "$field1" << "as" << "var2" << "in" << BSON("$multiply" << BSON_ARRAY("$$var1" << "$$var2")))); auto expressionMap = ExpressionMap::parse(expCtx, mapSpec.firstElement(), expCtx->variablesParseState); DepsTracker deps; expressionMap->addDependencies(&deps); ASSERT_EQ(deps.vars.size(), 1UL); ASSERT_EQ(expCtx->variablesParseState.getVariable("var1"), *deps.vars.begin()); } TEST(ExpressionObjectDependencies, LocalFilterVariablesShouldBeFilteredOutOfDependencies) { intrusive_ptr expCtx(new ExpressionContextForTest()); expCtx->variablesParseState.defineVariable("var1"); auto filterSpec = BSON("$filter" << BSON("input" << BSON_ARRAY(1 << 2 << 3) << "as" << "var2" << "cond" << BSON("$gt" << BSON_ARRAY("$$var1" << "$$var2")))); auto expressionFilter = ExpressionFilter::parse(expCtx, filterSpec.firstElement(), expCtx->variablesParseState); DepsTracker deps; expressionFilter->addDependencies(&deps); ASSERT_EQ(deps.vars.size(), 1UL); ASSERT_EQ(expCtx->variablesParseState.getVariable("var1"), *deps.vars.begin()); } // // Optimizations. // TEST(ExpressionObjectOptimizations, OptimizingAnObjectShouldOptimizeSubExpressions) { // Build up the object {a: {$add: [1, 2]}}. intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; auto addExpression = ExpressionAdd::parse(expCtx, BSON("$add" << BSON_ARRAY(1 << 2)).firstElement(), vps); auto object = expressionObjectCreateHelper(expCtx, {{"a", addExpression}}); ASSERT_EQ(object->getChildExpressions().size(), 1UL); auto optimized = object->optimize(); auto optimizedObject = dynamic_cast(optimized.get()); ASSERT_TRUE(optimizedObject); ASSERT_VALUE_EQ(optimizedObject->evaluate(Document(), &(expCtx->variables)), Value(BSON("a" << 3))); }; TEST(ExpressionObjectOptimizations, OptimizingAnObjectWithAllConstantsShouldOptimizeToExpressionConstant) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; // All constants should optimize to ExpressionConstant. auto objectWithAllConstants = ExpressionObject::parse(expCtx, BSON("b" << 1 << "c" << 1), vps); auto optimizedToAllConstants = objectWithAllConstants->optimize(); auto constants = dynamic_cast(optimizedToAllConstants.get()); ASSERT_TRUE(constants); // Not all constants should not optimize to ExpressionConstant. auto objectNotAllConstants = ExpressionObject::parse(expCtx, BSON("b" << 1 << "input" << "$inputField"), vps); auto optimizedNotAllConstants = objectNotAllConstants->optimize(); auto shouldNotBeConstant = dynamic_cast(optimizedNotAllConstants.get()); ASSERT_FALSE(shouldNotBeConstant); // Sub expression should optimize to constant expression. auto expressionWithConstantObject = ExpressionObject::parse( expCtx, BSON("willBeConstant" << BSON("$add" << BSON_ARRAY(1 << 2)) << "alreadyConstant" << "string"), vps); auto optimizedWithConstant = expressionWithConstantObject->optimize(); auto optimizedObject = dynamic_cast(optimizedWithConstant.get()); ASSERT_TRUE(optimizedObject); ASSERT_VALUE_EQ(optimizedObject->evaluate(Document(), &expCtx->variables), Value(BSON("willBeConstant" << 3 << "alreadyConstant" << "string"))); }; } // namespace Object namespace Or { class ExpectedResultBase { public: virtual ~ExpectedResultBase() {} void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONObj specObject = BSON("" << spec()); BSONElement specElement = specObject.firstElement(); VariablesParseState vps = expCtx->variablesParseState; intrusive_ptr expression = Expression::parseOperand(expCtx, specElement, vps); ASSERT_BSONOBJ_EQ(constify(spec()), expressionToBson(expression)); ASSERT_BSONOBJ_EQ( BSON("" << expectedResult()), toBson(expression->evaluate(fromBson(BSON("a" << 1)), &expCtx->variables))); intrusive_ptr optimized = expression->optimize(); ASSERT_BSONOBJ_EQ( BSON("" << expectedResult()), toBson(optimized->evaluate(fromBson(BSON("a" << 1)), &expCtx->variables))); } protected: virtual BSONObj spec() = 0; virtual bool expectedResult() = 0; }; class OptimizeBase { public: virtual ~OptimizeBase() {} void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONObj specObject = BSON("" << spec()); BSONElement specElement = specObject.firstElement(); VariablesParseState vps = expCtx->variablesParseState; intrusive_ptr expression = Expression::parseOperand(expCtx, specElement, vps); ASSERT_BSONOBJ_EQ(constify(spec()), expressionToBson(expression)); intrusive_ptr optimized = expression->optimize(); ASSERT_BSONOBJ_EQ(expectedOptimized(), expressionToBson(optimized)); } protected: virtual BSONObj spec() = 0; virtual BSONObj expectedOptimized() = 0; }; class NoOptimizeBase : public OptimizeBase { BSONObj expectedOptimized() { return constify(spec()); } }; /** $or without operands. */ class NoOperands : public ExpectedResultBase { BSONObj spec() { return BSON("$or" << BSONArray()); } bool expectedResult() { return false; } }; /** $or passed 'true'. */ class True : public ExpectedResultBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(true)); } bool expectedResult() { return true; } }; /** $or passed 'false'. */ class False : public ExpectedResultBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(false)); } bool expectedResult() { return false; } }; /** $or passed 'true', 'true'. */ class TrueTrue : public ExpectedResultBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(true << true)); } bool expectedResult() { return true; } }; /** $or passed 'true', 'false'. */ class TrueFalse : public ExpectedResultBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(true << false)); } bool expectedResult() { return true; } }; /** $or passed 'false', 'true'. */ class FalseTrue : public ExpectedResultBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(false << true)); } bool expectedResult() { return true; } }; /** $or passed 'false', 'false'. */ class FalseFalse : public ExpectedResultBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(false << false)); } bool expectedResult() { return false; } }; /** $or passed 'false', 'false', 'false'. */ class FalseFalseFalse : public ExpectedResultBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(false << false << false)); } bool expectedResult() { return false; } }; /** $or passed 'false', 'false', 'true'. */ class FalseFalseTrue : public ExpectedResultBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(false << false << true)); } bool expectedResult() { return true; } }; /** $or passed '0', '1'. */ class ZeroOne : public ExpectedResultBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(0 << 1)); } bool expectedResult() { return true; } }; /** $or passed '0', 'false'. */ class ZeroFalse : public ExpectedResultBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(0 << false)); } bool expectedResult() { return false; } }; /** $or passed a field path. */ class FieldPath : public ExpectedResultBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY("$a")); } bool expectedResult() { return true; } }; /** A constant expression is optimized to a constant. */ class OptimizeConstantExpression : public OptimizeBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(1)); } BSONObj expectedOptimized() { return BSON("$const" << true); } }; /** A non constant expression is not optimized. */ class NonConstant : public NoOptimizeBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY("$a")); } }; /** An expression beginning with a single constant is optimized. */ class ConstantNonConstantTrue : public OptimizeBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(1 << "$a")); } BSONObj expectedOptimized() { return BSON("$const" << true); } }; /** An expression beginning with a single constant is optimized. */ class ConstantNonConstantFalse : public OptimizeBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(0 << "$a")); } BSONObj expectedOptimized() { return BSON("$and" << BSON_ARRAY("$a")); } // note: using $and as serialization of ExpressionCoerceToBool rather than // ExpressionAnd }; /** An expression with a field path and '1'. */ class NonConstantOne : public OptimizeBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY("$a" << 1)); } BSONObj expectedOptimized() { return BSON("$const" << true); } }; /** An expression with a field path and '0'. */ class NonConstantZero : public OptimizeBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY("$a" << 0)); } BSONObj expectedOptimized() { return BSON("$and" << BSON_ARRAY("$a")); } }; /** An expression with two field paths and '1'. */ class NonConstantNonConstantOne : public OptimizeBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY("$a" << "$b" << 1)); } BSONObj expectedOptimized() { return BSON("$const" << true); } }; /** An expression with two field paths and '0'. */ class NonConstantNonConstantZero : public OptimizeBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY("$a" << "$b" << 0)); } BSONObj expectedOptimized() { return BSON("$or" << BSON_ARRAY("$a" << "$b")); } }; /** An expression with '0', '1', and a field path. */ class ZeroOneNonConstant : public OptimizeBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(0 << 1 << "$a")); } BSONObj expectedOptimized() { return BSON("$const" << true); } }; /** An expression with '0', '0', and a field path. */ class ZeroZeroNonConstant : public OptimizeBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(0 << 0 << "$a")); } BSONObj expectedOptimized() { return BSON("$and" << BSON_ARRAY("$a")); } }; /** Nested $or expressions. */ class Nested : public OptimizeBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(0 << BSON("$or" << BSON_ARRAY(0)) << "$a" << "$b")); } BSONObj expectedOptimized() { return BSON("$or" << BSON_ARRAY("$a" << "$b")); } }; /** Nested $or expressions containing a nested value evaluating to false. */ class NestedOne : public OptimizeBase { BSONObj spec() { return BSON("$or" << BSON_ARRAY(0 << BSON("$or" << BSON_ARRAY(BSON("$or" << BSON_ARRAY(1)))) << "$a" << "$b")); } BSONObj expectedOptimized() { return BSON("$const" << true); } }; } // namespace Or namespace Parse { namespace Object { /** * Parses the object given by 'specification', with the options given by 'parseContextOptions'. */ boost::intrusive_ptr parseObject(BSONObj specification) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; return Expression::parseObject(expCtx, specification, vps); }; TEST(ParseObject, ShouldAcceptEmptyObject) { auto resultExpression = parseObject(BSONObj()); // Should return an empty ExpressionObject. auto resultObject = dynamic_cast(resultExpression.get()); ASSERT_TRUE(resultObject); ASSERT_EQ(resultObject->getChildExpressions().size(), 0UL); } TEST(ParseObject, ShouldRecognizeKnownExpression) { auto resultExpression = parseObject(BSON("$and" << BSONArray())); // Should return an ExpressionAnd. auto resultAnd = dynamic_cast(resultExpression.get()); ASSERT_TRUE(resultAnd); } } // namespace Object namespace Expression { using mongo::Expression; /** * Parses an expression from the given BSON specification. */ boost::intrusive_ptr parseExpression(BSONObj specification) { const boost::intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; return Expression::parseExpression(expCtx, specification, vps); } TEST(ParseExpression, ShouldRecognizeConstExpression) { auto resultExpression = parseExpression(BSON("$const" << 5)); auto constExpression = dynamic_cast(resultExpression.get()); ASSERT_TRUE(constExpression); ASSERT_VALUE_EQ(constExpression->serialize(false), Value(Document{{"$const", 5}})); } TEST(ParseExpression, ShouldRejectUnknownExpression) { ASSERT_THROWS(parseExpression(BSON("$invalid" << 1)), AssertionException); } TEST(ParseExpression, ShouldRejectExpressionArgumentsWhichAreNotInArray) { ASSERT_THROWS(parseExpression(BSON("$strcasecmp" << "foo")), AssertionException); } TEST(ParseExpression, ShouldRejectExpressionWithWrongNumberOfArguments) { ASSERT_THROWS(parseExpression(BSON("$strcasecmp" << BSON_ARRAY("foo"))), AssertionException); } TEST(ParseExpression, ShouldRejectObjectWithTwoTopLevelExpressions) { ASSERT_THROWS(parseExpression(BSON("$and" << BSONArray() << "$or" << BSONArray())), AssertionException); } TEST(ParseExpression, ShouldRejectExpressionIfItsNotTheOnlyField) { ASSERT_THROWS(parseExpression(BSON("$and" << BSONArray() << "a" << BSON("$or" << BSONArray()))), AssertionException); } TEST(ParseExpression, ShouldParseExpressionWithMultipleArguments) { auto resultExpression = parseExpression(BSON("$strcasecmp" << BSON_ARRAY("foo" << "FOO"))); auto strCaseCmpExpression = dynamic_cast(resultExpression.get()); ASSERT_TRUE(strCaseCmpExpression); vector arguments = {Value(Document{{"$const", "foo"_sd}}), Value(Document{{"$const", "FOO"_sd}})}; ASSERT_VALUE_EQ(strCaseCmpExpression->serialize(false), Value(Document{{"$strcasecmp", arguments}})); } TEST(ParseExpression, ShouldParseExpressionWithNoArguments) { auto resultExpression = parseExpression(BSON("$and" << BSONArray())); auto andExpression = dynamic_cast(resultExpression.get()); ASSERT_TRUE(andExpression); ASSERT_VALUE_EQ(andExpression->serialize(false), Value(Document{{"$and", vector{}}})); } TEST(ParseExpression, ShouldParseExpressionWithOneArgument) { auto resultExpression = parseExpression(BSON("$and" << BSON_ARRAY(1))); auto andExpression = dynamic_cast(resultExpression.get()); ASSERT_TRUE(andExpression); vector arguments = {Value(Document{{"$const", 1}})}; ASSERT_VALUE_EQ(andExpression->serialize(false), Value(Document{{"$and", arguments}})); } TEST(ParseExpression, ShouldAcceptArgumentWithoutArrayForVariadicExpressions) { auto resultExpression = parseExpression(BSON("$and" << 1)); auto andExpression = dynamic_cast(resultExpression.get()); ASSERT_TRUE(andExpression); vector arguments = {Value(Document{{"$const", 1}})}; ASSERT_VALUE_EQ(andExpression->serialize(false), Value(Document{{"$and", arguments}})); } TEST(ParseExpression, ShouldAcceptArgumentWithoutArrayAsSingleArgument) { auto resultExpression = parseExpression(BSON("$not" << 1)); auto notExpression = dynamic_cast(resultExpression.get()); ASSERT_TRUE(notExpression); vector arguments = {Value(Document{{"$const", 1}})}; ASSERT_VALUE_EQ(notExpression->serialize(false), Value(Document{{"$not", arguments}})); } TEST(ParseExpression, ShouldAcceptObjectAsSingleArgument) { auto resultExpression = parseExpression(BSON("$and" << BSON("$const" << 1))); auto andExpression = dynamic_cast(resultExpression.get()); ASSERT_TRUE(andExpression); vector arguments = {Value(Document{{"$const", 1}})}; ASSERT_VALUE_EQ(andExpression->serialize(false), Value(Document{{"$and", arguments}})); } TEST(ParseExpression, ShouldAcceptObjectInsideArrayAsSingleArgument) { auto resultExpression = parseExpression(BSON("$and" << BSON_ARRAY(BSON("$const" << 1)))); auto andExpression = dynamic_cast(resultExpression.get()); ASSERT_TRUE(andExpression); vector arguments = {Value(Document{{"$const", 1}})}; ASSERT_VALUE_EQ(andExpression->serialize(false), Value(Document{{"$and", arguments}})); } } // namespace Expression namespace Operand { using mongo::Expression; /** * Parses an operand from the given BSON specification. The field name is ignored, since it is * assumed to have come from an array, or to have been the only argument to an expression, in which * case the field name would be the name of the expression. */ intrusive_ptr parseOperand(BSONObj specification) { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONElement specElement = specification.firstElement(); VariablesParseState vps = expCtx->variablesParseState; return Expression::parseOperand(expCtx, specElement, vps); } TEST(ParseOperand, ShouldRecognizeFieldPath) { auto resultExpression = parseOperand(BSON("" << "$field")); auto fieldPathExpression = dynamic_cast(resultExpression.get()); ASSERT_TRUE(fieldPathExpression); ASSERT_VALUE_EQ(fieldPathExpression->serialize(false), Value("$field"_sd)); } TEST(ParseOperand, ShouldRecognizeStringLiteral) { auto resultExpression = parseOperand(BSON("" << "foo")); auto constantExpression = dynamic_cast(resultExpression.get()); ASSERT_TRUE(constantExpression); ASSERT_VALUE_EQ(constantExpression->serialize(false), Value(Document{{"$const", "foo"_sd}})); } TEST(ParseOperand, ShouldRecognizeNestedArray) { auto resultExpression = parseOperand(BSON("" << BSON_ARRAY("foo" << "$field"))); auto arrayExpression = dynamic_cast(resultExpression.get()); ASSERT_TRUE(arrayExpression); vector expectedSerializedArray = {Value(Document{{"$const", "foo"_sd}}), Value("$field"_sd)}; ASSERT_VALUE_EQ(arrayExpression->serialize(false), Value(expectedSerializedArray)); } TEST(ParseOperand, ShouldRecognizeNumberLiteral) { auto resultExpression = parseOperand(BSON("" << 5)); auto constantExpression = dynamic_cast(resultExpression.get()); ASSERT_TRUE(constantExpression); ASSERT_VALUE_EQ(constantExpression->serialize(false), Value(Document{{"$const", 5}})); } TEST(ParseOperand, ShouldRecognizeNestedExpression) { auto resultExpression = parseOperand(BSON("" << BSON("$and" << BSONArray()))); auto andExpression = dynamic_cast(resultExpression.get()); ASSERT_TRUE(andExpression); ASSERT_VALUE_EQ(andExpression->serialize(false), Value(Document{{"$and", vector{}}})); } } // namespace Operand } // namespace Parse namespace Set { Value sortSet(Value set) { if (set.nullish()) { return Value(BSONNULL); } vector sortedSet = set.getArray(); ValueComparator valueComparator; sort(sortedSet.begin(), sortedSet.end(), valueComparator.getLessThan()); return Value(sortedSet); } class ExpectedResultBase { public: virtual ~ExpectedResultBase() {} void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); const Document spec = getSpec(); const Value args = spec["input"]; if (!spec["expected"].missing()) { FieldIterator fields(spec["expected"].getDocument()); while (fields.more()) { const Document::FieldPair field(fields.next()); const Value expected = field.second; const BSONObj obj = BSON(field.first << args); VariablesParseState vps = expCtx->variablesParseState; const intrusive_ptr expr = Expression::parseExpression(expCtx, obj, vps); Value result = expr->evaluate({}, &expCtx->variables); if (result.getType() == Array) { result = sortSet(result); } if (ValueComparator().evaluate(result != expected)) { string errMsg = str::stream() << "for expression " << field.first.toString() << " with argument " << args.toString() << " full tree: " << expr->serialize(false).toString() << " expected: " << expected.toString() << " but got: " << result.toString(); FAIL(errMsg); } // TODO test optimize here } } if (!spec["error"].missing()) { const vector& asserters = spec["error"].getArray(); size_t n = asserters.size(); for (size_t i = 0; i < n; i++) { const BSONObj obj = BSON(asserters[i].getString() << args); VariablesParseState vps = expCtx->variablesParseState; ASSERT_THROWS( [&] { // NOTE: parse and evaluatation failures are treated the // same const intrusive_ptr expr = Expression::parseExpression(expCtx, obj, vps); expr->evaluate({}, &expCtx->variables); }(), AssertionException); } } } private: virtual Document getSpec() = 0; }; class Same : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(1 << 2) << DOC_ARRAY(1 << 2)) << "expected" << DOC("$setIsSubset" << true << "$setEquals" << true << "$setIntersection" << DOC_ARRAY(1 << 2) << "$setUnion" << DOC_ARRAY(1 << 2) << "$setDifference" << vector())); } }; class Redundant : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(1 << 2) << DOC_ARRAY(1 << 2 << 2)) << "expected" << DOC("$setIsSubset" << true << "$setEquals" << true << "$setIntersection" << DOC_ARRAY(1 << 2) << "$setUnion" << DOC_ARRAY(1 << 2) << "$setDifference" << vector())); } }; class DoubleRedundant : public ExpectedResultBase { Document getSpec() { return DOC( "input" << DOC_ARRAY(DOC_ARRAY(1 << 1 << 2) << DOC_ARRAY(1 << 2 << 2)) << "expected" << DOC("$setIsSubset" << true << "$setEquals" << true << "$setIntersection" << DOC_ARRAY(1 << 2) << "$setUnion" << DOC_ARRAY(1 << 2) << "$setDifference" << vector())); } }; class Super : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(1 << 2) << DOC_ARRAY(1)) << "expected" << DOC("$setIsSubset" << false << "$setEquals" << false << "$setIntersection" << DOC_ARRAY(1) << "$setUnion" << DOC_ARRAY(1 << 2) << "$setDifference" << DOC_ARRAY(2))); } }; class SuperWithRedundant : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(1 << 2 << 2) << DOC_ARRAY(1)) << "expected" << DOC("$setIsSubset" << false << "$setEquals" << false << "$setIntersection" << DOC_ARRAY(1) << "$setUnion" << DOC_ARRAY(1 << 2) << "$setDifference" << DOC_ARRAY(2))); } }; class Sub : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(1) << DOC_ARRAY(1 << 2)) << "expected" << DOC("$setIsSubset" << true << "$setEquals" << false << "$setIntersection" << DOC_ARRAY(1) << "$setUnion" << DOC_ARRAY(1 << 2) << "$setDifference" << vector())); } }; class SameBackwards : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(1 << 2) << DOC_ARRAY(2 << 1)) << "expected" << DOC("$setIsSubset" << true << "$setEquals" << true << "$setIntersection" << DOC_ARRAY(1 << 2) << "$setUnion" << DOC_ARRAY(1 << 2) << "$setDifference" << vector())); } }; class NoOverlap : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(1 << 2) << DOC_ARRAY(8 << 4)) << "expected" << DOC("$setIsSubset" << false << "$setEquals" << false << "$setIntersection" << vector() << "$setUnion" << DOC_ARRAY(1 << 2 << 4 << 8) << "$setDifference" << DOC_ARRAY(1 << 2))); } }; class Overlap : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(1 << 2) << DOC_ARRAY(8 << 2 << 4)) << "expected" << DOC("$setIsSubset" << false << "$setEquals" << false << "$setIntersection" << DOC_ARRAY(2) << "$setUnion" << DOC_ARRAY(1 << 2 << 4 << 8) << "$setDifference" << DOC_ARRAY(1))); } }; class LastNull : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(1 << 2) << Value(BSONNULL)) << "expected" << DOC("$setIntersection" << BSONNULL << "$setUnion" << BSONNULL << "$setDifference" << BSONNULL) << "error" << DOC_ARRAY("$setEquals"_sd << "$setIsSubset"_sd)); } }; class FirstNull : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(Value(BSONNULL) << DOC_ARRAY(1 << 2)) << "expected" << DOC("$setIntersection" << BSONNULL << "$setUnion" << BSONNULL << "$setDifference" << BSONNULL) << "error" << DOC_ARRAY("$setEquals"_sd << "$setIsSubset"_sd)); } }; class LeftNullAndRightEmpty : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(Value(BSONNULL) << vector()) << "expected" << DOC("$setIntersection" << BSONNULL << "$setUnion" << BSONNULL << "$setDifference" << BSONNULL) << "error" << DOC_ARRAY("$setEquals"_sd << "$setIsSubset"_sd)); } }; class RightNullAndLeftEmpty : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(vector() << Value(BSONNULL)) << "expected" << DOC("$setIntersection" << BSONNULL << "$setUnion" << BSONNULL << "$setDifference" << BSONNULL) << "error" << DOC_ARRAY("$setEquals"_sd << "$setIsSubset"_sd)); } }; class NoArg : public ExpectedResultBase { Document getSpec() { return DOC( "input" << vector() << "expected" << DOC("$setIntersection" << vector() << "$setUnion" << vector()) << "error" << DOC_ARRAY("$setEquals"_sd << "$setIsSubset"_sd << "$setDifference"_sd)); } }; class OneArg : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(1 << 2)) << "expected" << DOC("$setIntersection" << DOC_ARRAY(1 << 2) << "$setUnion" << DOC_ARRAY(1 << 2)) << "error" << DOC_ARRAY("$setEquals"_sd << "$setIsSubset"_sd << "$setDifference"_sd)); } }; class EmptyArg : public ExpectedResultBase { Document getSpec() { return DOC( "input" << DOC_ARRAY(vector()) << "expected" << DOC("$setIntersection" << vector() << "$setUnion" << vector()) << "error" << DOC_ARRAY("$setEquals"_sd << "$setIsSubset"_sd << "$setDifference"_sd)); } }; class LeftArgEmpty : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(vector() << DOC_ARRAY(1 << 2)) << "expected" << DOC("$setIntersection" << vector() << "$setUnion" << DOC_ARRAY(1 << 2) << "$setIsSubset" << true << "$setEquals" << false << "$setDifference" << vector())); } }; class RightArgEmpty : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(1 << 2) << vector()) << "expected" << DOC("$setIntersection" << vector() << "$setUnion" << DOC_ARRAY(1 << 2) << "$setIsSubset" << false << "$setEquals" << false << "$setDifference" << DOC_ARRAY(1 << 2))); } }; class ManyArgs : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(8 << 3) << DOC_ARRAY("asdf"_sd << "foo"_sd) << DOC_ARRAY(80.3 << 34) << vector() << DOC_ARRAY(80.3 << "foo"_sd << 11 << "yay"_sd)) << "expected" << DOC("$setIntersection" << vector() << "$setEquals" << false << "$setUnion" << DOC_ARRAY(3 << 8 << 11 << 34 << 80.3 << "asdf"_sd << "foo"_sd << "yay"_sd)) << "error" << DOC_ARRAY("$setIsSubset"_sd << "$setDifference"_sd)); } }; class ManyArgsEqual : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(1 << 2 << 4) << DOC_ARRAY(1 << 2 << 2 << 4) << DOC_ARRAY(4 << 1 << 2) << DOC_ARRAY(2 << 1 << 1 << 4)) << "expected" << DOC("$setIntersection" << DOC_ARRAY(1 << 2 << 4) << "$setEquals" << true << "$setUnion" << DOC_ARRAY(1 << 2 << 4)) << "error" << DOC_ARRAY("$setIsSubset"_sd << "$setDifference"_sd)); } }; } // namespace Set namespace Strcasecmp { class ExpectedResultBase { public: virtual ~ExpectedResultBase() {} void run() { assertResult(expectedResult(), spec()); assertResult(-expectedResult(), reverseSpec()); } protected: virtual string a() = 0; virtual string b() = 0; virtual int expectedResult() = 0; private: BSONObj spec() { return BSON("$strcasecmp" << BSON_ARRAY(a() << b())); } BSONObj reverseSpec() { return BSON("$strcasecmp" << BSON_ARRAY(b() << a())); } void assertResult(int expectedResult, const BSONObj& spec) { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONObj specObj = BSON("" << spec); BSONElement specElement = specObj.firstElement(); VariablesParseState vps = expCtx->variablesParseState; intrusive_ptr expression = Expression::parseOperand(expCtx, specElement, vps); ASSERT_BSONOBJ_EQ(constify(spec), expressionToBson(expression)); ASSERT_BSONOBJ_EQ(BSON("" << expectedResult), toBson(expression->evaluate({}, &expCtx->variables))); } }; class NullBegin : public ExpectedResultBase { string a() { return string("\0ab", 3); } string b() { return string("\0AB", 3); } int expectedResult() { return 0; } }; class NullEnd : public ExpectedResultBase { string a() { return string("ab\0", 3); } string b() { return string("aB\0", 3); } int expectedResult() { return 0; } }; class NullMiddleLt : public ExpectedResultBase { string a() { return string("a\0a", 3); } string b() { return string("a\0B", 3); } int expectedResult() { return -1; } }; class NullMiddleEq : public ExpectedResultBase { string a() { return string("a\0b", 3); } string b() { return string("a\0B", 3); } int expectedResult() { return 0; } }; class NullMiddleGt : public ExpectedResultBase { string a() { return string("a\0c", 3); } string b() { return string("a\0B", 3); } int expectedResult() { return 1; } }; } // namespace Strcasecmp namespace StrLenBytes { TEST(ExpressionStrLenBytes, ComputesLengthOfString) { assertExpectedResults("$strLenBytes", {{{Value("abc"_sd)}, Value(3)}}); } TEST(ExpressionStrLenBytes, ComputesLengthOfEmptyString) { assertExpectedResults("$strLenBytes", {{{Value(StringData())}, Value(0)}}); } TEST(ExpressionStrLenBytes, ComputesLengthOfStringWithNull) { assertExpectedResults("$strLenBytes", {{{Value("ab\0c"_sd)}, Value(4)}}); } TEST(ExpressionStrLenCP, ComputesLengthOfStringWithNullAtEnd) { assertExpectedResults("$strLenBytes", {{{Value("abc\0"_sd)}, Value(4)}}); } } // namespace StrLenBytes namespace StrLenCP { TEST(ExpressionStrLenCP, ComputesLengthOfASCIIString) { assertExpectedResults("$strLenCP", {{{Value("abc"_sd)}, Value(3)}}); } TEST(ExpressionStrLenCP, ComputesLengthOfEmptyString) { assertExpectedResults("$strLenCP", {{{Value(StringData())}, Value(0)}}); } TEST(ExpressionStrLenCP, ComputesLengthOfStringWithNull) { assertExpectedResults("$strLenCP", {{{Value("ab\0c"_sd)}, Value(4)}}); } TEST(ExpressionStrLenCP, ComputesLengthOfStringWithNullAtEnd) { assertExpectedResults("$strLenCP", {{{Value("abc\0"_sd)}, Value(4)}}); } TEST(ExpressionStrLenCP, ComputesLengthOfStringWithAccent) { assertExpectedResults("$strLenCP", {{{Value("a\0bâ"_sd)}, Value(4)}}); } TEST(ExpressionStrLenCP, ComputesLengthOfStringWithSpecialCharacters) { assertExpectedResults("$strLenCP", {{{Value("ºabøåß"_sd)}, Value(6)}}); } } // namespace StrLenCP namespace SubstrBytes { class ExpectedResultBase { public: virtual ~ExpectedResultBase() {} void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONObj specObj = BSON("" << spec()); BSONElement specElement = specObj.firstElement(); VariablesParseState vps = expCtx->variablesParseState; intrusive_ptr expression = Expression::parseOperand(expCtx, specElement, vps); ASSERT_BSONOBJ_EQ(constify(spec()), expressionToBson(expression)); ASSERT_BSONOBJ_EQ(BSON("" << expectedResult()), toBson(expression->evaluate({}, &expCtx->variables))); } protected: virtual string str() = 0; virtual int offset() = 0; virtual int length() = 0; virtual string expectedResult() = 0; private: BSONObj spec() { return BSON("$substrBytes" << BSON_ARRAY(str() << offset() << length())); } }; /** Retrieve a full string containing a null character. */ class FullNull : public ExpectedResultBase { string str() { return string("a\0b", 3); } int offset() { return 0; } int length() { return 3; } string expectedResult() { return str(); } }; /** Retrieve a substring beginning with a null character. */ class BeginAtNull : public ExpectedResultBase { string str() { return string("a\0b", 3); } int offset() { return 1; } int length() { return 2; } string expectedResult() { return string("\0b", 2); } }; /** Retrieve a substring ending with a null character. */ class EndAtNull : public ExpectedResultBase { string str() { return string("a\0b", 3); } int offset() { return 0; } int length() { return 2; } string expectedResult() { return string("a\0", 2); } }; /** Drop a beginning null character. */ class DropBeginningNull : public ExpectedResultBase { string str() { return string("\0b", 2); } int offset() { return 1; } int length() { return 1; } string expectedResult() { return "b"; } }; /** Drop an ending null character. */ class DropEndingNull : public ExpectedResultBase { string str() { return string("a\0", 2); } int offset() { return 0; } int length() { return 1; } string expectedResult() { return "a"; } }; /** When length is negative, the remainder of the string should be returned. */ class NegativeLength : public ExpectedResultBase { string str() { return string("abcdefghij"); } int offset() { return 2; } int length() { return -1; } string expectedResult() { return "cdefghij"; } }; TEST(ExpressionSubstrTest, ThrowsWithNegativeStart) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; const auto str = "abcdef"_sd; const auto expr = Expression::parseExpression(expCtx, BSON("$substrCP" << BSON_ARRAY(str << -5 << 1)), vps); ASSERT_THROWS([&] { expr->evaluate({}, &expCtx->variables); }(), AssertionException); } } // namespace SubstrBytes namespace SubstrCP { TEST(ExpressionSubstrCPTest, DoesThrowWithBadContinuationByte) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; const auto continuationByte = "\x80\x00"_sd; const auto expr = Expression::parseExpression( expCtx, BSON("$substrCP" << BSON_ARRAY(continuationByte << 0 << 1)), vps); ASSERT_THROWS([&] { expr->evaluate({}, &expCtx->variables); }(), AssertionException); } TEST(ExpressionSubstrCPTest, DoesThrowWithInvalidLeadingByte) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; const auto leadingByte = "\xFF\x00"_sd; const auto expr = Expression::parseExpression( expCtx, BSON("$substrCP" << BSON_ARRAY(leadingByte << 0 << 1)), vps); ASSERT_THROWS([&] { expr->evaluate({}, &expCtx->variables); }(), AssertionException); } TEST(ExpressionSubstrCPTest, WithStandardValue) { assertExpectedResults("$substrCP", {{{Value("abc"_sd), Value(0), Value(2)}, Value("ab"_sd)}}); } TEST(ExpressionSubstrCPTest, WithNullCharacter) { assertExpectedResults("$substrCP", {{{Value("abc\0d"_sd), Value(2), Value(3)}, Value("c\0d"_sd)}}); } TEST(ExpressionSubstrCPTest, WithNullCharacterAtEnd) { assertExpectedResults("$substrCP", {{{Value("abc\0"_sd), Value(2), Value(2)}, Value("c\0"_sd)}}); } TEST(ExpressionSubstrCPTest, WithOutOfRangeString) { assertExpectedResults("$substrCP", {{{Value("abc"_sd), Value(3), Value(2)}, Value(StringData())}}); } TEST(ExpressionSubstrCPTest, WithPartiallyOutOfRangeString) { assertExpectedResults("$substrCP", {{{Value("abc"_sd), Value(1), Value(4)}, Value("bc"_sd)}}); } TEST(ExpressionSubstrCPTest, WithUnicodeValue) { assertExpectedResults("$substrCP", {{{Value("øø∫å"_sd), Value(0), Value(4)}, Value("øø∫å"_sd)}}); assertExpectedResults("$substrBytes", {{{Value("øø∫å"_sd), Value(0), Value(4)}, Value("øø"_sd)}}); } TEST(ExpressionSubstrCPTest, WithMixedUnicodeAndASCIIValue) { assertExpectedResults("$substrCP", {{{Value("a∫bøßabc"_sd), Value(1), Value(4)}, Value("∫bøß"_sd)}}); assertExpectedResults("$substrBytes", {{{Value("a∫bøßabc"_sd), Value(1), Value(4)}, Value("∫b"_sd)}}); } TEST(ExpressionSubstrCPTest, ShouldCoerceDateToString) { assertExpectedResults("$substrCP", {{{Value(Date_t::fromMillisSinceEpoch(0)), Value(0), Value(1000)}, Value("1970-01-01T00:00:00.000Z"_sd)}}); assertExpectedResults("$substrBytes", {{{Value(Date_t::fromMillisSinceEpoch(0)), Value(0), Value(1000)}, Value("1970-01-01T00:00:00.000Z"_sd)}}); } } // namespace SubstrCP namespace Trim { TEST(ExpressionTrimParsingTest, ThrowsIfSpecIsNotAnObject) { intrusive_ptr expCtx(new ExpressionContextForTest()); ASSERT_THROWS( Expression::parseExpression(expCtx, BSON("$trim" << 1), expCtx->variablesParseState), AssertionException); ASSERT_THROWS(Expression::parseExpression( expCtx, BSON("$trim" << BSON_ARRAY(1 << 2)), expCtx->variablesParseState), AssertionException); ASSERT_THROWS(Expression::parseExpression( expCtx, BSON("$ltrim" << BSONNULL), expCtx->variablesParseState), AssertionException); ASSERT_THROWS(Expression::parseExpression(expCtx, BSON("$rtrim" << "string"), expCtx->variablesParseState), AssertionException); } TEST(ExpressionTrimParsingTest, ThrowsIfSpecDoesNotSpecifyInput) { intrusive_ptr expCtx(new ExpressionContextForTest()); ASSERT_THROWS(Expression::parseExpression( expCtx, BSON("$trim" << BSONObj()), expCtx->variablesParseState), AssertionException); ASSERT_THROWS(Expression::parseExpression(expCtx, BSON("$ltrim" << BSON("chars" << "xyz")), expCtx->variablesParseState), AssertionException); } TEST(ExpressionTrimParsingTest, ThrowsIfSpecContainsUnrecognizedField) { intrusive_ptr expCtx(new ExpressionContextForTest()); ASSERT_THROWS(Expression::parseExpression( expCtx, BSON("$trim" << BSON("other" << 1)), expCtx->variablesParseState), AssertionException); ASSERT_THROWS(Expression::parseExpression(expCtx, BSON("$ltrim" << BSON("chars" << "xyz" << "other" << 1)), expCtx->variablesParseState), AssertionException); ASSERT_THROWS(Expression::parseExpression(expCtx, BSON("$rtrim" << BSON("input" << "$x" << "chars" << "xyz" << "other" << 1)), expCtx->variablesParseState), AssertionException); } TEST(ExpressionTrimTest, ThrowsIfInputIsNotString) { ASSERT_THROWS(evaluateNamedArgExpression("$trim", Document{{"input", 1}}), AssertionException); ASSERT_THROWS(evaluateNamedArgExpression("$trim", Document{{"input", BSON_ARRAY(1 << 2)}}), AssertionException); ASSERT_THROWS(evaluateNamedArgExpression("$ltrim", Document{{"input", 3}}), AssertionException); ASSERT_THROWS(evaluateNamedArgExpression("$rtrim", Document{{"input", Document{{"x", 1}}}}), AssertionException); } TEST(ExpressionTrimTest, ThrowsIfCharsIsNotAString) { ASSERT_THROWS(evaluateNamedArgExpression("$trim", Document{{"input", " x "_sd}, {"chars", 1}}), AssertionException); ASSERT_THROWS(evaluateNamedArgExpression( "$trim", Document{{"input", " x "_sd}, {"chars", BSON_ARRAY(1 << 2)}}), AssertionException); ASSERT_THROWS(evaluateNamedArgExpression("$ltrim", Document{{"input", " x "_sd}, {"chars", 3}}), AssertionException); ASSERT_THROWS(evaluateNamedArgExpression( "$rtrim", Document{{"input", " x "_sd}, {"chars", Document{{"x", 1}}}}), AssertionException); } TEST(ExpressionTrimTest, DoesTrimAsciiWhitespace) { // Trim from both sides. ASSERT_VALUE_EQ(evaluateNamedArgExpression("$trim", Document{{"input", " abc "_sd}}), Value{"abc"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$trim", Document{{"input", "\n abc \r\n "_sd}}), Value{"abc"_sd}); // Trim just from the right. ASSERT_VALUE_EQ(evaluateNamedArgExpression("$rtrim", Document{{"input", "abc "_sd}}), Value{"abc"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$rtrim", Document{{"input", "abc \r\n "_sd}}), Value{"abc"_sd}); // Trim just from the left. ASSERT_VALUE_EQ(evaluateNamedArgExpression("$ltrim", Document{{"input", " abc"_sd}}), Value{"abc"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$ltrim", Document{{"input", "\n abc"_sd}}), Value{"abc"_sd}); // Make sure we don't trim from the opposite side when doing $ltrim or $rtrim. ASSERT_VALUE_EQ(evaluateNamedArgExpression("$rtrim", Document{{"input", " abc"_sd}}), Value{" abc"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$rtrim", Document{{"input", "\t \nabc \r\n "_sd}}), Value{"\t \nabc"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$ltrim", Document{{"input", " abc "_sd}}), Value{"abc "_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$ltrim", Document{{"input", "\n abc \t\n "_sd}}), Value{"abc \t\n "_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$ltrim", Document{{"input", "abc "_sd}}), Value{"abc "_sd}); } TEST(ExpressionTrimTest, DoesTrimNullCharacters) { // Trim from both sides. ASSERT_VALUE_EQ(evaluateNamedArgExpression("$trim", Document{{"input", "\0\0abc\0"_sd}}), Value{"abc"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$trim", Document{{"input", "\0 \0 abc \0 "_sd}}), Value{"abc"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", "\n \0 abc \r\0\n "_sd}}), Value{"abc"_sd}); // Trim just from the right. ASSERT_VALUE_EQ(evaluateNamedArgExpression("$rtrim", Document{{"input", "abc\0\0"_sd}}), Value{"abc"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$rtrim", Document{{"input", "abc \r\0\n\0 "_sd}}), Value{"abc"_sd}); // Trim just from the left. ASSERT_VALUE_EQ(evaluateNamedArgExpression("$ltrim", Document{{"input", "\0\0abc"_sd}}), Value{"abc"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$ltrim", Document{{"input", "\n \0\0 abc"_sd}}), Value{"abc"_sd}); // Make sure we don't trim from the opposite side when doing $ltrim or $rtrim. ASSERT_VALUE_EQ(evaluateNamedArgExpression("$rtrim", Document{{"input", "\0\0abc"_sd}}), Value{"\0\0abc"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$rtrim", Document{{"input", " \0 abc"_sd}}), Value{" \0 abc"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$rtrim", Document{{"input", "\t\0\0 \nabc \r\n "_sd}}), Value{"\t\0\0 \nabc"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$ltrim", Document{{"input", " abc\0\0"_sd}}), Value{"abc\0\0"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$ltrim", Document{{"input", "\n abc \t\0\n \0\0 "_sd}}), Value{"abc \t\0\n \0\0 "_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$ltrim", Document{{"input", "abc\0\0"_sd}}), Value{"abc\0\0"_sd}); } TEST(ExpressionTrimTest, DoesTrimUnicodeWhitespace) { // Trim from both sides. ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", "\u2001abc\u2004\u200A"_sd}}), Value{"abc"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression( "$trim", Document{{"input", "\n\u0020 \0\u2007 abc \r\0\n\u0009\u200A "_sd}}), Value{"abc"_sd}); // Trim just from the right. ASSERT_VALUE_EQ(evaluateNamedArgExpression("$rtrim", Document{{"input", "abc\u2007\u2006"_sd}}), Value{"abc"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$rtrim", Document{{"input", "abc \r\u2009\u0009\u200A\n\0 "_sd}}), Value{"abc"_sd}); // Trim just from the left. ASSERT_VALUE_EQ(evaluateNamedArgExpression("$ltrim", Document{{"input", "\u2009\u2004abc"_sd}}), Value{"abc"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$ltrim", Document{{"input", "\n \u2000 \0\u2008\0 \u200Aabc"_sd}}), Value{"abc"_sd}); } TEST(ExpressionTrimTest, DoesTrimCustomAsciiCharacters) { // Trim from both sides. ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", "xxXXxx"_sd}, {"chars", "x"_sd}}), Value{"XX"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", "00123"_sd}, {"chars", "0"_sd}}), Value{"123"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", "30:00:12 I don't care about the time"_sd}, {"chars", "0123456789: "_sd}}), Value{"I don't care about the time"_sd}); // Trim just from the right. ASSERT_VALUE_EQ( evaluateNamedArgExpression("$rtrim", Document{{"input", "xxXXxx"_sd}, {"chars", "x"_sd}}), Value{"xxXX"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$rtrim", Document{{"input", "00123"_sd}, {"chars", "0"_sd}}), Value{"00123"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$rtrim", Document{{"input", "30:00:12 I don't care about the time"_sd}, {"chars", "0123456789: "_sd}}), Value{"30:00:12 I don't care about the time"_sd}); // Trim just from the left. ASSERT_VALUE_EQ( evaluateNamedArgExpression("$rtrim", Document{{"input", "xxXXxx"_sd}, {"chars", "x"_sd}}), Value{"xxXX"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$rtrim", Document{{"input", "00123"_sd}, {"chars", "0"_sd}}), Value{"00123"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$rtrim", Document{{"input", "30:00:12 I don't care about the time"_sd}, {"chars", "0123456789: "_sd}}), Value{"30:00:12 I don't care about the time"_sd}); } TEST(ExpressionTrimTest, DoesTrimCustomUnicodeCharacters) { ASSERT_VALUE_EQ( evaluateNamedArgExpression("$ltrim", Document{{"input", "∃x.x ≥ y"_sd}, {"chars", "∃"_sd}}), Value{"x.x ≥ y"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$rtrim", Document{{"input", "∃x.x ≥ y"_sd}, {"chars", "∃"_sd}}), Value{"∃x.x ≥ y"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", "∃x.x ≥ y"_sd}, {"chars", "∃"_sd}}), Value{"x.x ≥ y"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$ltrim", Document{{"input", "⌊x⌋"_sd}, {"chars", "⌊⌋"_sd}}), Value{"x⌋"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$rtrim", Document{{"input", "⌊x⌋"_sd}, {"chars", "⌊⌋"_sd}}), Value{"⌊x"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", "⌊x⌋"_sd}, {"chars", "⌊⌋"_sd}}), Value{"x"_sd}); } TEST(ExpressionTrimTest, DoesTrimCustomMixOfUnicodeAndAsciiCharacters) { ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$ltrim", Document{{"input", "∃x.x ≥ y"_sd}, {"chars", "∃y"_sd}}), Value{"x.x ≥ y"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$rtrim", Document{{"input", "∃x.x ≥ y"_sd}, {"chars", "∃y"_sd}}), Value{"∃x.x ≥ "_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", "∃x.x ≥ y"_sd}, {"chars", "∃y"_sd}}), Value{"x.x ≥ "_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$ltrim", Document{{"input", "⌊x⌋"_sd}, {"chars", "⌊x⌋"_sd}}), Value{""_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$rtrim", Document{{"input", "⌊x⌋"_sd}, {"chars", "⌊x⌋"_sd}}), Value{""_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", "⌊x⌋"_sd}, {"chars", "⌊x⌋"_sd}}), Value{""_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression( "$ltrim", Document{{"input", "▹▱◯□ I ▙◉VE Shapes □◯▱◃"_sd}, {"chars", "□◯▱◃▹ "_sd}}), Value{"I ▙◉VE Shapes □◯▱◃"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression( "$rtrim", Document{{"input", "▹▱◯□ I ▙◉VE Shapes □◯▱◃"_sd}, {"chars", "□◯▱◃▹ "_sd}}), Value{"▹▱◯□ I ▙◉VE Shapes"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression( "$trim", Document{{"input", "▹▱◯□ I ▙◉VE Shapes □◯▱◃"_sd}, {"chars", "□◯▱◃▹ "_sd}}), Value{"I ▙◉VE Shapes"_sd}); } TEST(ExpressionTrimTest, DoesNotTrimFromMiddle) { // Using ascii whitespace. ASSERT_VALUE_EQ(evaluateNamedArgExpression("$trim", Document{{"input", " a\tb c "_sd}}), Value{"a\tb c"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", "\n a\nb c \r\n "_sd}}), Value{"a\nb c"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$rtrim", Document{{"input", " a\tb c "_sd}}), Value{" a\tb c"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$rtrim", Document{{"input", "\n a\nb c \r\n "_sd}}), Value{"\n a\nb c"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$ltrim", Document{{"input", " a\tb c "_sd}}), Value{"a\tb c "_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$ltrim", Document{{"input", "\n a\nb c \r\n "_sd}}), Value{"a\nb c \r\n "_sd}); // Using unicode whitespace. ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$trim", Document{{"input", "\u2001a\u2001\u000Ab\u2009c\u2004\u200A"_sd}}), Value{"a\u2001\u000Ab\u2009c"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression( "$ltrim", Document{{"input", "\u2001a\u2001\u000Ab\u2009c\u2004\u200A"_sd}}), Value{"a\u2001\u000Ab\u2009c\u2004\u200A"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression( "$rtrim", Document{{"input", "\u2001a\u2001\u000Ab\u2009c\u2004\u200A"_sd}}), Value{"\u2001a\u2001\u000Ab\u2009c"_sd}); // With custom ascii characters. ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", "xxXxXxx"_sd}, {"chars", "x"_sd}}), Value{"XxX"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$rtrim", Document{{"input", "xxXxXxx"_sd}, {"chars", "x"_sd}}), Value{"xxXxX"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$ltrim", Document{{"input", "xxXxXxx"_sd}, {"chars", "x"_sd}}), Value{"XxXxx"_sd}); // With custom unicode characters. ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$trim", Document{{"input", "⌊y + 2⌋⌊x⌋"_sd}, {"chars", "⌊⌋"_sd}}), Value{"y + 2⌋⌊x"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$ltrim", Document{{"input", "⌊y + 2⌋⌊x⌋"_sd}, {"chars", "⌊⌋"_sd}}), Value{"y + 2⌋⌊x⌋"_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$rtrim", Document{{"input", "⌊y + 2⌋⌊x⌋"_sd}, {"chars", "⌊⌋"_sd}}), Value{"⌊y + 2⌋⌊x"_sd}); } TEST(ExpressionTrimTest, DoesTrimEntireString) { // Using ascii whitespace. ASSERT_VALUE_EQ(evaluateNamedArgExpression("$trim", Document{{"input", " \t \n "_sd}}), Value{""_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$ltrim", Document{{"input", " \t \n\0 "_sd}}), Value{""_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$rtrim", Document{{"input", " \t "_sd}}), Value{""_sd}); // Using unicode whitespace. ASSERT_VALUE_EQ( evaluateNamedArgExpression( "$trim", Document{{"input", "\u2001 \u2001\t\u000A \u2009\u2004\u200A"_sd}}), Value{""_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression( "$ltrim", Document{{"input", "\u2001 \u2001\t\u000A \u2009\u2004\u200A"_sd}}), Value{""_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression( "$rtrim", Document{{"input", "\u2001 \u2001\t\u000A \u2009\u2004\u200A"_sd}}), Value{""_sd}); // With custom characters. ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", "xxXxXxx"_sd}, {"chars", "x"_sd}}), Value{"XxX"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$rtrim", Document{{"input", "xxXxXxx"_sd}, {"chars", "x"_sd}}), Value{"xxXxX"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$ltrim", Document{{"input", "xxXxXxx"_sd}, {"chars", "x"_sd}}), Value{"XxXxx"_sd}); // With custom unicode characters. ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", "⌊y⌋⌊x⌋"_sd}, {"chars", "⌊xy⌋"_sd}}), Value{""_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$ltrim", Document{{"input", "⌊y⌋⌊x⌋"_sd}, {"chars", "⌊xy⌋"_sd}}), Value{""_sd}); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$rtrim", Document{{"input", "⌊y⌋⌊x⌋"_sd}, {"chars", "⌊xy⌋"_sd}}), Value{""_sd}); } TEST(ExpressionTrimTest, DoesNotTrimAnyThingWithEmptyChars) { ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", "abcde"_sd}, {"chars", ""_sd}}), Value{"abcde"_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", " "_sd}, {"chars", ""_sd}}), Value{" "_sd}); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", " ⌊y⌋⌊x⌋ "_sd}, {"chars", ""_sd}}), Value{" ⌊y⌋⌊x⌋ "_sd}); } TEST(ExpressionTrimTest, TrimComparisonsShouldNotRespectCollation) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto caseInsensitive = std::make_unique(CollatorInterfaceMock::MockType::kToLowerString); expCtx->setCollator(caseInsensitive.get()); auto trim = Expression::parseExpression(expCtx, BSON("$trim" << BSON("input" << "xxXXxx" << "chars" << "x")), expCtx->variablesParseState); ASSERT_VALUE_EQ(trim->evaluate({}, &expCtx->variables), Value("XX"_sd)); } TEST(ExpressionTrimTest, ShouldRejectInvalidUTFInCharsArgument) { const auto twoThirdsOfExistsSymbol = "\xE2\x88"_sd; // Full ∃ symbol would be "\xE2\x88\x83". ASSERT_THROWS(evaluateNamedArgExpression( "$trim", Document{{"input", "abcde"_sd}, {"chars", twoThirdsOfExistsSymbol}}), AssertionException); const auto stringWithExtraContinuationByte = "\xE2\x88\x83\x83"_sd; ASSERT_THROWS( evaluateNamedArgExpression( "$trim", Document{{"input", "ab∃"_sd}, {"chars", stringWithExtraContinuationByte}}), AssertionException); ASSERT_THROWS( evaluateNamedArgExpression("$ltrim", Document{{"input", "a" + twoThirdsOfExistsSymbol + "b∃"}, {"chars", stringWithExtraContinuationByte}}), AssertionException); } TEST(ExpressionTrimTest, ShouldIgnoreUTF8InputWithTruncatedCodePoint) { const auto twoThirdsOfExistsSymbol = "\xE2\x88"_sd; // Full ∃ symbol would be "\xE2\x88\x83". // We are OK producing invalid UTF-8 if the input string was invalid UTF-8, so if the truncated // code point is in the middle and we never examine it, it should work fine. ASSERT_VALUE_EQ( evaluateNamedArgExpression( "$rtrim", Document{{"input", "abc" + twoThirdsOfExistsSymbol + "edf∃"}, {"chars", "∃"_sd}}), Value("abc" + twoThirdsOfExistsSymbol + "edf")); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$trim", Document{{"input", twoThirdsOfExistsSymbol}, {"chars", "∃"_sd}}), Value(twoThirdsOfExistsSymbol)); ASSERT_VALUE_EQ( evaluateNamedArgExpression( "$rtrim", Document{{"input", "abc" + twoThirdsOfExistsSymbol}, {"chars", "∃"_sd}}), Value("abc" + twoThirdsOfExistsSymbol)); } TEST(ExpressionTrimTest, ShouldNotTrimUTF8InputWithTrailingExtraContinuationBytes) { const auto stringWithExtraContinuationByte = "\xE2\x88\x83\x83"_sd; ASSERT_VALUE_EQ( evaluateNamedArgExpression( "$trim", Document{{"input", stringWithExtraContinuationByte + "edf∃"}, {"chars", "∃"_sd}}), Value("\x83" + "edf"_sd)); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$trim", Document{{"input", "abc" + stringWithExtraContinuationByte + "edf∃"}, {"chars", "∃"_sd}}), Value("abc" + stringWithExtraContinuationByte + "edf"_sd)); ASSERT_VALUE_EQ( evaluateNamedArgExpression( "$trim", Document{{"input", "Abc" + stringWithExtraContinuationByte}, {"chars", "∃"_sd}}), Value("Abc" + stringWithExtraContinuationByte)); ASSERT_VALUE_EQ( evaluateNamedArgExpression( "$rtrim", Document{{"input", stringWithExtraContinuationByte}, {"chars", "∃"_sd}}), Value(stringWithExtraContinuationByte)); } TEST(ExpressionTrimTest, ShouldRetunNullIfInputIsNullish) { ASSERT_VALUE_EQ(evaluateNamedArgExpression("$trim", Document{{"input", BSONNULL}}), Value(BSONNULL)); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$trim", Document{{"input", "$missingField"_sd}}), Value(BSONNULL)); ASSERT_VALUE_EQ(evaluateNamedArgExpression("$trim", Document{{"input", BSONUndefined}}), Value(BSONNULL)); // Test with a chars argument provided. ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", BSONNULL}, {"chars", "∃"_sd}}), Value(BSONNULL)); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$trim", Document{{"input", "$missingField"_sd}, {"chars", "∃"_sd}}), Value(BSONNULL)); ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", BSONUndefined}, {"chars", "∃"_sd}}), Value(BSONNULL)); // Test other variants of trim. ASSERT_VALUE_EQ(evaluateNamedArgExpression("$ltrim", Document{{"input", BSONNULL}}), Value(BSONNULL)); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$rtrim", Document{{"input", "$missingField"_sd}, {"chars", "∃"_sd}}), Value(BSONNULL)); } TEST(ExpressionTrimTest, ShouldRetunNullIfCharsIsNullish) { ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", " x "_sd}, {"chars", BSONNULL}}), Value(BSONNULL)); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$trim", Document{{"input", " x "_sd}, {"chars", "$missingField"_sd}}), Value(BSONNULL)); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$trim", Document{{"input", " x "_sd}, {"chars", BSONUndefined}}), Value(BSONNULL)); // Test other variants of trim. ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$ltrim", Document{{"input", " x "_sd}, {"chars", "$missingField"_sd}}), Value(BSONNULL)); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$rtrim", Document{{"input", " x "_sd}, {"chars", BSONUndefined}}), Value(BSONNULL)); } TEST(ExpressionTrimTest, ShouldReturnNullIfBothCharsAndCharsAreNullish) { ASSERT_VALUE_EQ( evaluateNamedArgExpression("$trim", Document{{"input", BSONNULL}, {"chars", BSONNULL}}), Value(BSONNULL)); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$trim", Document{{"input", BSONUndefined}, {"chars", "$missingField"_sd}}), Value(BSONNULL)); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$trim", Document{{"input", "$missingField"_sd}, {"chars", BSONUndefined}}), Value(BSONNULL)); // Test other variants of trim. ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$rtrim", Document{{"input", BSONNULL}, {"chars", "$missingField"_sd}}), Value(BSONNULL)); ASSERT_VALUE_EQ(evaluateNamedArgExpression( "$ltrim", Document{{"input", "$missingField"_sd}, {"chars", BSONNULL}}), Value(BSONNULL)); } TEST(ExpressionTrimTest, DoesOptimizeToConstantWithNoChars) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto trim = Expression::parseExpression(expCtx, BSON("$trim" << BSON("input" << " abc ")), expCtx->variablesParseState); auto optimized = trim->optimize(); auto constant = dynamic_cast(optimized.get()); ASSERT_TRUE(constant); ASSERT_VALUE_EQ(constant->getValue(), Value("abc"_sd)); // Test that it optimizes to a constant if the input also optimizes to a constant. trim = Expression::parseExpression( expCtx, BSON("$trim" << BSON("input" << BSON("$concat" << BSON_ARRAY(" " << "abc ")))), expCtx->variablesParseState); optimized = trim->optimize(); constant = dynamic_cast(optimized.get()); ASSERT_TRUE(constant); ASSERT_VALUE_EQ(constant->getValue(), Value("abc"_sd)); } TEST(ExpressionTrimTest, DoesOptimizeToConstantWithCustomChars) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto trim = Expression::parseExpression(expCtx, BSON("$trim" << BSON("input" << " abc " << "chars" << " ")), expCtx->variablesParseState); auto optimized = trim->optimize(); auto constant = dynamic_cast(optimized.get()); ASSERT_TRUE(constant); ASSERT_VALUE_EQ(constant->getValue(), Value("abc"_sd)); // Test that it optimizes to a constant if the chars argument optimizes to a constant. trim = Expression::parseExpression( expCtx, BSON("$trim" << BSON("input" << " abc " << "chars" << BSON("$substrCP" << BSON_ARRAY(" " << 1 << 1)))), expCtx->variablesParseState); optimized = trim->optimize(); constant = dynamic_cast(optimized.get()); ASSERT_TRUE(constant); ASSERT_VALUE_EQ(constant->getValue(), Value("abc"_sd)); // Test that it optimizes to a constant if both arguments optimize to a constant. trim = Expression::parseExpression( expCtx, BSON("$trim" << BSON("input" << BSON("$concat" << BSON_ARRAY(" " << "abc ")) << "chars" << BSON("$substrCP" << BSON_ARRAY(" " << 1 << 1)))), expCtx->variablesParseState); optimized = trim->optimize(); constant = dynamic_cast(optimized.get()); ASSERT_TRUE(constant); ASSERT_VALUE_EQ(constant->getValue(), Value("abc"_sd)); } TEST(ExpressionTrimTest, DoesNotOptimizeToConstantWithFieldPaths) { intrusive_ptr expCtx(new ExpressionContextForTest()); // 'input' is field path. auto trim = Expression::parseExpression(expCtx, BSON("$trim" << BSON("input" << "$inputField")), expCtx->variablesParseState); auto optimized = trim->optimize(); auto constant = dynamic_cast(optimized.get()); ASSERT_FALSE(constant); // 'chars' is field path. trim = Expression::parseExpression(expCtx, BSON("$trim" << BSON("input" << " abc " << "chars" << "$secondInput")), expCtx->variablesParseState); optimized = trim->optimize(); constant = dynamic_cast(optimized.get()); ASSERT_FALSE(constant); // Both are field paths. trim = Expression::parseExpression(expCtx, BSON("$trim" << BSON("input" << "$inputField" << "chars" << "$secondInput")), expCtx->variablesParseState); optimized = trim->optimize(); constant = dynamic_cast(optimized.get()); ASSERT_FALSE(constant); } TEST(ExpressionTrimTest, DoesAddInputDependencies) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto trim = Expression::parseExpression(expCtx, BSON("$trim" << BSON("input" << "$inputField")), expCtx->variablesParseState); DepsTracker deps; trim->addDependencies(&deps); ASSERT_EQ(deps.fields.count("inputField"), 1u); ASSERT_EQ(deps.fields.size(), 1u); } TEST(ExpressionTrimTest, DoesAddCharsDependencies) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto trim = Expression::parseExpression(expCtx, BSON("$trim" << BSON("input" << "$inputField" << "chars" << "$$CURRENT.a")), expCtx->variablesParseState); DepsTracker deps; trim->addDependencies(&deps); ASSERT_EQ(deps.fields.count("inputField"), 1u); ASSERT_EQ(deps.fields.count("a"), 1u); ASSERT_EQ(deps.fields.size(), 2u); } TEST(ExpressionTrimTest, DoesSerializeCorrectly) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto trim = Expression::parseExpression(expCtx, BSON("$trim" << BSON("input" << " abc ")), expCtx->variablesParseState); ASSERT_VALUE_EQ(trim->serialize(false), trim->serialize(true)); ASSERT_VALUE_EQ( trim->serialize(false), Value(Document{{"$trim", Document{{"input", Document{{"$const", " abc "_sd}}}}}})); // Make sure we can re-parse it and evaluate it. auto reparsedTrim = Expression::parseExpression( expCtx, trim->serialize(false).getDocument().toBson(), expCtx->variablesParseState); ASSERT_VALUE_EQ(reparsedTrim->evaluate({}, &expCtx->variables), Value("abc"_sd)); // Use $ltrim, and specify the 'chars' option. trim = Expression::parseExpression(expCtx, BSON("$ltrim" << BSON("input" << "$inputField" << "chars" << "$$CURRENT.a")), expCtx->variablesParseState); ASSERT_VALUE_EQ( trim->serialize(false), Value(Document{{"$ltrim", Document{{"input", "$inputField"_sd}, {"chars", "$a"_sd}}}})); // Make sure we can re-parse it and evaluate it. reparsedTrim = Expression::parseExpression( expCtx, trim->serialize(false).getDocument().toBson(), expCtx->variablesParseState); ASSERT_VALUE_EQ(reparsedTrim->evaluate(Document{{"inputField", " , 4"_sd}, {"a", " ,"_sd}}, &expCtx->variables), Value("4"_sd)); } } // namespace Trim namespace Type { TEST(ExpressionTypeTest, WithMinKeyValue) { assertExpectedResults("$type", {{{Value(MINKEY)}, Value("minKey"_sd)}}); } TEST(ExpressionTypeTest, WithDoubleValue) { assertExpectedResults("$type", {{{Value(1.0)}, Value("double"_sd)}}); } TEST(ExpressionTypeTest, WithStringValue) { assertExpectedResults("$type", {{{Value("stringValue"_sd)}, Value("string"_sd)}}); } TEST(ExpressionTypeTest, WithObjectValue) { BSONObj objectVal = fromjson("{a: {$literal: 1}}"); assertExpectedResults("$type", {{{Value(objectVal)}, Value("object"_sd)}}); } TEST(ExpressionTypeTest, WithArrayValue) { assertExpectedResults("$type", {{{Value(BSON_ARRAY(1 << 2))}, Value("array"_sd)}}); } TEST(ExpressionTypeTest, WithBinDataValue) { BSONBinData binDataVal = BSONBinData("", 0, BinDataGeneral); assertExpectedResults("$type", {{{Value(binDataVal)}, Value("binData"_sd)}}); } TEST(ExpressionTypeTest, WithUndefinedValue) { assertExpectedResults("$type", {{{Value(BSONUndefined)}, Value("undefined"_sd)}}); } TEST(ExpressionTypeTest, WithOIDValue) { assertExpectedResults("$type", {{{Value(OID())}, Value("objectId"_sd)}}); } TEST(ExpressionTypeTest, WithBoolValue) { assertExpectedResults("$type", {{{Value(true)}, Value("bool"_sd)}}); } TEST(ExpressionTypeTest, WithDateValue) { Date_t dateVal = BSON("" << DATENOW).firstElement().Date(); assertExpectedResults("$type", {{{Value(dateVal)}, Value("date"_sd)}}); } TEST(ExpressionTypeTest, WithNullValue) { assertExpectedResults("$type", {{{Value(BSONNULL)}, Value("null"_sd)}}); } TEST(ExpressionTypeTest, WithRegexValue) { assertExpectedResults("$type", {{{Value(BSONRegEx("a.b"))}, Value("regex"_sd)}}); } TEST(ExpressionTypeTest, WithSymbolValue) { assertExpectedResults("$type", {{{Value(BSONSymbol("a"))}, Value("symbol"_sd)}}); } TEST(ExpressionTypeTest, WithDBRefValue) { assertExpectedResults("$type", {{{Value(BSONDBRef("", OID()))}, Value("dbPointer"_sd)}}); } TEST(ExpressionTypeTest, WithCodeWScopeValue) { assertExpectedResults( "$type", {{{Value(BSONCodeWScope("var x = 3", BSONObj()))}, Value("javascriptWithScope"_sd)}}); } TEST(ExpressionTypeTest, WithCodeValue) { assertExpectedResults("$type", {{{Value(BSONCode("var x = 3"))}, Value("javascript"_sd)}}); } TEST(ExpressionTypeTest, WithIntValue) { assertExpectedResults("$type", {{{Value(1)}, Value("int"_sd)}}); } TEST(ExpressionTypeTest, WithDecimalValue) { assertExpectedResults("$type", {{{Value(Decimal128(0.3))}, Value("decimal"_sd)}}); } TEST(ExpressionTypeTest, WithLongValue) { assertExpectedResults("$type", {{{Value(1LL)}, Value("long"_sd)}}); } TEST(ExpressionTypeTest, WithTimestampValue) { assertExpectedResults("$type", {{{Value(Timestamp(0, 0))}, Value("timestamp"_sd)}}); } TEST(ExpressionTypeTest, WithMaxKeyValue) { assertExpectedResults("$type", {{{Value(MAXKEY)}, Value("maxKey"_sd)}}); } } // namespace Type namespace IsNumber { TEST(ExpressionIsNumberTest, WithMinKeyValue) { assertExpectedResults("$isNumber", {{{Value(MINKEY)}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithDoubleValue) { assertExpectedResults("$isNumber", {{{Value(1.0)}, Value(true)}}); } TEST(ExpressionIsNumberTest, WithStringValue) { assertExpectedResults("$isNumber", {{{Value("stringValue"_sd)}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithNumericStringValue) { assertExpectedResults("$isNumber", {{{Value("5"_sd)}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithObjectValue) { BSONObj objectVal = fromjson("{a: {$literal: 1}}"); assertExpectedResults("$isNumber", {{{Value(objectVal)}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithArrayValue) { assertExpectedResults("$isNumber", {{{Value(BSON_ARRAY(1 << 2))}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithBinDataValue) { BSONBinData binDataVal = BSONBinData("", 0, BinDataGeneral); assertExpectedResults("$isNumber", {{{Value(binDataVal)}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithUndefinedValue) { assertExpectedResults("$isNumber", {{{Value(BSONUndefined)}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithOIDValue) { assertExpectedResults("$isNumber", {{{Value(OID())}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithBoolValue) { assertExpectedResults("$isNumber", {{{Value(true)}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithDateValue) { Date_t dateVal = BSON("" << DATENOW).firstElement().Date(); assertExpectedResults("$isNumber", {{{Value(dateVal)}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithNullValue) { assertExpectedResults("$isNumber", {{{Value(BSONNULL)}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithRegexValue) { assertExpectedResults("$isNumber", {{{Value(BSONRegEx("a.b"))}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithSymbolValue) { assertExpectedResults("$isNumber", {{{Value(BSONSymbol("a"))}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithDBRefValue) { assertExpectedResults("$isNumber", {{{Value(BSONDBRef("", OID()))}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithCodeWScopeValue) { assertExpectedResults("$isNumber", {{{Value(BSONCodeWScope("var x = 3", BSONObj()))}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithCodeValue) { assertExpectedResults("$isNumber", {{{Value(BSONCode("var x = 3"))}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithIntValue) { assertExpectedResults("$isNumber", {{{Value(1)}, Value(true)}}); } TEST(ExpressionIsNumberTest, WithDecimalValue) { assertExpectedResults("$isNumber", {{{Value(Decimal128(0.3))}, Value(true)}}); } TEST(ExpressionIsNumberTest, WithLongValue) { assertExpectedResults("$isNumber", {{{Value(1LL)}, Value(true)}}); } TEST(ExpressionIsNumberTest, WithTimestampValue) { assertExpectedResults("$isNumber", {{{Value(Timestamp(0, 0))}, Value(false)}}); } TEST(ExpressionIsNumberTest, WithMaxKeyValue) { assertExpectedResults("$isNumber", {{{Value(MAXKEY)}, Value(false)}}); } } // namespace IsNumber namespace BuiltinRemoveVariable { TEST(BuiltinRemoveVariableTest, TypeOfRemoveIsMissing) { assertExpectedResults("$type", {{{Value("$$REMOVE"_sd)}, Value("missing"_sd)}}); } TEST(BuiltinRemoveVariableTest, LiteralEscapesRemoveVar) { assertExpectedResults( "$literal", {{{Value("$$REMOVE"_sd)}, Value(std::vector{Value("$$REMOVE"_sd)})}}); } TEST(BuiltinRemoveVariableTest, RemoveSerializesCorrectly) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; auto expression = ExpressionFieldPath::parse(expCtx, "$$REMOVE", vps); ASSERT_BSONOBJ_EQ(BSON("foo" << "$$REMOVE"), BSON("foo" << expression->serialize(false))); } TEST(BuiltinRemoveVariableTest, RemoveSerializesCorrectlyWithTrailingPath) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; auto expression = ExpressionFieldPath::parse(expCtx, "$$REMOVE.a.b", vps); ASSERT_BSONOBJ_EQ(BSON("foo" << "$$REMOVE.a.b"), BSON("foo" << expression->serialize(false))); } TEST(BuiltinRemoveVariableTest, RemoveSerializesCorrectlyAfterOptimization) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; auto expression = ExpressionFieldPath::parse(expCtx, "$$REMOVE.a.b", vps); auto optimizedExpression = expression->optimize(); ASSERT(dynamic_cast(optimizedExpression.get())); ASSERT_BSONOBJ_EQ(BSON("foo" << "$$REMOVE"), BSON("foo" << optimizedExpression->serialize(false))); } } // namespace BuiltinRemoveVariable /* ------------------------- ExpressionMergeObjects -------------------------- */ namespace ExpressionMergeObjects { TEST(ExpressionMergeObjects, MergingWithSingleObjectShouldLeaveUnchanged) { assertExpectedResults("$mergeObjects", {{{}, {Document({})}}}); auto doc = Document({{"a", 1}, {"b", 1}}); assertExpectedResults("$mergeObjects", {{{doc}, doc}}); } TEST(ExpressionMergeObjects, MergingDisjointObjectsShouldIncludeAllFields) { auto first = Document({{"a", 1}, {"b", 1}}); auto second = Document({{"c", 1}}); assertExpectedResults("$mergeObjects", {{{first, second}, Document({{"a", 1}, {"b", 1}, {"c", 1}})}}); } TEST(ExpressionMergeObjects, MergingIntersectingObjectsShouldOverrideInOrderReceived) { auto first = Document({{"a", "oldValue"_sd}, {"b", 0}, {"c", 1}}); auto second = Document({{"a", "newValue"_sd}}); assertExpectedResults( "$mergeObjects", {{{first, second}, Document({{"a", "newValue"_sd}, {"b", 0}, {"c", 1}})}}); } TEST(ExpressionMergeObjects, MergingIntersectingEmbeddedObjectsShouldOverrideInOrderReceived) { auto firstSubDoc = Document({{"a", 1}, {"b", 2}, {"c", 3}}); auto secondSubDoc = Document({{"a", 2}, {"b", 1}}); auto first = Document({{"d", 1}, {"subDoc", firstSubDoc}}); auto second = Document({{"subDoc", secondSubDoc}}); auto expected = Document({{"d", 1}, {"subDoc", secondSubDoc}}); assertExpectedResults("$mergeObjects", {{{first, second}, expected}}); } TEST(ExpressionMergeObjects, MergingWithEmptyDocumentShouldIgnore) { auto first = Document({{"a", 0}, {"b", 1}, {"c", 1}}); auto second = Document({}); auto expected = Document({{"a", 0}, {"b", 1}, {"c", 1}}); assertExpectedResults("$mergeObjects", {{{first, second}, expected}}); } TEST(ExpressionMergeObjects, MergingSingleArgumentArrayShouldUnwindAndMerge) { std::vector first = {Document({{"a", 1}}), Document({{"a", 2}})}; auto expected = Document({{"a", 2}}); assertExpectedResults("$mergeObjects", {{{first}, expected}}); } TEST(ExpressionMergeObjects, MergingArrayWithDocumentShouldThrowException) { std::vector first = {Document({{"a", 1}}), Document({{"a", 2}})}; auto second = Document({{"b", 2}}); ASSERT_THROWS_CODE( evaluateExpression("$mergeObjects", {first, second}), AssertionException, 40400); } TEST(ExpressionMergeObjects, MergingArrayContainingInvalidTypesShouldThrowException) { std::vector first = {Value(Document({{"validType", 1}})), Value("invalidType"_sd)}; ASSERT_THROWS_CODE(evaluateExpression("$mergeObjects", {first}), AssertionException, 40400); } TEST(ExpressionMergeObjects, MergingNonObjectsShouldThrowException) { ASSERT_THROWS_CODE( evaluateExpression("$mergeObjects", {"invalidArg"_sd}), AssertionException, 40400); ASSERT_THROWS_CODE( evaluateExpression("$mergeObjects", {"invalidArg"_sd, Document({{"validArg", 1}})}), AssertionException, 40400); ASSERT_THROWS_CODE(evaluateExpression("$mergeObjects", {1, Document({{"validArg", 1}})}), AssertionException, 40400); } } // namespace ExpressionMergeObjects namespace ToLower { class ExpectedResultBase { public: virtual ~ExpectedResultBase() {} void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONObj specObj = BSON("" << spec()); BSONElement specElement = specObj.firstElement(); VariablesParseState vps = expCtx->variablesParseState; intrusive_ptr expression = Expression::parseOperand(expCtx, specElement, vps); ASSERT_BSONOBJ_EQ(constify(spec()), expressionToBson(expression)); ASSERT_BSONOBJ_EQ(BSON("" << expectedResult()), toBson(expression->evaluate({}, &expCtx->variables))); } protected: virtual string str() = 0; virtual string expectedResult() = 0; private: BSONObj spec() { return BSON("$toLower" << BSON_ARRAY(str())); } }; /** String beginning with a null character. */ class NullBegin : public ExpectedResultBase { string str() { return string("\0aB", 3); } string expectedResult() { return string("\0ab", 3); } }; /** String containing a null character. */ class NullMiddle : public ExpectedResultBase { string str() { return string("a\0B", 3); } string expectedResult() { return string("a\0b", 3); } }; /** String ending with a null character. */ class NullEnd : public ExpectedResultBase { string str() { return string("aB\0", 3); } string expectedResult() { return string("ab\0", 3); } }; } // namespace ToLower namespace ToUpper { class ExpectedResultBase { public: virtual ~ExpectedResultBase() {} void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONObj specObj = BSON("" << spec()); BSONElement specElement = specObj.firstElement(); VariablesParseState vps = expCtx->variablesParseState; intrusive_ptr expression = Expression::parseOperand(expCtx, specElement, vps); ASSERT_BSONOBJ_EQ(constify(spec()), expressionToBson(expression)); ASSERT_BSONOBJ_EQ(BSON("" << expectedResult()), toBson(expression->evaluate({}, &expCtx->variables))); } protected: virtual string str() = 0; virtual string expectedResult() = 0; private: BSONObj spec() { return BSON("$toUpper" << BSON_ARRAY(str())); } }; /** String beginning with a null character. */ class NullBegin : public ExpectedResultBase { string str() { return string("\0aB", 3); } string expectedResult() { return string("\0AB", 3); } }; /** String containing a null character. */ class NullMiddle : public ExpectedResultBase { string str() { return string("a\0B", 3); } string expectedResult() { return string("A\0B", 3); } }; /** String ending with a null character. */ class NullEnd : public ExpectedResultBase { string str() { return string("aB\0", 3); } string expectedResult() { return string("AB\0", 3); } }; } // namespace ToUpper namespace AllAnyElements { class ExpectedResultBase { public: virtual ~ExpectedResultBase() {} void run() { intrusive_ptr expCtx(new ExpressionContextForTest()); const Document spec = getSpec(); const Value args = spec["input"]; if (!spec["expected"].missing()) { FieldIterator fields(spec["expected"].getDocument()); while (fields.more()) { const Document::FieldPair field(fields.next()); const Value expected = field.second; const BSONObj obj = BSON(field.first << args); VariablesParseState vps = expCtx->variablesParseState; const intrusive_ptr expr = Expression::parseExpression(expCtx, obj, vps); const Value result = expr->evaluate({}, &expCtx->variables); if (ValueComparator().evaluate(result != expected)) { string errMsg = str::stream() << "for expression " << field.first.toString() << " with argument " << args.toString() << " full tree: " << expr->serialize(false).toString() << " expected: " << expected.toString() << " but got: " << result.toString(); FAIL(errMsg); } // TODO test optimize here } } if (!spec["error"].missing()) { const vector& asserters = spec["error"].getArray(); size_t n = asserters.size(); for (size_t i = 0; i < n; i++) { const BSONObj obj = BSON(asserters[i].getString() << args); VariablesParseState vps = expCtx->variablesParseState; ASSERT_THROWS( [&] { // NOTE: parse and evaluatation failures are treated the // same const intrusive_ptr expr = Expression::parseExpression(expCtx, obj, vps); expr->evaluate({}, &expCtx->variables); }(), AssertionException); } } } private: virtual Document getSpec() = 0; }; class JustFalse : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(false)) << "expected" << DOC("$allElementsTrue" << false << "$anyElementTrue" << false)); } }; class JustTrue : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(true)) << "expected" << DOC("$allElementsTrue" << true << "$anyElementTrue" << true)); } }; class OneTrueOneFalse : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(true << false)) << "expected" << DOC("$allElementsTrue" << false << "$anyElementTrue" << true)); } }; class Empty : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(vector()) << "expected" << DOC("$allElementsTrue" << true << "$anyElementTrue" << false)); } }; class TrueViaInt : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(1)) << "expected" << DOC("$allElementsTrue" << true << "$anyElementTrue" << true)); } }; class FalseViaInt : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(DOC_ARRAY(0)) << "expected" << DOC("$allElementsTrue" << false << "$anyElementTrue" << false)); } }; class Null : public ExpectedResultBase { Document getSpec() { return DOC("input" << DOC_ARRAY(BSONNULL) << "error" << DOC_ARRAY("$allElementsTrue"_sd << "$anyElementTrue"_sd)); } }; } // namespace AllAnyElements namespace GetComputedPathsTest { TEST(GetComputedPathsTest, ExpressionFieldPathDoesNotCountAsRenameWhenUsingRemoveBuiltin) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto expr = ExpressionFieldPath::parse(expCtx, "$$REMOVE", expCtx->variablesParseState); auto computedPaths = expr->getComputedPaths("a", Variables::kRootId); ASSERT_EQ(computedPaths.paths.size(), 1u); ASSERT_EQ(computedPaths.paths.count("a"), 1u); ASSERT(computedPaths.renames.empty()); } TEST(GetComputedPathsTest, ExpressionFieldPathDoesNotCountAsRenameWhenOnlyRoot) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto expr = ExpressionFieldPath::parse(expCtx, "$$ROOT", expCtx->variablesParseState); auto computedPaths = expr->getComputedPaths("a", Variables::kRootId); ASSERT_EQ(computedPaths.paths.size(), 1u); ASSERT_EQ(computedPaths.paths.count("a"), 1u); ASSERT(computedPaths.renames.empty()); } TEST(GetComputedPathsTest, ExpressionFieldPathDoesNotCountAsRenameWithNonMatchingUserVariable) { intrusive_ptr expCtx(new ExpressionContextForTest()); expCtx->variablesParseState.defineVariable("userVar"); auto expr = ExpressionFieldPath::parse(expCtx, "$$userVar.b", expCtx->variablesParseState); auto computedPaths = expr->getComputedPaths("a", Variables::kRootId); ASSERT_EQ(computedPaths.paths.size(), 1u); ASSERT_EQ(computedPaths.paths.count("a"), 1u); ASSERT(computedPaths.renames.empty()); } TEST(GetComputedPathsTest, ExpressionFieldPathDoesNotCountAsRenameWhenDotted) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto expr = ExpressionFieldPath::parse(expCtx, "$a.b", expCtx->variablesParseState); auto computedPaths = expr->getComputedPaths("c", Variables::kRootId); ASSERT_EQ(computedPaths.paths.size(), 1u); ASSERT_EQ(computedPaths.paths.count("c"), 1u); ASSERT(computedPaths.renames.empty()); } TEST(GetComputedPathsTest, ExpressionFieldPathDoesCountAsRename) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto expr = ExpressionFieldPath::parse(expCtx, "$a", expCtx->variablesParseState); auto computedPaths = expr->getComputedPaths("b", Variables::kRootId); ASSERT(computedPaths.paths.empty()); ASSERT_EQ(computedPaths.renames.size(), 1u); ASSERT_EQ(computedPaths.renames["b"], "a"); } TEST(GetComputedPathsTest, ExpressionFieldPathDoesCountAsRenameWithExplicitRoot) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto expr = ExpressionFieldPath::parse(expCtx, "$$ROOT.a", expCtx->variablesParseState); auto computedPaths = expr->getComputedPaths("b", Variables::kRootId); ASSERT(computedPaths.paths.empty()); ASSERT_EQ(computedPaths.renames.size(), 1u); ASSERT_EQ(computedPaths.renames["b"], "a"); } TEST(GetComputedPathsTest, ExpressionFieldPathDoesCountAsRenameWithExplicitCurrent) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto expr = ExpressionFieldPath::parse(expCtx, "$$CURRENT.a", expCtx->variablesParseState); auto computedPaths = expr->getComputedPaths("b", Variables::kRootId); ASSERT(computedPaths.paths.empty()); ASSERT_EQ(computedPaths.renames.size(), 1u); ASSERT_EQ(computedPaths.renames["b"], "a"); } TEST(GetComputedPathsTest, ExpressionFieldPathDoesCountAsRenameWithMatchingUserVariable) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto varId = expCtx->variablesParseState.defineVariable("userVar"); auto expr = ExpressionFieldPath::parse(expCtx, "$$userVar.a", expCtx->variablesParseState); auto computedPaths = expr->getComputedPaths("b", varId); ASSERT(computedPaths.paths.empty()); ASSERT_EQ(computedPaths.renames.size(), 1u); ASSERT_EQ(computedPaths.renames["b"], "a"); } TEST(GetComputedPathsTest, ExpressionObjectCorrectlyReportsComputedPaths) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto specObject = fromjson("{a: '$b', c: {$add: [1, 3]}}"); auto expr = Expression::parseObject(expCtx, specObject, expCtx->variablesParseState); ASSERT(dynamic_cast(expr.get())); auto computedPaths = expr->getComputedPaths("d"); ASSERT_EQ(computedPaths.paths.size(), 1u); ASSERT_EQ(computedPaths.paths.count("d.c"), 1u); ASSERT_EQ(computedPaths.renames.size(), 1u); ASSERT_EQ(computedPaths.renames["d.a"], "b"); } TEST(GetComputedPathsTest, ExpressionObjectCorrectlyReportsComputedPathsNested) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto specObject = fromjson( "{a: {b: '$c'}," "d: {$map: {input: '$e', as: 'iter', in: {f: '$$iter.g'}}}}"); auto expr = Expression::parseObject(expCtx, specObject, expCtx->variablesParseState); ASSERT(dynamic_cast(expr.get())); auto computedPaths = expr->getComputedPaths("h"); ASSERT(computedPaths.paths.empty()); ASSERT_EQ(computedPaths.renames.size(), 2u); ASSERT_EQ(computedPaths.renames["h.a.b"], "c"); ASSERT_EQ(computedPaths.renames["h.d.f"], "e.g"); } TEST(GetComputedPathsTest, ExpressionMapCorrectlyReportsComputedPaths) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto specObject = fromjson("{$map: {input: '$a', as: 'iter', in: {b: '$$iter.c', d: {$add: [1, 2]}}}}"); auto expr = Expression::parseObject(expCtx, specObject, expCtx->variablesParseState); ASSERT(dynamic_cast(expr.get())); auto computedPaths = expr->getComputedPaths("e"); ASSERT_EQ(computedPaths.paths.size(), 1u); ASSERT_EQ(computedPaths.paths.count("e.d"), 1u); ASSERT_EQ(computedPaths.renames.size(), 1u); ASSERT_EQ(computedPaths.renames["e.b"], "a.c"); } TEST(GetComputedPathsTest, ExpressionMapCorrectlyReportsComputedPathsWithDefaultVarName) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto specObject = fromjson("{$map: {input: '$a', in: {b: '$$this.c', d: {$add: [1, 2]}}}}"); auto expr = Expression::parseObject(expCtx, specObject, expCtx->variablesParseState); ASSERT(dynamic_cast(expr.get())); auto computedPaths = expr->getComputedPaths("e"); ASSERT_EQ(computedPaths.paths.size(), 1u); ASSERT_EQ(computedPaths.paths.count("e.d"), 1u); ASSERT_EQ(computedPaths.renames.size(), 1u); ASSERT_EQ(computedPaths.renames["e.b"], "a.c"); } TEST(GetComputedPathsTest, ExpressionMapCorrectlyReportsComputedPathsWithNestedExprObject) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto specObject = fromjson("{$map: {input: '$a', in: {b: {c: '$$this.d'}}}}"); auto expr = Expression::parseObject(expCtx, specObject, expCtx->variablesParseState); ASSERT(dynamic_cast(expr.get())); auto computedPaths = expr->getComputedPaths("e"); ASSERT(computedPaths.paths.empty()); ASSERT_EQ(computedPaths.renames.size(), 1u); ASSERT_EQ(computedPaths.renames["e.b.c"], "a.d"); } TEST(GetComputedPathsTest, ExpressionMapNotConsideredRenameWithWrongRootVariable) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto specObject = fromjson("{$map: {input: '$a', as: 'iter', in: {b: '$c'}}}"); auto expr = Expression::parseObject(expCtx, specObject, expCtx->variablesParseState); ASSERT(dynamic_cast(expr.get())); auto computedPaths = expr->getComputedPaths("d"); ASSERT_EQ(computedPaths.paths.size(), 1u); ASSERT_EQ(computedPaths.paths.count("d"), 1u); ASSERT(computedPaths.renames.empty()); } TEST(GetComputedPathsTest, ExpressionMapNotConsideredRenameWithWrongVariableNoExpressionObject) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto specObject = fromjson("{$map: {input: '$a', as: 'iter', in: '$b'}}"); auto expr = Expression::parseObject(expCtx, specObject, expCtx->variablesParseState); ASSERT(dynamic_cast(expr.get())); auto computedPaths = expr->getComputedPaths("d"); ASSERT_EQ(computedPaths.paths.size(), 1u); ASSERT_EQ(computedPaths.paths.count("d"), 1u); ASSERT(computedPaths.renames.empty()); } TEST(GetComputedPathsTest, ExpressionMapNotConsideredRenameWithDottedInputPath) { intrusive_ptr expCtx(new ExpressionContextForTest()); auto specObject = fromjson("{$map: {input: '$a.b', as: 'iter', in: {c: '$$iter.d'}}}}"); auto expr = Expression::parseObject(expCtx, specObject, expCtx->variablesParseState); ASSERT(dynamic_cast(expr.get())); auto computedPaths = expr->getComputedPaths("e"); ASSERT_EQ(computedPaths.paths.size(), 1u); ASSERT_EQ(computedPaths.paths.count("e"), 1u); ASSERT(computedPaths.renames.empty()); } } // namespace GetComputedPathsTest namespace expression_meta_test { TEST(ExpressionMetaTest, ExpressionMetaSearchScore) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; BSONObj expr = fromjson("{$meta: \"searchScore\"}"); auto expressionMeta = ExpressionMeta::parse(expCtx, expr.firstElement(), vps); MutableDocument doc; doc.metadata().setSearchScore(1.234); Value val = expressionMeta->evaluate(doc.freeze(), &expCtx->variables); ASSERT_EQ(val.getDouble(), 1.234); } TEST(ExpressionMetaTest, ExpressionMetaSearchHighlights) { intrusive_ptr expCtx(new ExpressionContextForTest()); VariablesParseState vps = expCtx->variablesParseState; BSONObj expr = fromjson("{$meta: \"searchHighlights\"}"); auto expressionMeta = ExpressionMeta::parse(expCtx, expr.firstElement(), vps); MutableDocument doc; Document highlights = DOC("this part" << 1 << "is opaque to the server" << 1); doc.metadata().setSearchHighlights(Value(highlights)); Value val = expressionMeta->evaluate(doc.freeze(), &expCtx->variables); ASSERT_DOCUMENT_EQ(val.getDocument(), highlights); } TEST(ExpressionMetaTest, ExpressionMetaGeoNearDistance) { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONObj expr = fromjson("{$meta: \"geoNearDistance\"}"); auto expressionMeta = ExpressionMeta::parse(expCtx, expr.firstElement(), expCtx->variablesParseState); MutableDocument doc; doc.metadata().setGeoNearDistance(1.23); Value val = expressionMeta->evaluate(doc.freeze(), &expCtx->variables); ASSERT_EQ(val.getDouble(), 1.23); } TEST(ExpressionMetaTest, ExpressionMetaGeoNearPoint) { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONObj expr = fromjson("{$meta: \"geoNearPoint\"}"); auto expressionMeta = ExpressionMeta::parse(expCtx, expr.firstElement(), expCtx->variablesParseState); MutableDocument doc; Document pointDoc = Document{fromjson("{some: 'document'}")}; doc.metadata().setGeoNearPoint(Value(pointDoc)); Value val = expressionMeta->evaluate(doc.freeze(), &expCtx->variables); ASSERT_DOCUMENT_EQ(val.getDocument(), pointDoc); } TEST(ExpressionMetaTest, ExpressionMetaIndexKey) { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONObj expr = fromjson("{$meta: \"indexKey\"}"); auto expressionMeta = ExpressionMeta::parse(expCtx, expr.firstElement(), expCtx->variablesParseState); MutableDocument doc; BSONObj ixKey = fromjson("{'': 1, '': 'string'}"); doc.metadata().setIndexKey(ixKey); Value val = expressionMeta->evaluate(doc.freeze(), &expCtx->variables); ASSERT_DOCUMENT_EQ(val.getDocument(), Document(ixKey)); } TEST(ExpressionMetaTest, ExpressionMetaRecordId) { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONObj expr = fromjson("{$meta: \"recordId\"}"); auto expressionMeta = ExpressionMeta::parse(expCtx, expr.firstElement(), expCtx->variablesParseState); MutableDocument doc; doc.metadata().setRecordId(RecordId(123LL)); Value val = expressionMeta->evaluate(doc.freeze(), &expCtx->variables); ASSERT_EQ(val.getLong(), 123LL); } TEST(ExpressionMetaTest, ExpressionMetaRandVal) { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONObj expr = fromjson("{$meta: \"randVal\"}"); auto expressionMeta = ExpressionMeta::parse(expCtx, expr.firstElement(), expCtx->variablesParseState); MutableDocument doc; doc.metadata().setRandVal(1.23); Value val = expressionMeta->evaluate(doc.freeze(), &expCtx->variables); ASSERT_EQ(val.getDouble(), 1.23); } TEST(ExpressionMetaTest, ExpressionMetaSortKey) { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONObj expr = fromjson("{$meta: \"sortKey\"}"); auto expressionMeta = ExpressionMeta::parse(expCtx, expr.firstElement(), expCtx->variablesParseState); MutableDocument doc; BSONObj sortKey = BSON("" << 1 << "" << 2); doc.metadata().setSortKey(sortKey); Value val = expressionMeta->evaluate(doc.freeze(), &expCtx->variables); ASSERT_DOCUMENT_EQ(val.getDocument(), Document(sortKey)); } TEST(ExpressionMetaTest, ExpressionMetaTextScore) { intrusive_ptr expCtx(new ExpressionContextForTest()); BSONObj expr = fromjson("{$meta: \"textScore\"}"); auto expressionMeta = ExpressionMeta::parse(expCtx, expr.firstElement(), expCtx->variablesParseState); MutableDocument doc; doc.metadata().setTextScore(1.23); Value val = expressionMeta->evaluate(doc.freeze(), &expCtx->variables); ASSERT_EQ(val.getDouble(), 1.23); } } // namespace expression_meta_test namespace ExpressionRegexTest { class ExpressionRegexTest { public: template static intrusive_ptr generateOptimizedExpression( const BSONObj& input, intrusive_ptr expCtx) { auto expression = ExpressionRegexSubClass::parse( expCtx, input.firstElement(), expCtx->variablesParseState); return expression->optimize(); } static void testAllExpressions(const BSONObj& input, bool optimized, const std::vector& expectedFindAllOutput) { intrusive_ptr expCtx(new ExpressionContextForTest()); { // For $regexFindAll. auto expression = generateOptimizedExpression(input, expCtx); auto regexFindAllExpr = dynamic_cast(expression.get()); ASSERT_EQ(regexFindAllExpr->hasConstantRegex(), optimized); Value output = expression->evaluate({}, &expCtx->variables); ASSERT_VALUE_EQ(output, Value(expectedFindAllOutput)); } { // For $regexFind. auto expression = generateOptimizedExpression(input, expCtx); auto regexFindExpr = dynamic_cast(expression.get()); ASSERT_EQ(regexFindExpr->hasConstantRegex(), optimized); Value output = expression->evaluate({}, &expCtx->variables); ASSERT_VALUE_EQ( output, expectedFindAllOutput.empty() ? Value(BSONNULL) : expectedFindAllOutput[0]); } { // For $regexMatch. auto expression = generateOptimizedExpression(input, expCtx); auto regexMatchExpr = dynamic_cast(expression.get()); ASSERT_EQ(regexMatchExpr->hasConstantRegex(), optimized); Value output = expression->evaluate({}, &expCtx->variables); ASSERT_VALUE_EQ(output, expectedFindAllOutput.empty() ? Value(false) : Value(true)); } } }; TEST(ExpressionRegexTest, BasicTest) { ExpressionRegexTest::testAllExpressions( fromjson("{$regexFindAll : {input: 'asdf', regex: '^as' }}"), true, {Value(fromjson("{match: 'as', idx:0, captures:[]}"))}); } TEST(ExpressionRegexTest, ExtendedRegexOptions) { ExpressionRegexTest::testAllExpressions( fromjson("{$regexFindAll : {input: 'FirstLine\\nSecondLine', regex: " "'^second' , options: 'mi'}}"), true, {Value(fromjson("{match: 'Second', idx:10, captures:[]}"))}); } TEST(ExpressionRegexTest, MultipleMatches) { ExpressionRegexTest::testAllExpressions( fromjson("{$regexFindAll : {input: 'a1b2c3', regex: '([a-c][1-3])' }}"), true, {Value(fromjson("{match: 'a1', idx:0, captures:['a1']}")), Value(fromjson("{match: 'b2', idx:2, captures:['b2']}")), Value(fromjson("{match: 'c3', idx:4, captures:['c3']}"))}); } TEST(ExpressionRegexTest, OptimizPatternWhenInputIsVariable) { ExpressionRegexTest::testAllExpressions( fromjson("{$regexFindAll : {input: '$input', regex: '([a-c][1-3])' }}"), true, {}); } TEST(ExpressionRegexTest, NoOptimizePatternWhenRegexVariable) { ExpressionRegexTest::testAllExpressions( fromjson("{$regexFindAll : {input: 'asdf', regex: '$regex' }}"), false, {}); } TEST(ExpressionRegexTest, NoOptimizePatternWhenOptionsVariable) { ExpressionRegexTest::testAllExpressions( fromjson("{$regexFindAll : {input: 'asdf', regex: '(asdf)', options: '$options' }}"), false, {Value(fromjson("{match: 'asdf', idx:0, captures:['asdf']}"))}); } TEST(ExpressionRegexTest, NoMatch) { ExpressionRegexTest::testAllExpressions( fromjson("{$regexFindAll : {input: 'a1b2c3', regex: 'ab' }}"), true, {}); } TEST(ExpressionRegexTest, FailureCaseBadRegexType) { ASSERT_THROWS_CODE(ExpressionRegexTest::testAllExpressions( fromjson("{$regexFindAll : {input: 'FirstLine\\nSecondLine', regex: " "{invalid : 'regex'} , options: 'mi'}}"), false, {}), AssertionException, 51105); } TEST(ExpressionRegexTest, FailureCaseBadRegexPattern) { ASSERT_THROWS_CODE( ExpressionRegexTest::testAllExpressions( fromjson("{$regexFindAll : {input: 'FirstLine\\nSecondLine', regex: '[0-9'}}"), false, {}), AssertionException, 51111); } TEST(ExpressionRegexTest, InvalidUTF8InInput) { std::string inputField = "1234 "; // Append an invalid UTF-8 character. inputField += '\xe5'; inputField += " 1234"; BSONObj input(fromjson("{$regexFindAll: {input: '" + inputField + "', regex: '[0-9]'}}")); // Verify that PCRE will error during execution if input is not a valid UTF-8. ASSERT_THROWS_CODE( ExpressionRegexTest::testAllExpressions(input, true, {}), AssertionException, 51156); } TEST(ExpressionRegexTest, InvalidUTF8InRegex) { std::string regexField = "1234 "; // Append an invalid UTF-8 character. regexField += '\xe5'; BSONObj input(fromjson("{$regexFindAll: {input: '123456', regex: '" + regexField + "'}}")); // Verify that PCRE will error if REGEX is not a valid UTF-8. ASSERT_THROWS_CODE( ExpressionRegexTest::testAllExpressions(input, false, {}), AssertionException, 51111); } } // namespace ExpressionRegexTest class All : public Suite { public: All() : Suite("expression") {} void setupTests() { add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); add(); } }; SuiteInstance myall; namespace NowAndClusterTime { TEST(NowAndClusterTime, BasicTest) { intrusive_ptr expCtx(new ExpressionContextForTest()); // $$NOW is the Date type. { auto expression = ExpressionFieldPath::parse(expCtx, "$$NOW", expCtx->variablesParseState); Value result = expression->evaluate(Document(), &(expCtx->variables)); ASSERT_EQ(result.getType(), Date); } // $$CLUSTER_TIME is the timestamp type. { auto expression = ExpressionFieldPath::parse(expCtx, "$$CLUSTER_TIME", expCtx->variablesParseState); Value result = expression->evaluate(Document(), &(expCtx->variables)); ASSERT_EQ(result.getType(), bsonTimestamp); } // Multiple references to $$NOW must return the same value. { auto expression = Expression::parseExpression( expCtx, fromjson("{$eq: [\"$$NOW\", \"$$NOW\"]}"), expCtx->variablesParseState); Value result = expression->evaluate(Document(), &(expCtx->variables)); ASSERT_VALUE_EQ(result, Value{true}); } // Same is true for the $$CLUSTER_TIME. { auto expression = Expression::parseExpression(expCtx, fromjson("{$eq: [\"$$CLUSTER_TIME\", \"$$CLUSTER_TIME\"]}"), expCtx->variablesParseState); Value result = expression->evaluate(Document(), &(expCtx->variables)); ASSERT_VALUE_EQ(result, Value{true}); } } } // namespace NowAndClusterTime } // namespace ExpressionTests