diff options
author | Paul J. Davis <paul.joseph.davis@gmail.com> | 2020-10-21 14:42:43 -0500 |
---|---|---|
committer | Paul J. Davis <paul.joseph.davis@gmail.com> | 2020-10-21 15:30:37 -0500 |
commit | ea80fe379d3548fd4b0e98827c17190fabcfd03b (patch) | |
tree | 7a84f04aabbf7a58bee49ea15237605c708056d5 | |
parent | 2428f0fcf4b36bfa88485e0f234124f28fbacf10 (diff) | |
download | couchdb-ea80fe379d3548fd4b0e98827c17190fabcfd03b.tar.gz |
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.
-rw-r--r-- | src/couch_views/src/couch_views_trees.erl | 25 | ||||
-rw-r--r-- | src/couch_views/test/couch_views_red_test.erl | 4 |
2 files changed, 7 insertions, 22 deletions
diff --git a/src/couch_views/src/couch_views_trees.erl b/src/couch_views/src/couch_views_trees.erl index d9340ad30..6c32f97f5 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}. diff --git a/src/couch_views/test/couch_views_red_test.erl b/src/couch_views/test/couch_views_red_test.erl index 707611f6e..84c64738d 100644 --- a/src/couch_views/test/couch_views_red_test.erl +++ b/src/couch_views/test/couch_views_red_test.erl @@ -213,7 +213,7 @@ should_reduce_empty_range({Db, _}) -> end_key => 100001 }, Result = run_query(Db, <<"baz_count">>, Args), - Expect = {ok, [row(null, 0)]}, + Expect = {ok, []}, ?assertEqual(Expect, Result). @@ -224,7 +224,7 @@ should_reduce_empty_range_rev({Db, _}) -> end_key => 100000 }, Result = run_query(Db, <<"baz_count">>, Args), - Expect = {ok, [row(null, 0)]}, + Expect = {ok, []}, ?assertEqual(Expect, Result). |