[SERVER-66558] $sort does not rewrite to bounded sort when multiple index options are available Created: 18/May/22  Updated: 29/Oct/23  Resolved: 01/Jun/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 6.0.0-rc9, 6.1.0-rc0, 5.0.16

Type: Bug Priority: Major - P3
Reporter: Matt Boros Assignee: Sam Mercier
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v6.0, v5.0
Sprint: QO 2022-05-16, QO 2022-05-30, QO 2022-06-13
Participants:

 Description   

This rewrite also does not happen when the plan cache is used.

db.c.drop();
assert.commandWorked(db.createCollection("c", {timeseries: {timeField: "t", metaField: "m"}}));
assert.commandWorked(db.c.createIndex({"m.b": 1, t: 1}));
assert.commandWorked(db.c.createIndex({"m.a": 1, t: 1}));
 
// No bounded sort in this explain
jsTestLog(db.c.explain().aggregate([{$sort: {t: 1}}, {$match: {"m.a": 1}}, {$match: {"m.b": 1}}]));
 
// Run it to get it in the plan cache
db.c.aggregate([{$sort: {t: 1}}, {$match: {"m.a": 1}}, {$match: {"m.b": 1}}]).toArray();
 
// Should have one plan cache entry
assert.eq(db.system.buckets.c.getPlanCache().list().length, 1);
 
// Plan retrieved from cache, no bounded sort
jsTestLog(db.c.explain().aggregate([{$sort: {t: 1}}, {$match: {"m.a": 1}}, {$match: {"m.b": 1}}]));

The issue appears to be here, where we need to handle a multiplan stage and a plan cache stage.

Ideally part of this fix would be to write a test to make sure the rewrite occurs when it comes from the plan cache and from a multiplan stage.



 Comments   
Comment by Githook User [ 21/Feb/23 ]

Author:

{'name': 'samontea', 'email': 'merciers.merciers@gmail.com', 'username': 'samontea'}

Message: SERVER-66558 $sort does not rewrite to bounded sort when multiple index options are available
Branch: v5.0
https://github.com/mongodb/mongo/commit/13a8377755e5481f13e47f202744f4739eed4b86

Comment by Githook User [ 31/May/22 ]

Author:

{'name': 'samontea', 'email': 'merciers.merciers@gmail.com', 'username': 'samontea'}

Message: SERVER-66558 $sort does not rewrite to bounded sort when multiple index options are available
Branch: v6.0
https://github.com/mongodb/mongo/commit/b4b8fa06d72d245b943dd7a9b849d103011fa954

Comment by Githook User [ 31/May/22 ]

Author:

{'name': 'samontea', 'email': 'merciers.merciers@gmail.com', 'username': 'samontea'}

Message: SERVER-66558 $sort does not rewrite to bounded sort when multiple index options are available
Branch: master
https://github.com/mongodb/mongo/commit/18699f723d45e03d1b08255975a5ec829b526875

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