[SERVER-41263] Disallow the empty string as an index key type Created: 21/May/19  Updated: 29/Oct/23  Resolved: 14/Jun/19

Status: Closed
Project: Core Server
Component/s: Index Maintenance
Affects Version/s: None
Fix Version/s: 4.3.1, 4.2.3

Type: Bug Priority: Major - P3
Reporter: Chris Harris Assignee: Xin Hao Zhang (Inactive)
Resolution: Fixed Votes: 0
Labels: neweng, schwerin-mms-impact
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Problem/Incident
Backwards Compatibility: Minor Change
Operating System: ALL
Backport Requested:
v4.2
Steps To Reproduce:

db.repro.createIndex({x:""})

Outputs:

> db.repro.createIndex({x:""})
{
	"createdCollectionAutomatically" : true,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}

A few additional lines from 4.1.10:

> db.version()
4.1.10
> db.repro.createIndex({x:""})
{
	"createdCollectionAutomatically" : true,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}
> db.repro.createIndex({x:1})
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 2,
	"numIndexesAfter" : 3,
	"ok" : 1
}
> db.repro.getIndices()
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_",
		"ns" : "repo.repro"
	},
	{
		"v" : 2,
		"key" : {
			"x" : ""
		},
		"name" : "x_",
		"ns" : "repo.repro"
	},
	{
		"v" : 2,
		"key" : {
			"x" : 1
		},
		"name" : "x_1",
		"ns" : "repo.repro"
	}
]

Sprint: Query 2019-06-17
Participants:
Case:

 Description   

At present it is possible to generate an index with an empty string as the key type (eg a definition of {x:""}).  The optimizer seems to be able to use this index when the key is a predicate in the query:

 

> db.repro.find({x:123}).explain()
{
	"queryPlanner" : {
		"plannerVersion" : 1,
		"namespace" : "repro.repro",
		"indexFilterSet" : false,
		"parsedQuery" : {
			"x" : {
				"$eq" : 123
			}
		},
		"queryHash" : "716F281A",
		"planCacheKey" : "0FA0E5FD",
		"winningPlan" : {
			"stage" : "FETCH",
			"inputStage" : {
				"stage" : "IXSCAN",
				"keyPattern" : {
					"x" : ""
				},
				"indexName" : "x_",
				"isMultiKey" : false,
				"multiKeyPaths" : {
					"x" : [ ]
				},
				"isUnique" : false,
				"isSparse" : false,
				"isPartial" : false,
				"indexVersion" : 2,
				"direction" : "forward",
				"indexBounds" : {
					"x" : [
						"[123.0, 123.0]"
					]
				}
			}
		},
		"rejectedPlans" : [ ]
	},
	"serverInfo" : {
		"host" : "Chriss-MacBook-Pro.local",
		"port" : 27017,
		"version" : "4.1.10",
		"gitVersion" : "8cdc51e7810f7fd8898a4c60b935e389f04659ee"
	},
	"ok" : 1
} 

However it is unable to use the index to sort:

> db.repro.find({x:123}).sort({x:1}).explain()
{
	"queryPlanner" : {
		"plannerVersion" : 1,
		"namespace" : "repro.repro",
		"indexFilterSet" : false,
		"parsedQuery" : {
			"x" : {
				"$eq" : 123
			}
		},
		"queryHash" : "87B348CE",
		"planCacheKey" : "F46EA305",
		"winningPlan" : {
			"stage" : "FETCH",
			"inputStage" : {
				"stage" : "SORT",
				"sortPattern" : {
					"x" : 1
				},
				"inputStage" : {
					"stage" : "SORT_KEY_GENERATOR",
					"inputStage" : {
						"stage" : "IXSCAN",
						"keyPattern" : {
							"x" : ""
						},
						"indexName" : "x_",
						"isMultiKey" : false,
						"multiKeyPaths" : {
							"x" : [ ]
						},
						"isUnique" : false,
						"isSparse" : false,
						"isPartial" : false,
						"indexVersion" : 2,
						"direction" : "forward",
						"indexBounds" : {
							"x" : [
								"[123.0, 123.0]"
							]
						}
					}
				}
			}
		},
		"rejectedPlans" : [ ]
	},
	"serverInfo" : {
		"host" : "Chriss-MacBook-Pro.local",
		"port" : 27017,
		"version" : "4.1.10",
		"gitVersion" : "8cdc51e7810f7fd8898a4c60b935e389f04659ee"
	},
	"ok" : 1
}
> db.repro.find().sort({x:1}).explain()
{
	"queryPlanner" : {
		"plannerVersion" : 1,
		"namespace" : "repro.repro",
		"indexFilterSet" : false,
		"parsedQuery" : {
			
		},
		"queryHash" : "B7791BAD",
		"planCacheKey" : "B7791BAD",
		"winningPlan" : {
			"stage" : "SORT",
			"sortPattern" : {
				"x" : 1
			},
			"inputStage" : {
				"stage" : "SORT_KEY_GENERATOR",
				"inputStage" : {
					"stage" : "COLLSCAN",
					"direction" : "forward"
				}
			}
		},
		"rejectedPlans" : [ ]
	},
	"serverInfo" : {
		"host" : "Chriss-MacBook-Pro.local",
		"port" : 27017,
		"version" : "4.1.10",
		"gitVersion" : "8cdc51e7810f7fd8898a4c60b935e389f04659ee"
	},
	"ok" : 1
} 

This type of index should fail validation and not be created.



 Comments   
Comment by Githook User [ 03/Jan/20 ]

Author:

{'name': 'James Wahlin', 'email': 'james.wahlin@mongodb.com', 'username': 'jameswahlin'}

Message: SERVER-41263 Disallow empty string in index key type

(cherry picked from commit d5d674930f1c67f6c87d73715f82f608668d72fa)

SERVER-41263 Add update and repl tests for index keys with empty strings

(cherry picked from commit e09c045111610a0820cea3265d1986be5dacb541)
Branch: v4.2
https://github.com/mongodb/mongo/commit/d6ec80aa3a767df33543d7d1a1efd7d92b9bcf4d

Comment by Githook User [ 10/Jun/19 ]

Author:

{'name': 'Xin Hao Zhang', 'email': 'xinhao.zhang@mongodb.com', 'username': 'xinhaoz'}

Message: SERVER-41263 Add update and repl tests for index keys with empty strings
Branch: master
https://github.com/mongodb/mongo/commit/e09c045111610a0820cea3265d1986be5dacb541

Comment by Githook User [ 05/Jun/19 ]

Author:

{'name': 'Xin Hao Zhang', 'email': 'xinhao.zhang@mongodb.com', 'username': 'xinhaoz'}

Message: SERVER-41263 Disallow empty string in index key type
Branch: master
https://github.com/mongodb/mongo/commit/d5d674930f1c67f6c87d73715f82f608668d72fa

Generated at Thu Feb 08 04:57:15 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.