[SERVER-68766] Add option to re-enable single value MapReduce optimization Created: 11/Aug/22  Updated: 29/Oct/23  Resolved: 29/Aug/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 4.4.17, 5.0.12, 6.0.2, 6.1.0-rc1, 6.2.0-rc0

Type: Bug Priority: Major - P3
Reporter: Davis Haupt (Inactive) Assignee: Davis Haupt (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Related
is related to DOCS-15563 Add note on mapReduce changes to 4.4 ... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v6.1, v6.0, v5.0, v4.4
Steps To Reproduce:

Running against mongod version 4.4:

var collName = "mrtest"
var coll = db[collName];
coll.drop()
coll.insert({a:1})
 
const runMR = () => coll.mapReduce(
    function(){emit(0, "mapped value")}, 
    function(key,value){return "reduced value"}, 
    {out: {inline: 1} }
).results;
 
db.adminCommand({setFeatureCompatibilityVersion: "4.2"})
printjson(runMR())
// >>> [ { "_id" : 0, "value" : "mapped value" } ]
 
db.adminCommand({setFeatureCompatibilityVersion: "4.4"})
printjson(runMR())
// >>> [ { "_id" : 0, "value" : "reduced value" } ]

Sprint: QO 2022-08-22, QO 2022-09-05
Participants:
Case:

 Description   

This is in the documentation for MapReduce from 4.2 to 6.0:

MongoDB will not call the reduce function for a key that has only a single value. The values argument is an array whose elements are the value objects that are "mapped" to the key.

However, from 4.4 onwards, the reduce function will be called regardless of how many values are associated with a given key. PM-766 added a new execution engine for MapReduce commands based on the Aggregation Framework that is active in 4.4 and above.
 
If the implementation is changed, then two possible fixes are:

  1. AccumulatorJsReduce::getValue() can be modified to not call into the JS reduce function if the length the _values vector is exactly 1. Instead, it will just return that value to the caller. This fix has the benefit of not adding any logic to the generated MQL.
  2. The translateReduce() function can be modified to only call the internal JsReduce accumulator if the $emits array has a length larger than 1, and otherwise, just return the document inside $emits.
     


 Comments   
Comment by Githook User [ 31/Aug/22 ]

Author:

{'name': 'Davis Haupt', 'email': 'davis.haupt@mongodb.com', 'username': 'davish'}

Message: SERVER-68766 add server parameter to enable single value reduce optimization
Branch: v4.4
https://github.com/mongodb/mongo/commit/59217e73260289054bcb91fea3f104d5f6cf34e6

Comment by Githook User [ 29/Aug/22 ]

Author:

{'name': 'Davis Haupt', 'email': 'davis.haupt@mongodb.com', 'username': 'davish'}

Message: SERVER-68766 add server parameter to enable single value reduce optimization
Branch: v5.0
https://github.com/mongodb/mongo/commit/79cfcdd83eb6f64e164a588d0daf9bb873328b45

Comment by Githook User [ 29/Aug/22 ]

Author:

{'name': 'Davis Haupt', 'email': 'davis.haupt@mongodb.com', 'username': 'davish'}

Message: SERVER-68766 add server parameter to enable single value reduce optimization
Branch: v6.0
https://github.com/mongodb/mongo/commit/5133ed46464a264b8b6b3a08434978014783f3eb

Comment by Githook User [ 29/Aug/22 ]

Author:

{'name': 'Davis Haupt', 'email': 'davis.haupt@mongodb.com', 'username': 'davish'}

Message: SERVER-68766 add server parameter to enable single value reduce optimization
Branch: v6.1
https://github.com/mongodb/mongo/commit/004a674a8421d9151c9e8f497401b0503fdcd6ca

Comment by Githook User [ 26/Aug/22 ]

Author:

{'name': 'Davis Haupt', 'email': 'davis.haupt@mongodb.com', 'username': 'davish'}

Message: SERVER-68766 add server parameter to enable single value reduce optimization
Branch: master
https://github.com/mongodb/mongo/commit/7bfcf3b42ebf7f4600abfeae30cae644ac5d72e7

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