I am building a very simple prototype Map/Reduce script just to get an idea of the data flow through each phase. I am seeing what I consider unexpected behaviour when passing data from the map phase to the reduce phase. It seems that no matter what type of data is written to the context during map, it will always come out as Strings during reduce.
Consider the following code (the module define and return object have been removed for brevity, but they are correct):
/** * The input data is just a list of Strings. */ function getInputData() { return ["a", "string", "array", "for", "testing", "data"]; } /** * The map phase gets the length of each String from the input data and * writes it back to the context in three different formats: Number, String, * Object */ function map(context) { log.debug(arguments.callee.name, context); context.write('numberData', context.value.length); context.write('stringData', context.value.length.toString()); context.write('objectData', {'length' : context.value.length}); } /** * The reduce phase currently just logs what it receives */ function reduce(context) { log.debug(arguments.callee.name + ':' + context.key, context.values); } function summarize(summary) { log.debug(arguments.callee.name, summary); }With this code, I would expect to see the following log statements from the reduce phase: [LIST=1][*]reduce:numberData | [1,6,5,3,7,4][*]reduce:stringData | ["1","6","5","3","7","4"][*]reduce.objectData | [{length:1},{length:6},{length:5},{length:3},{length:7},{length:4}][/LIST] Instead, I see that in each case, the elements are always converted to Strings: [LIST=1][*]reduce:numberData | ["1","6","5","3","7","4"][*]reduce:stringData | ["1","6","5","3","7","4"][*]reduce.objectData | ["{"length":"1"}","{"length":"6"}","{"length":"5"}","{"length":"3"}","{"length":"7"}","{"length":"4"}"][/LIST] This means that in order to process my data during the reduce phase, I will always have to expect an Array of Strings and convert accordingly. This seems like a completely unnecessary step to have to do. If I build an Array of Numbers in map, I would expect reduce to receive an Array of Numbers. I shouldn't have to convert my data twice.Is this intended behaviour by NetSuite? Is there a step or setting I am missing on the context?To illustrate with a more concrete example, let's say I want to take that list of Strings and calculate the sum of all of the lengths. In a general map/reduce flow, I could simply map the Array of Strings to an Array of the lengths, then reduce the lengths Array to a sum.In native JavaScript, that might look something like: function lengthMapper(currentValue, index, array) { return currentValue.length; } function sumReducer(currentValue, previousValue, index, array) { return previousValue + currentValue; } ["a", "string", "array", "for", "testing", "data"].map(lengthMapper).reduce(sumReducer, 0); // Returns 26, as expected A corresponding Map/Reduce script I would EXPECT to work would look something like:/** * The input data is just a list of Strings. */ function getInputData() { return ["a", "string", "array", "for", "testing", "data"]; } /** * The map phase maps each String to its length */ function map(context) { log.debug(arguments.callee.name, context); context.write('lengths', context.value.length); } /** * The reduce phase sums the lengths array */ function reduce(context) { log.debug(arguments.callee.name, context.values); context.write('sum', context.values.reduce(sumReducer, 0)); } /** * Reducer function for calculating the sum */ function sumReducer(currentValue, previousValue, index, array) { return previousValue + currentValue; } function summarize(summary) { log.debug(arguments.callee.name, summary); }However, instead of 26 as I would expect, instead this code returns "165374" - a concatenation of all the lengths as a single String. To get this to work correctly, I need to perform another type conversion inside my reduce phase to get Numbers, as I expect: function getInputData() { return ["a", "string", "array", "for", "testing", "data"]; } function map(context) { log.debug(arguments.callee.name, context); context.write('lengths', context.value.length); } /** * The reduce phase has to parse the values as numbers before it can sum the lengths array */ function reduce(context) { log.debug(arguments.callee.name, context.values); // Forced to do another mapping inside my reduce phase because all values are strings var intValues = context.values.map(toIntegerMapper); context.write('sum', context.values.reduce(sumReducer, 0)); } /** * Map function to convert strings to integers */ function toIntegerMapper(currentValue, index, array) { return parseInt(currentValue, 10) || 0; } /** * Reducer function for calculating the sum */ function sumReducer(currentValue, previousValue, index, array) { return previousValue + currentValue; } function summarize(summary) { log.debug(arguments.callee.name, summary); }Am I doing it wrong? Am I thinking about the map/reduce workflow wrong? Is this a bug? Is this a feature?