diff options
author | James Wahlin <james@mongodb.com> | 2017-07-25 08:59:47 -0400 |
---|---|---|
committer | James Wahlin <james@mongodb.com> | 2017-08-09 16:29:49 -0400 |
commit | 2087f94e8c6d7226a4dd48155082034ae51c3cfd (patch) | |
tree | 6a6dac53fe93bc29a174e008c26f2d6daf474a05 /src/mongo/db/matcher/expression_leaf_test.cpp | |
parent | c1aee1915536be2d1928d1098e133acbfdc3a575 (diff) | |
download | mongo-2087f94e8c6d7226a4dd48155082034ae51c3cfd.tar.gz |
SERVER-30046 Support $expr constant expression in ComparisonMatchExpression
Diffstat (limited to 'src/mongo/db/matcher/expression_leaf_test.cpp')
-rw-r--r-- | src/mongo/db/matcher/expression_leaf_test.cpp | 108 |
1 files changed, 107 insertions, 1 deletions
diff --git a/src/mongo/db/matcher/expression_leaf_test.cpp b/src/mongo/db/matcher/expression_leaf_test.cpp index 3d1c87fbd26..af279e5d933 100644 --- a/src/mongo/db/matcher/expression_leaf_test.cpp +++ b/src/mongo/db/matcher/expression_leaf_test.cpp @@ -35,6 +35,7 @@ #include "mongo/db/matcher/expression.h" #include "mongo/db/matcher/expression_leaf.h" #include "mongo/db/matcher/expression_parser.h" +#include "mongo/db/pipeline/aggregation_context_fixture.h" #include "mongo/db/query/collation/collator_interface_mock.h" namespace mongo { @@ -96,10 +97,31 @@ TEST(EqOp, MatchesElement) { ASSERT(eq.equivalent(&eq)); } +TEST(EqOp, ConstantAggExprMatchesElement) { + BSONObj operand = BSON("a" << BSON("$expr" + << "$$userVar")); + BSONObj match = BSON("a" << 5); + BSONObj notMatch = BSON("a" << 6); + + const boost::intrusive_ptr<ExpressionContextForTest> expCtx(new ExpressionContextForTest()); + auto varId = expCtx->variablesParseState.defineVariable("userVar"); + expCtx->variables.setValue(varId, Value(5)); + auto expr = Expression::parseOperand( + expCtx, operand.firstElement()["$expr"], expCtx->variablesParseState); + expr = expr->optimize(); + + EqualityMatchExpression eq; + ASSERT_OK(eq.init("a", expr)); + ASSERT(eq.matchesSingleElement(match.firstElement())); + ASSERT_FALSE(eq.matchesSingleElement(notMatch.firstElement())); + + ASSERT(eq.equivalent(&eq)); +} + TEST(EqOp, InvalidEooOperand) { BSONObj operand; EqualityMatchExpression eq; - ASSERT(!eq.init("", operand.firstElement()).isOK()); + ASSERT_FALSE(eq.init("", operand.firstElement()).isOK()); } TEST(EqOp, MatchesScalar) { @@ -418,6 +440,27 @@ TEST(LtOp, ElemMatchKey) { ASSERT_EQUALS("1", details.elemMatchKey()); } +TEST(LtOp, ConstantAggExprMatchesElement) { + BSONObj operand = BSON("a" << BSON("$lt" << BSON("$expr" + << "$$userVar"))); + BSONObj match = BSON("a" << 5); + BSONObj notMatch = BSON("a" << 10); + + const boost::intrusive_ptr<ExpressionContextForTest> expCtx(new ExpressionContextForTest()); + auto varId = expCtx->variablesParseState.defineVariable("userVar"); + expCtx->variables.setValue(varId, Value(6)); + auto expr = Expression::parseOperand( + expCtx, operand.firstElement()["$lt"]["$expr"], expCtx->variablesParseState); + expr = expr->optimize(); + + LTMatchExpression lt; + ASSERT_OK(lt.init("a", expr)); + ASSERT(lt.matchesSingleElement(match.firstElement())); + ASSERT_FALSE(lt.matchesSingleElement(notMatch.firstElement())); + + ASSERT(lt.equivalent(<)); +} + /** TEST( LtOp, MatchesIndexKeyScalar ) { BSONObj operand = BSON( "$lt" << 6 ); @@ -483,6 +526,27 @@ TEST(LteOp, MatchesElement) { ASSERT(!lte.matchesSingleElement(notMatchWrongType.firstElement())); } +TEST(LteOp, ConstantAggExprMatchesElement) { + BSONObj operand = BSON("a" << BSON("$lte" << BSON("$expr" + << "$$userVar"))); + BSONObj match = BSON("a" << 5); + BSONObj notMatch = BSON("a" << 10); + + const boost::intrusive_ptr<ExpressionContextForTest> expCtx(new ExpressionContextForTest()); + auto varId = expCtx->variablesParseState.defineVariable("userVar"); + expCtx->variables.setValue(varId, Value(6)); + auto expr = Expression::parseOperand( + expCtx, operand.firstElement()["$lte"]["$expr"], expCtx->variablesParseState); + expr = expr->optimize(); + + LTEMatchExpression lte; + ASSERT_OK(lte.init("a", expr)); + ASSERT(lte.matchesSingleElement(match.firstElement())); + ASSERT_FALSE(lte.matchesSingleElement(notMatch.firstElement())); + + ASSERT(lte.equivalent(<e)); +} + TEST(LteOp, InvalidEooOperand) { BSONObj operand; LTEMatchExpression lte; @@ -737,6 +801,27 @@ TEST(GtOp, ElemMatchKey) { ASSERT_EQUALS("1", details.elemMatchKey()); } +TEST(GtOp, ConstantAggExprMatchesElement) { + BSONObj operand = BSON("a" << BSON("$gt" << BSON("$expr" + << "$$userVar"))); + BSONObj match = BSON("a" << 10); + BSONObj notMatch = BSON("a" << 0); + + const boost::intrusive_ptr<ExpressionContextForTest> expCtx(new ExpressionContextForTest()); + auto varId = expCtx->variablesParseState.defineVariable("userVar"); + expCtx->variables.setValue(varId, Value(5)); + auto expr = Expression::parseOperand( + expCtx, operand.firstElement()["$gt"]["$expr"], expCtx->variablesParseState); + expr = expr->optimize(); + + GTMatchExpression gt; + ASSERT_OK(gt.init("a", expr)); + ASSERT(gt.matchesSingleElement(match.firstElement())); + ASSERT_FALSE(gt.matchesSingleElement(notMatch.firstElement())); + + ASSERT(gt.equivalent(>)); +} + /** TEST( GtOp, MatchesIndexKeyScalar ) { BSONObj operand = BSON( "$gt" << 6 ); @@ -897,6 +982,27 @@ TEST(GteOp, ElemMatchKey) { ASSERT_EQUALS("1", details.elemMatchKey()); } +TEST(GteOp, ConstantAggExprMatchesElement) { + BSONObj operand = BSON("a" << BSON("$gte" << BSON("$expr" + << "$$userVar"))); + BSONObj match = BSON("a" << 10); + BSONObj notMatch = BSON("a" << 0); + + const boost::intrusive_ptr<ExpressionContextForTest> expCtx(new ExpressionContextForTest()); + auto varId = expCtx->variablesParseState.defineVariable("userVar"); + expCtx->variables.setValue(varId, Value(5)); + auto expr = Expression::parseOperand( + expCtx, operand.firstElement()["$gte"]["$expr"], expCtx->variablesParseState); + expr = expr->optimize(); + + GTEMatchExpression gte; + ASSERT_OK(gte.init("a", expr)); + ASSERT(gte.matchesSingleElement(match.firstElement())); + ASSERT_FALSE(gte.matchesSingleElement(notMatch.firstElement())); + + ASSERT(gte.equivalent(>e)); +} + TEST(RegexMatchExpression, MatchesElementExact) { BSONObj match = BSON("a" << "b"); |