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

    https://github.com/apache/couchdb-couch/pull/229#discussion_r103788697
  
    --- 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 --
    
    Why we have to handle the same error twice. Am I understanding it 
correctly? 
    1. we catch invalid_value
    2. we return `{[{<<"error">>, ...}]}`
    3. on the outer recursion call we go into sum_values
    4. sum_values detects the `{[{<<"error">>, ...}]}` and throws 
`{builtin_reduce_error,...`
    5. we catch `{builtin_reduce_error,...` and return Obj which is our 
`{[{<<"error">>, ...}]}`
    
    Sounds complicated.


---
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