[SERVER-66812] Incorrect $group/$project optimization analysis can cause incorrect results for strange queries Created: 26/May/22  Updated: 29/Oct/23  Resolved: 07/Jun/22

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

Type: Bug Priority: Major - P3
Reporter: Charlie Swanson Assignee: Charlie Swanson
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
Participants:

 Description   

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.



 Comments   
Comment by Githook User [ 08/Jun/22 ]

Author:

{'name': 'Charlie Swanson', 'email': 'charlie.swanson@mongodb.com', 'username': 'cswanson310'}

Message: SERVER-66812 Fix bug with $group projection analysis

(cherry picked from commit 8a3af130c7dba9936b30e17d25dd52a5802658ad)
Branch: v6.0
https://github.com/mongodb/mongo/commit/904122d4b13bdc8382b36081afa47017ba7855dc

Comment by Githook User [ 06/Jun/22 ]

Author:

{'name': 'Charlie Swanson', 'email': 'charlie.swanson@mongodb.com', 'username': 'cswanson310'}

Message: SERVER-66812 Fix bug with $group projection analysis
Branch: master
https://github.com/mongodb/mongo/commit/8a3af130c7dba9936b30e17d25dd52a5802658ad

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