[SERVER-56255] $let should participate in constant folding Created: 22/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

Assigned Teams:
Query Optimization
Participants:
Case:

 Description   

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.


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