apache / apache/couchdb

Custom reduce functions don't work in partitioned DB

Open
#3,955 9 comments 0 reactions 0 assignees View on GitHub
beginner-friendly bug partitioned-db
Dominant language
Erlang
Stars
7k
Forks
1.1k
Avg merge
1d 16h
Merged PRs (30d)
9

Description

## Description

In a partitioned DB using custom reduce function doesn't work, all queries to that view fail with 500 error. Creating exact same view in non-partitioned db works. Built in reduce funtions work fine though.

## Steps to Reproduce

1. Set up CouchDB cluster
2. Create partitioned and non-partitioned DBs
3. Apply the example view on both
4. Try to query in both databases
5. Swap custom reduce for _count built-in
6. Now works on both DBs

## Expected Behaviour

Custom reduce functions should work fine on partitioned DB or should be documented non-working

## Your Environment

Docker Compose setup with three nodes using Docker Hub `couchdb:3` image
* CouchDB version used: 3.2.1
* Browser name and version: Postman 9.4.12
* Operating system and version: Docker container on MacOS 10.15.7

## Additional Context

Design doc:
````
{
"views": {
"one": {
"map": "mapfun = function (doc) {\n if (doc.id !== \"\") {\n emit(doc.id, 1);\n }\n};\n",
"reduce": "function(keys, values, rereduce) {\n if (rereduce) {\n return sum(values);\n } else {\n return values.length;\n }\n}"
}
},
"filters": {},
"updates": {},
"language": "javascript",
"options": {}
}
````

Error returned:
````
{
"error": "{{invalid_ejson,{p,<<\"8a1d99e3-db1d-4a8a-a61a-bac8dad0d3ba\">>,\n <<\"01d5a505-192f-44f7-bf99-2960797f1956\">>}},\n [{jiffy,encode,2,[{file,\"src/jiffy.erl\"},{line,99}]},\n {couch_os_process,writejson,2,[{file,\"src/couch_os_process.erl\"},{line,97}]},\n {couch_os_process,handle_call,3,\n [{file,\"src/couch_os_process.erl\"},{line,200}]},\n {gen_server,try_handle_call,4,[{file,\"gen_server.erl\"},{line,636}]},\n {gen_server,handle_msg,6,[{file,\"gen_server.erl\"},{line,665}]},\n {proc_lib,init_p_do_apply,3,[{file,\"proc_lib.erl\"},{line,247}]}]}",
"reason": "{gen_server,call,\n [<17837.15344.57>,\n {prompt,[<<\"reduce\">>,\n [<<\"function(keys, values, rereduce) {\\n if (rereduce) {\\n return sum(values);\\n } else {\\n return values.length;\\n }\\n}\">>],\n [[[{p,<<\"8a1d99e3-db1d-4a8a-a61a-bac8dad0d3ba\">>,\n <<\"01d5a505-192f-44f7-bf99-2960797f1956\">>},\n <<\"8a1d99e3-db1d-4a8a-a61a-bac8dad0d3ba:01d5a505-192f-44f7-bf99-2960797f1956\">>],\n 1],\n [[{p,<<\"8a1d99e3-db1d-4a8a-a61a-bac8dad0d3ba\">>,\n <<\"02d1378c-f30d-4148-b5fd-3b216f612463\">>},\n <<\"8a1d99e3-db1d-4a8a-a61a-bac8dad0d3ba:02d1378c-f30d-4148-b5fd-3b216f612463\">>],\n 1],\n [[{p,<<\"8a1d99e3-db1d-4a8a-a61a-bac8dad0d3ba\">>,\n <<\"037362ca-f266-49d2-ab19-4cc946613de6\">>},\n <<\"8a1d99e3-db1d-4a8a-a61a-bac8dad0d3ba:037362ca-f266-49d2-ab19-4cc946613de6\">>],\n 1],\n [[{p,<<\"8a1d99e3-db1d-4a8a-a61a-bac8dad0d3ba\">>,\n <<\"04a2bae3-cd74-42fd-9d5d-113c827d69de\">>},\n <<\"8a1d99e3-db1d-4a8a-a61a-bac8dad0d3ba:04a2bae3-cd74-42fd-9d5d-113c827d69de\">>],\n 1],\n [[{p,<<\"8a1d99e3-db1d-4a8a-a61a-bac8dad0d3ba\">>,\n <<\"061f26ec-36fb-41b5-a361-44bb759f3c5b\">>},\n <<\"8a1d99e3-db1d-4a8a-a61a-bac8dad0d3ba:061f26ec-36fb-41b5-a361-44bb759f3c5b\">>],\n 1],\n [[{p,<<\"8a1d99e3-db1d-4a8a-a61a-bac8dad0d3ba\">>,\n <<\"0850305e-0859-4fe2-b65b-d4f96aca0366\">>},\n <<\"8a1d99e3-db1d-4a8a-a61a-bac8dad0d3ba:0850305e-0859-4fe2-b65b-d4f96aca0366\">>],\n 1],\n [[{p,<<\"8a1d99e3-db1d-4a8a-a61a-bac8dad0d3ba\">>,\n <<\"0f4146b5-6014-4907-931d-0dc82b0d365c\">>},\n <<\"8a1d99e3-db1d-4a8a-a61a-bac8dad0d3ba:0f4146b5-6014-4907-931d-0dc82b0d365c\">>],\n 1]]]},\n infinity]}",
"ref": 157986948
}
````
Docker Compose setup:

[docker setup.zip](https://github.com/apache/couchdb/files/8224151/docker.setup.zip)

Contributor guide

Open the contributing guide

Research direction

Reproduce the partitioned and non-partitioned cases using the provided Docker Compose setup and the example design document. Inspect src/jiffy.erl and couch_os_process.erl around the reported invalid_ejson and reduce errors; done means custom reduce queries work on partitioned databases or the limitation is documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
erlang, javascript
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.