Repository navigation
Conversation
The debug assertions added in ClickHouse#106565 fired across many unrelated PRs (STID 2651-3359), including master, with the message `Virtual row boundary violated in MergingSortedAlgorithm ... the virtual row announced Decimal64_'0.000000000' but the source then produced ...`. Trigger query (`tests/queries/0_stateless/04229_distributed_cte_timestamp_rename_103508.sql` under random `read_in_order_use_virtual_row=1`): WITH A AS ( SELECT * FROM dist_t_two_shards WHERE timestamp >= toDateTime64('2023-01-01', 9) AND timestamp < toDateTime64('2025-01-01', 9) ) SELECT timestamp, id FROM A ORDER BY timestamp DESC LIMIT 10 SETTINGS distributed_product_mode = 'allow', optimize_read_in_order = 1, read_in_order_use_virtual_row = 1; The pipeline that hits the assertion is MergingSortedTransform 2 -> 1 BufferChunks * 2 Union ExpressionTransform * 2 -> ReverseTransform -> VirtualRowTransform -> MergeTreeSelect (shard 1) ExpressionTransform * 2 -> ReverseTransform -> VirtualRowTransform -> MergeTreeSelect (shard 2) The outer `MergingSortedTransform` is built by `SortingStep::mergingSorted` from the Union branch in `optimizeReadInOrder.cpp` (the case at line 1613, `sorting->convertToFinishSorting(*max_sort_descr, use_buffering, false);`), which deliberately passes `apply_virtual_row_conversions = false` because no single conversion fits all children of the Union. Each child, however, still emits virtual-row chunks carrying its own per-source conversion in `MergeTreeReadInfo::pk_block`, with the storage's primary-key column names (`timestamp`). Inside `setVirtualRow`, when `apply_virtual_row_conversions` is false the per-source rename is skipped, so `pk_block` keeps the inner name `timestamp` while the outer merge header has the analyzer-renamed `__table1.timestamp`. The lookup `findByName(col.name)` fails and the column falls back to `createColumnConstWithDefaultValue(1)` -- a Decimal64 zero. The debug assertions in `rememberVirtualRowBoundary` / `checkVirtualRowBoundary` then treat that zero as the announced boundary and abort once the source produces its real first row. Outside debug builds the merge still produces correct output because the virtual-row 1-row chunk is consumed via `skip_last_row = true` in `mergeImpl` / `mergeBatchImpl` and never reaches the output, so the only visible damage is the debug abort. Gate `rememberVirtualRowBoundary` and `checkVirtualRowBoundary` on `apply_virtual_row_conversions = true`. When the flag is false the recorded boundary is meaningless, so simply do not compare against the next chunk. The runtime path (`setVirtualRow` + `skip_last_row`) is unchanged. Add `tests/queries/0_stateless/04323_virtual_row_two_shards_pk_alias.sql` covering the two-shard union shape with `read_in_order_use_virtual_row = 1` both ASC and DESC, and with `read_in_order_use_virtual_row_per_block = 1`. The test fails on master with the assertion and passes with this fix. Caused by: ClickHouse#106565 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate
Session id: cron:clickhouse-ci-task-worker:20260607-042600 |
|
cc @vdimir @KochetovNicolai — could you review this? It gates the new debug virtual-row boundary checks (added in #106565) on |
|
Workflow [PR], commit [16b4458] Summary: ✅
AI ReviewSummary
Missing context / blind spots
Findings❌ Blockers
Tests
Final Verdict
|
| /// has its own conversion that is NOT applied here, so `setVirtualRow` falls back to default | ||
| /// values for any header column that does not match the inner pk_block by name. The resulting | ||
| /// boundary is meaningless and would always violate the next chunk's real values. Skip it. | ||
| if (apply_virtual_row_conversions) |
There was a problem hiding this comment.
This guard hides the same bad boundary from the merge scheduler, not only from the debug assertion. In the failing Union path, setVirtualRow still runs with apply_virtual_row_conversions=false; when the sort key is __table1.timestamp, pk_block.findByName misses timestamp and installs a default sort key. That one-row chunk remains in the MergingSortedAlgorithm queue with skip_last_row=true, and mergeImpl / mergeBatchImpl use its sort position to decide which source to request next before skipping it. For ORDER BY ... DESC, a default-zero boundary can make the source look exhausted and let another stream emit lower rows before this source's next real chunk, especially with per-block virtual rows or small blocks. The assertion from #106565 is therefore reporting a boundary that can mis-order the merge, not just a meaningless debug value.
Please either apply the per-chunk virtual-row conversion here (the conversion is carried in MergeTreeReadInfo) or strip/disable virtual rows before this outer merge when the boundary cannot be represented. The regression test should include a multi-block/heterogeneous-shard DESC LIMIT case so this cannot pass while only silencing the assertion.
There was a problem hiding this comment.
Thanks for the careful read. The merge-ordering consequence you describe is real - a virtual-row cursor whose sort key fell back to default zero will sink to the bottom of the DESC heap, so for ORDER BY ... DESC LIMIT N over multi-block heterogeneous shards the merge can request other sources before the virtual-row source's real chunk arrives, which would let lower-ranked rows be emitted before higher-ranked ones from the still-not-fetched source. This pre-dates #106565 - the assertion just made the bad boundary visible.
I do not, however, have a deterministic reproducer that demonstrates a wrong release-build result for this case (the local two-shard tests I tried return the expected rows; the failure shape requires specific shard data distributions that I have not isolated). Per the project's "no speculative defensive code without a deterministic reproducer" directive, I would rather not push either Path A (apply per-chunk MergeTreeReadInfo::virtual_row_conversions here) or Path B (strip virtual rows before this outer merge) without a release-build repro to validate them - both touch the read-in-order virtual-row contract that #106565 / #100603 / #100412 own.
Reframing this PR: it is now filed as CI Fix or Improvement rather than Bug Fix. The intent is narrowly to suppress the debug-only assertion abort that fires across master and unrelated PRs (CI is currently red). The Bugfix-validation harness runs against a release-build master HEAD where the chassert cannot fire, so "Failed to reproduce the bug" on master HEAD is structural, not informative for a debug-only change.
I have updated the PR description to call this out and to flag the deeper merge-ordering concern as a follow-up that should be designed by @ vdimir, who introduced the assertion and owns the read-in-order virtual-row machinery.
There was a problem hiding this comment.
What happens when we insert defaults because of name mismatch as you described? It can lead to a bug with negative values, for example. Why we can get virtual row with different column names when apply_virtual_row_conversions at all?
There was a problem hiding this comment.
@vdimir thanks for picking it up. Two answers:
(1) Why different column names with apply_virtual_row_conversions = false?
The flag is forced to false in two sites in ReadFromMergeTree.cpp (lines 1056 and 1491): the per-partition output_each_partition_through_separate_port merge, and the unitePipes for MergeTrees with non-coinciding sorting keys (the analyzer-rewritten "Union of read-in-order children" shape). In both, the outer MergingSortedTransform sees children that each carry their own MergeTreeReadInfo::virtual_row_conversions ExpressionActions, but there is no single conversion DAG that fits them all (each child's conversion was built against its own storage's sample block and per-storage aliasing). The outer merge's header is the analyzer-renamed shape (e.g. __table1.timestamp), while the inner pk_block is the raw storage column (timestamp). setVirtualRow skips read_info->virtual_row_conversions->execute(pk_block) (the rename), so pk_block.findByName(col.name) misses on every column that needed renaming and falls back to col.type->createColumnConstWithDefaultValue(1).
(2) Consequences of the default-value boundary
For numeric types getDefault() returns T() = zero. Two distinct effects:
- Heap position. The virtual-row cursor is pushed with this zero key. In
DESCit sinks to the heap bottom, inASCit floats to the top.mergeImpl/mergeBatchImplconsumeskip_last_rowonly after popping the cursor, so forORDER BY ... DESC LIMIT Nthe merge can request other sources before this source's real chunk arrives. Output stays sorted within each source, but cross-source ordering can be wrong if the LIMIT window closes before the virtual-row cursor pops. - Negative-value bug. Exactly the case you flagged. For signed integer /
Decimal/Date32the type-default zero is a perfectly valid mid-range value, so a real row with key-5looks "smaller" than the virtual-row boundary.checkVirtualRowBoundary'sdirection * compareAtwill trip on the first real chunk for either direction (ASC:-5 < 0; DESC: a real positive value is greater than the zero boundary).#106565's assertion makes this visible; releasing builds were already silently mis-using the boundary inmergeImpl/mergeBatchImplfor the heap-position decision.
This pre-dates #106565; the assertion just made the bad boundary visible. The merge-correctness consequence is real but I do not have a deterministic wrong-result reproducer for it. Per @ Algunenano's no-speculative-defensive-code rule I did not push a structural fix here, only the debug-only gate.
For the deeper fix, two paths from the bot review thread:
- Path A: apply the per-chunk
MergeTreeReadInfo::virtual_row_conversionsatsetVirtualRowtime even whenapply_virtual_row_conversions=false, since the conversion is carried per-chunk and the outer header is what we want. Risk: re-trigger the original mismatch the flag was meant to suppress in the per-partition case. - Path B: strip virtual rows before the outer merge in
ReadFromMergeTree.cpp:1041..1059and1471..1495when the boundary cannot be represented (always for these two sites). The only consumer that benefits from virtual rows here is the inner per-source merge; the outer pipe-uniting merge does not.
Path B looks smaller and safer. Tracked in our umbrella task (STID 2651-3359) as a follow-up. Happy to pick it up, or defer to you since this is read-in-order territory you own.
- src/.../MergingSortedAlgorithm.cpp: collapse the 6-line gate explanation next to `if (apply_virtual_row_conversions)` into a one-line invariant note. The full root-cause explanation (Union of read-in-order subtrees, per-source conversions, default-zero fallback in setVirtualRow) lives in the PR description and the original commit message of 28c0151. - tests/.../04323_virtual_row_two_shards_pk_alias.sql: collapse the 11-line header comment into a one-line "Regression test for STID 2651-3359 (debug assertion added in ClickHouse#106565)" - test name and the SET list make the purpose self-evident, full context lives in ClickHouse#106664. Also corrects the outdated test-file PR reference noted by clickhouse-gh inline (ClickHouse#106565 introduced the assertion; the test reproduces the assertion path covered by this PR, not ClickHouse#106654 which is unrelated). No code-behavior changes. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Pushed fix-up Pivot: category changed from This PR is a debug-only assertion suppression - the Bot inline review (correctness concern) addressed in #106664 (comment). Short version: the heap-position consequence the bot describes (default-zero virtual-row boundary sinking to the bottom of a Fix-up changes (per @ azat compactness directive):
No code-behavior changes from Pre-PR validation gate (refreshed)
Session id: cron:clickhouse-ci-task-worker:20260607-085100 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 3/3 (100.00%) | Lost baseline coverage: none · Uncovered code |
vdimir
left a comment
There was a problem hiding this comment.
Let's merge, since it won't break anything and stop CI failing, but I'll check if there are another bug or if we should really skip this case in assertion
The debug assertions added in #106565 fired across many unrelated PRs (STID 2651-3359), including master, with the message
Virtual row boundary violated in MergingSortedAlgorithm ... the virtual row announced Decimal64_'0.000000000' but the source then produced ....Root cause: when
optimize_read_in_orderandread_in_order_use_virtual_roware both enabled and the outerSortingStepsits over aUnionof read-in-order subtrees (e.g. aDistributedtable with two shards underdistributed_product_mode = 'allow'),optimizeReadInOrdercallsconvertToFinishSorting(..., apply_virtual_row_conversions = false)because no single conversion fits all children of theUnion. Each child still emits virtual-row chunks carrying its own per-source conversion inMergeTreeReadInfo::pk_block, with the storage's primary-key column names. Withapply_virtual_row_conversions = false,setVirtualRowskips the per-source rename, sopk_blockkeeps the inner name (e.g.timestamp) while the outer merge header has the analyzer-renamed__table1.timestamp.findByNamefails and the column falls back tocreateColumnConstWithDefaultValue(1)- aDecimal64zero. The debug assertions inrememberVirtualRowBoundary/checkVirtualRowBoundarythen treat that zero as the announced boundary and abort once the source produces its real first row.Fix: gate
rememberVirtualRowBoundaryandcheckVirtualRowBoundaryonapply_virtual_row_conversions = true. When the flag is false the recorded boundary is meaningless, so simply do not compare against the next chunk. The runtime path (setVirtualRow+skip_last_row) is unchanged.Scope of this PR (and what is left for follow-up)
This PR is a debug-only assertion fix - it suppresses the
chassert-driven abort that fires in debug builds and stress-test runs, restoring CI to green. It is filed asCI Fix or Improvementrather thanBug Fixbecause:#ifndef NDEBUG-gated; release builds never aborted.setVirtualRow+skip_last_row+mergeImpl/mergeBatchImpl) was unchanged by Add debug virtual row assertions to MergingSortedAlgorithm #106565 and is unchanged by this PR.clickhouse-gh[bot] correctly observed that the underlying heap-position consequence of the meaningless default-zero boundary is theoretically a real merge-ordering concern: in
DESC LIMIT Nqueries over aUnionof read-in-order subtrees, a virtual-row cursor whose sort key fell back to default zero sinks to the bottom of the DESC heap, so other sources are popped first and their rows can be emitted before the virtual-row source's real chunk is requested. This is a genuine correctness question that pre-dates #106565 (the assertion just made it visible). However, per the project's "no speculative defensive code without a deterministic reproducer" directive, I cannot construct a wrong-release-result reproducer here - the existing read-in-order tests with two shards return the expected rows, and the failure mode requires specific shard data distributions that I have not isolated. The deeper merge-ordering fix (apply per-chunk virtual-row conversion in the outer merge, OR strip virtual rows before they reach an outer merge that cannot represent their boundary) is left as a follow-up that should be designed by @ vdimir, who introduced the assertion in #106565 and owns the read-in-order virtual-row machinery (#100412 / #100603).Reproducer (debug build)
tests/queries/0_stateless/04323_virtual_row_two_shards_pk_alias.sqlexercises the debug assertion path and passes with the fix; on a debug build at master HEAD the same test aborts withLogical error: Virtual row boundary violated....CIDB confirms the assertion fired on master commit
daa4ce9ae47b4cfe3ed6c8d1a0020a71be375326and on 22+ unrelated PRs in the past 36 hours.CI report (master sighting): https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=daa4ce9ae47b4cfe3ed6c8d1a0020a71be375326&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29
Caused by: #106565
Related: #106429
Related: #106654
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
(No changelog entry - debug-only assertion fix.)
Documentation entry for user-facing changes
Version info
26.6.1.509