This is an automated email from the ASF dual-hosted git repository. davisp pushed a commit to branch fix-empty-reduce-output in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit 4d28a6e9c6c3502c8566265abe761eb1937cb026 Author: Paul J. Davis <paul.joseph.da...@gmail.com> AuthorDate: Wed Oct 21 14:42:43 2020 -0500 Fix empty reduce output to match 3.x behavior Before this change a reduce call that contained no rows would end up returning the "default" value of the given reduce function which is whatever it would return when given an empty array as input. This changes the behavior to return `{"rows": []"}` when there are no rows in the requested range. --- src/couch_views/src/couch_views_trees.erl | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/src/couch_views/src/couch_views_trees.erl b/src/couch_views/src/couch_views_trees.erl index d9340ad..6c32f97 100644 --- a/src/couch_views/src/couch_views_trees.erl +++ b/src/couch_views/src/couch_views_trees.erl @@ -144,15 +144,8 @@ fold_red_idx(TxDb, View, Idx, Options, Callback, Acc0) -> Callback(GroupKey, RedValue, WAcc) end, - case {GroupKeyFun, Dir} of - {group_all, fwd} -> - EBtreeOpts = [ - {dir, fwd}, - {inclusive_end, InclusiveEnd} - ], - Reduction = ebtree:reduce(Tx, Btree, StartKey, EndKey, EBtreeOpts), - Wrapper({null, Reduction}, Acc0); - {F, fwd} when is_function(F) -> + case Dir of + fwd -> EBtreeOpts = [ {dir, fwd}, {inclusive_end, InclusiveEnd} @@ -167,16 +160,7 @@ fold_red_idx(TxDb, View, Idx, Options, Callback, Acc0) -> Acc0, EBtreeOpts ); - {group_all, rev} -> - % Start/End keys swapped on purpose because ebtree. Also - % inclusive_start for same reason. - EBtreeOpts = [ - {dir, rev}, - {inclusive_start, InclusiveEnd} - ], - Reduction = ebtree:reduce(Tx, Btree, EndKey, StartKey, EBtreeOpts), - Wrapper({null, Reduction}, Acc0); - {F, rev} when is_function(F) -> + rev -> % Start/End keys swapped on purpose because ebtree. Also % inclusive_start for same reason. EBtreeOpts = [ @@ -404,8 +388,9 @@ to_red_opts(Options) -> {Dir, StartKey, EndKey, InclusiveEnd} = to_map_opts(Options), GroupKeyFun = case lists:keyfind(group_key_fun, 1, Options) of + {group_key_fun, group_all} -> fun({_Key, _DocId}) -> null end; {group_key_fun, GKF} -> GKF; - false -> fun({_Key, _DocId}) -> global_group end + false -> fun({_Key, _DocId}) -> null end end, {Dir, StartKey, EndKey, InclusiveEnd, GroupKeyFun}.