[DOCS-13309] MongoDB does not guarantee short circuit evaluation and it should not be mentioned Created: 18/Dec/19  Updated: 30/Oct/23  Resolved: 01/Sep/21

Status: Closed
Project: Documentation
Component/s: manual, Server
Affects Version/s: None
Fix Version/s: Server_Docs_20231030

Type: Improvement Priority: Major - P3
Reporter: Jacob Evans Assignee: Jason Price
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-37530 Provide a way to cause a well-defined... Backlog
related to SERVER-52619 Performance Regression on 4.4 compare... Closed
related to SERVER-45231 Provide a way to cause a well-defined... Closed
is related to SERVER-45123 Queries should not fail with type err... Closed
Participants:
Days since reply: 2 years, 23 weeks ago
Epic Link: DOCSP-11701
Story Points: 3

 Description   

Description

These pages:
https://docs.mongodb.com/manual/reference/operator/query/and/
https://docs.mongodb.com/manual/reference/operator/aggregation/and/

mention that

The $and operator uses short-circuit evaluation. If the first expression (e.g. <expression1>) evaluates to false, MongoDB will not evaluate the remaining expressions.

and

$and uses short-circuit logic: the operation stops evaluation after encountering the first false expression.

Although there is a kernel of truth here and $and does perform short-circuit evaluation for performance, the query system is free to change the order of predicates inside a $and. In fact the find query system currently sorts all of them in an order that's arbitrary to the user in find. Agg performs some reordering in unusual circumstances.

In general the query system must be free to reorder predicates as it sees fit during optimization and the order it chooses may change when MongoDB is upgraded or due to other factors.

When the order is changed, the user will not get the short-circuit evaluation they specified in the query.

I've opened a ticket to track a request for a way to optionally define predicate evaluation order: SERVER-45231. But for now it should be emphasized that there is no way to guarantee the order no matter what the query system empirically seems to be doing today. Following from this, we cannot guarantee that unnecessary $and expressions are evaluated or not evaluated at all. Also if a runtime error would occur due to the evaluation of an expression in a $and, we cannot guarantee it will or will not occur.

This is true for find and aggregation and for predicates listed outside of a $and within a find query. If we're defensive about it in the docs we can prevent customer surprise later.

Scope of changes

Impact to Other Docs

MVP (Work and Date)

Resources (Scope or Design Docs, Invision, etc.)



 Comments   
Comment by Githook User [ 01/Sep/21 ]

Author:

{'name': 'jason-price-mongodb', 'email': 'jshfjghsdfgjsdjh@aolsdjfhkjsdhfkjsdf.com'}

Message: DOCS-13309 short circuit evaluation
Branch: v4.0
https://github.com/mongodb/docs/commit/f6269067996c89f23cf3d3018fec780a4eade2bd

Comment by Githook User [ 01/Sep/21 ]

Author:

{'name': 'jason-price-mongodb', 'email': 'jshfjghsdfgjsdjh@aolsdjfhkjsdhfkjsdf.com'}

Message: DOCS-13309 short circuit evaluation
Branch: v4.2
https://github.com/mongodb/docs/commit/c25fac557742e725722e0e94c48d610260b7f185

Comment by Githook User [ 01/Sep/21 ]

Author:

{'name': 'jason-price-mongodb', 'email': 'jshfjghsdfgjsdjh@aolsdjfhkjsdhfkjsdf.com'}

Message: DOCS-13309 short circuit evaluation
Branch: v4.4
https://github.com/mongodb/docs/commit/da267b6a15b1bc0b2c6253a9ff1c80f9863149fb

Comment by Githook User [ 01/Sep/21 ]

Author:

{'name': 'jason-price-mongodb', 'email': 'jshfjghsdfgjsdjh@aolsdjfhkjsdhfkjsdf.com'}

Message: DOCS-13309 short circuit evaluation
Branch: v5.0
https://github.com/mongodb/docs/commit/4b16e2c3a5f841b264a68edf0b3ba148fd12cc50

Comment by Githook User [ 01/Sep/21 ]

Author:

{'name': 'jason-price-mongodb', 'email': 'jshfjghsdfgjsdjh@aolsdjfhkjsdhfkjsdf.com'}

Message: DOCS-13309 short circuit evaluation
Branch: master
https://github.com/mongodb/docs/commit/1c2026123f568b99fd7ca41a8ab50f40149be5ff

Comment by Charlie Swanson [ 02/Feb/21 ]

Just wanted to ping again on this - it came up again in the context of SERVER-52619 recently.

Comment by Charlie Swanson [ 07/Jan/20 ]

bduncan@visualmining.com the expression language also does not have any such guarantees. To the extent that it works today, it is only because the optimizer isn't sophisticated enough to do any transformations or reorderings to it. I think SERVER-37530 should remain as the ticket requesting any guaranteed order of evaluation, there is no such concept today - it just may happen to work out that way.

Comment by Bruce Duncan [ 29/Dec/19 ]

I would argue that the documentation should also address short-circuit evaluation within the expression language accessible using `$expr`.  To my knowledge, reordering does not happen within those expressions and so the user specified order is retained and thereby any benefit they were counting on from short-circuit evaluation.

Generated at Thu Feb 08 08:07:27 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.