summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Wahlin <james@mongodb.com>2022-05-17 15:11:42 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-07-21 13:36:54 +0000
commit3c2e77f7098157bcb30aa50f2ce3e0b53bb49a01 (patch)
tree54efc42324f860c1efbe242cfed0c8a58e8f8c00
parent92bbf4379baaf338b3942702961e1dcb6957bb9d (diff)
downloadmongo-3c2e77f7098157bcb30aa50f2ce3e0b53bb49a01.tar.gz
SERVER-66310 Make ExpressionSetUnion::isCommutative() collation aware
(cherry picked from commit 2c53b7b684c8dd90044b8ef19932453088f54869)
-rw-r--r--jstests/aggregation/expressions/collation_expressions.js13
-rw-r--r--src/mongo/db/pipeline/expression.h5
2 files changed, 17 insertions, 1 deletions
diff --git a/jstests/aggregation/expressions/collation_expressions.js b/jstests/aggregation/expressions/collation_expressions.js
index 5ecad4f02ec..c9643d49506 100644
--- a/jstests/aggregation/expressions/collation_expressions.js
+++ b/jstests/aggregation/expressions/collation_expressions.js
@@ -86,6 +86,19 @@ results = coll.aggregate([{$project: {out: {$setUnion: [["a", "B", "c"], ["d", "
assert.eq(1, results.length);
assert.eq(4, results[0].out.length);
+// Test that $setUnion is not commutative when a collation is set. A non-const value is required for
+// reordering to occur.
+assert(coll.drop);
+coll.drop();
+assert.commandWorked(coll.insert({_id: 1, upper: "A", lower: "a"}));
+var results1 = coll.aggregate([{$project: {out: {$setUnion: [["$upper"], ["a"]]}}}],
+ {collation: caseInsensitive})
+ .toArray();
+var results2 = coll.aggregate([{$project: {out: {$setUnion: [["A"], ["$lower"]]}}}],
+ {collation: caseInsensitive})
+ .toArray();
+assert.eq(results1, results2);
+
// Test that $setDifference respects the collation.
testExpressionWithCollation(coll, {$setDifference: [["a", "B"], ["b", "A"]]}, [], caseInsensitive);
diff --git a/src/mongo/db/pipeline/expression.h b/src/mongo/db/pipeline/expression.h
index 9cce6d0b1e2..1efea3742d4 100644
--- a/src/mongo/db/pipeline/expression.h
+++ b/src/mongo/db/pipeline/expression.h
@@ -2860,7 +2860,10 @@ public:
}
bool isCommutative() const final {
- return true;
+ // Only commutative when performing binary string comparison. The first value entered when
+ // multiple collation-equal but binary-unequal values are added will dictate what is stored
+ // in the set.
+ return getExpressionContext()->getCollator() == nullptr;
}
void acceptVisitor(ExpressionMutableVisitor* visitor) final {