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

Coalesce $setWindowFields stages

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major - P3
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: Backlog
    • Component/s: None
    • Labels:
      None

      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.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              backlog-query-optimization Backlog - Query Optimization
              Reporter:
              david.percy David Percy
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated: