couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rnew...@apache.org
Subject svn commit: r987343 - in /couchdb/trunk: share/www/script/test/reduce_builtin.js src/couchdb/couch_query_servers.erl
Date Thu, 19 Aug 2010 22:48:47 GMT
Author: rnewson
Date: Thu Aug 19 22:48:46 2010
New Revision: 987343

URL: http://svn.apache.org/viewvc?rev=987343&view=rev
Log:
COUCHDB-861 - enhance _sum to work with lists of numbers.

Modified:
    couchdb/trunk/share/www/script/test/reduce_builtin.js
    couchdb/trunk/src/couchdb/couch_query_servers.erl

Modified: couchdb/trunk/share/www/script/test/reduce_builtin.js
URL: http://svn.apache.org/viewvc/couchdb/trunk/share/www/script/test/reduce_builtin.js?rev=987343&r1=987342&r2=987343&view=diff
==============================================================================
--- couchdb/trunk/share/www/script/test/reduce_builtin.js (original)
+++ couchdb/trunk/share/www/script/test/reduce_builtin.js Thu Aug 19 22:48:46 2010
@@ -149,5 +149,30 @@ couchTests.reduce_builtin = function(deb
       T(equals(results.rows[5], {key:["d","b"],value:10*i}));
       T(equals(results.rows[6], {key:["d","c"],value:10*i}));
     };
+
+    map = function (doc) {emit(doc.keys, [1, 1])};
+
+    var results = db.query(map, "_sum", {group:true});
+    T(equals(results.rows[0], {key:["a"],value:[20*i,20*i]}));
+    T(equals(results.rows[1], {key:["a","b"],value:[20*i,20*i]}));
+    T(equals(results.rows[2], {key:["a", "b", "c"],value:[10*i,10*i]}));
+    T(equals(results.rows[3], {key:["a", "b", "d"],value:[10*i,10*i]}));
+
+    var results = db.query(map, "_sum", {group: true, limit: 2});
+    T(equals(results.rows[0], {key: ["a"], value: [20*i,20*i]}));
+    T(equals(results.rows.length, 2));
+
+    var results = db.query(map, "_sum", {group_level:1});
+    T(equals(results.rows[0], {key:["a"],value:[70*i,70*i]}));
+    T(equals(results.rows[1], {key:["d"],value:[40*i,40*i]}));
+
+    var results = db.query(map, "_sum", {group_level:2});
+    T(equals(results.rows[0], {key:["a"],value:[20*i,20*i]}));
+    T(equals(results.rows[1], {key:["a","b"],value:[40*i,40*i]}));
+    T(equals(results.rows[2], {key:["a","c"],value:[10*i,10*i]}));
+    T(equals(results.rows[3], {key:["d"],value:[10*i,10*i]}));
+    T(equals(results.rows[4], {key:["d","a"],value:[10*i,10*i]}));
+    T(equals(results.rows[5], {key:["d","b"],value:[10*i,10*i]}));
+    T(equals(results.rows[6], {key:["d","c"],value:[10*i,10*i]}));
   }
 }

Modified: couchdb/trunk/src/couchdb/couch_query_servers.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_query_servers.erl?rev=987343&r1=987342&r2=987343&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_query_servers.erl (original)
+++ couchdb/trunk/src/couchdb/couch_query_servers.erl Thu Aug 19 22:48:46 2010
@@ -165,12 +165,29 @@ builtin_reduce(Re, [<<"_stats",_/binary>
 
 builtin_sum_rows(KVs) ->
     lists:foldl(fun
-        ([_Key, Value], Acc) when is_number(Value) ->
+        ([_Key, Value], Acc) when is_number(Value), is_number(Acc) ->
             Acc + Value;
+        ([_Key, Value], Acc) when is_list(Value), is_list(Acc) ->
+            sum_terms(Acc, Value);
+        ([_Key, Value], Acc) when is_number(Value), is_list(Acc) ->
+            sum_terms(Acc, [Value]);
+        ([_Key, Value], Acc) when is_list(Value), is_number(Acc) ->
+            sum_terms([Acc], Value);
         (_Else, _Acc) ->
-            throw({invalid_value, <<"builtin _sum function requires map values to be
numbers">>})
+            throw({invalid_value, <<"builtin _sum function requires map values to be
numbers or lists of numbers">>})
     end, 0, KVs).
 
+sum_terms([], []) ->
+    [];
+sum_terms([_|_]=Xs, []) ->
+    Xs;
+sum_terms([], [_|_]=Ys) ->
+    Ys;
+sum_terms([X|Xs], [Y|Ys]) when is_number(X), is_number(Y) ->
+    [X+Y | sum_terms(Xs,Ys)];
+sum_terms(_, _) ->
+    throw({invalid_value, <<"builtin _sum function requires map values to be numbers
or lists of numbers">>}).
+
 builtin_stats(reduce, [[_,First]|Rest]) when is_number(First) ->
     Stats = lists:foldl(fun([_K,V], {S,C,Mi,Ma,Sq}) when is_number(V) ->
         {S+V, C+1, erlang:min(Mi,V), erlang:max(Ma,V), Sq+(V*V)};



Mime
View raw message