Skip to content

Allow using integer reference vectors for approximate nearest neighbor search - #115255

Merged
rschu1ze merged 1 commit into
masterfrom
vector-search-integer-reference-vector
Aug 18, 2026
Merged

rschu1ze merged 1 commit into
masterfrom
vector-search-integer-reference-vector

Conversation

@rschu1ze

@rschu1ze rschu1ze commented Aug 18, 2026 •

Copy link
Copy Markdown
Member

Closes: #112233

This PR is a continuation of #114316. I get a permission denied error when pushing to the PR.

Courtesy to @hamidr

Changelog category (leave one):

  • Improvement

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

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

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: #112233
@rschu1ze rschu1ze changed the title Use the vector similarity index for integer reference vectors Allow using integer reference vectors for approximate nearest neighbor search Aug 18, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a66b020]

Summary: ✅


AI Review

Summary

This PR teaches useVectorSearch.cpp to accept native integer array literals as ANN reference vectors and adds a stateless regression test for the vector-similarity-index path. That fixes the original indexed case, but the broader ANN contract is still incomplete because the sibling Quantized(...) approximate-search rewrite keeps the old float-only extraction and still falls back on integer literals.

Findings

⚠️ Majors

  • [src/Processors/QueryPlan/Optimizations/useVectorSearch.cpp:194] The new "integer reference vectors work for ANN queries" invariant is only implemented for the vector-similarity-index optimization. src/Processors/QueryPlan/Optimizations/useVectorSearchWithQuantizedCodes.cpp:206-222 still accepts only Array(Float32|Float64|BFloat16) with Field::Types::Float64, so ORDER BY L2Distance(vec, [1, 2]) LIMIT k on a Quantized(...) column still loses the approximate rewrite when vector_search_use_quantized_codes = 1. Mirror the same integer handling there, or narrow the PR's claim to the indexed-only path.
Final Verdict

Changes requested: propagate the integer-literal handling to the Quantized(...) ANN rewrite, or explicitly narrow the scope to the indexed path.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.80% 86.80% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.20% 79.20% +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 18, 2026
const auto * data_type_nested_bfloat16 = typeid_cast<const DataTypeBFloat16 *>(data_type_array_nested.get());
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());
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.

This widens the constant handling only for the vector-similarity-index path. The sibling ANN rewrite in src/Processors/QueryPlan/Optimizations/useVectorSearchWithQuantizedCodes.cpp still requires Array(Float32|Float64|BFloat16) and Field::Types::Float64, so ORDER BY L2Distance(vec, [1, 2]) LIMIT k still silently loses the approximate rewrite on Quantized(...) columns when vector_search_use_quantized_codes = 1.

Since the PR is framed as allowing integer reference vectors for ANN queries generally, I think this needs the same isNativeInteger + FieldVisitorConvertToNumber<Float64> handling there as well, or the scope/title/tests should be narrowed to the indexed-only path.

@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a66b0207d with master 2bb9dae14 (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 702.05 MiB 699.03 MiB -3.03 MiB (-0.43%)

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, 13 s compile time in total, 7 of them have a recent master baseline.

Job report

@shankar-iyer shankar-iyer self-assigned this Aug 18, 2026
@rschu1ze

Copy link
Copy Markdown
Member Author

I reviewed the original PR already, it's fine!

@rschu1ze
rschu1ze added this pull request to the merge queue Aug 18, 2026
Merged via the queue into master with commit 82e55a1 Aug 18, 2026
185 checks passed
@rschu1ze
rschu1ze deleted the vector-search-integer-reference-vector branch August 18, 2026 15:47
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 18, 2026
nickitat added a commit that referenced this pull request Aug 18, 2026
A query flagged in `report.html` was dropped when `all-query-metrics.tsv` had
no `client_time` row for it, and a shard whose TSV could not be read dropped
all of its flagged queries. With every flagged query dropped the run ended with
"the comparison ran and was clean" - a non-empty CI report turned into an
all-clear. The cause is deliberate on CI's side: `compare.sh` retracts a query
from the TSV when its confirmation rerun demoted it, while the report still
lists it under both "Changes in Performance" and "Unconfirmed Changes". On
PR #115255 that is `round_down` #2 and #3 and `and_compare_chain_derived` #2.

Those rows carry their own numbers, so they are now read from `report.html` and
marked `*` in the report - CI's verdict on them was already in doubt, which
makes them the most interesting queries to rerun, not the ones to discard. A
flagged query readable from neither source is reported as unresolved, and if
that leaves nothing to rerun the run fails instead of reporting a clean
comparison.

The commit under test drove only the report and binary lookup; the XMLs,
`perf.py`, its `stat_threshold`, and the configs still came from the working
tree. Query indices are positional and substitutions expand them, so a tree
that differs from the measured commit silently reruns a different query - on a
checkout one commit behind master, `and_compare_chain_derived.xml` has no query
#2 at all while CI flagged exactly that, and a `refs/pull/<n>/merge` checkout
is never the commit CI measured. `tests/performance`, `programs/server` and
`tests/config/top_level_domains` are now extracted from that commit into the
work dir and everything runs from there, fetching the commit when the clone
lacks it. `--use-working-tree-tests` opts out for iterating on a local test.

Verified: all six flagged queries of PR #115255 are now kept, the three the TSV
retracts carrying the report's numbers and flagged as unconfirmed; a dry run of
PR #112688 recovers `date_time_64` #5, which had been dropped the same way, and
runs against the extracted tree.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115278&sha=baefff6dcc4dbb3201337a52b5ca54453fb34562&name_0=PR&name_1=Style%20check
Pull request: #115278

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01GYYbBbrQpmi2zkYJ8LQw5Z
nickitat added a commit that referenced this pull request Aug 18, 2026
…to .git

`report.py` writes cell values into `<td>` raw - it does not import `html`, let
alone escape - so a query containing `<` and `>` puts them into the markup
directly, and stripping tags inside a cell ate the text between them:
`SELECT a < b AND c > d` became `SELECT a  d`. That name is half the key of the
historical-threshold lookup, so such a query missed its learned threshold and
was judged by a weaker bar. 90 distinct queries measured in the last two weeks
contain both characters.

The name is now derived from the pinned test tree instead, where it has an
exact source: perf.py builds `query_display_name` as `query_display(item)`
trimmed to 1000 characters, which is what `--print-queries` prints and what
reaches `query_metrics_v2`. Checked against CIDB, the derived name matches the
recorded one byte for byte, including the leading newline the query text starts
with. The report's query cell is no longer tag-stripped either, so the
displayed text stays intact.

Fetching a missing commit wrote to `.git` (`FETCH_HEAD` at least), which some
sandboxes mount read-only - and a historical PR is exactly when the commit is
missing. When the fetch into the clone fails, the commit is now fetched into a
scratch repository under the work dir, which needs nothing from `.git` but the
origin URL and yields the same archive.

Verified: against a clone with a read-only `.git` and without the commit, the
tree for PR #115255's head is still materialized - 558 perf XMLs plus
`tests/benchmarks` and the configs - and the clone is left untouched, with no
`FETCH_HEAD` created.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115278&sha=baefff6dcc4dbb3201337a52b5ca54453fb34562&name_0=PR&name_1=Style%20check
Pull request: #115278

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01GYYbBbrQpmi2zkYJ8LQw5Z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

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