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

$group with $first/$last to distinct scan optimization might incorrectly unwind arrays

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Query Optimization

      Consider the below scenario:

      data: [{mk: [1, 2, 3], notMk: 4}]
      index: {notMk: 1, mk: 1}
      query: {$group: {_id: "$notMk", acc: {$first: "$mk"}}}
      

      The correct result (which we get without the index) would be

      [ { _id: 4, acc: [ 1, 2, 3 ] } ] 

      But with the index, we get

      [ { _id: 4, acc: 1 } ] 

       

      The planner will incorrectly use a distinct scan to cover the query because we currently allow it if there's no multikey field before the distinct key. When analyzing if the query can be covered by the scan, we should look at the projection, not just the distinct key.

            Assignee:
            daniel.segel@mongodb.com Daniel Segel
            Reporter:
            henri.nikku@mongodb.com Henri Nikku
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: