[SERVER-13715] Aggregation pipeline execution can fail with $or and blocking sorts Created: 24/Apr/14  Updated: 11/Jul/16  Resolved: 29/Apr/14

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 2.6.0
Fix Version/s: 2.6.2, 2.7.0

Type: Bug Priority: Major - P3
Reporter: Mathias Stearn Assignee: David Storch
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Related
related to SERVER-14144 Aggregation cursor error message shou... Closed
Operating System: ALL
Backport Completed:
Participants:

 Description   
Issue Status as of June 6, 2014

ISSUE SUMMARY
During execution of an aggregation framework pipeline, a combination of an $or query in a $match operator with a subsequent $sort operator on an unindexed field causes the aggregation execution to fail with an exception "exception: cursor encountered an error", error code 17285.

USER IMPACT
The aggregation call fails under the given circumstances, but there is no data loss and the server remains running.

WORKAROUNDS
To work around this bug, place a $project pipeline operator immediately preceding the $sort operator.

Example:

Instead of using this aggregation pipeline:

db.coll.aggregate([ { '$match': { '$or': [ { name: 'red' }, { name: 'blue' }] } }, { '$sort': { value: 1 }}]);

Insert a $project with the desired fields before the $sort:

db.coll.aggregate([ { '$match': { '$or': [ { name: 'red' }, { name: 'blue' }] } }, {'$project': {_id: 1, name: 1, value: 1} }, { '$sort': { value: 1 }}]);

AFFECTED VERSIONS
Versions 2.6.0 and 2.6.1 are affected by this issue.

FIX VERSION
The fix is included in the 2.6.2 production release.

RESOLUTION DETAILS
Subqueries need to be planned on construction of a SubplanRunner rather than from inside the runner in order to fail fast if only blocking sort solutions exist.

Original description

db.orAndSort.aggregate([ { '$match': { '$or': [ { name: 'red' }, { name: 'blue' }] } }, { '$sort': { value: 1 }}]);
assert: command failed: {
        "errmsg" : "exception: cursor encountered an error",
        "code" : 17285,
        "ok" : 0
}
 
Detailed error object:
{ ok: 0.0, code: 2, errmsg: "error processing query: ns=test.orAndSort limit=0 skip=0
Tree: $or
    name == "red"
    name == "blue"
Sort: { value: 1 }
Proj: {}
 No query solutions" }

There is currently no way to get at the detailed error object without altering the code, but it does show that it is trying to do a sort and failing (due to NO_BLOCKING_SORT) after getRunner returned success.

Related mongodb-users post: https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/mongodb-user/YK_D4lJXysY/IHQxX3_sN8EJ



 Comments   
Comment by Githook User [ 15/May/14 ]

Author:

{u'username': u'dstorch', u'name': u'David Storch', u'email': u'david.storch@10gen.com'}

Message: SERVER-13715 fix use of auto_ptr after release in subplan runner
(cherry picked from commit 193aba81f1c9c9c0c0a7e9ba857e7538dc6b6ed0)
Branch: v2.6
https://github.com/mongodb/mongo/commit/bf471cd8e2aaf70e7950ea179b9c22822b6ddc2c

Comment by Githook User [ 15/May/14 ]

Author:

{u'username': u'dstorch', u'name': u'David Storch', u'email': u'david.storch@10gen.com'}

Message: SERVER-13715 plan subqueries on construction of a SubplanRunner

This enforces the error behavior expected by aggregation.
(cherry picked from commit 20263ce4e9c345a2d3748ef2071f6dc860a092f1)
Branch: v2.6
https://github.com/mongodb/mongo/commit/652d7a4c95719ccf619dba4a5b57ef21626df2a4

Comment by Githook User [ 09/May/14 ]

Author:

{u'username': u'dstorch', u'name': u'David Storch', u'email': u'david.storch@10gen.com'}

Message: SERVER-13715 fix use of auto_ptr after release in subplan runner
Branch: master
https://github.com/mongodb/mongo/commit/193aba81f1c9c9c0c0a7e9ba857e7538dc6b6ed0

Comment by Will Shaver [ 30/Apr/14 ]

To work around this bug, place a project pipeline operation immediately proceeding the sort call.

Comment by Githook User [ 29/Apr/14 ]

Author:

{u'username': u'dstorch', u'name': u'David Storch', u'email': u'david.storch@10gen.com'}

Message: SERVER-13715 plan subqueries on construction of a SubplanRunner

This enforces the error behavior expected by aggregation.
Branch: master
https://github.com/mongodb/mongo/commit/20263ce4e9c345a2d3748ef2071f6dc860a092f1

Generated at Thu Feb 08 03:32:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.