[SERVER-56574] Coalesce $setWindowFields stages with compatible sortBy arguments and window functions Created: 03/May/21  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Sam Mercier Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-56420 Coalesce $setWindowFields stages Backlog
is related to SERVER-56581 Flip window-function sortBy and bound... Backlog
Assigned Teams:
Query Optimization
Participants:

 Description   

SERVER-56420 handles coalescing setWindowFields stages where the sortBy arguments are identical, but it may be possible to coalesce without identical sortBy arguments if one of the window functions is order independent and the fields that are being sorted on match (even if the ascending/descending don't match).

For example $integral/$sum/$derivative are order independent so the following stages could be merged:

{$setWindowFields: {
  sortBy: {a: 1},
  output: {rank: {$rank: {}}},
}}
{$setWindowFields: {
  sortBy: {a: -1}
  output: {integral: {$integral: "$b"}}
}}

into one expression:

{$setWindowFields: {
  sortBy: {a: 1},
  output: {rank: {$rank: {}}, integral: {$integral: "$b"}},
}}

because $integral is order independent.

Care will have to be taken to ensure that the bounds are re-expressed in equivalent terms and the limitation on descending range-based sort isn't infringed.

Similarly to 56420, we will need to ensure that:

  • The second stage does not read from any 'output' field of the first.
  • The two stages don't have any 'output' field name in common.
  • The two stages have identical partitionBy arguments.


 Comments   
Comment by Joel Redman (Inactive) [ 03/May/21 ]

Both this and SERVER-56420 are probably more necessary than first glance would suggest, given that most of our input queries are generated by machine rather than humans. I'd expect a fairly slow uptake since the apps will have to catch up to the new feature set.

Generated at Thu Feb 08 05:39:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.