[SERVER-4916] finalize output is ignored unless top-level property is changed Created: 09/Feb/12  Updated: 06/Dec/22  Resolved: 04/Feb/22

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

Type: Bug Priority: Major - P3
Reporter: Keith Grennan Assignee: Backlog - Query Optimization
Resolution: Done Votes: 0
Labels: qopt-team, query-44-grooming
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File mapreduceissue.js    
Issue Links:
Related
Assigned Teams:
Query Optimization
Operating System: ALL
Participants:

 Description   

db.bug.insert({tag: "foo", date:ISODate("2012-02-09T08:39:20Z")});
db.bug.insert({tag: "bar", date:ISODate("2012-02-09T08:39:20Z")});
m = function(){emit(this.tag,this)};
r = function(k,v){return v[0]};
f = function(k,v){v.date.setUTCHours(0);return v;}
 
// bug: finalize output is not used, dates still have non-zero hours.
db.bug.mapReduce(m, r, {"out":{inline:1}, "finalize":f});
 
// workaround: make trivial change to finalize output so that m/r framework picks up the change:
f = function(k,v){v.date.setUTCHours(0); v.x = 1; return v;}
db.bug.mapReduce(m, r, {"out":{inline:1}, "finalize":f});

The results from the first MR:

	"results" : [
		{
			"_id" : "bar",
			"value" : {
				"_id" : ObjectId("4f338638400b97e04a51941e"),
				"tag" : "bar",
				"date" : ISODate("2012-02-09T08:39:20.396Z")
			}
		},
		{
			"_id" : "foo",
			"value" : {
				"_id" : ObjectId("4f338631400b97e04a51941d"),
				"tag" : "foo",
				"date" : ISODate("2012-02-09T08:39:13.820Z")
			}
		}
	],

The results from the second MR:

	"results" : [
		{
			"_id" : "bar",
			"value" : {
				"_id" : ObjectId("4f338638400b97e04a51941e"),
				"tag" : "bar",
				"date" : ISODate("2012-02-09T00:39:20.396Z"),
				"x" : 1
			}
		},
		{
			"_id" : "foo",
			"value" : {
				"_id" : ObjectId("4f338631400b97e04a51941d"),
				"tag" : "foo",
				"date" : ISODate("2012-02-09T00:39:13.820Z"),
				"x" : 1
			}
		}
	],



 Comments   
Comment by Esha Bhargava [ 04/Feb/22 ]

Closing these tickets as part of the deprecation of mapReduce.

Comment by Charlie Swanson [ 09/Mar/20 ]

Looks like this is still a bug on master, even after our recent project to reimplement mapReduce in terms of the aggregation framework. Flagging for scheduling by the query team.

Comment by Antoine Girbal [ 16/Feb/12 ]

problem is probably that the object is not marked as modified and hence the original bson code is used.
Here it's an inner field with a native type that gets modified through a method.
To be defensive we would need to mark object modified when such a type is getting accessed (same as for subobjects) but that means it'll be quite slower.

Comment by Keith Grennan [ 09/Feb/12 ]

Yeah, i realize this is kind of a weird example. I just tried to come up with the most trivial example possible that illustrates the behavior.

The reduce function in this example just returns the first value it is given (the v passed to reduce is an array of values), so it is guaranteed to be the same format as the map output, as far as I can tell.

Comment by Scott Hernandez (Inactive) [ 09/Feb/12 ]

It looks like the problem may be with your reduce function. You are not emit'n the same type of object in your map as your reduce is returning.

See the notes right above finalize: http://www.mongodb.org/display/DOCS/Mapreduce#MapReduce-FinalizeFunction

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