pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #1043: bit Jaccard distance returns 1.0 for identical all-zero bit vectors
7+ messages / 4 participants
[nested] [flat]
* [pgvector/pgvector] issue #1043: bit Jaccard distance returns 1.0 for identical all-zero bit vectors
@ 2026-10-20 03:24 "JoeyLYZ666 (@JoeyLYZ666)" <noreply+JoeyLYZ666@github.com>
0 siblings, 0 replies; 7+ messages in thread
From: JoeyLYZ666 (@JoeyLYZ666) @ 2026-10-20 03:24 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
## Summary
For `bit` values compared with pgvector’s Jaccard distance operator `<%>`, two identical all-zero bit vectors return distance `1.0`. Under the common set-distance convention `d(A, B) = 1 - |A ∩ B| / |A ∪ B|` with `d(∅, ∅) = 0`, the identical empty set should have distance `0` from itself. Hamming distance for the same pair returns `0`, so the anomaly is specific to Jaccard.
## Steps to reproduce
Run `reproduce.py` against PostgreSQL with pgvector enabled. It creates four `bit(8)` rows and queries with `00000000`:
```sql
SELECT id, b <%> '00000000'::bit(8) AS jaccard, b <~> '00000000'::bit(8) AS hamming
FROM codex_pgvector_bit_jaccard
ORDER BY b <%> '00000000'::bit(8), id;
```
[reproduce.py](https://github.com/user-attachments/files/33697916/reproduce.py)
## Expected behavior
The row with `b = '00000000'` is identical to the query. Its Jaccard distance should be `0.0` if empty-empty is treated as identical, matching the identity-of-indiscernibles MR.
## Actual behavior
The row with `b = '00000000'` returns Jaccard distance `1.0`, while Hamming distance is `0.0`.
## Metamorphic relation
Identity under self-query: for any stored vector `x`, querying with `x` should give the self row a distance of `0` for a distance operator, unless the operator explicitly rejects the input. This fails for the all-zero `bit` vector under Jaccard.
## Environment
Observed with pgvector in the local test environment used by `artifacts/codex-260925/independent10/probe_edge_semantics_batch2.py`.
## Notes on confidence
This is independent of the earlier pgvector dense/sparse cosine underflow/overflow artifacts. The remaining uncertainty is whether pgvector intentionally defines Jaccard distance for two empty bitsets as `1.0`; if that is intended, this should be treated as a documentation clarification request rather than a code bug.
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #1043: bit Jaccard distance returns 1.0 for identical all-zero bit vectors
@ 2026-10-20 04:04 "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2026-10-20 04:04 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @JoeyLYZ666, the current behavior is consistent with other libraries like scikit-learn and RDKit.
Edit: It looks like SciPy returns 0.
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #1043: bit Jaccard distance returns 1.0 for identical all-zero bit vectors
@ 2026-10-20 04:04 "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2026-10-20 04:04 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #1043: bit Jaccard distance returns 1.0 for identical all-zero bit vectors
@ 2026-10-20 09:00 "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2026-10-20 09:00 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
To expand on this, since the distance is undefined, the three main options are:
- NaN, which makes zero vectors furthest away from each other (since Postgres treats NaN values as greater than non-NaN values)
- 1, which makes zero vectors the same distance as they are to non-zero vectors
- 0, which makes zero vectors closest
It looks like SciPy switched from NaN to 0 in 2018 to make it a metric (https://github.com/scipy/scipy/pull/7373).
Will think about this a bit more. In the meantime, it'd be good to hear if there are practical applications that benefit from a specific option.
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #1043: bit Jaccard distance returns 1.0 for identical all-zero bit vectors
@ 2026-10-20 09:00 "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2026-10-20 09:00 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane reopened this.
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #1043: bit Jaccard distance returns 1.0 for identical all-zero bit vectors
@ 2026-10-30 18:23 "duongynhi000005-oss (@duongynhi000005-oss)" <noreply+duongynhi000005-oss@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: duongynhi000005-oss (@duongynhi000005-oss) @ 2026-10-30 18:23 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
I investigated #1043 and traced the all-zero bit vector edge case in `src/bitutils.c`:
### Root Cause
In `BitJaccardDistanceDefault` (`src/bitutils.c:127-130`), the function checks:
```c
if (ab == 0)
return 1;
```
When both bit vectors are all-zero (`'00000000'`), `ab == 0`, `aa == 0`, and `bb == 0`, so `BitJaccardDistanceDefault` returns `1.0` instead of `0.0` (violating $d(x, x) = 0$ and standard $d(\emptyset, \emptyset) = 0$).
### Proposed Fix
Add `if (aa + bb == 0) return 0;` before `if (ab == 0)` in `src/bitutils.c`.
I have a branch ready (`duongynhi000005-oss/pgvector:fix-issue-1043-bit-jaccard-distance-all-zero-vectors`) and would be happy to open a PR!
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #1043: bit Jaccard distance returns 1.0 for identical all-zero bit vectors
@ 2026-11-16 11:05 "catidegla (@catidegla)" <noreply+catidegla@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: catidegla (@catidegla) @ 2026-11-16 11:05 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
The three options are not equally valid for pgvector specifically, because `<%>` is an ANN distance backing an index, not just a pairwise metric, and that picks the answer. For the index to work a vector has to be its own nearest neighbor, i.e. `d(x, x) = 0` (or at least `d(x,x) <= d(x,y)` for every other y), and only option 0 gives that for the all-zero vector. With 1, the zero vector ties its own distance with its distance to arbitrary nonzero vectors, so it is no longer distinguished as the exact match; with NaN it is the worst case, because Postgres orders NaN greater than everything, so the zero vector becomes the furthest thing from itself and an all-zero query never retrieves the identical stored row. So on any dataset containing zero vectors, 1 or NaN silently drops recall for those rows, while 0 keeps them findable.
There is also an internal-consistency argument: Hamming `<~>` already returns 0 for the identical all-zero pair, so Jaccard returning 1 means the two bit-distance operators disagree about whether a vector is its own nearest neighbor. Option 0 realigns them, and it is why SciPy switched NaN to 0 in 2018, the same reasoning you half-noted, to satisfy `d(x,x) = 0`.
Concretely, building on the `src/bitutils.c` location above, the case to add is `union == 0` (both popcounts zero) returns 0, kept distinct from the `ab == 0` but `union > 0` case (disjoint nonzero sets) which correctly stays 1. So the guard becomes the union-zero check first, then the existing ab-zero branch.
^ permalink raw [nested|flat] 7+ messages in thread
end of thread, other threads:[~2026-11-16 11:05 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-20 03:24 [pgvector/pgvector] issue #1043: bit Jaccard distance returns 1.0 for identical all-zero bit vectors "JoeyLYZ666 (@JoeyLYZ666)" <noreply+JoeyLYZ666@github.com>
2026-10-20 04:04 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-10-20 04:04 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-10-20 09:00 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-10-20 09:00 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-10-30 18:23 ` "duongynhi000005-oss (@duongynhi000005-oss)" <noreply+duongynhi000005-oss@github.com>
2026-11-16 11:05 ` "catidegla (@catidegla)" <noreply+catidegla@github.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox