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

Results in incorrect order for query with bounded sort on multi-key field when SORT_MERGE plan used

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • None
    • Query
    • ALL

      The SORT_MERGE query stage uses BSONElement::woCompare() to compare WorkingSetMembers from its child stages, instead using the SortStageKeyGenerator to generate sort keys. As a result, query plans that use the SORT_MERGE stage which are performing a bounded sort on a multi-key field can generate results in incorrect order. This issue is quite similar to SERVER-11878.

      To reproduce:

      > db.bar.drop()
      true
      > db.bar.insert({_id: 0, a: [8], b: 1})
      WriteResult({ "nInserted" : 1 })
      > db.bar.insert({_id: 1, a: [6, 10], b: 1})
      WriteResult({ "nInserted" : 1 })
      > db.bar.ensureIndex({a: 1})
      {
      	"createdCollectionAutomatically" : false,
      	"numIndexesBefore" : 1,
      	"numIndexesAfter" : 2,
      	"ok" : 1
      }
      > db.bar.find({$or: [{a: {$gt: 7}}, {a: {$gt: 6}}]}).sort({a: 1}) // SORT_MERGE not used: correct order.
      { "_id" : 0, "a" : [ 8 ], "b" : 1 }
      { "_id" : 1, "a" : [ 6, 10 ], "b" : 1 }
      > db.bar.find({$or: [{a: {$gt: 7}, b: 1}, {a: {$gt: 6}, b: 1}]}).sort({a: 1}) // SORT_MERGE used: incorrect order.
      { "_id" : 1, "a" : [ 6, 10 ], "b" : 1 }
      { "_id" : 0, "a" : [ 8 ], "b" : 1 }
      

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            rassi J Rassi
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: