diff options
-rw-r--r-- | jstests/cqf/basic_agg_expr.js | 55 | ||||
-rw-r--r-- | src/mongo/db/pipeline/abt/agg_expression_visitor.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/query/optimizer/syntax/syntax.h | 13 |
3 files changed, 62 insertions, 8 deletions
diff --git a/jstests/cqf/basic_agg_expr.js b/jstests/cqf/basic_agg_expr.js index 4c66a8799e0..0578717f5fd 100644 --- a/jstests/cqf/basic_agg_expr.js +++ b/jstests/cqf/basic_agg_expr.js @@ -81,4 +81,59 @@ const t = db.cqf_agg_expr; assertArrayEq({actual: res, expected: [{_id: 1, a: [{b: 1}]}, {_id: 3, a: {b: [1]}}]}); } } +{ + t.drop(); + assert.commandWorked(t.insert({_id: 0, a: 1})); + assert.commandWorked(t.insert({_id: 1, a: 2})); + assert.commandWorked(t.insert({_id: 2, a: 3})); + + { + const res = t.aggregate([{$match: {$expr: {$lt: [2, "$a"]}}}]).toArray(); + + assert.eq(1, res.length); + assert.eq(3, res[0].a); + } + { + const res = t.aggregate([{$match: {$expr: {$gt: ["$a", 2]}}}]).toArray(); + + assert.eq(1, res.length); + assert.eq(3, res[0].a); + } + { + const res = t.aggregate([{$match: {$expr: {$lte: [2, "$a"]}}}]).toArray(); + + assert.eq(2, res.length); + assertArrayEq({actual: res, expected: [{_id: 1, a: 2}, {_id: 2, a: 3}]}); + } + { + const res = t.aggregate([{$match: {$expr: {$gte: ["$a", 2]}}}]).toArray(); + + assert.eq(2, res.length); + assertArrayEq({actual: res, expected: [{_id: 1, a: 2}, {_id: 2, a: 3}]}); + } + { + const res = t.aggregate([{$match: {$expr: {$gt: [3, "$a"]}}}]).toArray(); + + assert.eq(2, res.length); + assertArrayEq({actual: res, expected: [{_id: 0, a: 1}, {_id: 1, a: 2}]}); + } + { + const res = t.aggregate([{$match: {$expr: {$lt: ["$a", 3]}}}]).toArray(); + + assert.eq(2, res.length); + assertArrayEq({actual: res, expected: [{_id: 0, a: 1}, {_id: 1, a: 2}]}); + } + { + const res = t.aggregate([{$match: {$expr: {$gte: [3, "$a"]}}}]).toArray(); + + assert.eq(3, res.length); + assertArrayEq({actual: res, expected: [{_id: 0, a: 1}, {_id: 1, a: 2}, {_id: 2, a: 3}]}); + } + { + const res = t.aggregate([{$match: {$expr: {$lte: ["$a", 3]}}}]).toArray(); + + assert.eq(3, res.length); + assertArrayEq({actual: res, expected: [{_id: 0, a: 1}, {_id: 1, a: 2}, {_id: 2, a: 3}]}); + } +} }()); diff --git a/src/mongo/db/pipeline/abt/agg_expression_visitor.cpp b/src/mongo/db/pipeline/abt/agg_expression_visitor.cpp index f9c5849d172..39d51838982 100644 --- a/src/mongo/db/pipeline/abt/agg_expression_visitor.cpp +++ b/src/mongo/db/pipeline/abt/agg_expression_visitor.cpp @@ -183,7 +183,7 @@ public: isSimplePath(rightPtr->getPath()) && rightPtr->getInput() == _ctx.getRootProjVar()) { addEvalFilterFn( - std::move(rightPtr->getPath()), std::move(left), reverseComparisonOp(op)); + std::move(rightPtr->getPath()), std::move(left), flipComparisonOp(op)); return; } } diff --git a/src/mongo/db/query/optimizer/syntax/syntax.h b/src/mongo/db/query/optimizer/syntax/syntax.h index 9cafb04d549..3005384c276 100644 --- a/src/mongo/db/query/optimizer/syntax/syntax.h +++ b/src/mongo/db/query/optimizer/syntax/syntax.h @@ -202,24 +202,23 @@ inline constexpr bool isComparisonOp(Operations op) { /** * Flip the argument order of a comparison op. - * TODO SERVER-71500 Fix, test, and consider renaming reverseComparisonOp. * * Not to be confused with boolean negation: see 'negateComparisonOp'. */ -inline constexpr Operations reverseComparisonOp(Operations op) { +inline constexpr Operations flipComparisonOp(Operations op) { switch (op) { case Operations::Eq: case Operations::Neq: return op; case Operations::Lt: - return Operations::Gte; - case Operations::Lte: return Operations::Gt; + case Operations::Lte: + return Operations::Gte; case Operations::Gt: - return Operations::Lte; - case Operations::Gte: return Operations::Lt; + case Operations::Gte: + return Operations::Lte; default: MONGO_UNREACHABLE; @@ -232,7 +231,7 @@ inline constexpr Operations reverseComparisonOp(Operations op) { * If the op is not a comparison, return none. * If the op can't be negated (for example EqMember), return none. * - * Not to be confused with flipping the argument order: see 'reverseComparisonOp'. + * Not to be confused with flipping the argument order: see 'flipComparisonOp'. */ inline boost::optional<Operations> negateComparisonOp(Operations op) { switch (op) { |