[SERVER-6795] ExpressionCompare incorrectly optimizes reverse sense expressions to a comparison with the wrong inclusivity Created: 17/Aug/12  Updated: 11/Jul/16  Resolved: 21/Aug/12

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 2.2.0-rc2, 2.3.0

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

Operating System: ALL
Participants:

 Description   

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" },
    };



 Comments   
Comment by auto [ 21/Aug/12 ]

Author:

{u'date': u'2012-08-18T15:07:07-07:00', u'email': u'aaron@10gen.com', u'name': u'Aaron'}

Message: SERVER-6795 Correctly optimize reverse sense ExpressionCompare to ExpressionFieldRange.
Branch: v2.2
https://github.com/mongodb/mongo/commit/eae9f0ac98e3cfe43951a217106c2db53c7d9d28

Comment by auto [ 20/Aug/12 ]

Author:

{u'date': u'2012-08-18T15:07:07-07:00', u'email': u'aaron@10gen.com', u'name': u'Aaron'}

Message: SERVER-6795 Correctly optimize reverse sense ExpressionCompare to ExpressionFieldRange.
Branch: master
https://github.com/mongodb/mongo/commit/da6844241dacce4e14240ca67a64ee5c12b35d3c

Generated at Thu Feb 08 03:12:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.