[SERVER-31374] If map function string ends in a semicolon mapReduce fails silently returning no results Created: 03/Oct/17  Updated: 07/Jan/18  Resolved: 29/Nov/17

Status: Closed
Project: Core Server
Component/s: JavaScript
Affects Version/s: 3.4.9
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Robert Stam Assignee: Ramon Fernandez Marina
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-30009 MapReduce failure: missing ) in paren... Closed
Problem/Incident
causes CSHARP-2049 MapReduce does not work on DB version... Closed
Related
Operating System: ALL
Steps To Reproduce:

Create some test data:

use test
db.test.drop()
db.test.insert({ _id : 1, x : 1 })
db.test.insert({ _id : 2, x : 2 })
db.test.insert({ _id : 3, x : 2 })

Define map and reduce functions and call mapReduce:

var map = "function map() { emit(this.x, 1); }"
var reduce = "function reduce(key, values) { return values.length; }"
 
db.test.mapReduce(map, reduce, { out : { inline : 1 } })

The result is:

{
        "results" : [
                {
                        "_id" : 1,
                        "value" : 1
                },
                {
                        "_id" : 2,
                        "value" : 2
                }
        ],
        "timeMillis" : 12,
        "counts" : {
                "input" : 3,
                "emit" : 3,
                "reduce" : 1,
                "output" : 2
        },
        "ok" : 1
}

Now add a semicolon to the end of the string for the map function:

var map = "function map() { emit(this.x, 1); };"
 
db.test.mapReduce(map, reduce, { out : { inline : 1 } })

This time the result is:

{
        "results" : [ ],
        "timeMillis" : 14,
        "counts" : {
                "input" : 3,
                "emit" : 0,
                "reduce" : 0,
                "output" : 0
        },
        "ok" : 1
}

Note the empty results array and the 0 emit count.

Participants:

 Description   

If a string representing the map function for mapReduce ends in a semicolon the mapReduce fails silently and no results are returned.

I'm not saying this should necessarily work. But it should either work or result in an error. It's the silent part that's the problem.



 Comments   
Comment by Ramon Fernandez Marina [ 29/Nov/17 ]

This looks like a duplicate of SERVER-30009 to me.

Comment by Robert Stam [ 04/Oct/17 ]

A somewhat similar problem occurs if the reduce function string ends in a semicolon:

> var reduce = "function reduce(key, values) { return values.length; };"
> db.test.mapReduce(map, reduce, { out : { inline : 1 } })

The result is:

{
        "results" : [
                {
                        "_id" : 1,
                        "value" : 1
                },
                {
                        "_id" : 2,
                        "value" : null
                }
        ],
        "timeMillis" : 17,
        "counts" : {
                "input" : 3,
                "emit" : 3,
                "reduce" : 1,
                "output" : 2
        },
        "ok" : 1
}

Note that the result for _id 1 is correct (because reduce isn't called when there is only one value), but that the result for _id 2 is null instead of 2.

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