You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@couchdb.apache.org by "Adam Kocoloski (JIRA)" <ji...@apache.org> on 2010/08/19 16:42:16 UTC
[jira] Commented: (COUCHDB-861) Enhance _sum to handle lists of
numbers.
[ https://issues.apache.org/jira/browse/COUCHDB-861?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12900291#action_12900291 ]
Adam Kocoloski commented on COUCHDB-861:
----------------------------------------
I worked on something like this with Ben Anderson. I agree, it's a useful extension. Would you want to generalize it so that the lists of different lengths don't crash the reducer? Perhaps
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)].
> Enhance _sum to handle lists of numbers.
> ----------------------------------------
>
> Key: COUCHDB-861
> URL: https://issues.apache.org/jira/browse/COUCHDB-861
> Project: CouchDB
> Issue Type: Improvement
> Components: Database Core
> Reporter: Robert Newson
>
> _sum can currently only sum a single value but it is often useful to emit an array of numbers on the value side.
> The code below achieves this, I'm working on a test.
> diff --git a/src/couchdb/couch_query_servers.erl b/src/couchdb/couch_query_servers.erl
> index 92c30f8..e2a748e 100644
> --- a/src/couchdb/couch_query_servers.erl
> +++ b/src/couchdb/couch_query_servers.erl
> @@ -167,8 +167,13 @@ builtin_sum_rows(KVs) ->
> lists:foldl(fun
> ([_Key, Value], Acc) when is_number(Value) ->
> Acc + Value;
> + ([_Key, Value], Acc) when is_list(Value) ->
> + case Acc of
> + 0 -> Value;
> + _ -> lists:zipwith(fun(X, Y) -> X + Y end, Acc, Value)
> + end;
> (_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
> end, 0, KVs).
>
> builtin_stats(reduce, [[_,First]|Rest]) when is_number(First) ->
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.