Repository navigation
Support strides for the QBit data type - #108103
Conversation
Add an optional third parameter to `QBit`, the stride, e.g. `QBit(BFloat16, 4096, 1024)`. It partitions the `dimension` dimensions into `dimension / stride` contiguous groups and stores each group's bit planes in separate streams. A search over only the first `D` dimensions (a multiple of `stride`) then reads just the streams of the groups that cover them, which is useful for Matryoshka embeddings. The nested storage becomes a `Tuple` of `element_size * num_strides` `FixedString` columns in group-major order, so the positional subcolumns keep reading one stream each: `q.N` reads bit plane `(N-1) % element_size` of stride group `(N-1) / element_size`. When `stride == dimension` (the default) the type is unchanged and byte-identical to before. The transposed distance functions `L2DistanceTransposed` and `cosineDistanceTransposed` gain an optional fourth argument `dims`, the number of leading dimensions to read for a reduced-dimension search: `L2DistanceTransposed(q, ref, precision, dims)`. The `DistanceTransposedPartialReadsPass` is stride-aware and rewrites the call to read only `precision * (dims / stride)` plane streams. Validation: `dimension` must be a multiple of `stride`, and `stride` must be a multiple of 8 when it is smaller than `dimension`. For backward compatibility a new binary type encoding `QBitWithStride` is used only when `stride != dimension`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [d3d694e] Summary: ✅
AI ReviewSummaryThis PR adds PR Metadata
Findings
Final Verdict |
`Build (arm_tidy)` failed with `cppcoreguidelines-pro-type-member-init`:
the local `InternalArguments res;` left its `size_t` members uninitialized.
Value-initialize it with `InternalArguments res{};`.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The AST fuzzer found that `L2DistanceTransposed(vec, ref, precision, dims)` with a nullable `dims` argument (e.g. `toNullable(8)`) tripped the pass's own result-type guard: `A query tree node does not have a valid source node after running DistanceTransposedPartialReadsPass. Before: Nullable(Float64), after: Float64`. The function's result is `Nullable` when any of its arguments is nullable. The pass removes the precision and dims arguments, so to preserve the result nullability it forces it onto the trailing size constant — but it only checked the precision argument, not the new optional `dims` argument. Now it converts the size constant to nullable when either the precision or the dims argument is nullable. CI: #108103 (AST fuzzer (amd_debug, targeted)) Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Addresses the automated review feedback on the QBit strides PR: - Bound the number of stride groups (`dimension / stride`) to `MAX_STRIDE_GROUPS` in the `DataTypeQBit` constructor. The nested storage materializes `element_size * num_strides` `FixedString` streams, so a huge dimension with a tiny stride (e.g. `QBit(Float32, 8000008, 8)`, also reachable via a crafted `QBitWithStride` binary type) could otherwise exhaust memory in `getNestedType`. A non-strided QBit always has just `element_size` streams, so it is unaffected. - `04405_qbit_stride_distance`: prove the partial-read I/O contract. The test now counts the distinct `vec.N` streams referenced in the query plan, showing the reduced-dimension search reads only the covered groups' planes (16 of 64), the full search reads all 64, and with the optimization disabled none are read (the whole `vec` is read instead). Previously the numeric checks alone would pass even via the public QBit fallback without any stream reduction. - Docs: the `stride % 8` rule applies only when `stride` is smaller than the dimension; add the `QBitWithStride` row (`0x37`) to the binary-encoding spec. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
# Conflicts: # src/Functions/FunctionsConversion.cpp
The in-source `Documentation` block registered for the `QBit` data type still described the pre-stride layout: it listed a fixed number of subcolumns per element type and did not mention the group-major `vec.N` mapping or the optional fourth `dims` argument of the transposed distance functions. With a strided `QBit` such as `QBit(BFloat16, 4096, 1024)` there are 64 subcolumns, not 16, so the generated type-family documentation disagreed with the user-facing `qbit.md`. Mirror the subcolumn, group-major ordering, and reduced-dimension search wording from `docs/en/sql-reference/data-types/qbit.md` into the block. Addresses the AI review finding on `src/DataTypes/DataTypeQBit.cpp`.
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'.
Address review feedback: the strided `QBit` adds the `0x37` `QBitWithStride` binary type encoding and changes the Native column stream shape to `element_size * (dimension / stride)` `FixedString` streams grouped by stride. Update `docs/en/interfaces/specs/NativeFormat.md` so third-party Native readers do not have to rediscover this from the C++ source.
The format spec deliberately scopes the `QBit` family's on-wire payload
out ("outside the scope of this page"), the same as `AggregateFunction`.
Documenting the strided stream layout there would contradict that
sentence, so limit the spec change to the type spelling
`QBit(T, N[, stride])` and leave whether to bring the whole QBit family
in-scope (and how) to the spec author.
Address review feedback on the `Native` format spec: a strided `QBit` changes the on-wire stream shape and adds the `0x37` `QBitWithStride` binary type encoding, and the reviewer asked that third-party `Native` readers not have to recover this from the C++ source. The `QBit` payload is deliberately scoped out of `docs/en/interfaces/specs/NativeFormat.md` (the same as `AggregateFunction`), so rather than inlining the layout there — which would contradict that boundary — the out-of-scope note now cross-links to the two pages that already document it in full: the `QBit` data type page (strided stream layout, group-major `FixedString` streams) and the binary type encoding reference (tags `0x36` and `0x37`). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…06 -> 04489-04492)
…e reference vector The AST fuzzer hit a `LOGICAL_ERROR` while running `DistanceTransposedPartialReadsPass`: FUNCTION query tree node does not have a valid source node after running DistanceTransposedPartialReadsPass. Before: Nullable(Float64), after: Float64 `L2DistanceTransposed` and `cosineDistanceTransposed` use the default NULL implementation, so the original (user-facing) call is `Nullable` whenever any of its arguments is `Nullable` - including the reference vector (for example, a `Variant`-typed reference vector produced by the fuzzer via `multiIf`). When the pass rewrote the call into its internal form it dropped the precision and `dims` arguments and cast the reference vector to a plain `Array`, losing the nullability, and then the post-rewrite type-check threw. The pass only forced the nullability back onto the trailing size constant when the precision or `dims` arguments were `Nullable`, missing the reference-vector (and any other) case. Drive the decision off the actual original result type instead: force the size constant to `Nullable` whenever the original result was `Nullable`, which keeps the rewritten result type identical. Found by the AST fuzzer: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108103&sha=de8225ac7b3fe0ee2f55270b83780a767be2321b&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
The Root cause: the fuzzer produced a call whose reference vector has a type that makes the result The pass only restored nullability when the precision or Verified on a local debug build: the exact fuzzer query now returns |
CI triage (commit
|
|
@groeneai, provide a fix for |
…e groups with BAD_ARGUMENTS Two fixes for `@rienath`'s review of `DataTypeQBit`: - `updateHashImpl` hashed the derived nested `Tuple` structure plus `dimension` and `stride`. That is more expensive (it materializes the nested tuple) and less precise than hashing the element type directly, since the tuple only encodes the element *size*. Hash `element_type`, `dimension` and `stride` directly, matching the fields `equals` compares. - The `MAX_STRIDE_GROUPS` limit is reachable from ordinary user input (a `CREATE TABLE` with a well-formed AST but a dimension/stride combination producing too many groups), so it is a bad argument rather than an unexpected AST structure. Throw `BAD_ARGUMENTS` instead of `UNEXPECTED_AST_STRUCTURE`. The stride divisor and multiple-of-8 checks keep `UNEXPECTED_AST_STRUCTURE` (their behavior is pinned by existing tests). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…d SQL test `@rienath` noted the `MAX_STRIDE_GROUPS` bound was only exercised in the `gtest`, so it was not verified through the full SQL path. Move it to a new stateless test `04497_qbit_stride_max_groups.sql` that creates tables and checks that exactly `MAX_STRIDE_GROUPS` groups is accepted, one group over is rejected with `BAD_ARGUMENTS`, and a non-strided `QBit` is unaffected by the bound regardless of dimension. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…nsposed distance functions `@rienath` pointed out that `parseInternalArguments` in `distanceTransposed.cpp` hand-rolled the same supported-element-type check (`Int8`, `BFloat16`, `Float32`, `Float64`) that the new `DataTypeQBit::isSupportedElementType` helper now provides. Reuse the helper so the whitelist lives in one place. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`@rienath` asked that the comment on the `stride` field be written as if the
feature is already merged rather than referring to the previous behavior
("behaves exactly as before"). Describe the non-strided default directly:
every bit plane spans all dimensions in a single stream.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
continue-pr pass — addressed @rienath's review (commits
|
…ublic docs The AI Review on #108103 (head `711ddc16`) flagged that the structured documentation of the transposed distance functions still described the vector argument as the pre-stride two-argument form `QBit(T, UInt64)`, contradicting the `QBit` data type page which now documents the optional third `stride` parameter this PR adds. The same staleness affected the other public `QBit` syntax references in `annindexes.md` and the RowBinary `QBit` section. Update the `L2DistanceTransposed`, `cosineDistanceTransposed`, and `dotProductTransposed` argument docs in `vectorFunctions.cpp` to `QBit(T, UInt64[, UInt64])`, and add the optional `[, stride]` to the `QBit` syntax in `annindexes.md` (with a short bullet pointing at the `QBit` page) and in the RowBinary format doc (noting that `stride` is a server-side storage grouping and does not change the RowBinary wire format). Docs/docstring only; no behavior change. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
continue-pr pass — synced the remaining public
|
The AI review flagged that the rendered `RowBinary` and binary-encoding pages now advertise the optional `QBit` `stride` argument, but the source doc blocks they are maintained from were still out of sync: - `BinaryRowInputFormat.cpp`: the embedded `QBit` `RowBinary` doc block still showed `QBit(element_type, dimension)` without the optional `stride`; add it and note that `stride` is a server-side storage grouping that does not change the `RowBinary` wire format. - `DataTypesBinaryEncoding.h`: the source comment table listed the `0x36`/`0x37` rows but lacked the note that `stride == N` canonicalizes to the two-argument `0x36` form; add the same note the rendered `data-types-binary-encoding.md` already carries. No behavior change. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Per review, `dim` (the `QBit` dimension) and `dims` (the optional fourth argument of `L2DistanceTransposed`, `cosineDistanceTransposed` and `dotProductTransposed` selecting how many leading dimensions to read for a Matryoshka-style search) were easy to confuse. Rename the latter to `used_dims` consistently across the user-facing docs (`qbit.md`, the `DataTypeQBit` documentation), the function documentation and syntax strings in `vectorFunctions.cpp`, the error messages, and the internal variables, struct members and comments in `distanceTransposed.cpp` and `DistanceTransposedPartialReadsPass.cpp`. The arguments stay positional, so this is a documentation and naming change with no behavior change. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
continue-pr pass — synced source-level format docs and renamed the fourth distance-function argument (commits
|
Investigation: materialized-CTE
|
continue-pr pass — addressed the flipped AI ReviewThe AI Review re-ran on
Remaining unresolved threads are the two @rienath documentation suggestions ( CI is green on the current head (0 failures). No re-merge needed (merge-base ~14h old, no conflicts, |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 630/712 (88.48%) · Uncovered code |
The AST fuzzer found that `L2DistanceTransposed(vec, ref, precision, dims)` with a nullable `dims` argument (e.g. `toNullable(8)`) tripped the pass's own result-type guard: `A query tree node does not have a valid source node after running DistanceTransposedPartialReadsPass. Before: Nullable(Float64), after: Float64`. The function's result is `Nullable` when any of its arguments is nullable. The pass removes the precision and dims arguments, so to preserve the result nullability it forces it onto the trailing size constant — but it only checked the precision argument, not the new optional `dims` argument. Now it converts the size constant to nullable when either the precision or the dims argument is nullable. CI: ClickHouse#108103 (AST fuzzer (amd_debug, targeted)) Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ublic docs The AI Review on ClickHouse#108103 (head `711ddc16`) flagged that the structured documentation of the transposed distance functions still described the vector argument as the pre-stride two-argument form `QBit(T, UInt64)`, contradicting the `QBit` data type page which now documents the optional third `stride` parameter this PR adds. The same staleness affected the other public `QBit` syntax references in `annindexes.md` and the RowBinary `QBit` section. Update the `L2DistanceTransposed`, `cosineDistanceTransposed`, and `dotProductTransposed` argument docs in `vectorFunctions.cpp` to `QBit(T, UInt64[, UInt64])`, and add the optional `[, stride]` to the `QBit` syntax in `annindexes.md` (with a short bullet pointing at the `QBit` page) and in the RowBinary format doc (noting that `stride` is a server-side storage grouping and does not change the RowBinary wire format). Docs/docstring only; no behavior change. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Support strides for the QBit data type
Adds an optional third parameter to the
QBitdata type — the stride — for exampleQBit(BFloat16, 4096, 1024). It partitions thedimensiondimensions intodimension / stridecontiguous groups and stores each group's bit planes in separate streams. A search over only the firstDdimensions (a multiple ofstride) then reads just the streams of the groups that cover them, which is useful for Matryoshka embeddings, where the leading dimensions form a usable lower-dimensional embedding.Motivation: today every bit plane of a
QBitspans all dimensions in a single stream, so a vector search can read fewer bits but not fewer dimensions. Strides make the lower-dimensional prefix cheap to read.Details:
Tupleofelement_size * num_stridesFixedStringcolumns in group-major order, so positional subcolumns still read one stream each:q.Nreads bit plane(N-1) % element_sizeof stride group(N-1) / element_size. ForBFloat16,q.1 … q.16read the first stride,q.17 … q.32the second, and so on.stride == dimension(the default), the type is unchanged and byte-identical to before, including its binary type encoding, so existing tables and on-disk parts are unaffected.L2DistanceTransposedandcosineDistanceTransposedgain an optional fourth argumentused_dims— the number of leading dimensions to read:L2DistanceTransposed(q, ref, precision, used_dims). TheDistanceTransposedPartialReadsPassis stride-aware and rewrites the call to read onlyprecision * (used_dims / stride)plane streams. Verified viaEXPLAINthat an 8-bit search over the first 2048 of 4096 dimensions reads 16 of 64 streams, with results identical to a non-strided 2048-dimensionalQBit.dimensionmust be a multiple ofstride, andstridemust be a multiple of 8 when it is smaller thandimension. For backward compatibility, a new binary type encodingQBitWithStride(0x37) is used only whenstride != dimension.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added an optional stride parameter to the
QBitdata type (QBit(T, dimension, stride)) that stores groups of dimensions in separate streams, so a vector search can read only the first dimensions efficiently (e.g. for Matryoshka embeddings). The transposed distance functions accept an optional fourthused_dimsargument to read a reduced number of dimensions.Documentation entry for user-facing changes
QBitdata type page and the transposed distance functions are updated).🤖 Generated with Claude Code
Version info
26.7.1.397(included in26.7and later)