Repository navigation
Support CAST between QBit types with different parameters - #109387
Merged
Merged
Conversation
A CAST between two `QBit` types is now allowed whenever the dimension
(the number of vector elements) is preserved; the element type and the
stride (the number of stride groups) may both change. Previously only an
identity cast worked: a differing element type returned `NOT_IMPLEMENTED`
and a differing stride `TYPE_MISMATCH`.
The general case reconstructs the source vector at its native precision
into an `Array` and reuses the existing `Array -> QBit` path to convert
the element type and transpose it into the target layout, inheriting its
element-conversion (widening/narrowing) and NULL semantics.
Two casts take a fast path that repacks the bit planes directly, without
reconstructing the vector through floats:
* a stride change with the same element type is a pure byte permutation
of the bit-plane FixedStrings;
* a `Float32` <-> `BFloat16` change: `BFloat16` is `Float32` truncated
to its top 16 bits, so the two share their top 16 bit planes, and
`Float32`'s other 16 planes are the low mantissa bits that truncation
drops (and that widening back to `Float32` fills with zeros).
Both fast paths compose with a simultaneous stride change.
Changing the dimension is still rejected with `TYPE_MISMATCH`.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Contributor
|
Workflow [PR], commit [833f096] Summary: ✅
AI ReviewSummaryThis PR extends Final Verdict
|
`ColumnWithTypeAndName`'s constructor takes the column by `const ColumnPtr &`, so `std::move(native_array)` never moves and clang-tidy flags it as `performance-move-const-arg`, failing the `arm_tidy` build. Drop the pointless `std::move`; the `ColumnPtr` is a COW handle, so the copy is just a refcount bump. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109387&sha=c7310fa318d2c6ef3a254586c639ed0f8df905bc&name_0=PR&name_1=Build%20%28arm_tidy%29 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…a stride change The `04501_qbit_cast_between_params` "simultaneous element-type and stride change" block only exercised `Float32`/`Float64`, which take the reconstruct-and-retranspose slow path in `createQBitToQBitWrapper`. The `Float32` <-> `BFloat16` casts, which take `repackQBit`'s byte-permutation fast path (the two types share their top 16 bit planes), were never combined with a stride change, so a bug in that branch's restriding would have gone unnoticed. Add four focused cases covering both element-type directions and both stride directions, each comparing the fast-path result byte-for-byte with the target `QBit` built directly. The integers `0..15` are exact in `BFloat16`, so truncation equals rounding and `Float32`'s low 16 planes are zero, making the two constructions provably identical. Requested by the AI review on #109387 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
… path Address review (clickhouse-gh): the `QBit` -> `QBit` cast's `Float32`/`BFloat16` byte-repack fast path in `createQBitToQBitWrapper` drops the low mantissa bits unconditionally. Under `accurateCast` / `accurateCastOrNull`, a narrowing `Float32` -> `BFloat16` therefore returned a truncated value for a row that is not exactly representable (for example `0.1f`) instead of throwing / yielding NULL, violating the accurate-cast contract that `accurate::convertNumeric` enforces on the ordinary numeric path. Disable only that direction (narrowing `Float32` -> `BFloat16`) under the two accurate modes, so it falls through to the reconstruct-and-convert path, whose element conversion goes through the standard checked numeric conversion. The fast path stays for every case that is exact regardless of mode: a stride-only regrouping never changes a value, and the widening `BFloat16` -> `Float32` is always representable; a non-accurate `CAST` truncates too, matching the repack. Extend `04501_qbit_cast_between_params` with cases covering both accurate modes across the narrowing (NULL / throw), the exact narrowing, a stride-stacked narrowing, the widening, and the stride-only paths.
Contributor
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
The same-stride `Float32` <-> `BFloat16` fast path in `repackQBit` copies each shared bit plane wholesale. When the dimension is not a multiple of 8, the final byte of each stride group carries unused padding bits. A tuple-backed `QBit` value from a `VALUES` / `IN` section is not required to zero those bits (`convertFieldToType` validates only the tuple's element count and each string's length), so a wholesale copy could carry stray padding into the target. The reconstruct-and-convert path always leaves them zero and `QBit` equality compares the raw bytes, so the fast path could produce a value that compares unequal to the same vector built directly. Mask the unused padding bits of the copied byte so the fast path output stays canonical. Adds coverage to `04501_qbit_cast_between_params` for both directions (`Float32` -> `BFloat16` and `BFloat16` -> `Float32`) using a poisoned tuple-backed source with a padding bit set. Address review: #109387 (comment) Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Contributor
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 97/98 (98.98%) · Uncovered code |
zeekay
pushed a commit
to hanzoai/datastore
that referenced
this pull request
Sep 11, 2026
…a stride change The `04501_qbit_cast_between_params` "simultaneous element-type and stride change" block only exercised `Float32`/`Float64`, which take the reconstruct-and-retranspose slow path in `createQBitToQBitWrapper`. The `Float32` <-> `BFloat16` casts, which take `repackQBit`'s byte-permutation fast path (the two types share their top 16 bit planes), were never combined with a stride change, so a bug in that branch's restriding would have gone unnoticed. Add four focused cases covering both element-type directions and both stride directions, each comparing the fast-path result byte-for-byte with the target `QBit` built directly. The integers `0..15` are exact in `BFloat16`, so truncation equals rounding and `Float32`'s low 16 planes are zero, making the two constructions provably identical. Requested by the AI review on ClickHouse#109387 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
The same-stride `Float32` <-> `BFloat16` fast path in `repackQBit` copies each shared bit plane wholesale. When the dimension is not a multiple of 8, the final byte of each stride group carries unused padding bits. A tuple-backed `QBit` value from a `VALUES` / `IN` section is not required to zero those bits (`convertFieldToType` validates only the tuple's element count and each string's length), so a wholesale copy could carry stray padding into the target. The reconstruct-and-convert path always leaves them zero and `QBit` equality compares the raw bytes, so the fast path could produce a value that compares unequal to the same vector built directly. Mask the unused padding bits of the copied byte so the fast path output stays canonical. Adds coverage to `04501_qbit_cast_between_params` for both directions (`Float32` -> `BFloat16` and `BFloat16` -> `Float32`) using a poisoned tuple-backed source with a padding bit set. Address review: ClickHouse#109387 (comment) 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
Support CAST between QBit types with different parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Allow
CASTbetween twoQBittypes that differ in the element type and/or the stride (the number of stride groups), as long as the dimension (the number of vector elements) is preserved. Previously only an identityQBit -> QBitcast worked — a differing element type raisedNOT_IMPLEMENTEDand a differing strideTYPE_MISMATCH.Motivation: a
QBitcolumn can now be re-quantized (e.g.Float32<->BFloat16) or re-strided in place with a plainCAST, instead of having to round-trip through anArray.Implementation:
Arrayand reuse the existingArray -> QBitpath to convert the element type and transpose into the target layout, inheriting its element-conversion (widening/narrowing) andNULLsemantics.FixedStrings;Float32<->BFloat16change —BFloat16isFloat32truncated to its top 16 bits, so the two share their top 16 bit planes;Float32's other 16 planes are the low mantissa bits that truncation drops and that widening back fills with zeros.Both fast paths compose with a simultaneous stride change. Under
accurateCast/accurateCastOrNullthe narrowingFloat32 -> BFloat16direction skips the fast path and takes the reconstruct-and-convert path instead, so a value that is not exactly representable throws / becomesNULLas the accurate-cast contract requires; the stride-only and wideningBFloat16 -> Float32cases stay on the fast path in all modes because they are always exact.Changing the dimension is still rejected with
TYPE_MISMATCH(03364_qbit_negative). New coverage in04501_qbit_cast_between_params.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Allow
CASTbetweenQBittypes that differ in the element type and/or the stride, as long as the dimension stays the same (for exampleCAST(x AS QBit(Float64, N))from aQBit(Float32, N)). Stride-only changes are lossless; element-type changes follow the correspondingArrayconversion semantics (for exampleFloat32toBFloat16may lose precision, andaccurateCast/accurateCastOrNullreject rows that are not exactly representable).Documentation entry for user-facing changes
Version info
26.7.1.731(included in26.7and later)