Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-22833

MatchExpression::toBSON does not necessarily produce a valid query object.

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.3.4
    • Affects Version/s: None
    • Component/s: Querying
    • None
    • Fully Compatible
    • ALL
    • Query 11 (03/14/16)

      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.

            Assignee:
            benjamin.murphy Benjamin Murphy
            Reporter:
            benjamin.murphy Benjamin Murphy
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: