diff options
author | George Wangensteen <george.wangensteen@mongodb.com> | 2020-09-10 21:05:01 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-09-23 20:01:33 +0000 |
commit | 348db4a43b912824154dfe03e7b9559d5c464b7c (patch) | |
tree | a17c80548c5c53712a1051863e72f300294bd33e /src/mongo/db/cst/cst_match_translation_test.cpp | |
parent | 6e8989d533886a774700689b46b90542b569c5eb (diff) | |
download | mongo-348db4a43b912824154dfe03e7b9559d5c464b7c.tar.gz |
SERVER-48850 Implement element and \$comment operators in CST
Diffstat (limited to 'src/mongo/db/cst/cst_match_translation_test.cpp')
-rwxr-xr-x | src/mongo/db/cst/cst_match_translation_test.cpp | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/src/mongo/db/cst/cst_match_translation_test.cpp b/src/mongo/db/cst/cst_match_translation_test.cpp index 52cc8bac19a..63019eefb3c 100755 --- a/src/mongo/db/cst/cst_match_translation_test.cpp +++ b/src/mongo/db/cst/cst_match_translation_test.cpp @@ -177,5 +177,119 @@ TEST(CstMatchTranslationTest, TranslatesNestedLogicalTreeExpressions) { } } +TEST(CstMatchTranslationTest, TranslatesExistsBool) { + { + auto input = fromjson("{filter: {a: {$exists: true}}}"); + auto cst = parseMatchToCst(input); + auto match = cst_match_translation::translateMatchExpression(cst, getExpCtx()); + ASSERT_EQ(match->serialize().toString(), "{ $and: [ { a: { $exists: true } } ] }"); + } + { + auto input = fromjson("{filter: {a: {$exists: false}}}"); + auto cst = parseMatchToCst(input); + auto match = cst_match_translation::translateMatchExpression(cst, getExpCtx()); + ASSERT_EQ(match->serialize().toString(), + "{ $and: [ { a: { $not: { $exists: true } } } ] }"); + } +} + +TEST(CstMatchTranslationTest, TranslatesExistsNumeric) { + { + auto input = fromjson("{filter: {a: {$exists: 15.0}}}"); + auto cst = parseMatchToCst(input); + auto match = cst_match_translation::translateMatchExpression(cst, getExpCtx()); + ASSERT_EQ(match->serialize().toString(), "{ $and: [ { a: { $exists: true } } ] }"); + } + { + auto input = fromjson("{filter: {a: {$exists: 0}}}"); + auto cst = parseMatchToCst(input); + auto match = cst_match_translation::translateMatchExpression(cst, getExpCtx()); + ASSERT_EQ(match->serialize().toString(), + "{ $and: [ { a: { $not: { $exists: true } } } ] }"); + } +} + +TEST(CstMatchTranslationTest, TranslatesExistsNullAndCompound) { + { + auto input = fromjson("{filter: {a: {$exists: null}}}"); + auto cst = parseMatchToCst(input); + auto match = cst_match_translation::translateMatchExpression(cst, getExpCtx()); + ASSERT_EQ(match->serialize().toString(), + "{ $and: [ { a: { $not: { $exists: true } } } ] }"); + } + { + auto input = fromjson("{filter: {a: {$exists: [\"arbitrary stuff\", null]}}}"); + auto cst = parseMatchToCst(input); + auto match = cst_match_translation::translateMatchExpression(cst, getExpCtx()); + ASSERT_EQ(match->serialize().toString(), "{ $and: [ { a: { $exists: true } } ] }"); + } + { + auto input = fromjson("{filter: {a: {$exists: {doesnt: \"matter\"}}}}"); + auto cst = parseMatchToCst(input); + auto match = cst_match_translation::translateMatchExpression(cst, getExpCtx()); + ASSERT_EQ(match->serialize().toString(), "{ $and: [ { a: { $exists: true } } ] }"); + } +} + +TEST(CstMatchTranslationTest, TranslatesType) { + { + auto input = fromjson("{filter: {a: {$type: 1}}}"); + auto cst = parseMatchToCst(input); + auto match = cst_match_translation::translateMatchExpression(cst, getExpCtx()); + ASSERT_EQ(match->serialize().toString(), "{ $and: [ { a: { $type: [ 1 ] } } ] }"); + } + { + auto input = fromjson("{filter: {a: {$type: \"number\"}}}"); + auto cst = parseMatchToCst(input); + auto match = cst_match_translation::translateMatchExpression(cst, getExpCtx()); + ASSERT_EQ(match->serialize().toString(), "{ $and: [ { a: { $type: [ \"number\" ] } } ] }"); + // The compound "number" alias is not translated; instead the allNumbers flag of the typeset + // used by the MatchExpression is set. + auto andExpr = dynamic_cast<AndMatchExpression*>(match.get()); + ASSERT(andExpr); + ASSERT_EQ(1, andExpr->numChildren()); + auto type_match = dynamic_cast<TypeMatchExpression*>(andExpr->getChild(0)); + ASSERT(type_match); + ASSERT(type_match->typeSet().allNumbers); + } + { + auto input = fromjson("{filter: {a: {$type: [ \"number\", \"string\", 11]}}}"); + auto cst = parseMatchToCst(input); + auto match = cst_match_translation::translateMatchExpression(cst, getExpCtx()); + ASSERT_EQ(match->serialize().toString(), + "{ $and: [ { a: { $type: [ \"number\", 2, 11 ] } } ] }"); + // Direct type aliases (like "string" --> BSONType 2) are translated into their numeric + // type. + auto andExpr = dynamic_cast<AndMatchExpression*>(match.get()); + ASSERT(andExpr); + ASSERT_EQ(1, andExpr->numChildren()); + auto type_match = dynamic_cast<TypeMatchExpression*>(andExpr->getChild(0)); + ASSERT(type_match->typeSet().allNumbers); + } +} + +TEST(CstMatchTranslationTest, TranslatesComment) { + { + auto input = fromjson("{filter: {a: 1, $comment: \"hello, world\"}}}"); + auto cst = parseMatchToCst(input); + auto match = cst_match_translation::translateMatchExpression(cst, getExpCtx()); + ASSERT_EQ(match->serialize().toString(), "{ $and: [ { a: { $eq: 1 } } ] }"); + } + { + auto input = fromjson("{filter: {$comment: \"hello, world\"}}}"); + auto cst = parseMatchToCst(input); + auto match = cst_match_translation::translateMatchExpression(cst, getExpCtx()); + auto andExpr = dynamic_cast<AndMatchExpression*>(match.get()); + ASSERT(andExpr); + ASSERT_EQ(0, andExpr->numChildren()); + } + { + auto input = fromjson("{filter: {a: {$exists: true}, $comment: \"hello, world\"}}}"); + auto cst = parseMatchToCst(input); + auto match = cst_match_translation::translateMatchExpression(cst, getExpCtx()); + ASSERT_EQ(match->serialize().toString(), "{ $and: [ { a: { $exists: true } } ] }"); + } +} + } // namespace } // namespace mongo |