Repository navigation
Enable reading in reverse order with FINAL for ReplacingMergeTree - #115345
Conversation
When a query with FINAL sorts in reverse order of the sorting key (e.g. ORDER BY key DESC LIMIT n), the read-in-order optimization now applies instead of falling back to a full read. ReplacingSortedAlgorithm learns a read_in_reverse mode: a row with a strictly higher version always replaces the selected one; among rows with equal (or absent) versions, the previously selected row is kept unless the current row comes from a newer data part. This mirrors the "last written row wins" rule of the direct reading order, because in the reverse reading order rows within one part arrive backwards while parts still arrive from the oldest to the newest one. The other engines keep the old behavior, since their merging algorithms rely on the direct order of rows: the sequence of sign rows in CollapsingMergeTree, the order of rows fed to order-dependent aggregate functions in AggregatingMergeTree, etc. On a 110M row ReplacingMergeTree table, `SELECT x FROM t FINAL ORDER BY x DESC LIMIT 1` reads 1.71 million rows instead of 110.1 million, in 0.22s instead of 4.5s. Closes: #58035 Co-Authored-By: Claude Fable 5 <[email protected]>
The page stated that the read-in-order optimization does not work with the FINAL modifier at all, which was already inaccurate for the direct order, and is now inaccurate for the reverse order of a ReplacingMergeTree table. The new setting is named without a link, because the settings pages are regenerated by the nightly documentation workflow, so its anchor does not exist yet and the blocking link check would fail on it. Co-Authored-By: Claude Fable 5 <[email protected]>
# Conflicts: # src/Core/Settings.cpp
|
Workflow [PR], commit [5f22de8] Summary: ✅
AI ReviewSummaryThis PR enables reverse-order read-in-order for Final Verdict✅ Approved LLVM Coverage ReportMeasured on commit 5f22de8.
Changed lines: Changed C/C++ lines covered: 143/145 (98.62%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units124 translation units recompiled, 1368 s compile time in total, 124 of them have a recent master baseline. |
|
Fyi, AI review had the same findings on my PR #111609. It first wanted to extend it to the Eventually, I abandoned trying to please the AI and went back to just the straightforward FINAL on ReplacingMergeTree implementation that should cover 90% of workloads that benefit in #114479. |
|
🕵 Both stress-test failures on
@groeneai, please investigate the |
`ReadFromMerge::requestReadingInOrder` rejects a reverse order request with `FINAL`, so a `Merge` table over a `ReplacingMergeTree` does not use this optimization. That is conservative, not incorrect: the plan keeps its sorting step. Assert the result of such a query.
|
The fix for this one is already merged: #113422 (merged 2026-08-19 22:26:54Z, Root cause: the decision on Your head On the tail: CIDB has 6 rows for this signature timestamped after the merge, but every one belongs to a workflow run created before it (17:39Z through 22:22Z; the two most recent are re-run attempts of runs created at 18:04Z and 18:46Z). The last occurrence anywhere is 2026-08-20 02:19:44. Since then there are 8310 |
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
|
🕵 Merged The only red on The one remaining unresolved thread (the |
|
🕵 Merged Both reds on the previous head
The latest AI Review verdict on |
… `SettingsChangesHistory` The branch predates the 26.9 rollover on master, so the style check requires the new setting to move from the 26.8 block to 26.9. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115345&sha=63b0fabe4574b0bb142a50600be511edcc36c3ef&name_0=PR&name_1=Style%20check #115345 Co-Authored-By: Claude Fable 5 <[email protected]>
|
🕵 Merged
|
|
My original PR #114479 was already approved and this is the same. Can we merge it? |
…e `FINAL` reads The autogenerated page `docs/reference/statements/select/order-by.mdx` is generated from the doc text in `ParserSelectQuery.cpp`, which still said the read-in-order optimization does not work together with the `FINAL` modifier. Update it to state that with `FINAL` the optimization works in the direct order of the sorting key, and for `ReplacingMergeTree` tables also in the reverse order, controlled by `optimize_read_in_reverse_order_final`. Co-Authored-By: Claude Fable 5 <[email protected]>
|
🕵 Merged
|
|
🕵 Merged Nothing to fix from the previous run on
@cwurm the change is unchanged in substance from your #114479 — it just keeps being brought up to date with a moving |
|
🕵 Merged All three reds on the previous head
The AI Review verdict on |
|
@cwurm, yes as it is 100% green and passed review. |
…roughJoin `ReadFromMergeTree` gets `canReadInReverseOrder`, which holds the check that `requestReadingInOrder` did inline: without `FINAL` a reverse read is always accepted, with `FINAL` only for `ReplacingMergeTree` when `optimize_read_in_reverse_order_final` is enabled. `ReadFromMerge::requestReadingInOrder` rejected every reverse order request with `FINAL`. It now asks the reading steps of all children with `canReadInReverseOrder` before switching any of them, and passes the request on when all of them accept it. `topKThroughJoin` deferred to the read-in-order optimization through the join only when all sort columns were ascending with `FINAL`. It now gets the input order the second pass would request from `getInputOrderIfReadInOrderIsUseful` (renamed from `wouldReadInOrderBeUseful`) and defers exactly when the second pass accepts that direction, which also handles descending sorting keys. Closes: ClickHouse#117961 Related: ClickHouse#115345 Related: ClickHouse#111609 Co-Authored-By: Claude Fable 5.1 <[email protected]>
Continue #114479 from @cwurm. This replacement makes its reverse
FINALread optimization merge-clean with currentmaster; the original fork branch could not be updated because the Git HTTPS push transport stalled and the fork API does not grant Git-object write access.Related: #114479
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Enable reading
ReplacingMergeTreedata in reverse order withFINAL.Workflow [PR]
Sync PR [sync-upstream/pr/115345]