summaryrefslogtreecommitdiff
path: root/src/couchdb
diff options
context:
space:
mode:
authorRobert Newson <rnewson@apache.org>2010-08-19 22:48:46 +0000
committerRobert Newson <rnewson@apache.org>2010-08-19 22:48:46 +0000
commite8e814755df487f16afbd22ec59d6c69c39f7c99 (patch)
tree94e0b747ff718741acb01998ac7ae41d7ad710ba /src/couchdb
parent83f5171b8c2c8d30a9858e98679c045dc8a53a28 (diff)
COUCHDB-861 - enhance _sum to work with lists of numbers.
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@987343 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/couchdb')
-rw-r--r--src/couchdb/couch_query_servers.erl21
1 files changed, 19 insertions, 2 deletions
diff --git a/src/couchdb/couch_query_servers.erl b/src/couchdb/couch_query_servers.erl
index 92c30f82..6fc806f9 100644
--- a/src/couchdb/couch_query_servers.erl
+++ b/src/couchdb/couch_query_servers.erl
@@ -165,12 +165,29 @@ builtin_reduce(Re, [<<"_stats",_/binary>>|BuiltinReds], KVs, Acc) ->
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)};