Repository navigation
Conversation
…keys The read-in-order virtual row could announce a wrong merge boundary in two cases: a later optimization (distinct-in-order, LIMIT BY) widening the read to a longer sort-key prefix than the conversion was built for, and a fixed key column skipped by ORDER BY (WHERE b = 1 ORDER BY a, c on key (a, b, c)), which mapped later sort columns onto wrong key columns. Both tripped "Virtual row boundary violated in MergingSortedAlgorithm" in debug builds and could silently mis-order the merge in release builds; a Nullable key threw "Virtual row has different type" even in release builds. Keep the optimization instead of disabling it: read index values for the whole used key prefix (exact bounds for the in-order merges inside the read step), announce sort columns after a skipped key as extreme constants of their sort direction, and drop the conversion only when a re-requested prefix makes it unsound. The virtual row is disabled fail-close when no extreme is representable (e.g. no greatest String). Alternative to: #110725 Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [7405685] Summary: ❌
|
Replace the extreme-constant padding with covered-prefix semantics: the virtual row conversion outputs only the sort-description prefix it can announce exactly (index values while the key prefix is contiguous, and the constant a filter fixes a column to - extracted from the equals condition - for fixed columns), and the merge compares the virtual row only on the covered prefix, placing it first on a tie. This handles any key type uniformly (e.g. a descending String column after a skipped key no longer disables the optimization) and avoids encoding comparator semantics into padded values. The covered prefix is derived at the merge from the pk block column names, carried per cursor in SortCursorImpl::sort_prefix_limit and honored by the generic SortCursor::greaterAt. Truncation can only occur with a multi-column sort description, which always uses the generic cursor, so the single-column specialized queues are unaffected; the JIT comparator is bypassed when a truncated cursor participates. Co-Authored-By: Claude Fable 5 <[email protected]>
Restore the pre-existing behavior: a column fixed by the filter that stays in ORDER BY disables the virtual row, as on master. Announcing it with the constant extracted from the filter condition would be a new feature, not part of this bug fix. Drop the FixedColumns value map and the constant extraction; the covered-prefix truncation remains only for the cases where the virtual row used to be applied and produced a wrong boundary: the widened in-order prefix and the skipped fixed key. Co-Authored-By: Claude Fable 5 <[email protected]>
…imit Keep the sort cursor machinery untouched: instead of carrying a covered sort prefix in SortCursorImpl and honoring it in the comparators, a virtual row that covers only a prefix of the sort description does not enter the sorting queue at all. MergingSortedAlgorithm remembers its boundary per source and does not emit rows that are not strictly below every announced boundary on its covered prefix; reaching a boundary fetches the real data of that source (the boundary is cleared before requesting, so an exhausted source cannot stall the merge). Virtual rows covering the full sort description ride the queue as before. Co-Authored-By: Claude Fable 5 <[email protected]>
…prefix limit" This reverts commit ea771c1.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 126/146 (86.30%) · Uncovered code |
The merge of master (6b600a1) kept both the old #110725 block, which references the deleted widened_over_previous_request variable, and its replacement guard above, so every build of the PR failed with 'use of undeclared identifier'. Keep only the replacement: the covered-prefix comparison makes the old sample-block-width heuristic unnecessary. Co-Authored-By: Claude Fable 5 <[email protected]>
…al row The arm_debug stress test keeps hitting STID 2651-3359 through a fuzzed variant of this test's own query: ORDER BY 1 ASC, CounterID DESC fuzzed into ORDER BY toIntervalMinute(1) > toIntervalWeek(59) ASC, CounterID DESC. The constant first key becomes an extra conversion output, so the merged sample-block-width guard from #110725 miscounted the covered key prefix and kept the conversion that announces nothing for EventDate. Reproduced on the master arm_debug build of 38b6b20; fixed by the covered-prefix comparison in this PR. https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=38b6b2033bca801499eae3c06559c99a57be8328&name_0=MasterCI&name_1=Stress%20test%20%28arm_debug%29 Co-Authored-By: Claude Fable 5 <[email protected]>
…ster merge The 6b600a1 master merge resolved the fixed-key conflict in buildInputOrderFromSortDescription to master's side (#110725's blanket can_optimize_virtual_row = false from c87e2ed), dropping this branch's key_index_values_usable = false. That silently reverted the PR's behavior: the virtual row stayed disabled for skipped-middle-key reads and the EXPLAIN assertions in 04549 expecting 'Virtual row conversions' failed. Verified against a synthesized clean merge of the pre-merge tip with current master: this was the last divergence in src. Co-Authored-By: Claude Fable 5 <[email protected]>
|
Master keeps hitting the boundary assertion this PR fixes — near-daily in stress tests since 2026-07-20 (tracked by #106630, e.g. today's Since the PR was blocked, I pushed three commits (plus a master merge) to unblock it:
Verified on a local aarch64 debug build (boundary assertions compiled in): the fuzzed query returns correct results with the optimization enabled, and I also added |
Resolved conflicts with #113291: keep this PR's covered-prefix comparison (`sort_prefix_limit`) and drop the strict `checkVirtualRowCoversSortDescription` debug check it supersedes; adopt master's `do_debug_checks` constexpr style and the `setVirtualRow` pk-block return both sides introduced.
…ead of dropping it The AI review Major: `requestReadingInOrder` kept the conversion based on the logical primary-key width, while `readInOrder` and `MergeTreeSelectProcessor::buildVirtualRowFromIndex` silently skipped the virtual row for every part whose loaded index is shorter than the widened prefix (`optimizeIndexColumns` drops near-unique suffix index columns, ratio default 0.9), leaving the plan-level decisions (read-in-order through inner join, skipped preliminary merge in per-block mode) believing virtual rows exist. Now the virtual row degrades to the loaded index prefix — the merges compare it only on the covered prefix — and is skipped only when the loaded index cannot feed the conversion's inputs. Test coverage asked by the review: a truncated-index section asserting the virtual row reaches the pipeline (EXPLAIN PIPELINE) in both modes, LIMIT BY sections for both plan shapes (`optimizeLimitByInOrder` cannot cross a `SortingStep`, see the comments), `read_in_order_use_virtual_row_per_block = 1` cases for the widened and skipped-key paths, and `SYSTEM STOP MERGES` + four parts in the skipped-key sections so the multi-source merge is deterministic.
|
The fix is in progress in #123354 (same red as on #122482). Query #1 is |
Conflict in `ReadFromMergeTree.h`: master added `canReadInReverseOrder` next to the virtual row conversion setters; kept it together with this branch's `setVirtualRowConversions` taking `key_prefix_size` and the reset of `virtual_row_key_prefix_size`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Merged current |
Conflict in `MergingSortedAlgorithm::initialize`: master switched containers to `VectorWithMemoryTracking`; kept the PR's `virtual_row_blocks` using the same container. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…refix Mirror the truncated-cursor handling of `SortCursor::greaterAt` in `SortCursorWithCollation::greaterAt`: compare a read-in-order virtual row only on its covered sort-key prefix and sort the more truncated cursor first on a tie. Add a test of per-block virtual rows with collated ORDER BY. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s a key `buildInputOrderFromSortDescription` truncates `match_infos` when the NULL/NaN placement check rejects the current key, but kept the `num_virtual_row_columns` counter incremented for it. The virtual row conversion then indexed `match_infos` past its end, which aborted the server in the Fast test (`00940_order_by_read_in_order_query_plan`: `select * from tab order by (a + b) * c, sin(a / b) desc`). https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111457&sha=213645f8dbdab541de581345a1020fb579e9473f&name_0=PR&name_1=Fast%20test #111457 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 The red |
Resolve conflicts in `ReadFromMergeTree` with the `FINAL` virtual row refactoring from master (`getVirtualRowBound`, `makeVirtualRowHeader`, `everyPartHasVirtualRowBound`): - `makeVirtualRowHeader` takes `include_used_key_prefix`: the non-`FINAL` in-order read widens the header to the whole used key prefix (as this PR did), the `FINAL` read keeps master's header. - `getVirtualRowBound` takes the minimal number of key columns (those the conversion reads) and builds the bound over the loaded index prefix when `optimizeIndexColumns` dropped suffix columns. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…escription` The NULL placement rollback restored `virtual_row_prefix_open` right before `break`, so the value was never read. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111457&sha=f8e2989e99daf4a1c5c3d10fe237aab9f36ac3ed&name_0=PR&name_1=Build%20%28arm_tidy%2C%204%2F4%29 PR: #111457 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Merged current
Also fixed the red Locally, all 29 |
…distinct_in_order_boundary` The `COLLATE` cases guard the covered-prefix comparison introduced by this PR, so they pass on master and made the bugfix validation job red (each new test that passes on the unfixed binary counts as a failure there). Keep them in the regression test that does reproduce the bug on master. The file now needs `no-fasttest` because collations require ICU. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111457&sha=latest&name_0=PR&name_1=Bugfix%20validation%20(functional%20tests%2C%20amd64) Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Merged current |
|
🕵 Merged current |
|
🕵 The only red check is |
|
🕵 Merged current |
|
🕵 Merged current |
|
🕵 Merged current |
`master` now keeps the read-in-order virtual row for a sorting key column that is fixed by the filter and listed in `ORDER BY` (f148849, `Keep the read-in-order virtual row for a fixed ORDER BY key column`): the virtual row takes such a column from the index like any other key column. That branch of `buildInputOrderFromSortDescription` pushes a `MatchInfo` but, after the merge, did not advance `num_virtual_row_columns`, so the virtual row conversion was cut one column short. For `WHERE a = 1 ORDER BY a, b` over the key `(a, b)` the virtual row covered only `a`, announced nothing about `b`, and the parts far from the result were read anyway, which failed `05620_read_in_order_virtual_row_fixed_key` in the Fast test. Extend the virtual row prefix in that branch under the same conditions as for an ordinary matched key column (only while the key prefix is contiguous, i.e. no key column was skipped before it). `04549_virtual_row_distinct_in_order_boundary` pinned the old behavior (no virtual row for `WHERE b = 1 ORDER BY a, b, c` over the key `(a, b, c)`). The virtual row is now kept there, as `master` intends, and the result still matches the unoptimized read, so the expectation is updated. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111457&sha=5e248134af3b6480389dba366cb879fc9e820da6&name_0=PR&name_1=Fast%20test PR: #111457 Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
🕵 The Fast test was red on
Two things are left for the author:
|
# Conflicts: # src/Processors/QueryPlan/ReadFromMergeTree.h
…rsion The merge with `master` brought `copyVirtualRowConversions` (used by `optimizeLazyFinal`), which now copies `virtual_row_key_prefix_size` along with `virtual_row_conversion`. Do the same in `ReadFromMergeTree::clone` and in the rebuilt parallel replicas read, so that `requestReadingInOrder` on the copy still recognizes a prefix narrower than the one the conversion was built for. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
🕵 Merged current
|
Closes: #106740
Closes: #106630
Closes: #116155
Related: #110725
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a wrong result (mis-ordered merge) for read-in-order queries with a virtual row when
distinct-in-orderorLIMIT BYwidens the read to a longer sort-key prefix thanORDER BYset it up for, and when a key column fixed by the filter is skipped byORDER BY(e.g.WHERE b = 1 ORDER BY a, con key(a, b, c)). The virtual row announced a wrong merge boundary: in release builds the merge could be silently mis-ordered, in debug builds the boundary assertion fired, and aNullablekey column after the skipped one threw theVirtual row has different typeexception. The virtual row optimization now stays enabled in these cases.Description
Alternative to #110725: instead of dropping the virtual row conversion when the in-order read prefix changes (and disabling it for skipped key columns), keep the optimization enabled and compare the virtual row only on the sort-key prefix it validly covers.
Root cause. The read-in-order virtual row announced a wrong merge boundary in two ways:
optimizeReadInOrderbuilds the virtual row conversion for the prefixORDER BYneeds (e.g.CounterID). A later optimization (optimizeDistinctInOrder,optimizeLimitByInOrder) re-requests the read with a longer prefix (CounterID, EventDate), but thepk_blockwidth was derived from the conversion's input count, so the extra sort column was default-filled with0insetVirtualRow. In reverse order0understates the real values, so a real row exceeded the announced boundary:Virtual row boundary violated in MergingSortedAlgorithm ... the virtual row announced UInt64_0 but the source then produced UInt64_1in debug builds, a silently mis-ordered merge in release builds.(a, b, c)andWHERE b = 1 ORDER BY a, c, the fixed keybis skipped without anORDER BYcounterpart, but the conversion DAG indexed key columns densely, mappingconto key columnb(visible inEXPLAIN actions=1: inputbaliased to__table1.c). The wrong value tripped the boundary check; a wrong type (Nullablekey) threw theVirtual row has different typelogical error even in release builds. Moreover, index values of the columns after the skipped key are semantically unusable: the index describes pre-filter data, so the entry(5, 0, 9)does not bound the filtered row(5, 1, 3)projected to(a, c).Fix.
ORDER BY. A skipped fixed key column ends the index-backed part: the index entry at a mark boundary may hold a filtered-out value for it, so its later components bound nothing in the filtered stream. A column fixed by the filter that stays inORDER BYkeeps disabling the virtual row, as before this fix.MergingSortedAlgorithmand carried per cursor inSortCursorImpl::sort_prefix_limit, honored by the genericSortCursor::greaterAt. A truncated virtual row can only occur with a multi-column sort description (its coverage is at least the first column), which always uses the generic cursor, so the single-column specialized queues are unaffected; the JIT comparator is bypassed when a truncated cursor participates.ReadFromMergeTree::readInOrderreads index values for the whole used sorting-key prefix instead of only the conversion inputs. The in-order merges inside the read step sort by the full prefix, and index values are exact bounds for it even after filtering (a filter only removes rows), so these merges always see fully covered virtual rows.ReadFromMergeTree::requestReadingInOrderdrops the conversion only when a re-request makes it unsound: a prefix narrower than the one it was built for (the conversion could lose its inputs), or one not fully backed by the primary index.setVirtualRowConversionsbuilds the conversion withproject_inputsso a raw index column cannot shadow a same-named conversion output when the merge looks sort columns up by name (matters with the old analyzer).This handles all key types uniformly — e.g. a descending
Stringcolumn after a skipped key keeps the optimization even though the type has no greatest value to pad with.Verified on release and debug builds (the boundary assertion is compiled only into debug builds): the previously aborting repros now return correct results, and
EXPLAINkeepsVirtual row conversionsfor the widened and skipped-key reads. The test is based on the one from #110725, extended with checks that the optimization stays enabled, a descending skipped-key case, aStringcase in both directions, and a fixed key kept inORDER BY(still disabled, as before).Workflow [PR]
Sync PR [sync-upstream/pr/111457]