You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by "davidchambers (via GitHub)" <gi...@apache.org> on 2025/06/30 13:02:25 UTC
[I] `$and` does not observe vacuous truth [couchdb]
davidchambers opened a new issue, #5582:
URL: https://github.com/apache/couchdb/issues/5582
I have three documents in my database:
- `{"name": "foo"}`
- `{"name": "bar"}`
- `{"name": "baz"}`
I ran the following Mango query:
```json
{
"selector": {
"name": {
"$and": []
}
}
}
```
I expected all three documents to be returned, due to [vacuous truth][1]. Instead, no documents were returned.
I investigated and found code for handling [empty `$and`][2] and [empty `$or`][3]:
```erlang
match({[{<<"$and">>, []}]}, _, _) ->
true;
```
```erlang
match({[{<<"$or">>, []}]}, _, _) ->
true;
```
Confusingly, it's actually the `$or` code that looks wrong to me. :thinking:
[1]: https://en.wikipedia.org/wiki/Vacuous_truth
[2]: https://github.com/apache/couchdb/blob/3.5.0/src/mango/src/mango_selector.erl#L367-L368
[3]: https://github.com/apache/couchdb/blob/3.5.0/src/mango/src/mango_selector.erl#L372-L373
--
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: notifications-unsubscribe@couchdb.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] `$and` does not observe vacuous truth [couchdb]
Posted by "davidchambers (via GitHub)" <gi...@apache.org>.
davidchambers commented on issue #5582:
URL: https://github.com/apache/couchdb/issues/5582#issuecomment-3089184681
Thanks for your response, @rnewson. :)
It's a shame that special cases such as this will continue to be necessary:
```kotlin
// It should be acceptable to use {"creationEpoch": {"$and": []}} when neither createdAfterInclusive
// nor createdBeforeExclusive is specified, but CouchDB does not understand vacuous truth. 🙈
val creationSelector = listOfNotNull(
f.createdAfterInclusive?.let { mapOf("\$gte" to it.toNanoEpoch().epoch.toString()) },
f.createdBeforeExclusive?.let { mapOf("\$lt" to it.toNanoEpoch().epoch.toString()) },
).toNonEmptyListOrNull()?.let { mapOf("creationEpoch" to mapOf("\$and" to it)) } ?: emptyMap()
```
I'd like to be able to write this instead:
```kotlin
val creationSelector = mapOf(
"creationEpoch" to mapOf(
"\$and" to listOfNotNull(
f.createdAfterInclusive?.let { mapOf("\$gte" to it.toNanoEpoch().epoch.toString()) },
f.createdBeforeExclusive?.let { mapOf("\$lt" to it.toNanoEpoch().epoch.toString()) },
),
),
)
```
> we are happy that `"$and": []` does not vacuously match everything
Could you elaborate? Are you concerned about backwards compatibility, or is there some reason that CouchDB should behave differently from other programming contexts?
Haskell:
```haskell
ghci> all even []
True
```
Python:
```python
>>> all(x % 2 == 0 for x in [])
True
```
JavaScript:
```javascript
> [].every(x => x % 2 === 0)
true
```
Clojure:
```clojure
user=> (every? even? [])
true
user=> (and)
true
```
--
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: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] `$and` does not observe vacuous truth [couchdb]
Posted by "ArkadySkv (via GitHub)" <gi...@apache.org>.
ArkadySkv commented on issue #5582:
URL: https://github.com/apache/couchdb/issues/5582#issuecomment-3053025123
Do you mean as in MongoDB/PostgreSQL query behavior?
--
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: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] `$and` does not observe vacuous truth [couchdb]
Posted by "rnewson (via GitHub)" <gi...@apache.org>.
rnewson commented on issue #5582:
URL: https://github.com/apache/couchdb/issues/5582#issuecomment-3063157178
interesting question but I think we are happy that `"$and": []` does not vacuously match everything. I'm closing this as won't fix, but thanks for the ticket!
--
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: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] `$and` does not observe vacuous truth [couchdb]
Posted by "rnewson (via GitHub)" <gi...@apache.org>.
rnewson closed issue #5582: `$and` does not observe vacuous truth
URL: https://github.com/apache/couchdb/issues/5582
--
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: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] `$and` does not observe vacuous truth [couchdb]
Posted by "davidchambers (via GitHub)" <gi...@apache.org>.
davidchambers commented on issue #5582:
URL: https://github.com/apache/couchdb/issues/5582#issuecomment-3057005385
@ArkadySkv, I don't understand your question. This issue concerns the behaviour of the `$and` [operator][1] of the Mango query language for CouchDB.
[1]: https://docs.couchdb.org/en/stable/ddocs/mango.html#combination-operators
--
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: notifications-unsubscribe@couchdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org