yihui504 opened a new issue, #6088:
URL: https://github.com/apache/couchdb/issues/6088

   ### Version
   
   3.5.1
   
   ### Describe the problem you're encountering
   
   
   The documentation 
([stable/api/database/changes.html](https://docs.couchdb.org/en/stable/api/database/changes.html))
 states:
   
   > `limit` (number) – Limit number of result rows to the specified value 
(note that using `0` here has the same effect as `1`).
   
   But `limit=0` returns 0 rows, not 1:
   
   - `GET /testdb/_changes?limit=0` → `{"results":[], "last_seq":"0-...", 
"pending":N}` (**0 rows**)
   - `GET /testdb/_changes?limit=1` → `{"results":[{...}], "last_seq":"1-...", 
"pending":N-1}` (1 row)
   
   The `last_seq` also differs: `limit=0` returns `last_seq:"0-..."` (seq 
unchanged), while `limit=1` returns `last_seq:"1-..."` (seq advanced). And 
`pending` is `N` for `limit=0` vs `N-1` for `limit=1` — so `limit=0` does not 
behave as if it returned one row.
   
   
   ### Expected Behaviour
   
   
   Either `limit=0` returns 1 row (honoring the documented "same effect as 1"), 
or the documentation note should be removed/corrected.
   
   ### Steps to Reproduce
   
   
   ```bash
   docker run -d -p 5984:5984 -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=admin 
--name couchdb-t couchdb:3.5
   curl -s -X POST http://admin:admin@localhost:5984/_cluster_setup -H 
"Content-Type: application/json" \
     -d 
'{"action":"enable_single_node","bind_address":"0.0.0.0","port":5984,"singlenode":true}'
   curl -s -X PUT http://admin:admin@localhost:5984/testdb
   curl -s -X POST http://admin:admin@localhost:5984/testdb/_bulk_docs -H 
"Content-Type: application/json" \
     -d '{"docs":[{"_id":"d1","v":1},{"_id":"d2","v":2},{"_id":"d3","v":3}]}'
   
   # limit=0: 0 rows (doc says "same effect as 1")
   curl -s "http://admin:admin@localhost:5984/testdb/_changes?limit=0";
   # {"results":[],"last_seq":"0-...","pending":3}
   
   # limit=1: 1 row
   curl -s "http://admin:admin@localhost:5984/testdb/_changes?limit=1";
   # {"results":[{"seq":"1-...","id":"d1",...}],"last_seq":"1-...","pending":2}
   ```
   
   
   ### Your Environment
   
   
   - CouchDB 3.4.3 and 3.5.1/3.5.2 (official Docker images `couchdb:3.4` and 
`couchdb:3.5`)
   - Single-node setup (`_cluster_setup enable_single_node`)
   - DB with 3–7 docs
   - OS: Windows 11 host, Docker Desktop
   
   
   ### Additional Context
   
   
   ## Root cause (source)
   
   `src/couch/src/couch_changes.erl` 
([3.5.0](https://github.com/apache/couchdb/blob/3.5.0/src/couch/src/couch_changes.erl),
 line 674):
   
   ```erlang
   Go =
       if
           (Limit =< 1) andalso Results =/= [] -> stop;
           true -> ok
       end,
   ```
   
   When `Limit = 0`, the guard `Limit =< 1` is true, but because `Results =/= 
[]` is checked *before* the first row is added (the row is appended in the 
`case Results of _ ->` branch below), the loop stops with an empty result set. 
The documentation's "same effect as 1" claim predates this control flow.
   
   `diff` of `couch_changes.erl` between the 3.4.3 and 3.5.0 tags exits 0 
(byte-identical), and the behaviour reproduces on Docker `couchdb:3.5` (Server 
header reports 3.5.2).
   
   ## Resolution options
   
   1. **Code fix**: adjust the stop condition so `limit=0` returns 1 row (match 
the doc).
   2. **Doc fix**: remove the "(note that using `0` here has the same effect as 
`1`)" remark if the current 0-row behaviour is intended.
   
   Either resolves the contradiction; the current state is a 
doc-vs-implementation mismatch either way.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to