[SERVER-17978] Sparse indexes can be assigned to incompatible predicates when query plan is already cached Created: 10/Apr/15  Updated: 19/Sep/15  Resolved: 07/May/15

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: 3.1.3

Type: Bug Priority: Major - P3
Reporter: J Rassi Assignee: J Rassi
Resolution: Done Votes: 0
Labels: UT
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: Quint Iteration 3.1.2, Quint Iteration 3
Participants:

 Description   

Sparse indexes are considered incompatible with equality predicates that compare to null. This prevents the query {a: null} from selecting a sparse index {a: 1}, as documents that do not contain the field "a" (such as the empty document) would incorrectly be withheld from this query were it to use this index.

However, this compatibility check is not performed on assignments created from the plan cache. This can lead to missing query results.

To illustrate, see the following code snippet. The assertion on the last line fails.

db.foo.drop();
 
db.foo.insert({});
db.foo.insert({a: 1, b: 1});
db.foo.insert({a: 1, b: 2});
 
db.foo.ensureIndex({a: 1});
db.foo.ensureIndex({a: 1, b: 1}, {sparse: true});
 
assert.eq(1, db.foo.find({a: null, b: null}).itcount()); // Picks/caches "a_1" ("a_1_b_1" is incompatible).
db.foo.getPlanCache().clear();
assert.eq(1, db.foo.find({a: 1, b: 2}).itcount()); // Picks/caches "a_1_b_1".
assert.eq(1, db.foo.find({a: null, b: null}).itcount()); // Uses cached plan, assertion fails.



 Comments   
Comment by Githook User [ 07/May/15 ]

Author:

{u'username': u'jrassi', u'name': u'Jason Rassi', u'email': u'rassi@10gen.com'}

Message: SERVER-17978 Sparse index discriminator for PlanCacheIndexabilityState

Adds additional encoding to the plan cache key for {$eq: null}
predicates (and equivalent) if a sparse index is present for the field
in question.
Branch: master
https://github.com/mongodb/mongo/commit/24b21e0a205ef2cea090105ea634018a3d8343f9

Generated at Thu Feb 08 03:46:11 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.