Skip to content

Fix DEFAULT recomputation for QBit subcolumns in transposed distance functions (Sonnet) - #110638

Closed
alexey-milovidov wants to merge 2 commits into
masterfrom
fix-qbit-transposed-default-mutation
Closed

alexey-milovidov wants to merge 2 commits into
masterfrom
fix-qbit-transposed-default-mutation

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

cosineDistanceTransposed and cosineDistanceTransposedQuantized rewrite their QBit argument into a read of individual bit-plane subcolumns (qb.1, qb.2, ...) via DistanceTransposedPartialReadsPass, instead of reading the whole QBit column. When the QBit column is not yet physically materialized in a part — e.g. right after ALTER TABLE ... ADD COLUMN qb QBit(...) DEFAULT ..., or while a MATERIALIZE COLUMN mutation for it is still running — a plain read of the column correctly recomputes the DEFAULT on the fly, but the subcolumn-rewritten transposed read did not, and instead fed an empty array into the implicit QBit cast:

Code: 190. DB::Exception: Array arguments must have size 128 for QBit conversion, got 0:
In scope _CAST([], 'QBit(BFloat16, 128)') AS qb, _CAST([], 'Array(Nothing)') AS v: ...

The root cause is in injectRequiredColumnsRecursively (src/Storages/MergeTree/MergeTreeBlockReadUtils.cpp): when a requested column is missing from the part, it looks up the column's DEFAULT expression to determine which real columns must be read to recompute it. For a subcolumn request such as qb.1, it looked up the DEFAULT using that raw subcolumn name — but ColumnsDescription::getDefault only indexes top-level column names, so the lookup silently failed, and the DEFAULT's actual dependency (v in qb DEFAULT CAST(v, 'Array(BFloat16)')) was never injected into the set of physically-read columns. IMergeTreeReader::evaluateMissingDefaults then had no data for v and fell back to the type's placeholder default (an empty array), which fails QBit's strict per-vector size check.

The fix resolves the column to its top-level storage name before the DEFAULT lookup, matching what the plain (non-subcolumn) read path already does. A regression test reproduces the bug deterministically by stopping merges on a table right after ADD COLUMN qb QBit(...) DEFAULT ..., so the column stays permanently un-materialized in the existing part.

Closes: #110634

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed cosineDistanceTransposed and cosineDistanceTransposedQuantized throwing Code: 190. SIZES_OF_ARRAYS_DONT_MATCH (Array arguments must have size N for QBit conversion, got 0) when their QBit column argument has a DEFAULT expression and is not yet physically materialized in a part, e.g. right after ADD COLUMN ... DEFAULT ... or while a MATERIALIZE COLUMN mutation for it is still in progress.

…nsposed)

`cosineDistanceTransposed` and `cosineDistanceTransposedQuantized` rewrite
their `QBit` argument into a read of the individual bit-plane subcolumns
(e.g. `qb.1`, `qb.2`, ...) via `DistanceTransposedPartialReadsPass`. When the
`QBit` column is not yet physically materialized in a part (its value comes
from a `DEFAULT` expression, e.g. right after `ADD COLUMN ... DEFAULT ...`
or mid-`MATERIALIZE COLUMN` mutation), `injectRequiredColumnsRecursively`
looked up the `DEFAULT` using the raw, possibly-subcolumn name (`qb.1`)
instead of resolving it to the top-level storage column name (`qb`) first.

`ColumnsDescription::getDefault` only indexes top-level column names, so the
lookup silently failed for subcolumn requests, and the `DEFAULT`'s real
dependency columns (e.g. `v` in `qb DEFAULT CAST(v, 'Array(BFloat16)')`) were
never injected for physical reading. `IMergeTreeReader::evaluateMissingDefaults`
then fell back to a zero-length placeholder for the missing dependency, which
fails `QBit`'s strict per-vector size check with `Code: 190. SIZES_OF_ARRAYS_DONT_MATCH`.

A plain (non-transposed) read of the same column was unaffected, since it
requests the top-level column name directly.

Closes: #110634
@clickhouse-gh

clickhouse-gh Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [daea90a]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_tsan, s3 storage, sequential, 2/2) FAIL
Server died FAIL cidb
03912_encrypted_s3_cache_cancel_read UNKNOWN cidb
03912_encrypted_s3_cache_smoke UNKNOWN cidb
ThreadSanitizer: data race (STID: 1679-3e61) FAIL cidb

AI Review

Summary

This PR fixes the missing-DEFAULT dependency lookup for subcolumn reads from unmaterialized QBit columns in the transposed-distance path. The code change itself looks consistent with the existing full-column default-evaluation flow, but the added regression test is weaker than the behavior the PR claims to protect: it proves the functions return correct results, not that they still go through the partial-read rewrite where the bug actually lived.

Findings

⚠️ Majors

  • [tests/queries/0_stateless/04512_qbit_default_column_transposed_distance.sql:31-36] The new regression never proves that DistanceTransposedPartialReadsPass rewrites these calls into qb.N / qi.N subcolumn reads. Because the unoptimized whole-column path already recomputes the DEFAULT correctly, the test can stay green if a future change disables the rewrite for unmaterialized QBit columns or if it runs under an old-analyzer profile. Force enable_analyzer = 1 / the optimized path here and add a plan-level assertion, similar to the existing partial-read tests.
Final Verdict

⚠️ Needs changes before merge: strengthen the regression so it demonstrably exercises the optimized subcolumn-read path.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 16, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Merged in the latest master (no conflicts). The two CI failures on this run are unrelated to this change:

@groeneai, could you take a look at the MergingSortedAlgorithm recurrence (STID: 2651-3359) and provide a fix, or reopen #106740 if it's the same root cause resurfacing?

@alexey-milovidov
alexey-milovidov marked this pull request as draft July 16, 2026 14:11
@alexey-milovidov alexey-milovidov changed the title Fix DEFAULT recomputation for QBit subcolumns in transposed distance functions Fix DEFAULT recomputation for QBit subcolumns in transposed distance functions (Sonnet) Jul 16, 2026
@alexey-milovidov alexey-milovidov added the hold Skip for auto-updating. label Jul 16, 2026
Comment on lines +31 to +36
WITH (SELECT CAST(qb, 'Array(Float32)') FROM t_qbit_default_transposed WHERE id = 0) AS r
SELECT id, round(cosineDistanceTransposed(qb, r, 16, 4), 4) FROM t_qbit_default_transposed ORDER BY id;

SELECT 'cosineDistanceTransposedQuantized must recompute the DEFAULT too, instead of reading an empty source column';
WITH (SELECT CAST(qi, 'Array(Float32)') FROM t_qbit_default_transposed WHERE id = 0) AS r
SELECT id, round(cosineDistanceTransposedQuantized(qi, r, 8, 4), 4) FROM t_qbit_default_transposed ORDER BY id;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This regression only checks the final result, but the failure being fixed only happens after DistanceTransposedPartialReadsPass rewrites qb / qi into subcolumn reads. A plain full-column read already recomputes the DEFAULT correctly (the block above proves that), so this stays green if a later change disables the rewrite for unmaterialized QBit columns, or if the query runs under an old-analyzer profile. Please force the analyzer/optimized path here and assert that the plan actually reads qb.N / qi.N (similar to 03375, 04489, 04491), otherwise the test does not prove the broken path is fixed.

@groeneai

Copy link
Copy Markdown
Collaborator

On the MergingSortedAlgorithm failure (STID 2651-3359): confirmed it is the same root cause as #106740 resurfacing, not caused by this PR. I posted the analysis and asked to reopen #106740 (I lack reopen permission): #106740 (comment)

Summary: it recurs on Stress test debug builds across master 231acce84a (07-14), PR #79490 (07-15), and this PR 9331deb5 (07-16), with both #106664 and #107053 as ancestors. The trigger here is a single-table DISTINCT read-in-order (SELECT DISTINCT CounterID, EventDate FROM test.hits ORDER BY 1 ASC, CounterID DESC), so #107053's union-specific resetVirtualRowConversions() does not cover it. setVirtualRow default-fills EventDate with 0 when the merge sort description (CounterID, EventDate) is longer than the virtual_row_conversion (which maps only CounterID from the ORDER BY); the remembered boundary EventDate=0 then trips against a real EventDate=16151 at a CounterID tie. Release builds compile the assertion out, so it is a silent mis-ordered merge on that path.

I could not build a deterministic local reproducer (it needs the exact granule-boundary tie under stress data/scheduling), so per the no-speculative-change policy I am not opening a fix PR. The fix belongs in the virtual-row optimization: truncate the remembered/checked boundary to the sort-column prefix the virtual row actually populates from the index.

@groeneai

Copy link
Copy Markdown
Collaborator

The MergingSortedAlgorithm recurrence (STID 2651-3359) is a single-table read-in-order bug, not caused by this QBit change. Root cause: distinct-in-order widens the read to a longer sort-key prefix than the one ORDER BY built the virtual row for, so the extra column is default-filled and mis-orders the reverse merge (#107053's union fix does not cover this path). Reproduced on debug and fixed in #110725 (Closes #106740).

@clickhouse-gh

clickhouse-gh Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

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

Changed lines: Changed C/C++ lines covered: 4/4 (100.00%) · Uncovered code

Full report · Diff report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hold Skip for auto-updating. pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

QBit: cosineDistanceTransposed / cosineDistanceTransposedQuantized fail (Code 190, empty source) on a non-materialized DEFAULT column

2 participants