Skip to content

Add Int8 support to the QBit data type - #108105

Merged
alexey-milovidov merged 5 commits into
masterfrom
qbit-int8
Jun 28, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
qbit-int8

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jun 22, 2026 •

Copy link
Copy Markdown
Member

Adds Int8 as a supported element type for the QBit data type. Previously QBit accepted only BFloat16, Float32, and Float64. With this change QBit(Int8, N) stores quantized 8-bit integer vectors (8 bit planes per element) and supports the transposed distance functions L2DistanceTransposed and cosineDistanceTransposed over them, including the partial-reads (precision) optimization and the simsimd i8 SIMD kernels.

The change touches the full element-type machinery: DataTypeQBit validation, ColumnQBit naming, the bit-transposition serialization in SerializationQBit, Array ↔ QBit conversion, convertFieldToType, the distance functions, and the query fuzzer. Documentation and tests are included.

Changelog category (leave one):

  • New Feature

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

Added support for the Int8 element type in the QBit data type, enabling storage and transposed-distance vector search (L2DistanceTransposed, cosineDistanceTransposed) over quantized 8-bit integer vectors.

Version info

  • Merged into: 26.7.1.185 (included in 26.7 and later)

`QBit` previously supported only `BFloat16`, `Float32`, and `Float64`
element types. This adds `Int8` (an 8-bit element stored as 8 bit
planes), so `QBit` can hold quantized integer vectors and use the
transposed distance functions over them.

The change covers the full element-type machinery:
- `DataTypeQBit` validation and `ColumnQBit` naming;
- bit-transposition serialization in `SerializationQBit`;
- `Array` to `QBit` conversion and `convertFieldToType`;
- `L2DistanceTransposed` and `cosineDistanceTransposed`, including the
  `simsimd` `i8` kernels and the partial-reads optimization;
- the query fuzzer.

Note: the 8-bit transposition word is `uint8_t` rather than ClickHouse's
`UInt8`, because `UInt8` is `char8_t`, which does not satisfy
`std::countr_zero`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [22a7835]

Summary: ✅


AI Review

Summary

This PR adds Int8 as a supported QBit element type, covering validation, bit-transposed serialization, Array and field conversion, transposed distance execution, query fuzzer generation, docs, and focused stateless tests. I found no remaining correctness issues after the latest fixes; the prior clickhouse-gh review threads are addressed, and I resolved the remaining overflow thread after verifying the current code falls back before the simsimd Int8 accumulator can overflow.

Missing context / blind spots
  • ⚠️ I did not run the focused stateless tests locally because this checkout has no build directory or clickhouse binary. The Praktika report I fetched showed no failed checks, but some GitHub checks were still in progress; completed CI would close this gap.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Jun 22, 2026
@alexey-milovidov
alexey-milovidov requested a review from rienath June 22, 2026 06:49
Comment thread src/Interpreters/convertFieldToType.cpp Resolved
- In `convertFieldToType`, convert numeric fields to `Int8` by truncating
  (wrapping), matching how `toInt8` / `CAST(... AS Int8)` and the
  `Array(Int8)` conversion handle out-of-range values (e.g. 128 -> -128).
  The previous `FieldVisitorConvertToNumber<Int8>` range-checked `Float64`
  but wrapped integers, which diverged from those types; now both wrap.
- Add a regression to `04404_qbit_int8` covering out-of-range wrapping via
  `VALUES` and `CAST`, asserted equal to `Int8` / `Array(Int8)`.
- Update the RowBinary `QBit` documentation (`RowBinary.md` and the source
  doc in `BinaryRowInputFormat.cpp`) to list `Int8` as a valid element type.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread src/Interpreters/convertFieldToType.cpp Resolved
- In `convertFieldToType`, convert numeric fields to `Int8` via
  `accurate::convertNumeric` (with an explicit `isFinite` guard for
  `Float64`), rejecting out-of-range and non-finite values uniformly for
  integer and floating-point fields. This matches the accurate conversion
  `convertNumericType` already uses and removes the undefined behaviour of
  casting a non-finite or out-of-range `Float64` to a narrow integer.
- Add `04404_qbit_int8` cases asserting `nan`/`inf` are rejected with
  `CANNOT_CONVERT_TYPE`, consistent with `Array(Int8)`.
- Document `Int8` as a valid `QBit` element type in the vector-search
  guide (`annindexes.md`).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@rienath rienath self-assigned this Jun 22, 2026
Make `convertFieldToType` truncate (wrap) numeric fields to `Int8`, so
`QBit(Int8)` construction has a single contract across `VALUES`, `CAST`,
and `convertFieldToType`: finite values wrap (e.g. 128 -> -128, matching
`toInt8` / `Array(Int8)`), and non-finite or out-of-Int64-range `Float64`
values are rejected explicitly (they cannot be wrapped, and casting them
to a narrow integer would be undefined behaviour).

This resolves the contract ambiguity raised in review while keeping the
inf/nan guard: the earlier `accurate::convertNumeric` variant rejected
in-range out-of-bounds values, diverging from the wrapping `VALUES`/`CAST`
path. The stateless tests in `04404_qbit_int8` already pass on this path.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
SimSIMD's `i8` `L2`/`cosine` kernels accumulate in `int32` (e.g. the serial
`SIMSIMD_MAKE_L2SQ(serial, i8, i32, ...)`), which overflows for large but
valid `QBit(Int8)` dimensions — `255^2 * 33026 > INT32_MAX` for `L2`, and
`128^2 * 131072 > INT32_MAX` for `cosine` — producing wrong results (or UB).
The scalar fallback accumulates in `Float64` and is correct.

Gate the `i8` SimSIMD path by dimension: each kernel exposes the largest
per-element term it accumulates, and beyond `INT32_MAX / term` elements we
fall back to the scalar `Float64`-accumulator path. Small dimensions keep
using the fast SIMD kernels.

Add `04405_qbit_int8_distance` regressions: `L2DistanceTransposed` over
`QBit(Int8, 33026)` and `cosineDistanceTransposed` over `QBit(Int8, 131072)`
with worst-case values, checked against the exact `Float64` results.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.30% 85.30% +0.00%
Functions 92.60% 92.60% +0.00%
Branches 77.50% 77.50% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 134/184 (72.83%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 3 line(s) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Everything looks clear.

@alexey-milovidov alexey-milovidov self-assigned this Jun 28, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 28, 2026
Merged via the queue into master with commit db08ea0 Jun 28, 2026
167 checks passed
@alexey-milovidov
alexey-milovidov deleted the qbit-int8 branch June 28, 2026 02:43
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 28, 2026
alexey-milovidov added a commit that referenced this pull request Jun 28, 2026
Resolve conflicts between the QBit stride feature and the QBit(Int8)
support that landed in master (#108105):
- QueryFuzzer: keep Int8 in the element-type list and the strided branch.
- convertFieldToType: combine the Int8 element conversion (with the
  wrap/reject contract) with the per-stride-group transpose.
- ColumnQBit::getName, DataTypeQBit/qbit.md docs: list Int8 alongside the
  per-stride-group subcolumn counts.
- distanceTransposed: accept Int8 reference vectors in the internal form
  and bound the int8 SimSIMD int32 accumulator by 'dims' (the number of
  dimensions actually read) instead of the removed 'qbit_size'.
alexey-milovidov added a commit that referenced this pull request Jun 28, 2026
Following the addition of `Int8` to the `QBit` data type (#108105), extend
the new `dotProductTransposed` kernel to handle `Int8` `QBit` columns, the
same way `L2DistanceTransposed` and `cosineDistanceTransposed` already do
after merging `master`.

The `DotProductTransposed` kernel gains the `Int8` scalar branch and the
`max_int8_simd_term` overflow bound (max `|a * b|` is `128 * 128`, when both
elements are `-128`). The rest of the `Int8` machinery - reference type
acceptance, accumulator dispatch, SimSIMD `i8` kernel resolution, and the
int32 overflow guard - is shared by `FunctionArrayDistance` and already
applies once `master` is merged.

Bump the documented `IntroducedIn` version to 26.7 to match the current
development version, and add `Int8` coverage to 04403_dot_product_transposed:
exact full-precision dot products, reduced precision, and a large-dimension
case that exercises the int32-overflow fallback to the `Float64` scalar path.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Add Int8 support to the QBit data type
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Following the addition of `Int8` to the `QBit` data type (ClickHouse#108105), extend
the new `dotProductTransposed` kernel to handle `Int8` `QBit` columns, the
same way `L2DistanceTransposed` and `cosineDistanceTransposed` already do
after merging `master`.

The `DotProductTransposed` kernel gains the `Int8` scalar branch and the
`max_int8_simd_term` overflow bound (max `|a * b|` is `128 * 128`, when both
elements are `-128`). The rest of the `Int8` machinery - reference type
acceptance, accumulator dispatch, SimSIMD `i8` kernel resolution, and the
int32 overflow guard - is shared by `FunctionArrayDistance` and already
applies once `master` is merged.

Bump the documented `IntroducedIn` version to 26.7 to match the current
development version, and add `Int8` coverage to 04403_dot_product_transposed:
exact full-precision dot products, reduced precision, and a large-dimension
case that exercises the int32-overflow fallback to the `Float64` scalar path.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Resolve conflicts between the QBit stride feature and the QBit(Int8)
support that landed in master (ClickHouse#108105):
- QueryFuzzer: keep Int8 in the element-type list and the strided branch.
- convertFieldToType: combine the Int8 element conversion (with the
  wrap/reject contract) with the per-stride-group transpose.
- ColumnQBit::getName, DataTypeQBit/qbit.md docs: list Int8 alongside the
  per-stride-group subcolumn counts.
- distanceTransposed: accept Int8 reference vectors in the internal form
  and bound the int8 SimSIMD int32 accumulator by 'dims' (the number of
  dimensions actually read) instead of the removed 'qbit_size'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature Pull request with new product feature 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.

3 participants