Repository navigation
Read only the rescoring candidates of vector column with one row per block - #131027
shankar-iyer wants to merge 29 commits into
Conversation
When a query reads known rows of a part - the second stage of lazy materialization, or the candidates of a vector similarity index with `vector_search_with_rescoring` - the new setting `merge_tree_use_one_row_per_block_layout` reads an `Array(BFloat16)`, `Array(Float32)`, `Array(Float64)` or `Array(Int8)` column only for these rows, instead of whole granules. This needs one row per block, stored without compression: `CODEC(NONE)` or `CODEC(Quantized(...))`, and the column-level settings `min_compress_block_size` and `max_compress_block_size` both equal to the size of a row. `tryGetOneRowPerBlockLayout` checks the settings and the file sizes, and `checkOneRowPerBlockArraySizes` checks once per part, on first use, that every row has the same number of elements. The result is kept in `IMergeTreeDataPart::hasOneRowPerBlockArraySizes`. `MergeTreeReadTask` passes the selected rows to the main reader with the new hook `IMergeTreeReader::setSelectedRows`. `MergeTreeReaderWide` then reads the qualifying columns with `MergeTreeOneRowPerBlockReader`: it seeks the usual stream to the block of each selected row, takes the block as stored with `readCompressedBlock`, and copies the row into the column. The other rows get empty arrays, which the filter that keeps the selected rows removes. Marks and array sizes of the column are not read. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The column-level `max_compress_block_size` of a `Quantized` column is set to the size of one vector for one row per block. The writer applied it to every stream of the column, so the array sizes and the quantized codes were also stored in blocks of that size: the codes of a 768-dimensional `int8` column in blocks of about 4 rows. Scanning the codes in the first stage of the quantized search then processed 66671 blocks instead of 175 for 200000 rows. Now it applies only to the stream of the array elements, as in ClickHouse#111180, and the other streams get the default. The scan of the codes takes 11-12 ms instead of 15-16 ms, and the whole rerank query 46-47 ms instead of 49-50 ms. Related: ClickHouse#111180 Co-Authored-By: Claude Opus 5.5 <[email protected]>
A column that is missing from the part and has a default expression is computed before the filter that keeps the selected rows, so the expression saw the empty arrays of the other rows: with `DEFAULT intDiv(64, length(vec))`, rescoring with `merge_tree_use_one_row_per_block_layout` threw `ILLEGAL_DIVISION`. The selected rows are not passed to the reader of such a part now. A part written with other block sizes than the column has now can pass the checks of the layout, and then fails to read with an error about the codec or the checksum. Nothing before the read can tell it from a corrupted part, so the exception now explains both causes and how to resolve the second: merge the part or disable the setting. Both cases are covered by the tests, found by the local codex review. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ow reads A column dropped by a pending mutation and added again is still in the part, but it is computed from its default expression before the filter that keeps the selected rows, like a missing column. The guard checked only missing columns, so such a default expression saw the placeholder arrays. It now covers every column that is not read from the part: missing, partly missing, or skipped by a pending mutation. A codec that compresses can produce blocks of exactly the size of a row, and then the file has the sizes of one uncompressed block per row and passed the checks of the layout. The declared codec of the column is now checked first: every codec of it must be `NONE` or `Quantized`. Both cases were found by the local codex review and are covered by the tests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…column A default expression can be evaluated before the filter that keeps the selected rows, for a column that is not read from the part. Earlier fixes checked the reasons for that one by one - a missing column, a column dropped and added again by a pending mutation - and missed others, such as a pending rename, where the column in the part has its old name. Now the reads of only the selected rows are disabled when the default expression of a requested column uses a column that gets placeholder values, directly or through the default expressions of other columns, whatever the reason the default would be evaluated. Defaults that do not use the vector column, such as `DEFAULT now()`, keep the optimization. The check runs before the one-time scan of the array sizes, so such a part is not scanned. Also make the test of the block sizes of the quantized codes independent of the randomized `index_granularity`: it counts the blocks decompressed, which grows when the read is split into tasks that start at different marks. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The usual read path calls `adjustRightMark` whenever it takes a stream, to move the read boundary to the end of the current task. The reads of only the selected rows took the stream without it, so a reader reused for another task, as `MergeTreePrefetchedReadPool` does when the number of prefetches is limited, kept the boundary of the previous task and failed with `CANNOT_READ_ALL_DATA` past it. Found by the local codex review, and covered by a test that fails without the fix. Also remove the claim that the marks of the column are not read: the streams load them, and the boundary above uses them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [0c38b47] Summary: ✅
AI ReviewSummaryThe PR adds an explicitly declared fixed-length array layout and an experimental selective reader for lazy materialization and vector rescoring. The writer enforces the row lengths and block geometry; metadata changes and unsupported part imports are rejected. Reviewed head: The consumer pass covered Missing context / blind spots
Final VerdictStatus: ✅ Approve |
…rows During a pending `MODIFY COLUMN`, the type of a column in the part differs from the requested type, and the reader converts the column before the filter that keeps the selected rows. The conversion then ran on the placeholder arrays of the other rows: for an `Array(Float32)` column being converted to `QBit(Float32, 64)`, an HNSW rescoring query failed with `Array arguments must have size 64 for QBit conversion, got 0`. Such columns are read as usual now. Found by the AI review of ClickHouse#131027 Co-Authored-By: Claude Opus 5.5 <[email protected]>
During a pending rename, the column in the part has its old name. The checks of the layout and of the default expressions then used that name, which can belong to another column: after `RENAME COLUMN payload TO renamed_payload` and `ADD COLUMN payload ...` in one `ALTER`, a default expression using `renamed_payload` was not found, and it was evaluated on the placeholder arrays, failing with `ILLEGAL_DIVISION`. A column with another name in the part than requested is now read as usual, like one with another type. The case of the reused reader in the test printed the ids returned by the approximate vector index, which differ between runs. It now only checks that the query does not fail, and still fails without the fix it covers. Found by the AI review of ClickHouse#131027 Co-Authored-By: Claude Opus 5.5 <[email protected]>
A member of `Nested`, such as `n.d`, is requested as a subcolumn of `n`, but its default expression is registered under `n.d`. The check of the default expressions looked it up only by the name in storage, `n`, so a default of a `Nested` member using the vector column was evaluated on the placeholder arrays. The default is now looked up under both names. Found by the local codex review. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The check of the default expressions mapped the requested columns to the columns whose defaults could be evaluated, and missed some of them, such as `n.d` when `n.d.x` of a `Nested` member is requested: each way of naming a column was another case. Now the reads of only the selected rows are disabled when any default expression of the table uses a column that gets placeholder values. Chains of defaults need no longer be followed, as the last default of a chain uses such a column directly. A table with a default expression derived from the vector column does not get the optimization. Found by the local codex review. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ed-row reads A default expression can name the vector column with a qualifier, such as `dummy.vec`, which is resolved to `vec` when the default is evaluated. The check compared whole identifiers with the names of the columns that get placeholder values, and missed it. Now any part of an identifier is compared, which covers qualified names and subcolumns alike. Found by the local codex review. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The reads of only the selected rows give placeholder values to the other rows before the filter that removes them, which is a new way of reading, so the setting is experimental, and `allow_feature_tier` can forbid it. Found by the local codex review. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rom its default Instead of looking for the vector columns in the default expressions, which missed names such as `dummy.a.b` for a column `a.b`, the selected-row reads are disabled when any requested column of the table is absent from the part, partially read, dropped by a pending mutation, or marked missing in the part. Only then is a default evaluated before the filter that keeps the selected rows. Virtual columns such as `_part_offset` are not filled from defaults. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ved in master `isColumnPartiallyRead` no longer exists in `master`, so the Fast test and the tidy build failed after merging it. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=131027&sha=6016ba1c3912f7341647f70fb2aea6d99ee6f9e8&name_0=PR&name_1=Fast%20test ClickHouse#131027 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes124 object files changed (+395.95 KiB total), 2 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units684 translation units recompiled, 4855 s compile time in total, 682 of them have a recent master baseline. Translation units without a recent master baseline:
|
The layout was inferred from the column-level settings when a part was read, with checks of the totals of the file sizes and, once per part, of the array sizes. The settings describe the parts written from now on, so a part written before `ALTER TABLE ... MODIFY COLUMN ... SETTINGS`, or with an adaptive write buffer, can have other blocks whose sizes add up to the same totals. A computed position then lands in the middle of another row, which was read without an error. Now the writer of a Wide part checks what it wrote to the file of the array elements of a column: one block per row, every block of one row stored without compression (`CompressedWriteBuffer::getBlockStatistics`), and every row of the same number of elements. Only then it writes a marker file, `<stream>.<hash of the file>.one_row_per_block`, which is in the checksums of the part. The reader checks the marker against its checksum, built from the column settings and the checksum of the file of the elements, so it reads nothing from disk and keeps no state in the part. Parts without a matching marker are read as usual. The name contains the hash of the file it describes, so a mutation that rewrites the column writes a marker of another name instead of writing into one hardlinked from the source part. This removes the per-part cache of the array-size check from `IMergeTreeDataPart` (64 bytes per part, which failed the storage memory check `07_many_parts.sql`), and the error of the hand-crafted part in `05617` on S3 and Azure, where the read now gives the same result with the setting on and off. `05617` gets on/off regressions for a part written with an adaptive write buffer, a merged part and mutated parts. `05618` failed in the flaky check because it ran longer than 180 s: the thread fuzzer of that job migrates the thread on every mutex lock and unlock, which makes building HNSW indexes very slow. The tables are now smaller (500 rows instead of 2000, 4000 instead of 20000); with the same thread fuzzer environment locally, 8 concurrent runs take 50-58 s instead of 170-193 s. Related: ClickHouse#131027 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-length name Found by a local review: - The marker was bound only to the file of the array elements. A mutation that changes the sizes of the arrays but not the bytes of the elements, e.g. rows of 64 ones that become rows of 63 and 65 ones, keeps that file as it is, and the marker hardlinked from the source part still matched, so rows were read across the blocks: `length` and `arraySum` gave 64 instead of 63 and 65 with the setting on. The marker now also holds the size and the hash of the file of the array sizes, which defines where the rows begin, and is used only if both match. - The name of the marker was the name of the stream with 51 bytes appended, which exceeds the limit of the file system for long column names kept as is with a large `max_file_name_length`, and the insert failed with `File name too long`, even with the setting off. The name is now a hash of the name of the stream and of both checksums, `<hash>.one_row_per_block`, of the same length for every column. `05617` gets regressions for both cases. Related: ClickHouse#131027 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…mutation Found by a local review: a mutation that hardlinks the files it does not rewrite also hardlinked the markers of the files it rewrote, together with their checksums. Each rewrite of a vector column added a marker, so the checksums of a part depended on the history of mutations: two separate updates left three markers, the same updates as one mutation left two, with identical files of data. Replicas that execute mutations in other batches would get different checksums for the same part. At the end of such a mutation, the markers that are not named after the current files of array elements and array sizes of the part are removed from the checksums and from the part (`getStaleOneRowPerBlockMarkers`), so the markers of a part depend only on its files. `05617` checks that separate and combined mutations give the same `hash_of_all_files`. Related: ClickHouse#131027 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…essed_fixed_array_size` Replace the write-time marker files with a column-level MergeTree setting that can be specified only in `CREATE TABLE`. It declares the number of elements of every row of an `Array(Float32/Float64/BFloat16/Int8)` column and makes the Wide writer store every row as one uncompressed block, so every part of the table has the layout. - `CREATE` validates the type, the codec (`NONE` or `Quantized`), no TTL, and no `min_compress_block_size`/`max_compress_block_size` alongside. - `ALTER` cannot add, change or remove the setting, change the type, the codec or the TTL of such a column, make it `ALIAS`/`EPHEMERAL`, or clear it. - `ATTACH PART`/`PARTITION` from the detached directory is not supported for such tables; `ATTACH`/`REPLACE PARTITION FROM` requires the same setting in both tables. - The Compact and Wide writers reject rows of another length; the Wide writer asserts one block per row after writing. - The reader trusts the table definition and throws `CORRUPTED_DATA` if the file sizes of a part contradict it. - Selected rows are read only if no column has a default expression with an identifier, instead of checking the state of every part. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…mpressed_fixed_array_size` - `RESTORE` with `allow_different_table_def` is not supported for a table with a column with the setting. Without it, the definition in the backup must be the same. - Lightweight updates check the updated values against the metadata of the table, as the metadata of a patch part has no column settings. - `ATTACH` from the detached directory is rejected in `attachPartition` instead of `tryLoadPartsToAttach`, so that the recovery of a replica after the loss of its metadata in Keeper can attach its own parts again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…pressed_fixed_array_size` Every operation that brings in parts that the table did not write throws `NOT_IMPLEMENTED` for such a table: `ATTACH PART` and `ATTACH PARTITION` from the detached directory, `ATTACH`, `REPLACE` and `MOVE PARTITION` between tables (also with the same setting), `RESTORE` of data, and `ATTACH TABLE` with a definition. The parts would be read at positions computed from the setting. Support can be added later. The recovery of a replica, which attaches its own parts, is not affected. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…array_size` - `RESTORE` of data is rejected in `restorePartsFromBackup`, which the restore of `StorageReplicatedMergeTree` also calls; it overrides `restoreDataFromBackup`. - `ATTACH TABLE` with a definition is rejected also when the columns are copied from another table with `AS`. - The codec of such a column must be a single `NONE` or `Quantized` codec: a chain of codecs adds a header to every block, even `CODEC(NONE, NONE)`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The formatter writes `ATTACH TABLE t UUID '...' ENGINE = ... AS template` as `ATTACH TABLE t UUID '...' AS template ENGINE = ...`, which the parser reads as `AS [NOT] REPLICATED`, so debug builds failed with `Inconsistent AST formatting`. The test uses `ATTACH TABLE ... FROM '<path>' ... AS template` instead. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=131027&sha=83e52e0c209c23c0918f7ab4eadcc5bcf4148637&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20flaky%20check%29 ClickHouse#131027 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-block layout Co-Authored-By: Claude Opus 5.5 <[email protected]>
…w-per-block tests Co-Authored-By: Claude Opus 5.5 <[email protected]>
… private `master` Moves the include of `MergeTreeOneRowPerBlockLayout.h` in `InterpreterCreateQuery.cpp` and the declaration of `setSelectedRows` in `IMergeTreeReader.h`, away from lines changed in `master` and in the private `master`. Nothing else changes. Related: ClickHouse#131027 Related: ClickHouse/clickhouse-private#87499 Co-Authored-By: Claude Opus 5.5 <[email protected]>
`master` enables `modernize-use-nodiscard`, and the tidy build failed on it. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=131027&sha=167c7e7e32ceb0dfecb2c60aa433deaa4da959f1&name_0=PR&name_1=Build%20%28arm_tidy%2C%201%2F4%29 ClickHouse#131027 Co-Authored-By: Claude Opus 5.5 <[email protected]>
AI review of a later commit does not approve the PR, see the AI Review comment.
Resolves a conflict with ClickHouse#131027 writer code after ClickHouse#130784, which changed how `MergeTreeDataPartWriterWide` finds the streams of a column. `addStreams` now registers the streams of every column once, in `resolved_streams_by_column`, and the functions called for every granule, `getCurrentMarksForColumn` and `writeColumn`, iterate that list instead of calling `enumerateStreams` and building the stream names again. - `writeColumn`: both sides added a line at the same place, the count of the rows of a column with the setting `uncompressed_fixed_array_size` and the lookup of the resolved streams. Both are kept. - `getCurrentMarksForColumn`: the check that flushes the buffer of the elements of such a column after every row used the `substream_path` of the callback of `enumerateStreams`, which the loop over the resolved streams does not have. The merge had no textual conflict there, but did not compile. The stream of the elements is now recognized by its name, which `addStreams` records in `FixedArrayColumn::stream_name` and is the `escaped_column_name` of the stream. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`master` is now 26.12, and the style check requires the settings added by this PR, `merge_tree_use_one_row_per_block_layout` and `uncompressed_fixed_array_size`, to be recorded in the history under the current version. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=131027&sha=39325ac952b30957e122db167470622d07dd36ce&name_0=PR&name_1=Style%20check ClickHouse#131027 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@rschu1ze Refreshed to latest master to clear CI failures, this run should be green. Please continue review. |
|
📊 Cloud Performance Report
PR verdict: not_sure. 3 measured difference(s) not attributable to this PR. clickbench Q10: The measured difference cannot be attributed to this PR from the available source evidence. Tested merge 45d16045 (first parent 3757c4d9, PR head 0c38b47) adds the uncompressed_fixed_array_size / merge_tree_use_one_row_per_block_layout feature. On the read side, MergeTreeReaderWide::readRows takes the new branch only when one_row_per_block_readers is non-empty. setSelectedRows fills that list only when the setting is enabled (default false) and a column has the uncompressed_fixed_array_size setting, which applies only to Array(Float*/Int8) columns. Q10 reads MobilePhoneModel and UserID from … clickbench Q30: The measured difference cannot be attributed to this PR from the available source evidence. In tested merge 45d16045 the new MergeTreeReaderWide::readOneRowPerBlock branch runs only when setSelectedRows found an eligible column. That needs merge_tree_use_one_row_per_block_layout=1 (default false) and an Array column with uncompressed_fixed_array_size. Q30 reads only scalar hits columns (SearchPhrase, SearchEngineID, ClientIP, IsRefresh, ResolutionWidth), so it uses the existing read path, and its time goes to hash aggregation that this PR does not change. The query history is noisy, and the … clickbench Q31: The measured difference cannot be attributed to this PR from the available source evidence. Tested merge 45d16045 gates the new read path in MergeTreeReaderWide (setSelectedRows/readOneRowPerBlock) on settings.use_one_row_per_block_layout, which defaults to false, and on Array columns carrying uncompressed_fixed_array_size. Q31 reads only scalar hits columns and is dominated by building a large aggregation hash table. The PR's other runtime changes are on the write side: CompressedWriteBuffer block statistics and the checkUncompressedFixedArraySizes calls in the part writers. A SELECT … clickbenchFlagged queries (3 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|


When only known rows of a part are needed, namely a) the second stage of lazy materialization (the rescore of the
quantized-codes search) and b) the candidates of a quantized vector similarity index with
vector_search_with_rescoring, the new settingmerge_tree_use_one_row_per_block_layoutreads anArray(BFloat16 | Float32 | Float64 | Int8)column only for these rows, instead of whole granules.The column declares the layout with the new column-level setting
uncompressed_fixed_array_size, the number ofelements of every row:
The
Widewriter then stores every row of the column as one compressed block without compression (NONE, or column has a singleQuantizedcodec), whatever the block sizes of the table. Note this has overhead of 25 bytes per row or per vector, but that is acceptable because each vector could be 3072 bytes or 6144 bytes or even more. The writers of all part types reject rows of any another length, and theWidewriter checks after writing that every block holds exactly one row. A row is then located arithmetically. The reader seeks the usual stream to its block, takes the block as stored (readCompressedBlock), checks it and copies it into the column. Other rows get empty arrays, which the existing filter removes. NOTE : Disk and S3 reads keep their usual chunk sizes; the saving is in checksums, copies and deserialization.The setting can be specified only in
CREATE TABLE, so every part of the table is written with it:ALTERcannot add, change or remove it, change the type, the codec or the TTL of the column, or clear the column. The column cannot have a TTL.ATTACH PARTand
ATTACH PARTITIONfrom the detached directory,ATTACH,REPLACEandMOVE PARTITIONbetween tables,RESTOREof data, andATTACH TABLEwith a definition. They can be added in a follow-up.settings.
Rows are read this way only if no column of the table has a default expression with an identifier: a column that a
part does not have is filled from its default before the filter, and such an expression could use the values of the
rows that are not read. Defaults such as
now()are fine. The setting has no effect with the uncompressed cache or the columns cache.On 200k rows × 768
Float32, a rerank of 100 candidates: 99 ms → 49 ms, 498 MiB → 150 MiB checksummed. Theremaining 150 MiB is the scan of the codes in the first stage.
Of the 1804 added lines, 725 are tests and 1079 are source code:
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added the column-level setting
uncompressed_fixed_array_sizeand the query settingmerge_tree_use_one_row_per_block_layout. A vector column declared withuncompressed_fixed_array_sizestores every vector as one uncompressed block, so that rescoring in vector search reads the full-precision vectors only for the candidate rows instead of whole granules.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/131027]