[SERVER-43666] Sort by "randVal" is not random Created: 26/Sep/19 Updated: 27/Oct/23 Resolved: 23/Oct/19 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Justin Seyster | Assignee: | David Storch |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||
| Operating System: | ALL | ||||||||
| Steps To Reproduce: |
|
||||||||
| Sprint: | Query 2019-11-04 | ||||||||
| Participants: | |||||||||
| Description |
|
The SortKeyGenerator::extractKeyPart() function gets a "randVal" from the Document metadata (by way of Expression::evaluate()) when the key pattern has {$meta: ""randVal"}. However, nowhere in the code base ever sets a random value for this piece of metadata, so the return value is always eoo. The sort deals with this lack of randomness by returning the documents in their natural order. I discovered this problem when I attempted to add an invariant after this line to ensure that:
Currently, this invariant always fails, but when we figure out a way to ensure that documents have "randVal" metadata, we should be able to add that invariant in. |
| Comments |
| Comment by David Storch [ 23/Oct/19 ] | |
|
I'm closing this ticket as "Works as Designed". Sort by $meta:"randVal" was implemented internally in order to support $sample. It is not documented: you can see that it is never mentioned in the manual page for $sort. If we ever do want to expose a query operator that causes the order of the result set to be randomized, we would need to either 1) fully implement and expose a "randVal" meta-sort, or 2) expect applications to rely on
The behavior noted here was not introduced in 4.3 development, but rather exists in stable releases. I tested 4.2.0 in particular, though I suspect this behavior goes back to when $sample was first implemented. |