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

[CQF] Sargable rewrite bug with $ne inside a $elemMatch against empty array

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Query Optimization
    • ALL
    • Hide

      Enable sargable rewrites.

      > db.test.find() 
      { "_id" : 0, "num" : 2, "a" : { "array" : [ ] } }   
      
      > db.adminCommand({setParameter: 1, internalQueryFrameworkControl: "forceClassicEngine"}) 
      { "was" : "trySbeEngine", "ok" : 1 } 
      > db.test.aggregate({$match: {$or: [{nonexistent: 1}, {$and: [{"a.array": {$elemMatch: {$ne: "str"}}}, {"num": {$gte: 1}}]}]}}) 
      // no results   
      
      > db.adminCommand({setParameter: 1, internalQueryFrameworkControl: "tryBonsai"}) 
      { "was" : "forceClassicEngine", "ok" : 1 } 
      > db.test.aggregate({$match: {$or: [{nonexistent: 1}, {$and: [{"a.array": {$elemMatch: {$ne: "str"}}}, {"num": {$gte: 1}}]}]}}) 
      { "_id" : 0, "num" : 2, "a" : { "array" : [ ] } } 
      > 
      Show
      Enable sargable rewrites. > db.test.find() { "_id" : 0, "num" : 2, "a" : { "array" : [ ] } }   > db.adminCommand({setParameter: 1, internalQueryFrameworkControl: "forceClassicEngine" }) { "was" : "trySbeEngine" , "ok" : 1 } > db.test.aggregate({$match: {$or: [{nonexistent: 1}, {$and: [{ "a.array" : {$elemMatch: {$ne: "str" }}}, { "num" : {$gte: 1}}]}]}}) // no results   > db.adminCommand({setParameter: 1, internalQueryFrameworkControl: "tryBonsai" }) { "was" : "forceClassicEngine" , "ok" : 1 } > db.test.aggregate({$match: {$or: [{nonexistent: 1}, {$and: [{ "a.array" : {$elemMatch: {$ne: "str" }}}, { "num" : {$gte: 1}}]}]}}) { "_id" : 0, "num" : 2, "a" : { "array" : [ ] } } >
    • 130

      There is a bug in the sargable rewrites that comes about when there is a field whose value is an empty array and there is a $ne inside an $elemMatch against that field, among some other stages (see the repro). Note that for the repro, SBE matches classic

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            militsa.sotirova@mongodb.com Militsa Sotirova
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: