[SERVER-54128] Computed projection should not be pushed down past sort+limit Created: 29/Jan/21  Updated: 29/Oct/23  Resolved: 23/Jun/21

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 5.0.0-rc4, 5.1.0-rc0

Type: Bug Priority: Major - P3
Reporter: David Percy Assignee: David Percy
Resolution: Fixed Votes: 0
Labels: post-rc0
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Depends
Related
related to SERVER-54768 Sort/project re-ordering is inconsist... Backlog
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v5.0
Sprint: Query Optimization 2021-06-14, Query Optimization 2021-06-28
Participants:
Linked BF Score: 136

 Description   

In PipelineD::prepareExecutor, we are sometimes pushing down a computed projection past a sort+limit. This is incorrect because the projection can raise an error when it sees documents that the sort+limit would not return.

We should change buildProjectionForPushdown so that it never returns a computed projection:

/**
 * Given a dependency set and a pipeline, builds a projection BSON object to push down into the
 * PlanStage layer. The rules to push down the projection are as follows:
 *    1. If there is an inclusion projection at the front of the pipeline, it will be pushed down
 *       as is.
 *    2. If there is no inclusion projection at the front of the pipeline, but there is a finite
 *       dependency set, a projection representing this dependency set will be pushed down.
 *    3. Otherwise, an empty projection is returned and no projection push down will happen.
 */
auto buildProjectionForPushdown(const DepsTracker& deps, Pipeline* pipeline) {

https://github.com/mongodb/mongo/blob/0d6199bb52dcae5978551816c6ac4ad98bda165b/src/mongo/db/pipeline/pipeline_d.cpp#L480-L489

Computed projections should be ineligible for case 1. For example, if the pipeline starts with {$project: {b: {$round: "$a"}}}, we'll fall through to case 2 and push down {a: 1}, based on the dependencies.



 Comments   
Comment by Vivian Ge (Inactive) [ 06/Oct/21 ]

Updating the fixversion since branching activities occurred yesterday. This ticket will be in rc0 when it’s been triggered. For more active release information, please keep an eye on #server-release. Thank you!

Comment by Githook User [ 24/Jun/21 ]

Author:

{'name': 'David Percy', 'email': 'david.percy@mongodb.com', 'username': 'dpercy'}

Message: SERVER-54128 Don't push down expressions past sort+limit
Branch: v5.0
https://github.com/mongodb/mongo/commit/9a313631ab20d2996811bc7cb3b4ccaec6ba5a82

Comment by Githook User [ 23/Jun/21 ]

Author:

{'name': 'David Percy', 'email': 'david.percy@mongodb.com', 'username': 'dpercy'}

Message: SERVER-54128 Don't push down expressions past sort+limit
Branch: master
https://github.com/mongodb/mongo/commit/93e25299fbf8e0279cd855f4ce19becae230d2a1

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