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 69f1c6076fd29a7ef925dbeeb7fef2527ad8b68d
Author: Paul J. Davis <[email protected]>
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 +++++--------------------
 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 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}.
diff --git a/src/couch_views/test/couch_views_red_test.erl 
b/src/couch_views/test/couch_views_red_test.erl
index 707611f..84c6473 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).
 
 

Reply via email to