Repository navigation
Conversation
ReadFromMergeTree::readByLayers() hardcoded ReadType::InOrder when creating per-layer readers for the join shard-by-PK-ranges optimization. For tables with reverse primary keys (ORDER BY col DESC), InReverseOrder is needed to produce ascending query output. Each layer would read in physical DESC order instead of the requested ASC order, producing wrong results like [4,3,2,1,0,9,8,7,6,5] instead of [0,1,2,3,4,5,6,7,8,9]. The bug triggers when all three conditions are met: - read_in_order_use_virtual_row = 1 (enables read-in-order for INNER joins) - enable_join_runtime_filters = 0 (removes BuildRuntimeFilter step, allowing the shard optimization to traverse both join sides) - Table uses allow_experimental_reverse_key = 1 with DESC ordering Fix: use input_order_info->direction to select the correct ReadType. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Pre-PR Validation Gate (Session d2a0ab6c)a) Deterministic repro? ✅ YES CREATE TABLE t0 (c0 Int) ENGINE = MergeTree() ORDER BY (c0 DESC)
SETTINGS index_granularity = 1, allow_experimental_reverse_key = 1;
INSERT INTO TABLE t0 (c0) SELECT number FROM numbers(10);
SELECT c0 FROM t0 JOIN t0 tx USING (c0) ORDER BY c0
SETTINGS query_plan_join_shard_by_pk_ranges = 1, max_threads = 2,
query_plan_read_in_order_through_join = 1,
read_in_order_use_virtual_row = 1, enable_join_runtime_filters = 0;Returns b) Root cause explained? ✅ YES c) Fix matches root cause? ✅ YES d) Test intent preserved? ✅ YES e) Both directions demonstrated? ✅ YES
|
|
So this seems related to |
|
@amosbird Yes, exactly — the bug is specifically in the Thanks for pointing to #94017 — that's a great stress-testing approach. Once that lands, it would exercise exactly this code path (reverse keys + read-in-order through joins) across the entire test suite, which should catch any similar bugs. My fix here ensures |
|
Workflow [PR], commit [f16377f] Summary: ❌
AI ReviewSummaryThis PR adds a regression test ( Findings❌ Blockers
ClickHouse Rules
Final Verdict
|
385a177 to
a629546
Compare
@amosbird good point. In general, it should work since it simply takes the first/last row from the index. So if the order is reversed, it should also pick values accordingly. That said, it probably needs more testing, and there may be other related issues. |
vdimir
left a comment
There was a problem hiding this comment.
Test was missing, but I've added it to current branch
a629546 to
996c25a
Compare
New test 04000_reverse_key_join_shard_read_by_layers pins the settings that deterministically trigger the readByLayers bug: read_in_order_use_virtual_row=1 and enable_join_runtime_filters=0. Without the fix, these settings cause the join shard optimization to read reverse-key table layers in physical DESC order instead of the requested ASC order, producing wrong results. Co-Authored-By: Claude Opus 4.6 <[email protected]>
04ea70f to
f13ac9b
Compare
|
@groeneai, check the CI, the test has failed. |
The test 04000_reverse_key_join_shard_read_by_layers uses `-- { echo ON }`
which echoes all comments and Tags to stdout. The reference file was missing
the Tags line and bugfix comment block, causing a Fast test diff failure.
Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
@alexey-milovidov Thanks for the heads up — the failure was on the previous commit (f13ac9b). The |
LLVM Coverage Report
Changed lines: 92.86% (13/14) · Uncovered code |
…d-by-layers # Conflicts: # src/Processors/QueryPlan/ReadFromMergeTree.cpp
|
The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix. |
| DROP TABLE IF EXISTS t_reverse_key; | ||
|
|
||
| CREATE TABLE t_reverse_key (c0 Int) ENGINE = MergeTree() ORDER BY (c0 DESC) SETTINGS index_granularity = 1, allow_experimental_reverse_key = 1; | ||
|
|
There was a problem hiding this comment.
The PR description says this change fixes ReadFromMergeTree::readByLayers by switching from hardcoded ReadType::InOrder to direction-aware read type, but this PR diff contains only tests (04000_...sql/.reference) and no corresponding change under src/.
As it stands, this looks like a regression test without the implementation fix, so the wrong-results bug can still reproduce in production paths while the PR title/changelog claim it is fixed. Please include the actual code change (or clarify if this PR is intentionally test-only and update title/changelog accordingly).
|
The test |
|
The MSan stress test failure (MemorySanitizer: use-of-uninitialized-value, STID 4179-5154 or 4148-3044) is a known pre-existing issue unrelated to this PR. Fix: #102158 |
|
Closing as redundant — PR #101654 by @vitlibar merged the identical fix on April 5. What happened:
CIDB confirmation: Zero master failures for Thank you @vdimir for the review and approval! |
ReadFromMergeTree::readByLayers()hardcodedReadType::InOrderwhen creating per-layerreaders for the
query_plan_join_shard_by_pk_rangesoptimization. For tables with reverseprimary keys (
ORDER BY col DESCwithallow_experimental_reverse_key = 1),InReverseOrderis required to produce ascending query output. Instead, each layer read in physical DESC order,
producing wrong results like
4, 3, 2, 1, 0, 9, 8, 7, 6, 5instead of0, 1, 2, 3, 4, 5, 6, 7, 8, 9.The bug requires all three conditions to be met simultaneously:
read_in_order_use_virtual_row = 1— enables virtual rows, which allows read-in-order-through-join for INNER joins (without it, read-in-order is skipped for INNER joins and sharding never activates)enable_join_runtime_filters = 0— removes theBuildRuntimeFilterstep from the plan, which otherwise blocksoptimizeJoinByShardsfrom traversing the right side of the joinallow_experimental_reverse_key = 1with a DESC primary keyWhen all three conditions are met,
optimizeJoinByShardsruns afteroptimizeReadInOrderhasalready set up
InReverseOrderreading. The shard optimization splits data into layers andcalls
readByLayers(), which creates per-layer readers — but hardcodesReadType::InOrderinstead of respecting
input_order_info->direction.Fix: use
input_order_info->directionto select the correctReadType. Direction > 0 meansInOrder, direction < 0 meansInReverseOrder.This was a flaky test failure in
03668_shard_join_in_reverse_order— 12 failures in 70K runs(0.017%) because CI randomizes both
read_in_order_use_virtual_rowandenable_join_runtime_filters, and the failure requires both to be in non-default states.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix wrong query results for tables with reverse primary keys (
allow_experimental_reverse_key) whenquery_plan_join_shard_by_pk_ranges,read_in_order_use_virtual_row, and disabled runtime join filters are all active simultaneously. The join shard optimization would read data in physical (descending) order instead of the requested ascending order.Documentation entry for user-facing changes