Repository navigation
Conversation
52f8aad to
eaa13d6
Compare
|
This is an automated comment for commit 2d24cb4 with description of existing statuses. It's updated for the latest CI running ❌ Click here to open a full report in a separate page
Successful checks
|
|
As in #58035 (comment), I can foresee two cases that read-in-reverse-order may lead to wrong FINAL behaviour:
I cannot figure out if there is any issue with |
Simple way to bypass these issues is to only use read-in-reverse-order if there is no level-0 part in source parts. |
Indeed, you're right. I need to think about what to do about this case and others. For now, I've added a test for it that will fail with the current implementation.
Yeah, that could be the easy way to go. |
They aren't stable if you have the same number for value. |
ed60fd4 to
8191fb6
Compare
| ssize_t version_column_number = -1; | ||
| bool cleanup = false; | ||
|
|
||
| int read_direction = 1; |
There was a problem hiding this comment.
when we have only two alternatives - it is handy to use some type with two possible values
|
|
||
| CREATE TABLE t_02947_reverse_order_final (x int, y int, z int) | ||
| ENGINE = ReplacingMergeTree ORDER BY (x, y); | ||
|
|
There was a problem hiding this comment.
| system stop merges t_02947_reverse_order_final; |
| ENGINE = ReplacingMergeTree(ver) ORDER BY x; | ||
|
|
||
| -- Level-0 part with duplicate rows | ||
| INSERT INTO t_02947_reverse_order_final SETTINGS optimize_on_insert = 0 VALUES (0, 0, 0), (0, 1, 0), (1, 0, 0), (1, 1, 1); |
There was a problem hiding this comment.
let's add a similar test, but each row will be inserted separately (to form 4 distinct parts).
I am a little concerned about these lines: https://github.com/ClickHouse/ClickHouse/pull/58361/files#diff-c2317ca02dca707ab63917057efaf36ca941e500948fc64857e1b7b0f29bff51R127-R128
but probably everything is ok, since we continue to process parts themselves from left to right
|
pls merge with master |
|
@cwurm, please merge with master. |
8191fb6 to
2d24cb4
Compare
|
I've rebased it. But the problem now is that it doesn't seem to work anymore with the Level-0 parts; it doesn't de-duplicate. I need to look into it but it's hard to find the time. |
|
@cwurm, 57 tests from the fast test suite have failed. |
Second attempt at ClickHouse#58361, scoped to ReplacingMergeTree only. Addresses ClickHouse#58035. 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: among rows with equal keys and equal (or absent) versions, the previously selected row is kept unless the current row comes from another (newer) data part, which mirrors the "last row wins" rule of the direct reading order. The other engines keep the old behavior since their merging algorithms rely on the direct order of rows. The new setting optimize_read_in_reverse_order_final (default true) allows disabling the optimization. Not built or tested yet (WIP). Co-Authored-By: Claude Fable 5 <[email protected]>
Second attempt at ClickHouse#58361, scoped to ReplacingMergeTree only. Addresses ClickHouse#58035. 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 (no reverse reading with FINAL), 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. The new setting optimize_read_in_reverse_order_final (default true) allows disabling the optimization. Co-Authored-By: Claude Fable 5 <[email protected]>
Second attempt at ClickHouse#58361, scoped to ReplacingMergeTree only. Addresses ClickHouse#58035. 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 (no reverse reading with FINAL), 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. The new setting optimize_read_in_reverse_order_final (default true) allows disabling the optimization. Co-Authored-By: Claude Fable 5 <[email protected]>
Enable reading in reverse order when sorting against the sorting key in FINAL queries. Addresses slowness explained in #58035. Closes #58035.
The reason for previously disabling it seems to have been some concerns about incorrect behavior of merging algorithms (see deleted comment). But no specific incorrect behavior is mentioned, and all the tests I wrote seem to work fine. Maybe CI will find something (I don't have a full setup myself that can run all tests).
As expected, performance is much improved for the queries mentioned in #58035:
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Enable reading in reverse order optimization for FINAL queries.