Repository navigation
Add dotProductTransposed distance function for QBit - #108100
Conversation
`QBit` already supports the transposed (bit-plane) distance functions `L2DistanceTransposed` and `cosineDistanceTransposed`, but had no inner product counterpart. This adds `dotProductTransposed`, the transposed analogue of `dotProduct`. Unlike the distance functions, the dot product is a similarity measure: larger values mean more similar vectors. It returns `Float64` and accepts the same `(qbit, reference, p)` arguments, where `p` selects how many bit planes to read for the precision-speed trade-off. Implementation reuses the generic `FunctionArrayDistance` kernel machinery: the new `DotProductTransposed` kernel resolves the SimSIMD `dot` metric and provides a scalar fallback. The function is also recognized by `DistanceTransposedPartialReadsPass` so the partial-reads optimization applies to it as well. `scalarProductTransposed` is registered as an alias, mirroring the `dotProduct`/`scalarProduct` pair. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [1f4cb31] Summary: ✅
AI ReviewSummaryThis PR adds Final VerdictStatus: ✅ Approve |
alexey-milovidov
left a comment
There was a problem hiding this comment.
LGTM and it is fairly simple. Only need to add the path for Int8.
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]>
Address the AI review on PR #108100. The existing `04403_dot_product_transposed` test only compares the returned values with `optimize_qbit_distance_function_reads` disabled and enabled, so it would still pass if `dotProductTransposed` were dropped from `DistanceTransposedPartialReadsPass` and the query read the whole `QBit` column. Add `04489_dot_product_transposed_partial_reads_pass`, the plan-level companion to `03375_l2_distance_transposed_partial_reads_pass`. It uses `EXPLAIN` to prove that `dotProductTransposed` (and its `scalarProductTransposed` alias) read only the requested bit planes `vec.1` .. `vec.p` when the optimization is enabled, and the whole `vec` column when it is disabled. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Updated this PR:
The only CI red was the Upgrade check (amd_release) "Error message in clickhouse-server.log" failure ( @groeneai, could you investigate this upgrade-check log-noise flake and provide a fix in a separate PR (or link an existing one if it is already being tracked)? I could not find an open issue for it. |
|
@alexey-milovidov Confirmed. This is the chronic background-mutation upgrade-check log-noise (issue #39174 class), CIDB (30d, Already tracked and fixed by #108651 ("Match benign mutation parse errors by class in the Upgrade check"), which you approved. It allow-lists this benign class in No separate PR needed; #108651 covers it. |
|
Merged the latest The three red checks on the previous run are all unrelated flakes, none of which a new
The fresh CI run triggered by this merge should clear the transient failures. |
|
Merged the latest Review status is unchanged: AI Review verdict is ✅ Approve with no required actions, the single inline thread is resolved, and there are no unresolved threads. Still The sole CI red on the previous run was the known transient phantom-memory flake in the AST fuzzer ( |
|
The fresh CI run (commit thrown from The root cause and fix are already in progress in #108854, which disconnects out-of-sync pooled connections in Otherwise the PR is in good shape: the branch is 0 commits behind |
|
Re-checked the current run (commit
Both CI reds are known flakes, unrelated to this PR (it adds only a
The remaining checks ( |
|
Continue-PR pass (no code change): independently verified the feature is correct and complete, and confirmed both CI reds are known unrelated flakes. Implementation review — sound and complete:
Review status: AI Review verdict is ✅ Approve; the single inline thread is resolved; no unresolved threads. The CI: both reds on
I re-ran the failed |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 69/72 (95.83%) · Uncovered code |
|
Continue-PR pass (no code change). The feature is complete and review feedback is addressed (AI Review verdict ✅ Approve, the single inline thread is resolved, no unresolved threads, The fresh run on
I re-ran the failed @groeneai, could you investigate this recurring debug-only Otherwise the PR is in good shape and remains |
Merging master brought in the just-merged `dotProductTransposed` (PR #108100). Because all three transposed distance functions (`L2DistanceTransposed`, `cosineDistanceTransposed`, `dotProductTransposed`) share the generic `FunctionArrayDistance<Kernel>` template and the `DistanceTransposedPartialReadsPass`, the optional fourth `dims` argument for Matryoshka-style reduced-dimension search on a strided `QBit` now works for `dotProductTransposed` (and its `scalarProductTransposed` alias) too, with no function-specific code. Advertise the optional `dims` argument in the `dotProductTransposed` function documentation (syntax and argument list), matching the other two transposed functions, and add a focused test `04494_qbit_stride_dot_product_transposed.sql` that proves value equality against a non-strided baseline, the `scalarProductTransposed` alias, nullability propagation, the validation rules, and the partial-read I/O contract (an 8-bit search over the first 2048 of 4096 dimensions reads 16 of 64 plane streams). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Investigated the Root cause (not general inaccurate monotonicity — the cast is genuinely strict): the Deterministic repro on debug ( Fix in #109023: only promote a key column to |
matchesExactContinuousRange gates the exact continuous-range (implicit count projection) optimization and classifies a key column as an exact POINT when its monotonic-function chain is strict. Comparing a key column against a wider-typed constant (e.g. a UInt64 key vs toUInt256(...)) inserts a widening CAST into that chain. The widening cast is genuinely strictly monotonic, so the column was marked POINT. But checkInRange applies the chain to the granule bounds via applyMonotonicFunctionsChainToRange, which forces the bounds closed. For a granule that straddles the key boundary, the closed-bound mapped range only partially matches, so checkInRange honestly reports can_be_false = true. That contradicts the POINT "exact continuous" promise, and the exact range computation in markRangesFromPKRange then finds an interior granule that can_be_false and throws the debug-only "Inconsistent KeyCondition behavior" logical error (the ClickHouse#90461 monotonicity canary, #ifndef NDEBUG). Found by the AST fuzzer mutating 04303_key_condition_ifnull_coalesce_boolean.sql (team_id = 1 -> team_id = toUInt256(1)) in Stress test (arm_debug) on PR ClickHouse#108100 (STID 5182-2f27). There is no behavior change in release builds (the branch is compiled out under NDEBUG); the count result was already correct, the wider-typed comparison just reads a couple more granules. Fix: only promote a key column to POINT when its monotonic-function chain is empty (a native key column). A non-empty chain, even a strict one, cannot promise exact continuity across boundary granules because the chain application forces closed bounds; treat such a column as a RANGE. This mirrors how the function already downgrades non-strict chains and only disables the exact-range fast path for the affected queries (pruning and results are unchanged). Related: ClickHouse#90461 Co-Authored-By: Claude Opus 4.8 <[email protected]>
matchesExactContinuousRange gates the exact continuous-range (implicit count projection) optimization and classifies a key column as an exact POINT when its monotonic-function chain is strict. Comparing a key column against a wider-typed constant (e.g. a UInt64 key vs toUInt256(...)) inserts a widening CAST into that chain. The widening cast is genuinely strictly monotonic, so the column was marked POINT. But checkInRange applies the chain to the granule bounds via applyMonotonicFunctionsChainToRange, which forces the bounds closed. For a granule that straddles the key boundary, the closed-bound mapped range only partially matches, so checkInRange honestly reports can_be_false = true. That contradicts the POINT "exact continuous" promise, and the exact range computation in markRangesFromPKRange then finds an interior granule that can_be_false and throws the debug-only "Inconsistent KeyCondition behavior" logical error (the ClickHouse#90461 monotonicity canary, #ifndef NDEBUG). Found by the AST fuzzer mutating 04303_key_condition_ifnull_coalesce_boolean.sql (team_id = 1 -> team_id = toUInt256(1)) in Stress test (arm_debug) on PR ClickHouse#108100 (STID 5182-2f27). There is no behavior change in release builds (the branch is compiled out under NDEBUG); the count result was already correct, the wider-typed comparison just reads a couple more granules. Fix: only promote a key column to POINT when its monotonic-function chain is empty (a native key column). A non-empty chain, even a strict one, cannot promise exact continuity across boundary granules because the chain application forces closed bounds; treat such a column as a RANGE. This mirrors how the function already downgrades non-strict chains and only disables the exact-range fast path for the affected queries (pruning and results are unchanged). Related: ClickHouse#90461 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Address the AI review on PR ClickHouse#108100. The existing `04403_dot_product_transposed` test only compares the returned values with `optimize_qbit_distance_function_reads` disabled and enabled, so it would still pass if `dotProductTransposed` were dropped from `DistanceTransposedPartialReadsPass` and the query read the whole `QBit` column. Add `04489_dot_product_transposed_partial_reads_pass`, the plan-level companion to `03375_l2_distance_transposed_partial_reads_pass`. It uses `EXPLAIN` to prove that `dotProductTransposed` (and its `scalarProductTransposed` alias) read only the requested bit planes `vec.1` .. `vec.p` when the optimization is enabled, and the whole `vec` column when it is disabled. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…-transposed Add dotProductTransposed distance function for QBit
Merging master brought in the just-merged `dotProductTransposed` (PR ClickHouse#108100). Because all three transposed distance functions (`L2DistanceTransposed`, `cosineDistanceTransposed`, `dotProductTransposed`) share the generic `FunctionArrayDistance<Kernel>` template and the `DistanceTransposedPartialReadsPass`, the optional fourth `dims` argument for Matryoshka-style reduced-dimension search on a strided `QBit` now works for `dotProductTransposed` (and its `scalarProductTransposed` alias) too, with no function-specific code. Advertise the optional `dims` argument in the `dotProductTransposed` function documentation (syntax and argument list), matching the other two transposed functions, and add a focused test `04494_qbit_stride_dot_product_transposed.sql` that proves value equality against a non-strided baseline, the `scalarProductTransposed` alias, nullability propagation, the validation rules, and the partial-read I/O contract (an 8-bit search over the first 2048 of 4096 dimensions reads 16 of 64 plane streams). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Adds
dotProductTransposed, the transposed (bit-plane) inner-product function for theQBitdata type.QBitalready supported the transposed distance functionsL2DistanceTransposedandcosineDistanceTransposed, but had no inner-product counterpart. This change addsdotProductTransposed, the transposed analogue ofdotProduct. Unlike the distance functions, the dot product is a similarity measure: larger values mean more similar vectors.The function returns
Float64and takes the same(qbit, reference, p)arguments as the other transposed functions, wherepselects how many bit planes to read for the precision-speed trade-off. It reuses the genericFunctionArrayDistancekernel machinery: the newDotProductTransposedkernel resolves the SimSIMDdotmetric with a scalar fallback. It is also recognized byDistanceTransposedPartialReadsPass, so the partial-reads optimization (optimize_qbit_distance_function_reads) applies to it.scalarProductTransposedis registered as an alias, mirroring the existingdotProduct/scalarProductpair.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added the
dotProductTransposedfunction (aliasscalarProductTransposed) that computes the approximate inner product between aQBitcolumn and a reference vector, complementing the existingL2DistanceTransposedandcosineDistanceTransposedfunctions.Version info
26.7.1.329(included in26.7and later)