You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Nicholas Retallack <ni...@gmail.com> on 2008/08/15 13:50:03 UTC

Reduce is Terrible Quirky!

Hi guys.  I'm using CouchDBX, which claims to be couchdb version 0.8.
Reduce is acting really funny for me, so I tried logging it in some
temporary views, and this is what I found out:

It seems reduce is always called at least twice.  When groups=true is in the
query, it runs once with all values and then again for each group of
values.  When groups=true is not in the query, it is called with all of the
values, twice.

Now, I thought perhaps there was a way to tell whether I was in the first
call to reduce or not.  Other people seem to be using this third parameter
to reduce (see http://jchris.mfdz.com/posts/107).  But for me, that third
paramater always comes up false!

Upon further investigation, I noticed that the first call to reduce doesn't
seem to affect anything.  It's as if it is simply run and discarded.  To
illustrate, try using this as your reduce function:
function(keys,values,combine){ log(values); return "foo" }

Your final result will be "foo" of course, but look at what you've just
logged.  If you're not using group=true, it'll log the same values both
times (They may show up in different orders if you're not strict about
sorting, but that seems common with couchdb).  If you're using group=true,
the first call will log all the values, and the remaining calls will have
groups of values.  What happened to that "foo" I returned from the first
call to reduce?

I noticed a curious thing while trying to sort my output: if you have a
stable sorting order, the first call to reduce receives its values in the
proper sorted order, but the remaining calls (the one you get results from)
receive values in precisely the opposite order!

In fact, this has a very visible consequence.  Simply adding a reduce
function like this will effectively reverse the sort order of your output:
function(keys,values,combine){ return values }

If you're using groups=true, it wont reverse the order your groups come out
in, but the values inside your groups will come out sorted precisely the
opposite of the way you would have expected.

This is pretty crazy!  Sure, I can work around all these weird little
quirks, but I really think someone should look at this before more people
get confused.