[SERVER-48905] Allow $rand in command-level 'let' Created: 16/Jun/20  Updated: 29/Oct/23  Resolved: 17/Aug/20

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 4.7.0

Type: Improvement Priority: Major - P3
Reporter: David Percy Assignee: David Percy
Resolution: Fixed Votes: 0
Labels: qopt-team
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-75927 Shards see different values for 'let'... Closed
is related to SERVER-82042 Sharded write commands should evaluat... Closed
Backwards Compatibility: Fully Compatible
Sprint: Query 2020-07-13, Query 2020-08-10, Query 2020-08-24
Participants:

 Description   

Currently, command-level 'let' only accepts expressions that optimize to a constant. We'd like to allow $rand in a command-level 'let': it would pick a random value once up front, and the bound variable would be constant throughout the query. We can make this work by calling evaluate() instead of optimize().

But we should still report an error when the expression tries to refer to any field (as opposed to returning missing). We can use DepsTracker to uassert when the command-level-let-bound expressions have any unmet dependencies (fields, variables, or metadata).



 Comments   
Comment by Githook User [ 17/Aug/20 ]

Author:

{'name': 'David Percy', 'email': 'david.percy@mongodb.com', 'username': 'dpercy'}

Message: SERVER-48905 Allow $rand in command-level 'let'

This relaxes a restriction on command-level 'let' bindings: instead of
requiring them to optimize() to a constant, we evaluate() them, once,
before running the query. This means {$rand: {}} is now allowed in a
command-level 'let'.

Referring to a field doesn't make sense in a command-level 'let', because
there is no "current document" yet. So we use DepsTracker to report an error,
rather than evaluate to missing.

Unbound variables are disallowed with the usual mechanism.
Branch: master
https://github.com/mongodb/mongo/commit/209bec74d8716aa97080b84aadc58ef811277f3d

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