[SERVER-85337] $bucketAuto $first does not obey input order Created: 17/Jan/24 Updated: 02/Feb/24 |
|
| Status: | In Code Review |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | David Percy | Assignee: | Chi-I Huang |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | query-director-triage | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||||||
| Assigned Teams: |
Query Optimization
|
||||||||||||||||||||
| Operating System: | ALL | ||||||||||||||||||||
| Sprint: | QO 2024-02-05, QO 2024-02-19 | ||||||||||||||||||||
| Participants: | |||||||||||||||||||||
| Description |
|
If I sort by {a: -1} and use $first, I should get the largest 'a' value within each group. For example, using $bucket I get the expected result:
While the same thing with $bucketAuto gives the smallest value within each group:
It looks like internally $bucketAuto is doing something like:
But this doesn’t work for order-sensitive accumulators like $first, because the sort loses some information about the original order--even using a stable sort. The documents within each dynamically-chosen bucket are sorted by (group key, input position), instead of by input position. |
| Comments |
| Comment by David Percy [ 22/Jan/24 ] |
|
I think the solution would look like:
It would not be enough to sort by (group key, counter) because when multiple group keys land in the same bucket, we want $first to choose whichever document came first in the input (has the smallest counter value), regardless of group key. |
| Comment by David Percy [ 17/Jan/24 ] |
|
This came up because brad.cater@mongodb.com was wondering where we rely on stable sorting, as part of SERVER-81571. I think the stable sorting is not helpful for $bucketAuto. Within each bucket it needs to either:
|