[SERVER-65049] Optimize away { $expr : true } in $match clauses Created: 29/Mar/22  Updated: 29/Oct/23  Resolved: 18/Aug/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 5.0.6
Fix Version/s: 6.1.0-rc0

Type: Improvement Priority: Major - P3
Reporter: James Kovacs Assignee: Henri Nikku
Resolution: Fixed Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Problem/Incident
Related
related to SERVER-33925 Queries that are known to return no r... Backlog
is related to CSHARP-4116 Optimize { $expr : true } to { } Closed
Backwards Compatibility: Fully Compatible
Participants:
Linked BF Score: 161

 Description   
{ $expr : true }

is equivalent to { }, but the two generate different query plans (see below). The .NET/C# driver will generate

{ $expr : true }

if all variables are known at run-time on the client-side and evaluate to true. (Other query frameworks that generate MQL dynamically will likely do something similar.)

The .NET/C# driver is implementing a client-side optimization to render

{ $expr : true }

as { } in $match clauses, but older versions (and other drivers) won't have this optimization. It is desirable for the query engine to implement this optimization itself.

Note that this is similar to SERVER-33925, which considers the case of

{ $expr : false }

.

Query plans...

> db.coll.explain().aggregate([{$match:{ $expr: true }}])
{
  "explainVersion": "1",
  "queryPlanner": {
    "namespace": "test.coll",
    "indexFilterSet": false,
    "parsedQuery": {
      "$expr": {
        "$const": true
      }
    },
    "queryHash": "0C022C46",
    "planCacheKey": "C3C05147",
    "optimizedPipeline": true,
    "maxIndexedOrSolutionsReached": false,
    "maxIndexedAndSolutionsReached": false,
    "maxScansToExplodeReached": false,
    "winningPlan": {
      "stage": "COLLSCAN",
      "filter": {
        "$expr": {
          "$const": true
        }
      },
      "direction": "forward"
    },
    "rejectedPlans": [ ]
  },
...

> db.coll.explain().aggregate([{$match:{}}])
{
  "explainVersion": "1",
  "queryPlanner": {
    "namespace": "test.coll",
    "indexFilterSet": false,
    "parsedQuery": {
 
    },
    "queryHash": "8B3D4AB8",
    "planCacheKey": "D542626C",
    "optimizedPipeline": true,
    "maxIndexedOrSolutionsReached": false,
    "maxIndexedAndSolutionsReached": false,
    "maxScansToExplodeReached": false,
    "winningPlan": {
      "stage": "COLLSCAN",
      "direction": "forward"
    },
    "rejectedPlans": [ ]
  },
...



 Comments   
Comment by Githook User [ 26/Jul/22 ]

Author:

{'name': 'Uladzimir Makouski', 'email': 'uladzimir.makouski@mongodb.com', 'username': 'umakouski'}

Message: Revert "SERVER-65049 Optimize away

{ $expr : true }

in $match clauses"

This reverts commit 3b51f4acaa6a57e8ced17971f85c7d6d35ac0cca.
Branch: master
https://github.com/mongodb/mongo/commit/46b4aa084aad86fd79a122f13a846f6c015eddda

Comment by Githook User [ 25/Jul/22 ]

Author:

{'name': 'Henri Nikku', 'email': 'henri.nikku@mongodb.com', 'username': 'henrinikku'}

Message: SERVER-65049 Optimize away

{ $expr : true }

in $match clauses
Branch: master
https://github.com/mongodb/mongo/commit/3b51f4acaa6a57e8ced17971f85c7d6d35ac0cca

Generated at Thu Feb 08 06:01:45 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.