Skip to content

PS-11167: Add DISTANCE() for VECTOR data type#5976

Open
catalinbp wants to merge 1 commit into
percona:9.7from
catalinbp:ps-11167
Open

PS-11167: Add DISTANCE() for VECTOR data type#5976
catalinbp wants to merge 1 commit into
percona:9.7from
catalinbp:ps-11167

Conversation

@catalinbp

Copy link
Copy Markdown
Contributor

Implement SQL DISTANCE(vector, vector, metric) and the VECTOR_DISTANCE() synonym for vector similarity queries. Supported metrics: EUCLIDEAN (L2), MANHATTAN (L1), COSINE, and DOT.

Core library (vector-common/vector_distance.*):

  • Runtime SIMD dispatch across Scalar, SSE4.2/NEON, AVX2, AVX-512F, and SVE2 tiers; per-kernel target attributes, no global -march=native
  • Dim-aware wide/narrow dispatch (dims >= 16 use widest tier; smaller vectors use 128-bit tier to avoid AVX setup overhead)
  • Unaligned load intrinsics throughout: VECTOR data may be misaligned (e.g. SUBSTR blobs); on modern CPUs unaligned and aligned loads have identical throughput when data is aligned; aligned loads would fault on misaligned inputs without a performance benefit. Loads that span a 64-byte cache-line boundary may still be slower (two line fetches); that depends on runtime address, not on loadu vs load, and is not avoided by switching to aligned intrinsics
  • Float32 SIMD accumulation with double-precision horizontal sum and scalar tail: preserves correctness for large dimensions and extreme values (e.g. 2e38 Euclidean distance) without sacrificing SIMD width
  • Manhattan (L1): sum of absolute differences;

@catalinbp catalinbp force-pushed the ps-11167 branch 2 times, most recently from 49cb0f0 to 4b02d77 Compare June 19, 2026 10:00
@catalinbp catalinbp changed the base branch from vector-mvp to 9.7 June 22, 2026 06:56
@catalinbp catalinbp force-pushed the ps-11167 branch 2 times, most recently from fb27919 to d9aa7ef Compare June 22, 2026 12:54
Comment thread sql/item_strfunc.cc Outdated
Comment thread include/my_pointer_arithmetic.h Outdated
@percona-mhansson

Copy link
Copy Markdown
Contributor

Getting unit test failure on Mac:

[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from VectorDistanceTest
[ RUN      ] VectorDistanceTest.UnalignedMatchesAligned
/home/martin/gitroot/worktrees/ps-11167/9.7/unittest/gunit/vector_distance-t.cc:208: Failure
Value of: is_aligned_to(ma, alignof(float))
  Actual: true
Expected: false

[  FAILED  ] VectorDistanceTest.UnalignedMatchesAligned (0 ms)
[----------] 1 test from VectorDistanceTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] VectorDistanceTest.UnalignedMatchesAligned

 1 FAILED TEST

Which is weird, but clang+linux+arm seems to align stuff a bit weirdly

@catalinbp

Copy link
Copy Markdown
Contributor Author

Seems like buf_a + 1 isn't guaranteed misaligned on apple silicon. I'll push a change that tries harder to miss align

Implement SQL DISTANCE(vector, vector, metric) and the VECTOR_DISTANCE()
synonym for vector similarity queries. Supported metrics:
EUCLIDEAN (L2), EUCLIDEAN_SQUARED, MANHATTAN (L1), COSINE, and DOT.

Core library (vector-common/vector_distance.*):
- Runtime SIMD dispatch across Scalar, SSE4.2/NEON, AVX2, AVX-512F, and
  SVE2 tiers; per-kernel target attributes, no global -march=native
- Dim-aware wide/narrow dispatch (dims >= 16 use widest tier; smaller
  vectors use 128-bit tier to avoid AVX setup overhead)
- Unaligned load intrinsics throughout: VECTOR data may be misaligned;
  on modern CPUs unaligned and aligned loads have identical throughput
  when data is aligned; aligned loads would fault on misaligned inputs
  without a performance benefit.  Loads that span a 64-byte cache-line
  boundary may still be slower (two line fetches); that depends on
  runtime address, not on loadu vs load, and is not avoided by switching
  to aligned intrinsics
- Float32 SIMD accumulation with double-precision horizontal sum and
  scalar tail: preserves correctness for large dimensions and extreme
  values (e.g. 2e38 Euclidean distance) without sacrificing SIMD width
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.

3 participants