Skip to content

fix: reject overflowing vec_add results - #366

Open
shenxi200-eng wants to merge 1 commit into
asg017:mainfrom
shenxi200-eng:fix/vec-add-overflow-errors
Open

shenxi200-eng wants to merge 1 commit into
asg017:mainfrom
shenxi200-eng:fix/vec-add-overflow-errors

Conversation

@shenxi200-eng

@shenxi200-eng shenxi200-eng commented Nov 9, 2026 •

Copy link
Copy Markdown

Proposed behavior — maintainer policy confirmation requested

Refs #363.

The current API reference does not specify overflow behavior. This PR proposes SQLite errors while retaining the existing result type, rather than returning wrapped int8 values or non-finite float32 vectors. Please confirm the intended policy before merging. This is an intentional compatibility change, not a claim that error-on-overflow is already documented behavior.

Changes

  • Accumulate an int8 pair in int, check INT8_MIN..INT8_MAX, then narrow.
  • Check the stored float32 result with isfinite() before returning the result blob. In particular, retain ordinary float32 rounding: a sum that rounds to finite FLT_MAX is still accepted.
  • On overflow, free the output and use the existing cleanup path for both inputs; never publish a partial vector.
  • Document the behavior change and add 25 regression cases covering both signs, first/middle/last positions, JSON/BLOB inputs, connection reuse, exact int8 boundaries, float32 rounding/cancellation, and result subtype.

No changes to vec_sub, distance/normalization/index kernels, allocation-size handling, dependencies, version, or CI.

Validation

Linux aarch64 scalar/default ARM paths; GCC 13.3, CPython 3.12.3, SQLite runtime 3.45.1 with official 3.45.3 build headers, NumPy 1.26.4. No GPU or model downloads.

  • Original main full Python suite: 239 passed, 66 skipped; 204 snapshots passed.
  • New regression cases against the original compiled extension: 24 failed, 1 passed; corrected extension: 25 passed.
  • Final full Python suite: 264 passed, 66 skipped; 204 snapshots passed. The optional IVF backend is disabled; existing unrelated warnings remain.
  • An additional public SQL API oracle exhausted all 65,536 int8 pairs: 49,152 exact in-range results and 16,384 expected overflow errors. It also checked 2,324 float32 pairs against NumPy at the byte level and ran 2,000 JSON-input overflow/recovery calls. All passed on normal and UBSan builds (halt_on_error=1). These additional checks are local validation, not added dependencies/tests.
  • git diff --check passes; the new Python test region conforms to Black. Full-file formatting has pre-existing differences, so no global reformat was applied.
  • Normal baseline/head GCC warning messages match (13 each); UBSan baseline/head warning messages match (11 each). No warnings-clean build is claimed.

Commands (after the repository's normal vendor/header setup):

make -B loadable OMIT_SIMD=1
python -m pytest -q tests/test-*.py

The local environment lacked envsubst, so its ignored generated header was expanded with Python using the Makefile's template values; local rebuilds used make -B -o sqlite-vec.h loadable OMIT_SIMD=1. No build files were changed.

ASan built but could not start in this proot environment: its allocator address-range reservation failed before test execution. ASan/leak validation is therefore incomplete. SIMD, Windows/macOS, experimental IVF, and the complete CI matrix were not tested. No performance improvement is claimed.

AI assisted the implementation, tests, and code review.

@shenxi200-eng
shenxi200-eng marked this pull request as ready for review November 10, 2026 09:08

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.

1 participant