A newer version of this documentation is available.

# Re-reduce Argument

For `reduce()` functions, they should be both transparent and standalone. For example, the `_sum` function did not rely on global variables or parsing of existing data, and didn’t need to call itself, hence it is also transparent.

In order to handle incremental map/reduce functionality (i.e. updating an existing view), each function must also be able to handle and consume the functions own output. This is because in an incremental situation, the function must be handle both the new records, and previously computed reductions.

This can be explicitly written as follows:

`f(keys, values) = f(keys, [ f(keys, values) ])`

The following diagram shows previous reductions included within the array of information that are re-supplied to the reduce function as an element of the array of values supplied to the reduce function. That is, the input of a reduce function can be not only the raw data from the map phase, but also the output of a previous reduce phase. This is called `rereduce`, and can be identified by the third argument to the `reduce()`. When the `rereduce` argument is true, both the `key` and `values` arguments are arrays, with the corresponding element in each containing the relevant key and value. I.e., `key` is the key related to the value of `value`.

An example of this can be seen by considering an expanded version of the `sum` function showing the supplied values for the first iteration of the view index building:

`function('James', [ 13000,20000,5000 ]) {...}`

When a document with the ‘James’ key is added to the database, and the view operation is called again to perform an incremental update, the equivalent call is:

`function('James', [ 19000, function('James', [ 13000,20000,5000 ]) ]) { ... }`

In reality, the incremental call is supplied the previously computed value, and the newly emitted value from the new document:

`function('James', [ 19000, 38000 ]) { ... }`

Fortunately, the simplicity of the structure for `sum` means that the function both expects an array of numbers, and returns a number, so these can easily be recombined.

If writing more complex reductions, where a compound key is output, the `reduce()` function must be able to handle processing an argument of the previous reduction as the compound value in addition to the data generated by the `map()` phase. For example, to generate a compound output showing both the total and count of values, a suitable `reduce()` function could be written like this:

```function(key, values, rereduce) {
var result = {total: 0, count: 0};
for(i=0; i < values.length; i++) {
if(rereduce) {
result.total = result.total + values[i].total;
result.count = result.count + values[i].count;
} else {
result.total = sum(values);
result.count = values.length;
}
}
return(result);
}```

Each element of the array supplied to the function is checked using the built-in `typeof` function to identify whether the element was an object (as output by a previous reduce), or a number (from the map phase), and then updates the return value accordingly.

Using the sample sales data, and group level of two, the output from a reduced view may look like this:

```{"rows":[
{"key":["Adam", "Paris"], "value":{"total":19000, "count":1}},
{"key":["Adam", "Tokyo"], "value":{"total":17000, "count":1}},
{"key":["James","Paris"], "value":{"total":118000,"count":3}},
{"key":["James","Tokyo"], "value":{"total":20000, "count":1}},
{"key":["John", "London"],"value":{"total":10000, "count":2}},
{"key":["John", "Paris"], "value":{"total":22000, "count":1}}
]
}```

Reduce functions must be written to cope with this scenario in order to cope with the incremental nature of the view and index building. If this is not handled correctly, the index will fail to be built correctly.

The `reduce()` function is designed to reduce and summarize the data emitted during the `map()` phase of the process. It should only be used to summarize the data, and not to transform the output information or concatenate the information into a single structure.

When using a composite structure, the size limit on the composite structure within the `reduce()` function is 64KB.
{"ft_lang_label":"__label__en","ft_lang_prob":0.81750315,"math_prob":0.96832436,"size":4159,"snap":"2019-13-2019-22","text_gpt3_token_len":949,"char_repetition_ratio":0.14392298,"word_repetition_ratio":0.0,"special_character_ratio":0.2714595,"punctuation_ratio":0.19156626,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9583208,"pos_list":[0,1,2],"im_url_duplicate_count":[null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-03-21T18:03:29Z\",\"WARC-Record-ID\":\"<urn:uuid:44bf9e8e-6e93-400e-bc9a-3e20dea925a6>\",\"Content-Length\":\"139066\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4f5b45d6-b10b-4a31-ba7b-ddbd7fa98f82>\",\"WARC-Concurrent-To\":\"<urn:uuid:d4bd5348-5fec-4460-bc6f-70a626fe27c3>\",\"WARC-IP-Address\":\"13.249.44.119\",\"WARC-Target-URI\":\"https://docs.couchbase.com/server/4.6/views/views-writing-rereduce.html\",\"WARC-Payload-Digest\":\"sha1:AEUYTC2GHBE6C5PJ2KE2TQQYO2WHPXD7\",\"WARC-Block-Digest\":\"sha1:M4JHFQVSCLYXRICWIHZKWJYZYWZQ5LM2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-13/CC-MAIN-2019-13_segments_1552912202530.49_warc_CC-MAIN-20190321172751-20190321194751-00256.warc.gz\"}"}