Skip to content

Support CAST between QBit types with different parameters - #109387

Merged
alexey-milovidov merged 6 commits into
masterfrom
qbit-cast-params
Jul 9, 2026
Merged

alexey-milovidov merged 6 commits into
masterfrom
qbit-cast-params

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 4, 2026 •

Copy link
Copy Markdown
Member

Allow CAST between two QBit types 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 identity QBit -> QBit cast worked — a differing element type raised NOT_IMPLEMENTED and a differing stride TYPE_MISMATCH.

Motivation: a QBit column can now be re-quantized (e.g. Float32 <-> BFloat16) or re-strided in place with a plain CAST, instead of having to round-trip through an Array.

Implementation:

  • General case: reconstruct the source vector at its native precision into an Array and reuse the existing Array -> QBit path to convert the element type and transpose into the target layout, inheriting its element-conversion (widening/narrowing) and NULL semantics.
  • Fast path (pure byte repack of the bit planes, no float reconstruction):
    • a stride-only 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; 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 / accurateCastOrNull the narrowing Float32 -> BFloat16 direction skips the fast path and takes the reconstruct-and-convert path instead, so a value that is not exactly representable throws / becomes NULL as the accurate-cast contract requires; the stride-only and widening BFloat16 -> Float32 cases 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 in 04501_qbit_cast_between_params.

Changelog category (leave one):

  • Improvement

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

Allow CAST between QBit types that differ in the element type and/or the stride, as long as the dimension stays the same (for example CAST(x AS QBit(Float64, N)) from a QBit(Float32, N)). Stride-only changes are lossless; element-type changes follow the corresponding Array conversion semantics (for example Float32 to BFloat16 may lose precision, and accurateCast / accurateCastOrNull reject rows that are not exactly representable).

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

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

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]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [833f096]

Summary: ✅


AI Review

Summary

This PR extends CAST between QBit types so the dimension must stay fixed while the element_type and/or stride may change, with a byte-repack fast path for stride-only casts and the Float32/BFloat16 pair. I re-checked the current head against the prior review threads, the changed code, and the added tests/docs, and I did not find any remaining correctness or contract issues in the implementation.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 4, 2026
@alexey-milovidov
alexey-milovidov requested a review from rienath July 4, 2026 03:34
`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]>
Comment thread src/Functions/FunctionsConversion.cpp Resolved
… 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.
@mintlify

mintlify Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
ClickHouse-docs 🟡 Building – Jul 8, 2026, 11:44 PM

Comment thread src/Functions/FunctionsConversion.cpp Resolved
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]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.80% 85.90% +0.10%
Functions 92.70% 92.70% +0.00%
Branches 78.00% 78.00% +0.00%

Changed lines: Changed C/C++ lines covered: 97/98 (98.98%) · 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.

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Jul 9, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 9, 2026
Merged via the queue into master with commit e2e183e Jul 9, 2026
344 of 345 checks passed
@alexey-milovidov
alexey-milovidov deleted the qbit-cast-params branch July 9, 2026 22:04
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 9, 2026
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
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.

2 participants