Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add _approx_count_distinct as a builtin reduce function #1346

Merged
merged 12 commits into from
Jun 6, 2018
Prev Previous commit
Next Next commit
Rename to _approx_count_distinct and return an int
  • Loading branch information
kocolosk committed May 28, 2018
commit 5802afd342b10a39d2266f4244b3607e03d94be3
10 changes: 5 additions & 5 deletions src/couch/src/couch_query_servers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ finalize(Reductions) ->
{ok, lists:map(fun(Reduction) ->
case hyper:is_hyper(Reduction) of
true ->
hyper:card(Reduction);
round(hyper:card(Reduction));
false ->
Reduction
end
Expand Down Expand Up @@ -183,8 +183,8 @@ builtin_reduce(rereduce, [<<"_count",_/binary>>|BuiltinReds], KVs, Acc) ->
builtin_reduce(Re, [<<"_stats",_/binary>>|BuiltinReds], KVs, Acc) ->
Stats = builtin_stats(Re, KVs),
builtin_reduce(Re, BuiltinReds, KVs, [Stats|Acc]);
builtin_reduce(Re, [<<"_distinct",_/binary>>|BuiltinReds], KVs, Acc) ->
Distinct = count_distinct_keys(Re, KVs),
builtin_reduce(Re, [<<"_approx_count_distinct",_/binary>>|BuiltinReds], KVs, Acc) ->
Distinct = approx_count_distinct(Re, KVs),
builtin_reduce(Re, BuiltinReds, KVs, [Distinct|Acc]).


Expand Down Expand Up @@ -318,11 +318,11 @@ get_number(Key, Props) ->
end.

% TODO allow customization of precision in the ddoc.
count_distinct_keys(reduce, KVs) ->
approx_count_distinct(reduce, KVs) ->
lists:foldl(fun([[Key, _Id], _Value], Filter) ->
hyper:insert(term_to_binary(Key), Filter)
end, hyper:new(11), KVs);
count_distinct_keys(rereduce, Reds) ->
approx_count_distinct(rereduce, Reds) ->
hyper:union([Filter || [_, Filter] <- Reds]).

% use the function stored in ddoc.validate_doc_update to test an update.
Expand Down
2 changes: 1 addition & 1 deletion src/couch_mrview/src/couch_mrview.erl
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ validate(DbName, DDoc) ->
ok;
({_RedName, <<"_stats", _/binary>>}) ->
ok;
({_RedName, <<"_distinct", _/binary>>}) ->
({_RedName, <<"_approx_count_distinct", _/binary>>}) ->
ok;
({_RedName, <<"_", _/binary>> = Bad}) ->
Msg = ["`", Bad, "` is not a supported reduce function."],
Expand Down