Repository navigation
Test the read-in-order prefix guard from SQL - #119682
Conversation
`ReadFromMergeTree::requestReadingInOrder` refuses a prefix wider than the read's own sorting key since #116364. That guard went in with a preservation arm only, because no SQL input was known to reach it: `spreadMarkRangesAmongStreamsWithOrder` does ```cpp size_t prefix_size = input_order_info->used_prefix_of_sorting_key_size; auto order_key_prefix_ast = storage_snapshot->metadata->getSortingKey().expression_list_ast->clone(); order_key_prefix_ast->children.resize(prefix_size); ``` and `resize` grows the list with null `ASTPtr`s when the prefix exceeds the key, which `TreeRewriter::normalize` then walks until one of the `Customize*` visitors dereferences a node. There are two such inputs. The one the server-side fuzzer reached is projections: `ReadFromMerge` optimizes a child plan when it creates it, so `optimizeUseNormalProjections` can already have replaced the read of the table with a read of a projection by the time `ReadFromMerge::requestReadingInOrder` walks the plan, and nothing relates the two sorting keys. In the `02784_projections_read_in_order_bug` fixture the table is ordered by `(organisation_id, session_id, timestamp)`, so the prefix is 3, while the child plan reads the `events_by_session` projection, ordered by `(session_id, timestamp)`: ```sql SELECT id, timestamp, payload FROM merge(currentDatabase(), '^events$') WHERE organisation_id = reinterpretAsUUID(1) AND session_id = reinterpretAsUUID(0) ORDER BY timestamp ASC, payload ASC, id ASC NULLS LAST -- read_in_order_two_level_merge_threshold = 1 ``` That input depends on which projection the optimizer picks, so the test uses the other one, which is deterministic. An `Alias` child resolves its target by name twice - once when `createChildrenPlans` builds its child plan, once when `buildInputOrderFromSortDescription` reads `getInMemoryMetadataPtr` to compute the prefix - so an `EXCHANGE TABLES` between the two leaves a read with a one-column sorting key holding a two-column prefix. The already-present `storage_merge_create_children_plans_pause` failpoint pins the moment. Reverting the guard makes the test fail: the server dies mid-query, frame for frame the stack the stress tests reported. ``` Address: 0x10. Access: <not available>. Address not mapped to object. boost::container::vector<boost::intrusive_ptr<DB::IAST>>::end() DB::InDepthNodeVisitor<...CustomizeFunctionsData<countdistinct>...>::visitChildren<false> DB::TreeRewriter::normalize DB::ReadFromMergeTree::spreadMarkRangesAmongStreamsWithOrder DB::ReadFromMerge::buildPipeline ``` `read_in_order_two_level_merge_threshold = 0` and `read_in_order_use_virtual_row_per_block = 0` pin the preliminary merge that cuts the sorting key down to the announced prefix; without them `need_preliminary_merge` is false and the vulnerable block does not run. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [a21d5f2] Summary: ✅ AI ReviewSummaryThis PR adds a deterministic stateless regression test for the widened read-in-order prefix guard on Final Verdict✅ Approve. No new findings. |
The `planned:` lines only proved the child-planning choreography: a plain `Sorting` fallback returns the same five rows and the same two lines, so the test would keep passing if the planner stopped calling `requestReadingInOrder` for this shape. Measured on the unraced shape, that is not hypothetical - it spreads both children with `Spreading mark ranges among streams (default reading)` and never reaches the read-in-order path. Each `ReadFromMergeTree` logs which way it spread its mark ranges, so the raced query leaves a direct witness in `system.text_log`: `z_plain`, whose sorting key is as wide as the announced prefix, logs `Spreading ranges among streams with order`, while the alias child - one column short of the prefix - is the one the guard sends back to the default path. Assert that pair of counts; if the shape ever stops reading in order both counts move and the test fails instead of passing vacuously. Also pin `enable_parallel_replicas = 0` and `automatic_parallel_replicas_mode = 0` on the query under test: the harness randomizes them on, and re-planning the children through `ClusterProxy` changes both the `planned:` lines and the new spreading counts. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119682&sha=42757a345dbb20e868e799595de982d5e5b355d3&name_0=PR&name_1=Stress%20test%20%28arm_msan%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. It is unrelated to this PR, which only adds a stateless test - the failure is a logical error in CIDB shows it is a rare pre-existing stress flake rather than something this branch introduced - |
|
No fix is in progress, so there is nothing to link yet. #115979 is the only other open PR in the patch-parts area and it does not touch Root cause
/// MergeTreeBlockReadUtils.cpp:430
if (has_lightweight_delete && patch_part_columns.has(RowExistsColumn::name))
patch_columns_to_read_set.insert(RowExistsColumn::name);
/// MergeTreeReadPoolBase.cpp:235
bool has_lightweight_delete = data_part->hasLightweightDelete() || read_task_info.alter_conversions->hasLightweightDelete();So when one Join-mode patch part is shared by two main parts and only one of them carries a lightweight delete, the two read tasks build different headers for the same cache entry, and There is a second, independent source of divergence: I read the assertion as correct and the divergent header as the defect, so I plan to make the patch read header a function of the patch part alone rather than relax the check. The same positional comparison at the sibling site was already normalized in On the frequencyThe family is larger than a single STID suggests: the assertion message embeds the column names, so each column pair hashes to its own STID. Querying on the message text instead gives 4 hits in 180 days across 4 unrelated PRs: SELECT check_start_time, pull_request_number, check_name FROM checks
WHERE check_start_time > now() - INTERVAL 180 DAY
AND test_context_raw ILIKE '%patch join cache stream%'
All four are debug or sanitizer builds, which matches the check sitting under I would rather have the fix PR be the tracking artifact than open an issue next to it, but say the word and I will file one. |
|
Opened #121044. Two corrections to my comment above, both measured:
So the fix makes the structure part of the entry key rather than relaxing the check, and |
Related: #116364
#116364 made
ReadFromMergeTree::requestReadingInOrderrefuse a prefix wider than the read's ownsorting key, but shipped with a preservation arm only: no SQL input was known to reach the guard, so
the test there passes on master by design and would not fail if the guard were reverted. This adds a
test that does fail.
What reaches the guard
spreadMarkRangesAmongStreamsWithOrdercuts the sorting key down to the announced prefix withorder_key_prefix_ast->children.resize(prefix_size), which grows the expression list with nullASTPtrs when the prefix is longer than the key.TreeRewriter::normalizethen walks them until oneof the
Customize*visitors dereferences a node, and the server segfaults.The channel the server-side fuzzer reached is projections.
ReadFromMergeoptimizes a child planwhen it creates it, so
optimizeUseNormalProjectionscan already have replaced the read of the tablewith a read of a projection by the time
ReadFromMerge::requestReadingInOrderwalks the plan, andnothing relates the two sorting keys. In the
02784_projections_read_in_order_bugfixture the tableis ordered by
(organisation_id, session_id, timestamp), so the prefix is 3, while the child planreads the
events_by_sessionprojection, ordered by(session_id, timestamp):That is the query from the master stress report below, and
EXPLAIN indexes = 1over that fixtureconfirms the child read is the projection (
PrimaryKey Keys: session_id), not the table.What the test uses instead
Which projection the optimizer picks is not something a test should depend on, so the test takes the
second channel, which is deterministic. An
Aliaschild resolves its target by name twice - oncewhen
createChildrenPlansbuilds its child plan, once whenbuildInputOrderFromSortDescriptionreads
getInMemoryMetadataPtrto compute the prefix - so anEXCHANGE TABLESbetween the twoleaves a read with a one-column sorting key holding a two-column prefix. The already-present
storage_merge_create_children_plans_pausefailpoint pins the moment, and the test asserts the orderthe children were planned in so the choreography cannot silently stop covering the bug.
With the guard reverted, the server dies mid-query, frame for frame the reported stack:
read_in_order_two_level_merge_threshold = 0andread_in_order_use_virtual_row_per_block = 0pinthe preliminary merge that consumes the prefix; without them
need_preliminary_mergeis false andthe vulnerable block does not run.
The test is
no-parallelbecause it waits on a process-globalPAUSEABLEfailpoint, so a concurrentinstance pausing or resuming the same channel would break the synchronisation.
The crash on public CI
Between 2026-09-05 and 2026-09-12 it was reported 12 times, on master and on 10 pull requests
(
STID: 2076-288c,2076-3a72,2447-4aac):Master report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=3476b12ea2ce2ab61713714166e91b4712105c74&name_0=MasterCI&name_1=Stress%20test%20%28arm_msan%29
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added a regression test for a read-in-order prefix wider than the sorting key of the read that has to honor it, reached through an
Aliaschild of aMergetable.Workflow [PR]
Sync PR [sync-upstream/pr/119682]
Version info
26.10.1.190(included in26.10and later)