[SERVER-22833] MatchExpression::toBSON does not necessarily produce a valid query object. Created: 24/Feb/16  Updated: 12/Jan/17  Resolved: 14/Mar/16

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

Type: Bug Priority: Major - P3
Reporter: Benjamin Murphy Assignee: Benjamin Murphy
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-12307 Provide a means in query framework to... Closed
is depended on by SERVER-19415 PlanCache.listQueryShapes() reports t... Closed
is depended on by SERVER-20506 Conditionally order $match with $unwind Closed
Duplicate
is duplicated by SERVER-25485 explain shows incorrect parsed syntax... Closed
Related
related to SERVER-27649 Don't error on serialization of $elem... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: Query 11 (03/14/16)
Participants:

 Description   

Queries are represented internally as a MatchExpression tree, which has a toBSON method that, when given a BSONObjBuilder, serializes the tree and produces a BSONObj representing the query. At the moment, there are several types of MatchExpression ($not, particularly) that do not serialize to valid query objects, and thus parsing the result of a toBSON call will result in an error. It should be the case that any MatchExpression can be roundtripped through BSON and will result in a new MatchExpression that is logically equivalent to the first.

Note:
Most of the difficulties for this ticket are because of our complex $not semantics. Rather than go through complex logic in the parser or serialization code, we instead opted to always turn a $not into the following structure:

{$not: {...}} --> {$nor: [{$and: [...]}]}

While logically equivalent, the query planner is unable to perform the same level of optimization with a $nor as with a $not. At the moment, MatchExpression::toBSON is only being used in aggregation pipeline optimization, so as a result of this workaround, any $match stage that is roundtripped through BSON and involves a $not may not be correctly optimized for in the query planner, and will give unexpected explain() output. A solution to this problem is to update CanonicalQuery::normalizeTree() to recognize that a $nor with one child is logically equivalent to a $not, and perform that swap.



 Comments   
Comment by Githook User [ 14/Mar/16 ]

Author:

{u'username': u'benjaminmurphy', u'name': u'Benjamin Murphy', u'email': u'benjamin_murphy@me.com'}

Message: SERVER-22833 Rename MatchExpression::toBSON() to serialize(), and ensure it produces parseable output.
Branch: master
https://github.com/mongodb/mongo/commit/488f48f2f497a4e844aa3057e49f96d2a4be1bcb

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