- 
    Type:Bug 
- 
    Resolution: Unresolved
- 
    Priority:Major - P3 
- 
    None
- 
    Affects Version/s: None
- 
    Component/s: Query Planning
- 
        Query Optimization
- 
        ALL
- 
        
- 
        None
- 
        None
- 
        None
- 
        None
- 
        None
- 
        None
- 
        None
This is a new manifestation of a category of problems introduced by SERVER-34741 and subsequently patched in SERVER-91102 and then again in SERVER-102698. I reproduced the problem only on 8.2.1, but I'm fairly confident that it affects 8.0.x and 7.0.x versions as well. Note that we did a CA evaluation for SERVER-91102 and SERVER-102698 but declined to issue a CA. We also declined to backport the fixes for those two previous bug reports to older branches. We should discuss in the proper venue, but I imagine the same treatment is warranted here: fix the bug, but neither issue a CA nor do a backport to older branches.
The problem is conceptually the same as the one originally reported in SERVER-91102: the semantics used for equality in $group and the semantics used in matching may not align. In SERVER-34741, we introduced an optimization to swap $match before $group when the $match is on the _id field of the $group. But this is an incorrect optimization if the $match distinguishes between values that $group considers equivalent. The known cases of this were existence predicates (null/missing are grouped into one bucket) and $type predicates (e.g. numbers of different types that are numerically equal are grouped into one bucket). We fixed these known cases for regular match expressions but failed to do so for $expr. See "Steps for Reproduce" for detailed examples of how to exercise the bug using $expr.
I discovered this bug by code inspection while working on the fix for SERVER-106505, since my proposed fix involves changes to how $expr predicates are pushed down.
- is caused by
- 
                    SERVER-34741 Move $match in front of $group if condition is on group key -         
- Closed
 
-         
- is related to
- 
                    SERVER-91102 Moving $match before $group is incorrect when predicate distinguishes equal values -         
- Closed
 
-         
- 
                    SERVER-102698 Moving $match before $group with compound _id is incorrect when predicate distinguishes equal values -         
- Closed
 
-         
- 
                    SERVER-106505 $sequentialCache optimization can interfere with $match pushdown in a $lookup sub-pipeline -         
- In Code Review
 
-