Skip to content

fix: avoid overflow in large int8 L1 distances - #365

Open
flysheep-ai wants to merge 1 commit into
asg017:mainfrom
flysheep-ai:fix/int8-l1-overflow
Open

flysheep-ai wants to merge 1 commit into
asg017:mainfrom
flysheep-ai:fix/int8-l1-overflow

Conversation

@flysheep-ai

Copy link
Copy Markdown

vec_distance_l1(vec_int8(a), vec_int8(b)) can return a negative SQLite integer when the exact distance exceeds INT32_MAX. For example, 8,500,001 components of 127 versus -128 should yield 2,167,500,255, but currently return -2,127,467,041.

Fixes #362.

Use a 64-bit scalar accumulator and dispatcher return type, and return the result through sqlite3_result_int64. Keep the existing NEON kernel for dimensions whose worst-case distance (255 per component) fits in a signed 32-bit integer; use the 64-bit scalar implementation above that bound. This preserves the optimized path for ordinary embedding sizes and avoids modifying the NEON kernel being discussed in #322. Very large ARM vectors may run more slowly through the fallback; no performance improvement is claimed.

The regression checks exact SQL integer results immediately below and above the 32-bit limit, plus an odd larger dimension. It also checks symmetry and the zero distance of identical vectors. Both an ARM NEON build and an OMIT_SIMD=1 build failed two of the three new cases before this patch.

Validation on macOS arm64 with Python 3.12 and the repository's managed test environment:

./scripts/vendor.sh
uv sync --directory tests --python 3.12
make -B loadable OMIT_SIMD=1
uv run --managed-python --project tests pytest tests/test-loadable.py -q
make -B loadable
uv run --managed-python --project tests pytest tests/test-loadable.py -q
uv run --managed-python --project tests pytest tests/test-knn-distance-constraints.py tests/test-insert-delete.py tests/test-general.py -q

Each build passed 44 tests with 4 existing skips. The related suite passed 30 tests and 17 snapshots. The builds retain an existing unused-parameter warning; the Python suite retains NumPy numerical warnings and an existing invalid-escape SyntaxWarning on fresh parsing. git diff --check passes. Black reports pre-existing formatting differences on both HEAD and the modified file, with no changes requested in the new test. clang-format was unavailable; no claim of a passing full C formatting check. Linux, Windows, x86/AVX, and performance benchmarks were not run.

The scalar SQL API accepts these large blobs; this does not imply that vec0 virtual tables support the same dimensions. There is no schema, stored-data, or SQL return-type change: the result remains a SQLite integer, now with its full 64-bit range.

AI assisted the investigation, implementation, tests, and draft.

Signed-off-by: 沈飞洋 <sfyhdu@gmail.com>
vlasky added a commit to vlasky/sqlite-vec that referenced this pull request Nov 8, 2026
…olumn types

Ports, as they stand, three open pull requests against asg017/sqlite-vec
whose bugs reproduce on this fork:

- asg017#323 by ariannamethod: vec_each() read bit vectors most
  significant bit first, while vec_to_json(), vec_quantize_binary() and the
  stored bitmaps are least significant bit first. The API reference example
  showed the wrong order too.
- asg017#365 by flysheep-ai (upstream issue asg017#362):
  vec_distance_l1() on int8 vectors summed in 32 bits and wrapped negative
  from 8,421,505 elements at opposite extremes. The sum is now 64-bit and
  the NEON kernel is only used within its 32-bit range. vec0 columns are
  capped at 8192 dimensions and were never affected.
- asg017#310 by stumpylog: vector column types were matched by
  prefix, so float16[4], floaty[4] and bitcoin[8] were accepted as float and
  bit columns. The name must now match exactly; float32 stays accepted. Two
  tests declared a column as float8[1] and now say float[1].

Tests for the first two follow with the fork's own fixes.
@vlasky

vlasky commented Nov 9, 2026

Copy link
Copy Markdown

For anyone waiting on this: the community fork vlasky/sqlite-vec has merged this change as is in v0.2.9-alpha (changelog). The int8 L1 distance sums in 64 bits and the NEON kernel is only used within its 32-bit range; vec0 columns were never affected because of the 8192-dimension cap. Thanks for the fix.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] vec_distance_L1 wraps a valid int8 Manhattan distance at INT32_MAX

2 participants