diff options
author | David Storch <david.storch@10gen.com> | 2016-12-07 11:39:16 -0500 |
---|---|---|
committer | David Storch <david.storch@10gen.com> | 2016-12-08 13:50:27 -0500 |
commit | bc1e45dc19c488eb27dd93aaff33ad0145e420ab (patch) | |
tree | 71599f4fe894b3a44ce009166843c514df785247 /src/mongo/db/query | |
parent | 38990ad5a23445474fa4fb888c6bc775ac90b073 (diff) | |
download | mongo-bc1e45dc19c488eb27dd93aaff33ad0145e420ab.tar.gz |
SERVER-27300 fail indexing of the Symbol type when the collation is non-simple
Diffstat (limited to 'src/mongo/db/query')
-rw-r--r-- | src/mongo/db/query/collation/collation_index_key.cpp | 8 | ||||
-rw-r--r-- | src/mongo/db/query/collation/collation_index_key_test.cpp | 41 |
2 files changed, 49 insertions, 0 deletions
diff --git a/src/mongo/db/query/collation/collation_index_key.cpp b/src/mongo/db/query/collation/collation_index_key.cpp index edd8612bcb5..d0750d451e5 100644 --- a/src/mongo/db/query/collation/collation_index_key.cpp +++ b/src/mongo/db/query/collation/collation_index_key.cpp @@ -109,6 +109,14 @@ void translateElement(StringData fieldName, ctxStack->emplace(element.Obj().begin(), &out->subarrayStart(fieldName)); return; } + case BSONType::Symbol: { + uasserted(ErrorCodes::CannotBuildIndexKeys, + str::stream() + << "Cannot index type Symbol with a collation. Failed to index element: " + << element + << ". Index collation: " + << collator->getSpec().toBSON()); + } default: out->appendAs(element, fieldName); } diff --git a/src/mongo/db/query/collation/collation_index_key_test.cpp b/src/mongo/db/query/collation/collation_index_key_test.cpp index 9630755c8ec..88d635187a3 100644 --- a/src/mongo/db/query/collation/collation_index_key_test.cpp +++ b/src/mongo/db/query/collation/collation_index_key_test.cpp @@ -148,4 +148,45 @@ TEST(CollationIndexKeyTest, CollationAwareAppendCorrectlyReversesComplexNesting) ASSERT_BSONOBJ_EQ(out.obj(), expected); } +TEST(CollationIndexKeyTest, CollationAwareAppendThrowsIfSymbol) { + CollatorInterfaceMock collator(CollatorInterfaceMock::MockType::kReverseString); + BSONObj dataObj = BSON("" << BSONSymbol("mySymbol")); + BSONObjBuilder out; + ASSERT_THROWS_CODE( + CollationIndexKey::collationAwareIndexKeyAppend(dataObj.firstElement(), &collator, &out), + UserException, + ErrorCodes::CannotBuildIndexKeys); +} + +TEST(CollationIndexKeyTest, CollationAwareAppendDoesNotThrowOnSymbolIfNoCollation) { + BSONObj dataObj = BSON("" << BSONSymbol("mySymbol")); + BSONObj expected = BSON("" << BSONSymbol("mySymbol")); + BSONObjBuilder out; + CollationIndexKey::collationAwareIndexKeyAppend(dataObj.firstElement(), nullptr, &out); + ASSERT_BSONOBJ_EQ(out.obj(), expected); +} + +TEST(CollationIndexKeyTest, CollationAwareAppendThrowsIfSymbolInsideObject) { + CollatorInterfaceMock collator(CollatorInterfaceMock::MockType::kReverseString); + BSONObj dataObj = BSON("" << BSON("a" + << "foo" + << "b" + << BSONSymbol("mySymbol"))); + BSONObjBuilder out; + ASSERT_THROWS_CODE( + CollationIndexKey::collationAwareIndexKeyAppend(dataObj.firstElement(), &collator, &out), + UserException, + ErrorCodes::CannotBuildIndexKeys); +} + +TEST(CollationIndexKeyTest, CollationAwareAppendThrowsIfSymbolInsideArray) { + CollatorInterfaceMock collator(CollatorInterfaceMock::MockType::kReverseString); + BSONObj dataObj = BSON("" << BSON_ARRAY("foo" << BSONSymbol("mySymbol"))); + BSONObjBuilder out; + ASSERT_THROWS_CODE( + CollationIndexKey::collationAwareIndexKeyAppend(dataObj.firstElement(), &collator, &out), + UserException, + ErrorCodes::CannotBuildIndexKeys); +} + } // namespace |