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

ExpressionCompare incorrectly optimizes reverse sense expressions to a comparison with the wrong inclusivity

    • ALL

      Observed behavior:

      An expression $gt:[ '$a', 1 ] is optimized to a proper field path.
      An expression $gt:[ 1, '$a' ] is converted to $lte:[ '$a', 1 ] (wrong, should be $lt:[ '$a', 1 ]) and optimized to a field path.

      The other comparison operators are similarly affected.

      Test

      c = db.c;
      c.drop();
      
      c.save( { a:1 } );
      
      result = c.aggregate( { $project:{ _id:0, x:{ $gt:[ '$a', 1 ] }, y:{ $gt:[ 1, '$a' ] } } } );
      printjson( result );
      assert.eq( { x:false, y:false }, result.result[ 0 ] );
      

      Probably just need to fix this configuration array.

          static const CmpLookup cmpLookup[7] = {
              /*             -1      0      1      reverse          name   */
              /* EQ  */ { { false, true,  false }, Expression::EQ,  "$eq"  },
              /* NE  */ { { true,  false, true },  Expression::NE,  "$ne"  },
              /* GT  */ { { false, false, true },  Expression::LTE, "$gt"  },
              /* GTE */ { { false, true,  true },  Expression::LT,  "$gte" },
              /* LT  */ { { true,  false, false }, Expression::GTE, "$lt"  },
              /* LTE */ { { true,  true,  false }, Expression::GT,  "$lte" },
              /* CMP */ { { false, false, false }, Expression::CMP, "$cmp" },
          };
      

            Assignee:
            aaron Aaron Staple
            Reporter:
            aaron Aaron Staple
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: