$let should participate in constant folding

XMLWordPrintableJSON

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

      When a $let variable simplifies to a constant, references to that variable should simplify to a constant too. For example, in

      {$match: {$expr:
        {$let: {
          vars: {upperBound: {$add: [2, 3]}},
          in: {$lt: ["$field", {$add: ["$$upperBound", 10]}]}
        }}
      }}
      

      This should simplify down to

      {$match: {$expr:
        {$lt: ["$field", 15]}
      }}
      

      which thanks to SERVER-39943 can be indexed.

      Possibly this is actually 2 separate optimizations:
      1. replace each occurrence of "$$upperBound" with 5
      2. remove the $let expression since $$upperBound is unused

      If some of the vars are constant and some are not, we can apply rule 1 but not rule 2.

            Assignee:
            [DO NOT USE] Backlog - Query Optimization
            Reporter:
            David Percy (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: