diff options
author | Dan Pasette <dan@10gen.com> | 2015-09-11 17:38:51 -0400 |
---|---|---|
committer | Dan Pasette <dan@mongodb.com> | 2015-09-14 10:31:12 -0400 |
commit | fd23b6ac8500f5d8ef759e0a1064f5f396a1fcc2 (patch) | |
tree | 43a129683d5d45fdb0cbd4e2de4bfca77ff2478d /src/mongo/db/query | |
parent | 21c6a574d2cd497089d41d1b418fdfdabf7fc953 (diff) | |
download | mongo-fd23b6ac8500f5d8ef759e0a1064f5f396a1fcc2.tar.gz |
SERVER-20347 $in containing a regex element cannot use a hashed index
Diffstat (limited to 'src/mongo/db/query')
-rw-r--r-- | src/mongo/db/query/planner_ixselect.cpp | 9 | ||||
-rw-r--r-- | src/mongo/db/query/query_planner_test.cpp | 16 |
2 files changed, 24 insertions, 1 deletions
diff --git a/src/mongo/db/query/planner_ixselect.cpp b/src/mongo/db/query/planner_ixselect.cpp index 0b274c69ebf..6cb0062befc 100644 --- a/src/mongo/db/query/planner_ixselect.cpp +++ b/src/mongo/db/query/planner_ixselect.cpp @@ -236,7 +236,14 @@ bool QueryPlannerIXSelect::compatible(const BSONElement& elt, invariant(0); return true; } else if (IndexNames::HASHED == indexedFieldType) { - return exprtype == MatchExpression::MATCH_IN || exprtype == MatchExpression::EQ; + if (exprtype == MatchExpression::EQ) { + return true; + } + if (exprtype == MatchExpression::MATCH_IN) { + const InMatchExpression* expr = static_cast<const InMatchExpression*>(node); + return expr->getData().numRegexes() == 0; + } + return false; } else if (IndexNames::GEO_2DSPHERE == indexedFieldType) { if (exprtype == MatchExpression::GEO) { // within or intersect. diff --git a/src/mongo/db/query/query_planner_test.cpp b/src/mongo/db/query/query_planner_test.cpp index 3d3a07e9e45..fbb7be3069f 100644 --- a/src/mongo/db/query/query_planner_test.cpp +++ b/src/mongo/db/query/query_planner_test.cpp @@ -65,6 +65,15 @@ TEST_F(QueryPlannerTest, EqualityIndexScanWithTrailingFields) { assertSolutionExists("{fetch: {filter: null, node: {ixscan: {pattern: {x: 1, y: 1}}}}}"); } +// $eq can use a hashed index because it looks for values of type regex; +// it doesn't evaluate the regex itself. +TEST_F(QueryPlannerTest, EqCanUseHashedIndexWithRegex) { + addIndex(BSON("a" + << "hashed")); + runQuery(fromjson("{a: {$eq: /abc/}}")); + ASSERT_EQUALS(getNumSolutions(), 2U); +} + // // indexFilterApplied // Check that index filter flag is passed from planner params @@ -1983,6 +1992,13 @@ TEST_F(QueryPlannerTest, InWithSortAndLimitTrailingField) { " {ixscan: {pattern: {a:1,b:-1,c:1}}}]}}}}}}"); } +TEST_F(QueryPlannerTest, InCantUseHashedIndexWithRegex) { + addIndex(BSON("a" + << "hashed")); + runQuery(fromjson("{a: {$in: [/abc/]}}")); + ASSERT_EQUALS(getNumSolutions(), 1U); +} + // // Multiple solutions // |