[SERVER-3839] Scope "leaking" with map-reduce and CodeWScope Created: 13/Sep/11  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: MapReduce
Affects Version/s: 2.0.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Daniel Crosta Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 0
Labels: query-44-grooming
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Pymongo 2.0.1/OS X


Assigned Teams:
Query Optimization
Operating System: ALL
Participants:

 Description   

Re-using the same variable in the scope option to two different CodeWScope objects (bson.code.Code, with scope keyword arg) results in the scope from reduce "leaking" into the scope of map.

I've read the Python code, and I don't believe this is due to a bug in Pymongo.

Short Python script to reproduce:

from bson.code import Code
import pymongo
 
conn = pymongo.Connection()
db = conn.test
 
db.foo.drop()
db.foo.save({'a': 1})
db.foo.save({'a': 1})
db.foo.save({'a': 2})
db.foo.save({'a': 2})
 
 
map = Code(
    """
    function() {
        emit( this.a, {one: scopevar, two: 0} );
    }
    """,
    scope={'scopevar': 1}
)
 
reduce = Code(
    """
    function(key, values) {
        var out = {one: 0, two: 0};
        for (var i=0; i<values.length; i++) {
            out.one += values[i].one;
            out.two += scopevar;
        }
        return out;
    }""",
    scope={'scopevar': 2}
)
 
# should give in 2 results, each with
# field "one" == 2, and field "two" == 4
for result in db.foo.inline_map_reduce(map, reduce):
    print result

Actual results:

{u'_id': 1.0, u'value': {u'two': 4.0, u'one': 4.0}}
{u'_id': 2.0, u'value': {u'two': 4.0, u'one': 4.0}}


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