[SERVER-46863] findAndModify hits collator mismatch invariant when collection has non-simple default collation Created: 13/Mar/20  Updated: 29/Oct/23  Resolved: 31/Mar/20

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: None
Fix Version/s: 4.4.0-rc0, 4.7.0

Type: Bug Priority: Major - P3
Reporter: Bernard Gorman Assignee: Ian Boros
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Depends
Problem/Incident
is caused by SERVER-45406 Provide ExpressionContext in PlanStag... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v4.4
Steps To Reproduce:

function reproBF16515() {
    const coll = db.reproBF16515;
    coll.drop();
 
    assert.commandWorked(db.createCollection("reproBF16515", {collation: { locale: 'en_US' }}));
 
    // Works
    coll.findAndModify({query: {_id: 1}, update: {}});
    print("Succeeded: fAM with _id query");
    // Works
    coll.findAndModify({query: {a: 1}, update: {}, fields: {a: 1}});
    print("Succeeded: fAM with non-_id query + projection");
    // Invariant
    print("About to run: fAM with _id query + projection");
    coll.findAndModify({
        query: { _id: 3 },
        update: {},
        fields: { a: 1 }
    });
    print("Unreachable: fAM with _id query + projection");
}

Sprint: Query 2020-04-06
Participants:
Linked BF Score: 5

 Description   

A findAndModify with an equality query on _id, an update, a projection, and no explicit collation will hit an invariant when run on a collection with a non-simple default collation, due to a mismatch between the collator set on the ExpressionContext and the collator parsed by CanonicalQuery from the QueryRequest.

When running findAndModify under the conditions outlined above:

This bug was introduced in SERVER-45406.



 Comments   
Comment by Githook User [ 31/Mar/20 ]

Author:

{'name': 'Ian Boros', 'email': 'ian.boros@mongodb.com', 'username': 'puppyofkosh'}

Message: SERVER-46863 prevent collator mismatch in findAndModify/update

(cherry picked from commit f105232d6594336e35cfee8f7f6d4ef49182ce84)
Branch: v4.4
https://github.com/mongodb/mongo/commit/68df73a1d0700577209becb31cc3447f955fe898

Comment by Githook User [ 31/Mar/20 ]

Author:

{'name': 'Ian Boros', 'email': 'ian.boros@mongodb.com', 'username': 'puppyofkosh'}

Message: SERVER-46863 prevent collator mismatch in findAndModify/update
Branch: master
https://github.com/mongodb/mongo/commit/f105232d6594336e35cfee8f7f6d4ef49182ce84

Comment by Ian Boros [ 20/Mar/20 ]

A few notes:
This is not an issue in 4.2 because ParsedUpdates do not create their own ExpressionContext in that branch. They rely on CQ to create it for them, see here. This means that the path in CQ::canonicalize() which has this invariant is not taken.

This does not appear to be an issue for the delete command either, though its execution machinery is constructed in a similar manner. This is because there's no need to set the collation on the ParsedDelete before canonicalizing the query, as is done with update.

Comment by Bernard Gorman [ 13/Mar/20 ]

Note: this bug will also manifest if the findAndModify request has a hint instead of a projection (or has both).

Generated at Thu Feb 08 05:12:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.