summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Storch <david.storch@10gen.com>2016-12-01 12:49:59 -0500
committerDavid Storch <david.storch@10gen.com>2016-12-06 13:36:14 -0500
commit4e7fa3664849a4b8e26ea6606cf977b4a27a74dc (patch)
treef8f4dbf071c69f6e1955dcdbf17b4bc20ef075f9
parentfa06d911194356baeb709bf68c36e52afa9a9968 (diff)
downloadmongo-4e7fa3664849a4b8e26ea6606cf977b4a27a74dc.tar.gz
SERVER-27200 fix CodeWScope comparison to not use collator
(cherry picked from commit 6bee18129c27a82ebcbd316c7900afb04c6f69dc)
-rw-r--r--src/mongo/bson/bsonelement.cpp14
-rw-r--r--src/mongo/db/pipeline/document_comparator_test.cpp32
-rw-r--r--src/mongo/db/pipeline/value_comparator_test.cpp26
-rw-r--r--src/mongo/db/query/collation/SConscript10
-rw-r--r--src/mongo/db/query/collation/collation_bson_comparison_test.cpp106
5 files changed, 178 insertions, 10 deletions
diff --git a/src/mongo/bson/bsonelement.cpp b/src/mongo/bson/bsonelement.cpp
index 34b4b07576d..5131d5943ed 100644
--- a/src/mongo/bson/bsonelement.cpp
+++ b/src/mongo/bson/bsonelement.cpp
@@ -993,16 +993,10 @@ int compareElementValues(const BSONElement& l,
if (cmp)
return cmp;
- return l.codeWScopeObject().woCompare(
- // woCompare parameters: r, ordering, considerFieldName, comparator.
- // r: the BSONObj to compare with.
- // ordering: the sort directions for each key.
- // considerFieldName: whether field names should be considered in comparison.
- // comparator: used for all string comparisons, if non-null.
- r.codeWScopeObject(),
- BSONObj(),
- true,
- comparator);
+ // When comparing the scope object, we should consider field names. Special string
+ // comparison semantics do not apply to strings nested inside the CodeWScope scope
+ // object, so we do not pass through the string comparator.
+ return l.codeWScopeObject().woCompare(r.codeWScopeObject(), BSONObj(), true);
}
default:
verify(false);
diff --git a/src/mongo/db/pipeline/document_comparator_test.cpp b/src/mongo/db/pipeline/document_comparator_test.cpp
index 40229e982c1..27e73f7edd6 100644
--- a/src/mongo/db/pipeline/document_comparator_test.cpp
+++ b/src/mongo/db/pipeline/document_comparator_test.cpp
@@ -30,6 +30,8 @@
#include "mongo/db/pipeline/document_comparator.h"
+#include "mongo/bson/bsonmisc.h"
+#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/db/query/collation/collator_interface_mock.h"
#include "mongo/unittest/unittest.h"
@@ -165,5 +167,35 @@ TEST(DocumentComparatorTest, NestedArrayEqualityRespectsCollator) {
ASSERT_FALSE(DocumentComparator(&collator).evaluate(doc3 == doc1));
}
+TEST(DocumentComparatorTest, ComparingCodeWScopeShouldNotRespectCollation) {
+ const CollatorInterfaceMock collator(CollatorInterfaceMock::MockType::kAlwaysEqual);
+ const DocumentComparator comparator(&collator);
+ const Document doc1{{"a",
+ BSONCodeWScope("js code",
+ BSON("foo"
+ << "bar"))}};
+ const Document doc2{{"a",
+ BSONCodeWScope("js code",
+ BSON("foo"
+ << "not bar"))}};
+ ASSERT_TRUE(comparator.evaluate(doc1 != doc2));
+}
+
+TEST(DocumentComparatorTest, HashingCodeWScopeShouldNotRespectCollation) {
+ const CollatorInterfaceMock collator(CollatorInterfaceMock::MockType::kAlwaysEqual);
+ const Document doc1{{"a",
+ BSONCodeWScope("js code",
+ BSON("foo"
+ << "bar"))}};
+ const Document doc2{{"a",
+ BSONCodeWScope("js code",
+ BSON("foo"
+ << "not bar"))}};
+ size_t seed1, seed2 = 0;
+ doc1.hash_combine(seed1, &collator);
+ doc2.hash_combine(seed2, &collator);
+ ASSERT_NE(seed1, seed2);
+}
+
} // namespace
} // namespace mongo
diff --git a/src/mongo/db/pipeline/value_comparator_test.cpp b/src/mongo/db/pipeline/value_comparator_test.cpp
index afa364b11a8..cbfb5eb36bd 100644
--- a/src/mongo/db/pipeline/value_comparator_test.cpp
+++ b/src/mongo/db/pipeline/value_comparator_test.cpp
@@ -30,6 +30,8 @@
#include "mongo/db/pipeline/value_comparator.h"
+#include "mongo/bson/bsonmisc.h"
+#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/db/pipeline/document_value_test_util.h"
#include "mongo/db/query/collation/collator_interface_mock.h"
#include "mongo/unittest/unittest.h"
@@ -269,5 +271,29 @@ TEST(ValueComparatorTest, UnorderedMapOfValueRespectsCollation) {
ASSERT_EQ(map[Value("fooZ")], 3);
}
+TEST(ValueComparatorTest, ComparingCodeWScopeShouldNotRespectCollation) {
+ const CollatorInterfaceMock collator(CollatorInterfaceMock::MockType::kAlwaysEqual);
+ const ValueComparator comparator(&collator);
+ const Value val1{BSONCodeWScope("js code",
+ BSON("foo"
+ << "bar"))};
+ const Value val2{BSONCodeWScope("js code",
+ BSON("foo"
+ << "not bar"))};
+ ASSERT_TRUE(comparator.evaluate(val1 != val2));
+}
+
+TEST(ValueComparatorTest, HashingCodeWScopeShouldNotRespectCollation) {
+ const CollatorInterfaceMock collator(CollatorInterfaceMock::MockType::kAlwaysEqual);
+ const ValueComparator comparator(&collator);
+ const Value val1{BSONCodeWScope("js code",
+ BSON("foo"
+ << "bar"))};
+ const Value val2{BSONCodeWScope("js code",
+ BSON("foo"
+ << "not bar"))};
+ ASSERT_NE(comparator.hash(val1), comparator.hash(val2));
+}
+
} // namespace
} // namespace mongo
diff --git a/src/mongo/db/query/collation/SConscript b/src/mongo/db/query/collation/SConscript
index e838ba8fff6..6be08908c78 100644
--- a/src/mongo/db/query/collation/SConscript
+++ b/src/mongo/db/query/collation/SConscript
@@ -49,6 +49,16 @@ env.CppUnitTest(
)
env.CppUnitTest(
+ target="collation_bson_comparison_test",
+ source=[
+ "collation_bson_comparison_test.cpp",
+ ],
+ LIBDEPS=[
+ "collator_interface_mock",
+ ],
+)
+
+env.CppUnitTest(
target="collation_index_key_test",
source=[
"collation_index_key_test.cpp",
diff --git a/src/mongo/db/query/collation/collation_bson_comparison_test.cpp b/src/mongo/db/query/collation/collation_bson_comparison_test.cpp
new file mode 100644
index 00000000000..fc07f1ac244
--- /dev/null
+++ b/src/mongo/db/query/collation/collation_bson_comparison_test.cpp
@@ -0,0 +1,106 @@
+/**
+ * Copyright (C) 2016 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the GNU Affero General Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#include "mongo/platform/basic.h"
+
+#include "mongo/bson/bsonelement_comparator.h"
+#include "mongo/bson/bsonmisc.h"
+#include "mongo/bson/bsonobj_comparator.h"
+#include "mongo/bson/bsonobjbuilder.h"
+#include "mongo/bson/simple_bsonelement_comparator.h"
+#include "mongo/bson/simple_bsonobj_comparator.h"
+#include "mongo/db/query/collation/collator_interface_mock.h"
+#include "mongo/unittest/unittest.h"
+
+namespace mongo {
+namespace {
+
+TEST(CollationBSONComparisonTest, CompareCodeWScopeElementWithCollationShouldNotRespectCollation) {
+ const CollatorInterfaceMock collator(CollatorInterfaceMock::MockType::kAlwaysEqual);
+ const BSONElementComparator comparator(BSONElementComparator::FieldNamesMode::kConsider,
+ &collator);
+ BSONObj obj1 = BSON("a" << BSONCodeWScope("js code",
+ BSON("foo"
+ << "bar")));
+ BSONObj obj2 = BSON("a" << BSONCodeWScope("js code",
+ BSON("foo"
+ << "not bar")));
+ // The elements are not equal with or without the "always equal" collator.
+ ASSERT(comparator.evaluate(obj1["a"] != obj2["a"]));
+ ASSERT_BSONELT_NE(obj1["a"], obj2["a"]);
+}
+
+TEST(CollationBSONComparisonTest, CompareCodeWScopeObjWithCollationShouldNotRespectCollation) {
+ const CollatorInterfaceMock collator(CollatorInterfaceMock::MockType::kAlwaysEqual);
+ const BSONObjComparator comparator(
+ BSONObj(), BSONObjComparator::FieldNamesMode::kConsider, &collator);
+ BSONObj obj1 = BSON("a" << BSONCodeWScope("js code",
+ BSON("foo"
+ << "bar")));
+ BSONObj obj2 = BSON("a" << BSONCodeWScope("js code",
+ BSON("foo"
+ << "not bar")));
+ // The elements are not equal with or without the "always equal" collator.
+ ASSERT(comparator.evaluate(obj1 != obj2));
+ ASSERT_BSONOBJ_NE(obj1, obj2);
+}
+
+TEST(CollationBSONComparisonTest, HashingCodeWScopeElementWithCollationShouldNotRespectCollation) {
+ const CollatorInterfaceMock collator(CollatorInterfaceMock::MockType::kAlwaysEqual);
+ const BSONElementComparator comparator(BSONElementComparator::FieldNamesMode::kConsider,
+ &collator);
+ BSONObj obj1 = BSON("a" << BSONCodeWScope("js code",
+ BSON("foo"
+ << "bar")));
+ BSONObj obj2 = BSON("a" << BSONCodeWScope("js code",
+ BSON("foo"
+ << "not bar")));
+ // The elements are not equal with or without the "always equal" collator.
+ ASSERT_NE(SimpleBSONElementComparator::kInstance.hash(obj1["a"]),
+ SimpleBSONElementComparator::kInstance.hash(obj2["a"]));
+ ASSERT_NE(comparator.hash(obj1["a"]), comparator.hash(obj2["a"]));
+}
+
+TEST(CollationBSONComparisonTest, HashingCodeWScopeObjWithCollationShouldNotRespectCollation) {
+ const CollatorInterfaceMock collator(CollatorInterfaceMock::MockType::kAlwaysEqual);
+ const BSONObjComparator comparator(
+ BSONObj(), BSONObjComparator::FieldNamesMode::kConsider, &collator);
+ BSONObj obj1 = BSON("a" << BSONCodeWScope("js code",
+ BSON("foo"
+ << "bar")));
+ BSONObj obj2 = BSON("a" << BSONCodeWScope("js code",
+ BSON("foo"
+ << "not bar")));
+ // The elements are not equal with or without the "always equal" collator.
+ ASSERT_NE(SimpleBSONObjComparator::kInstance.hash(obj1),
+ SimpleBSONObjComparator::kInstance.hash(obj2));
+ ASSERT_NE(comparator.hash(obj1), comparator.hash(obj2));
+}
+
+} // namespace
+} // namespace mongo