Skip to content

Use the vector similarity index for integer reference vectors - #114316

Closed
hamidr wants to merge 4 commits into
ClickHouse:masterfrom
hamidr:vector-search-integer-reference-vector
Closed

hamidr wants to merge 4 commits into
ClickHouse:masterfrom
hamidr:vector-search-integer-reference-vector

Conversation

@hamidr

@hamidr hamidr commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #112233
Related: #114291

The reference vector of an ANN query is extracted only when its array type is Float64, Float32 or BFloat16 and every element is a Float64 field. An integer literal such as [1, 2] is typed Array(UInt8), so tryUseVectorSearch bails out and the query silently falls back to a brute-force scan over the whole table, although [1, 2] denotes the same point as [1.0, 2.0] and L2Distance accepts it. EXPLAIN indexes = 1 shows no vector_similarity entry and gives no hint why, so a one-character difference in a literal becomes a sharp performance cliff on large tables.

Native integer arrays are now accepted as reference vectors and their elements are converted to Float64, which is the type the reference vector is stored in anyway.

Added 02354_vector_search_bug112233, covering unsigned, signed, mixed integer/float, and not-exactly-representable reference vectors, plus an equality check between the integer and float spellings of the same query.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Vector search queries now use the vector_similarity index when the reference vector is written as an integer array literal, e.g. ORDER BY L2Distance(vec, [1, 2]). Previously such queries silently fell back to a brute-force scan.

@CLAassistant

CLAassistant commented Aug 11, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

The reference vector of an ANN query is extracted only when its array type
is Float64, Float32 or BFloat16 and every element is a Float64 field. An
integer literal such as [1, 2] is typed Array(UInt8), so the optimization
bailed out and the query silently fell back to a brute-force scan over the
whole table, even though [1, 2] denotes the same point as [1.0, 2.0] and
L2Distance accepts it. Nothing in EXPLAIN told the user why the index was
gone, which turns a one-character difference in a literal into a sharp
performance cliff.

Accept native integer arrays as well and convert the elements to Float64,
which is the type the reference vector is stored in anyway.

Closes: ClickHouse#112233
@hamidr
hamidr force-pushed the vector-search-integer-reference-vector branch from e42f47c to b8fc861 Compare August 11, 2026 12:56
@SmitaRKulkarni SmitaRKulkarni added the can be tested Allows running workflows for external contributors label Aug 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [645bf1a]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_debug) FAIL
Logical error: Invalid number of columns in chunk pushed to OutputPort. Expected A, found B (STID: 2270-3258) FAIL cidb, issue

AI Review

Summary

This PR fixes the vector_similarity-index planner so integer array literals such as [1, 2] no longer disable that optimization, and the added regression test covers the indexed L2Distance path well. I still see one remaining contract gap: the sibling quantized-codes ANN planner still rejects the same integer reference arrays, so the same one-character literal difference continues to trigger a silent exact-scan fallback on the other supported vector-search access path.

Findings

⚠️ Majors

  • [src/Processors/QueryPlan/Optimizations/useVectorSearch.cpp:194] The new acceptance rule only fixes the vector_similarity-index planner. The sibling ANN path in src/Processors/QueryPlan/Optimizations/useVectorSearchWithQuantizedCodes.cpp:206-222 still accepts only Array(Float64/Float32/BFloat16) with Float64 Field elements, so ORDER BY L2Distance(vec, [1, 2]) ... SETTINGS vector_search_use_quantized_codes = 1 on Quantized(...)-coded tables still falls back to the exact full-vector scan. That leaves the same sharp performance cliff in the other supported vector-search planner, so the invariant this PR introduces is still inconsistent across ANN access paths. Suggested fix: mirror the integer-array acceptance and Float64 conversion there, and add a focused regression that compares [1, 2] with [1.0, 2.0] under vector_search_use_quantized_codes = 1.
Final Verdict

⚠️ Needs changes before merge: align useVectorSearchWithQuantizedCodes.cpp with the new integer-reference handling, or explicitly narrow the contract to explain why the quantized-codes ANN path is intentionally excluded.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.70% 86.70% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.10% 79.10% +0.00%

Changed lines: Changed C/C++ lines covered: 12/15 (80.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Aug 11, 2026
@hamidr

hamidr commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

The Stress test (arm_asan_ubsan) failure (Sanitizer (STID: 2064-22d0), LeakSanitizer: detected memory leaks, SUMMARY: AddressSanitizer: 108057 byte(s) leaked in 625 allocation(s)) is unrelated to this change.

It is the known fleet-wide leak tracked in #114291 - the leaked cluster in this report is the same one described there: indirect leaks reaching Context::createCopy from QueryTreeBuilder::buildSelectExpression, carrying the Settings / SettingsImpl copies.

This pull request only touches reference-vector extraction in useVectorSearch.cpp, which allocates no Context.

Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=114316&sha=b8fc8618fc236110cc6796759767cd8ad73e49be&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%29

Related: #114291

@hamidr

hamidr commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

@rschu1ze could you take a look when you have a moment? This is a small change to reference-vector extraction in useVectorSearch.cpp: integer array literals such as [1, 2] are now accepted and converted to Float64, so an ANN query no longer falls back silently to a brute-force scan just because the literal was written without decimal points.

I could not add you as a reviewer directly (no write access on the repository), hence the mention.

The only red check is Stress test (arm_asan_ubsan), which is the fleet-wide LeakSanitizer failure tracked in #114291 and unrelated to this change - this pull request touches no code that allocates a Context. It should clear once #113397 merges.

@rschu1ze rschu1ze self-assigned this Aug 12, 2026
hamidr added 2 commits August 13, 2026 09:15
Reword the type comments as requested, cover Array(BFloat16) reference
vectors, and drop the claim that lossiness starts at a certain magnitude:
integer-to-float conversion is lossy in general.

Values the column's float type cannot represent exactly, such as 2^53 + 1
against Array(Float64) or an Array(UInt256) literal, are rejected by the
distance function with NO_COMMON_TYPE and never reach the optimization,
so the test pins down the largest integer reference vectors that do reach
it instead.
The previous commit claimed such reference vectors cannot reach the
optimization because the distance function resolves a common type first
and fails with NO_COMMON_TYPE. That holds for `L2Distance` and
`cosineDistance` only. `dotProduct` accepts mismatched argument types, so
`ORDER BY dotProduct(vec, [9007199254740993, 2]) DESC` on an
`Array(Float32)` column does reach it, with the element rounded down to
2^53.

Test that path, including the extremal 64-bit values, and correct the
comment about wider integers - `dotProduct` rejects them with
ILLEGAL_TYPE_OF_ARGUMENT rather than NO_COMMON_TYPE.
if (data_type_nested_float64 == nullptr && data_type_nested_float32 == nullptr && data_type_nested_bfloat16 == nullptr)
WhichDataType which_data_type_array_nested(data_type_array->getNestedType());
/// Wider integers are left out: the distance functions reject them, so they never get here.
if (!which_data_type_array_nested.isFloat() && !which_data_type_array_nested.isNativeInteger())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new acceptance rule only covers the vector_similarity-index planner. The sibling ANN path in useVectorSearchWithQuantizedCodes.cpp:206-222 still accepts only Array(Float64/Float32/BFloat16) with Float64 Field elements, so ORDER BY L2Distance(vec, [1, 2]) ... SETTINGS vector_search_use_quantized_codes = 1 still falls back to the exact full-vector scan on Quantize(...)-coded tables. If the invariant here is that [1, 2] and [1.0, 2.0] denote the same reference vector, this extraction needs to be mirrored there too or the same performance cliff remains on the other supported ANN planner.

@clickhouse-gh

clickhouse-gh Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 645bf1a50 with master 3662ba8a9 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 691.73 MiB 688.72 MiB -3.01 MiB (-0.44%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

7 translation units recompiled, 16 s compile time in total, 7 of them have a recent master baseline.

Job report

The comment on why wider integers are excluded was misleading: they are
unsupported because such magnitudes are unrealistic for vector search and
usearch does not support wide integers, not because the distance functions
reject them. The dotProduct test block tested no relevant distance function.

ClickHouse#114316
@hamidr

hamidr commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

Thank for the help.
What can be done about the AST fuzzer thingy situation?
@rschu1ze

@rschu1ze

Copy link
Copy Markdown
Member

@hamidr Can you please merge from master again?

@rschu1ze

Copy link
Copy Markdown
Member

Superseded by #115255

@rschu1ze rschu1ze closed this Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vector similarity index is silently skipped when the reference vector is an integer array literal — [1, 2] brute-forces, [1.0, 2.0] uses the index

4 participants