Skip to content
Browse files

SERVER-4665 account for $ne bounds in QueryPattern

  • Loading branch information...
1 parent 1392e88 commit f08b9980df7667642af651a02a3bffd748b82072 @astaple astaple committed
Showing with 35 additions and 5 deletions.
  1. +12 −0 jstests/queryoptimizer6.js
  2. +4 −0 src/mongo/db/querypattern.cpp
  3. +2 −1 src/mongo/db/querypattern.h
  4. +17 −4 src/mongo/dbtests/queryutiltests.cpp
View
12 jstests/queryoptimizer6.js
@@ -0,0 +1,12 @@
+// Test that $ne constraints are accounted for in QueryPattern. SERVER-4665
+
+t = db.jstests_queryoptimizer6;
+t.drop();
+
+t.save( {a:1} );
+t.ensureIndex( {b:1}, {sparse:true} );
+
+// The sparse index will be used, and recorded for this query pattern.
+assert.eq( 0, t.find( {a:1,b:{$ne:1}} ).itcount() );
+// The query pattern should be different, and the sparse index should not be used.
+assert.eq( 1, t.find( {a:1} ).itcount() );
View
4 src/mongo/db/querypattern.cpp
@@ -37,6 +37,8 @@ namespace mongo {
_fieldTypes[ i->first ] = QueryPattern::UpperBound;
else if ( lower )
_fieldTypes[ i->first ] = QueryPattern::LowerBound;
+ else
+ _fieldTypes[ i->first ] = QueryPattern::ConstraintPresent;
}
}
setSort( sort );
@@ -65,6 +67,8 @@ namespace mongo {
return "UpperBound";
case QueryPattern::UpperAndLowerBound:
return "UpperAndLowerBound";
+ case QueryPattern::ConstraintPresent:
+ return "ConstraintPresent";
}
return "";
}
View
3 src/mongo/db/querypattern.h
@@ -37,7 +37,8 @@ namespace mongo {
Equality,
LowerBound,
UpperBound,
- UpperAndLowerBound
+ UpperAndLowerBound,
+ ConstraintPresent
};
bool operator<( const QueryPattern &other ) const;
/** for testing only */
View
21 src/mongo/dbtests/queryutiltests.cpp
@@ -248,7 +248,14 @@ namespace QueryUtilTests {
}
};
- class QueryPatternTest {
+ class QueryPatternBase {
+ protected:
+ static QueryPattern p( const BSONObj &query, const BSONObj &sort = BSONObj() ) {
+ return FieldRangeSet( "", query, true ).pattern( sort );
+ }
+ };
+
+ class QueryPatternTest : public QueryPatternBase {
public:
void run() {
ASSERT( p( BSON( "a" << 1 ) ) == p( BSON( "a" << 1 ) ) );
@@ -268,9 +275,14 @@ namespace QueryUtilTests {
ASSERT( p( BSON( "a" << 1 ), BSON( "b" << 1 << "c" << 1 ) ) != p( BSON( "a" << 4 ), BSON( "b" << 1 ) ) );
ASSERT( p( BSON( "a" << 1 ), BSON( "b" << 1 ) ) != p( BSON( "a" << 4 ), BSON( "b" << 1 << "c" << 1 ) ) );
}
- private:
- static QueryPattern p( const BSONObj &query, const BSONObj &sort = BSONObj() ) {
- return FieldRangeSet( "", query, true ).pattern( sort );
+ };
+
+ class QueryPatternNeConstraint : public QueryPatternBase {
+ public:
+ void run() {
+ ASSERT( p( BSON( "a" << NE << 5 ) ) != p( BSON( "a" << GT << 1 ) ) );
+ ASSERT( p( BSON( "a" << NE << 5 ) ) != p( BSONObj() ) );
+ ASSERT( p( BSON( "a" << NE << 5 ) ) == p( BSON( "a" << NE << "a" ) ) );
}
};
@@ -913,6 +925,7 @@ namespace QueryUtilTests {
add< FieldRangeTests::Equality >();
add< FieldRangeTests::SimplifiedQuery >();
add< FieldRangeTests::QueryPatternTest >();
+ add< FieldRangeTests::QueryPatternNeConstraint >();
add< FieldRangeTests::NoWhere >();
add< FieldRangeTests::Numeric >();
add< FieldRangeTests::InLowerBound >();

0 comments on commit f08b998

Please sign in to comment.
Something went wrong with that request. Please try again.