Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-56574

Coalesce $setWindowFields stages with compatible sortBy arguments and window functions

    • Type: Icon: New Feature New Feature
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Query Optimization

      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.

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            samuel.mercier@mongodb.com Sam Mercier
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: