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

Incorrect $group/$project optimization analysis can cause incorrect results for strange queries

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 6.0.0-rc9, 6.1.0-rc0
    • Affects Version/s: 5.3.0, 5.2.0
    • Component/s: None
    • None
    • Fully Compatible
    • ALL
    • v6.0

      We have some logic to detect when a projection underneath a $group stage is not needed and optimize it away. This analysis has some code that appears to be trying to check for a case like this and avoid removing the projection since it would impact the query results:

      coll.aggregate([
          {$project: {_id: 0, a: 1 /* NO b field output */}},
          {$group: {_id: {a: "$a", b: "$b"}}}
      ]
      

      Unfortunately it looks like that analysis was implemented incorrectly and instead of checking if the projection provides all the fields that the $group needs, it appears to be checking something different. I think it's whether none of the projection fields are needed by the $group? In any case, there is a demonstrable bug around this optimization that should be fixed. I'll attach a patch to demonstrate.

            Assignee:
            charlie.swanson@mongodb.com Charlie Swanson
            Reporter:
            charlie.swanson@mongodb.com Charlie Swanson
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: