[SERVER-62362] Hints are allowed on special indexes like 2d and text but lead to incorrect results Created: 05/Jan/22  Updated: 14/Apr/22

Status: Open
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Blocker - P1
Reporter: Mathias Stearn Assignee: Katya Kamenieva
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-18449 Hinting sparse index without sparse-c... Backlog
is related to SERVER-26413 Hinting an incompatible partial index... Backlog
Operating System: ALL
Participants:

 Description   

We should do one of A) disallow these hints, B) ignore the hints, C) (somehow) make them produce the correct query results. I strongly prefer option A or B with a slight preference for A over B.

> db.foo.insert({a:1})
WriteResult({ "nInserted" : 1 })
> db.foo.createIndex({x: '2d'})
{
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"createdCollectionAutomatically" : false,
	"ok" : 1
}
> db.foo.createIndex({x: '2dsphere'})
{
	"numIndexesBefore" : 2,
	"numIndexesAfter" : 3,
	"createdCollectionAutomatically" : false,
	"ok" : 1
}
> db.foo.createIndex({x: 'text'})
{
	"numIndexesBefore" : 3,
	"numIndexesAfter" : 4,
	"createdCollectionAutomatically" : false,
	"ok" : 1
}
 
// finds a doc
> db.foo.find({a:1})
{ "_id" : ObjectId("61d1143d15ce86cd9a31d385"), "a" : 1 }
 
// None of these find the doc
> db.foo.find({a:1}).hint({x: '2d'})
> db.foo.find({a:1}).hint({x: '2dsphere'})
> db.foo.find({a:1}).hint('x_text')
>

I didn't test with "somewhat special" indexes like partial indexes or filtered indexes, but they may have the same issue.



 Comments   
Comment by David Storch [ 07/Jan/22 ]

I'm marking this ticket as related to SERVER-26413, which describes a similar issue for partial indexes, and SERVER-18449, which describes a similar issue for sparse indexes.

Comment by James Wahlin [ 05/Jan/22 ]

Sparse indexes have the same issue:

> db.test.insert({x: null})
WriteResult({ "nInserted" : 1 })
> db.test.insert({})
WriteResult({ "nInserted" : 1 })
> db.test.createIndex({x: 1}, {sparse: true})
{
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"createdCollectionAutomatically" : false,
	"ok" : 1
}
> db.test.find({x: null})
{ "_id" : ObjectId("61d5d10844807525e4080055"), "x" : null }
{ "_id" : ObjectId("61d5d10e44807525e4080056") }
> db.test.find({x: null}).hint({x: 1})
{ "_id" : ObjectId("61d5d10844807525e4080055"), "x" : null }
> 

I agree that it would be reasonable to not allow hints to affect query correctness.

Comment by Mathias Stearn [ 05/Jan/22 ]

james.wahlin I realized that I actually meant sparse rather than (or in addition to) partial indexes. Do sparse indexes also have the same issue?

It seems like a reasonable rule should be that hint() is not allowed to change the results of a query, only how it is executed. (This obviously only applies to a single snapshot/version or to a quiescent system, so we don't consider it a problem if the altered query plan changes which concurrent modifications you do or do not observe)

Comment by James Wahlin [ 05/Jan/22 ]

Partial / filtered indexes are in the same position and this has been known/accepted behavior. Definitely worth revisiting and also considering whether text and geo indexes should be handled in the same way.

Generated at Thu Feb 08 05:54:55 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.