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

Coalesce $setWindowFields stages

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

      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.

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            david.percy@mongodb.com David Percy
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: