[SERVER-56420] Coalesce $setWindowFields stages Created: 27/Apr/21  Updated: 06/Dec/22

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

Type: Improvement Priority: Major - P3
Reporter: David Percy Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-56574 Coalesce $setWindowFields stages with... Backlog
Assigned Teams:
Query Optimization
Participants:

 Description   

When two $setWindowFields stages appear in a row, it may be beneficial to combine them into one stage.

For example, this query:

{$setWindowFields: {
    partitionBy: "$zip",
    output: {total: {$sum: "$x"}},
}},
{$setWindowFields: {
    partitionBy: "$zip",
    output: {avg: {$avg: "$x"}},
}},

has the same result as this one:

{$setWindowFields: {
    partitionBy: "$zip",
    output: {
        total: {$sum: "$x"},
        avg: {$avg: "$x"},
    },
}},

After desugaring and dropping a redundant $sort (SERVER-55464), the original query is:

{$sort: {zip: 1}},
{$_internalSetWindowFields: {
    partitionBy: "$zip",
    output: {total: {$sum: "$x"}},
}},
{$sort: {zip: 1}},
{$_internalSetWindowFields: {
    partitionBy: "$zip",
    output: {avg: {$avg: "$x"}},
}},

I think a valid set of criteria for combining the two _internalSetWindowFields stages would be:

  • 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 and sortBy arguments.

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