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

Queries using partial indexes with $or filter can return incorrect results when cache entry exists

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 6.0.21, 8.0.6, 7.0.18, 8.1.0-rc1
    • Component/s: None
    • Query Optimization
    • ALL
    • Hide
      // Used to create a cache entry
      const q1 = [{$match: {$or: [{a: 1}, {a: {$lte: 'a string'}}], _id: {$lte: 5}}}];
      // Returns incorrect results once the cache entry is made
      const q2 = [{$match: {$or: [{a: 1}, {a: {$lte: 10}}], _id: {$lte: 5}}}];
      
      const coll = db.coll;
      assert(coll.drop())
      assert.commandWorked(coll.insert({_id: 0, a: 0}))
      
      const expectedResults = coll.aggregate(q2).toArray();
      // Matches document
      
      assert.commandWorked(
          coll.createIndex({a: 1}, {partialFilterExpression: {$or: [{a: 1}, {a: {$lte: 'a string'}}]}}))
      for (let i = 0; i < 3; i++) {
          coll.aggregate(q1).toArray()
      }
      const actualResults = coll.aggregate(q2).toArray()
      // Does not match document
      
      assert.eq(expectedResults, actualResults, {expectedResults, actualResults})
      
      Show
      // Used to create a cache entry const q1 = [{$match: {$or: [{a: 1}, {a: {$lte: 'a string' }}], _id: {$lte: 5}}}]; // Returns incorrect results once the cache entry is made const q2 = [{$match: {$or: [{a: 1}, {a: {$lte: 10}}], _id: {$lte: 5}}}]; const coll = db.coll; assert (coll.drop()) assert .commandWorked(coll.insert({_id: 0, a: 0})) const expectedResults = coll.aggregate(q2).toArray(); // Matches document assert .commandWorked( coll.createIndex({a: 1}, {partialFilterExpression: {$or: [{a: 1}, {a: {$lte: 'a string' }}]}})) for (let i = 0; i < 3; i++) { coll.aggregate(q1).toArray() } const actualResults = coll.aggregate(q2).toArray() // Does not match document assert .eq(expectedResults, actualResults, {expectedResults, actualResults})
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      The repro below shows that when we make a cache entry for a query that uses a partial index, a similar query when run after that will return incorrect results.

      No hinting is involved so it is not related to SERVER-26413. It also does not involve $exists or $ne or null, so it seems unrelated to SERVER-36635.

            Assignee:
            ben.shteinfeld@mongodb.com Ben Shteinfeld
            Reporter:
            matt.boros@mongodb.com Matt Boros
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              None
              None
              None
              None