Github user davisp commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch/pull/229#discussion_r103793859
  
    --- Diff: src/couch_query_servers.erl ---
    @@ -142,25 +142,34 @@ os_rereduce(Lang, OsRedSrcs, KVs) ->
     builtin_reduce(_Re, [], _KVs, Acc) ->
         {ok, lists:reverse(Acc)};
     builtin_reduce(Re, [<<"_sum",_/binary>>|BuiltinReds], KVs, Acc) ->
    -    Sum = builtin_sum_rows(KVs),
    +    Sum = builtin_sum_rows(KVs, []),
         builtin_reduce(Re, BuiltinReds, KVs, [Sum|Acc]);
     builtin_reduce(reduce, [<<"_count",_/binary>>|BuiltinReds], KVs, Acc) ->
         Count = length(KVs),
         builtin_reduce(reduce, BuiltinReds, KVs, [Count|Acc]);
     builtin_reduce(rereduce, [<<"_count",_/binary>>|BuiltinReds], KVs, Acc) ->
    -    Count = builtin_sum_rows(KVs),
    +    Count = builtin_sum_rows(KVs, []),
         builtin_reduce(rereduce, BuiltinReds, KVs, [Count|Acc]);
     builtin_reduce(Re, [<<"_stats",_/binary>>|BuiltinReds], KVs, Acc) ->
         Stats = builtin_stats(Re, KVs),
         builtin_reduce(Re, BuiltinReds, KVs, [Stats|Acc]).
     
    -builtin_sum_rows(KVs) ->
    -    lists:foldl(fun([_Key, Value], Acc) -> sum_values(Value, Acc) end, 0, 
KVs).
     
    -sum_values({Props}, 0) ->
    -    {Props};
    -sum_values({Props}, {AccProps}) ->
    -    {sum_objects(lists:sort(Props), lists:sort(AccProps))};
    +builtin_sum_rows([], Acc) ->
    +    Acc;
    +builtin_sum_rows([[Key, Value] | RestKVs], Acc) ->
    +    try sum_values(Value, Acc) of
    +        NewAcc ->
    +            builtin_sum_rows(RestKVs, NewAcc)
    +    catch
    +        throw:{builtin_reduce_error, Obj} ->
    +            Obj;
    +        throw:{invalid_value, Reason, Cause} ->
    +            {[{<<"error">>, <<"builtin_reduce_error">>},
    --- End diff --
    
    This is because a reduction function has to operate on its own output, not 
because of recursion. What we're doing here is setting up a try/catch so that 
we can have a non-local return when we detect an error. That error might either 
be when doing a reduce and hitting a bad value emitted in the index, or it 
might be later on when we're rereducing a group of reductions that contains an 
error. In both cases we want to pass the error up immediately and cease 
recursion.
    
    Technically we could have split this into two different functions that are 
called based on reduce vs rereduce but the existing logic precluded that from 
being very simple without a massively more complicated change.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to