Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-66558

$sort does not rewrite to bounded sort when multiple index options are available

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 6.0.0-rc9, 6.1.0-rc0, 5.0.16
    • Affects Version/s: None
    • Component/s: None
    • None
    • Fully Compatible
    • ALL
    • v6.0, v5.0
    • QO 2022-05-16, QO 2022-05-30, QO 2022-06-13

      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.

            Assignee:
            samuel.mercier@mongodb.com Sam Mercier
            Reporter:
            matt.boros@mongodb.com Matt Boros
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: