[SERVER-80772] Stage builders generate invalid plan for simple project after sort query Created: 05/Sep/23  Updated: 29/Oct/23  Resolved: 15/Sep/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 7.0.0
Fix Version/s: 7.2.0-rc0, 7.0.2, 7.1.0-rc3

Type: Bug Priority: Critical - P2
Reporter: Ian Boros 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
Assigned Teams:
Query Execution
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v7.1, v7.0
Sprint: QE 2023-09-18
Participants:
Linked BF Score: 160

 Description   

The stage builders generate a plan that assumes a "root" document is available when it is not. E.g.

[j0:prim] [4] mkbson s16 [_id = s12, out = s15] true false
[j0:prim] [4] project [s15 = getElement(s13, 0)]
[j0:prim] [4] group [s12] [s13 = addToArrayCapped(s2, 104857600)] spillSlots[s14] mergingExprs[aggConcatArraysCapped(s14, 104857600)]
[j0:prim] [4] project [s12 = (s11 ?: null)]
[j0:prim] [4] project [s11 = traverseP(s10, lambda(l101.0) { getField(move(l101.0), "hostname") }, 1)]
[j0:prim] [3] project [s10 = getField(s9, "tags")]
[j0:prim] [3] mkbson s9 s4 [_id] keep [] true false
[j0:prim] [2] sort [s7, s8] [desc, asc] [s2]
[j0:prim] [2] project [s7 =
[j0:prim]     if (isArray(s3) ?: false)
[j0:prim]     then (_internalGreatest(s3) ?: undefined)
[j0:prim]     else (s3 ?: null)
[j0:prim] , s8 =
[j0:prim]     if (isArray(s1) ?: false)
[j0:prim]     then (_internalLeast(s1) ?: undefined)
[j0:prim]     else (s1 ?: null)
[j0:prim] ]
[j0:prim] [2] project [s6 = (!((isArray(s3) ?: false)) || (!((isArray(s1) ?: false)) || fail(2, "cannot sort with keys that are parallel arrays")))]
[j0:prim] [1] scan s4 s5 none none none none none none lowPriority [s1 = ExceededTimeLimit, s2 = _id, s3 = assertAlways] @"6f634a83-ec81-486b-a994-07d88e095f25" true false

This assumes s4 is available after the sort when in fact, only the _id value was carried through the sort.

The result is that the query fails due to a tassert.



 Comments   
Comment by Githook User [ 15/Sep/23 ]

Author:

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

Message: SERVER-80772 Fix stage builder bug for sort project queries
Branch: v7.0
https://github.com/mongodb/mongo/commit/2e87deff38a2393e8a79d146cdaf914475b65b68

Comment by Githook User [ 15/Sep/23 ]

Author:

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

Message: SERVER-80772 Fix stage builders generating invalid plan for sort/project queries

(cherry picked from commit d4c429c6228f1809d7825564fc1ae6f2a82fcf15)
Branch: v7.1
https://github.com/mongodb/mongo/commit/939758f3957356db3837feeadb4432f71454821b

Comment by Githook User [ 14/Sep/23 ]

Author:

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

Message: SERVER-80772 Fix stage builders generating invalid plan for sort/project queries
Branch: master
https://github.com/mongodb/mongo/commit/d4c429c6228f1809d7825564fc1ae6f2a82fcf15

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