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-08-11 14:23:44 +0000
commitc4771eda44b12596546ce97d5a7ddc28b18e7cbf (patch)
tree1b29e51ae9b46d0fa3d31034ac8ef64846ed57bd
parent7c44523a40af295c95fadbb4550d114bedeab424 (diff)
downloadmongo-c4771eda44b12596546ce97d5a7ddc28b18e7cbf.tar.gz
SERVER-66310 Make ExpressionSetUnion::isCommutative() collation aware
-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 1b8b98eda2b..8ef91220be5 100644
--- a/jstests/aggregation/expressions/collation_expressions.js
+++ b/jstests/aggregation/expressions/collation_expressions.js
@@ -84,6 +84,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 95ba8820523..3845a6e23e8 100644
--- a/src/mongo/db/pipeline/expression.h
+++ b/src/mongo/db/pipeline/expression.h
@@ -2157,7 +2157,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(ExpressionVisitor* visitor) final {