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 16:01:36 +0000
commit88f6fb33c3608ed20b55a7e0566815886a9d45f5 (patch)
tree4781c474e83353c019401540cce84d384b3e63dc
parent4e4eea8e18803e697ef10df4516419371fe31937 (diff)
downloadmongo-88f6fb33c3608ed20b55a7e0566815886a9d45f5.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 dc959791f2f..43eb1c080ea 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 02f7858745b..f1daa29afe9 100644
--- a/src/mongo/db/pipeline/expression.h
+++ b/src/mongo/db/pipeline/expression.h
@@ -2025,7 +2025,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 {