Repository navigation
Keep the read-in-order virtual row for a key column fixed by WHERE and listed in ORDER BY - #130893
Conversation
Since #94103 a sorting key column fixed by the filter (`WHERE a = 1`) and listed in `ORDER BY` does not determine the read direction. That change also disabled the virtual row for such a column, and without the virtual row reading in order through an `INNER JOIN` is refused. So `WHERE a = 1 ORDER BY a, b` over the key `(a, b)` was no longer read in order through an `INNER JOIN`. If the read goes in the direction of the fixed column anyway, `ORDER BY` follows the sorting key as it is, and the virtual row takes the column from the index like any other key column, as before that change. Also do not skip the key column when only a function of it is fixed: `WHERE toDate(ts) = '2024-01-01' ORDER BY toDate(ts), b` over the key `(ts, b)` returned rows in a wrong order. Related: #94103 Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
Workflow [PR], commit [f148849] Summary: ✅
AI ReviewSummaryReviewed #130893 at Final VerdictStatus: ✅ Approve. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units7 translation units recompiled, 21 s compile time in total, 7 of them have a recent master baseline. |
|
@groeneai, please find an expert to review this PR and kindly ask them to take a look. |
|
@NIKTONIKTO717, could you please take a look at this PR? It changes the read-in-order fixed-column logic you fixed in #126019: an |
ch-samuel-reviewer-1
left a comment
There was a problem hiding this comment.
Thank you @vdimir for a nice fix! I left two comments about the test, otherwise looks good.
This review was checked, modified and approved by human (@NIKTONIKTO717).
| direction = match.monotonicity->strict ? direction * match.monotonicity->direction : 0; | ||
| const bool requests_nulls_last = sort_column_description.direction == sort_column_description.nulls_direction; | ||
| if (column_is_nullable && (direction * reverse_indicator == 1) != requests_nulls_last) | ||
| direction = 0; |
There was a problem hiding this comment.
I think there is no test which would cover Nullable or Float key, so nothing covers these three lines. Without them, 05620_read_in_order_virtual_row_fixed_key still passes. This query would return 7, 6, 5 instead of 107, 106, 105:
CREATE TABLE t_fixed_key_nullable (a Nullable(UInt32), b UInt32) ENGINE = MergeTree ORDER BY (a, b) SETTINGS index_granularity = 4, allow_nullable_key = 1;
SYSTEM STOP MERGES t_fixed_key_nullable;
INSERT INTO t_fixed_key_nullable SELECT 1, number FROM numbers(8);
INSERT INTO t_fixed_key_nullable SELECT if(number < 8, 1, NULL), 100 + number FROM numbers(12);
SELECT b FROM t_fixed_key_nullable WHERE a = 1 ORDER BY a DESC, b DESC LIMIT 3;Could you please consider adding this case, and maybe the Float64 one, to the test?
There was a problem hiding this comment.
Would it be possible to move all tests to some other already existing test file, for example 03789_read_in_order_constant_column_direction?
|
Thanks for the review! Since all the comments relate only to the test, I'll merge this PR now so we don't have to wait for another CI round, and then I'll merge the suggestion PR. I'm not sure merging two tests into one makes much sense. Adding a new test for each change usually makes it easier to investigate things like test flakiness. But it may also be fine, since it seems the test also covers this part of the code. |
Backport #130893 to 26.10: Keep the read-in-order virtual row for a key column fixed by `WHERE` and listed in `ORDER BY`
Backport #130893 to 26.9: Keep the read-in-order virtual row for a key column fixed by `WHERE` and listed in `ORDER BY`
Cherry pick #130893 to 26.3: Keep the read-in-order virtual row for a key column fixed by `WHERE` and listed in `ORDER BY`
Resolve the conflict of the cherry-pick of #130893 to 26.8. The only conflict is modify/delete on `05049_projection_shorter_sort_prefix_than_pk.reference`: the test 05049 was added by #116854, which is not in 26.8, so the file stays deleted. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Resolve the conflict of the cherry-pick of #130893 to 26.3. The only conflict is modify/delete on `05049_projection_shorter_sort_prefix_than_pk.reference`: the test 05049 was added by #116854, which is not in 26.3, so the file stays deleted. Co-Authored-By: Claude Fable 5.2 <[email protected]>
The test of #130893 does not run on 26.3 as it is: - The settings `read_in_order_use_virtual_row_per_block`, `max_bytes_ratio_before_external_join`, `max_bytes_before_external_join`, `join_runtime_filter_min_probe_rows` and `query_plan_optimize_join_order_randomize` do not exist in 26.3. The two cases with a virtual row after each block are removed. - `EXPLAIN` prints `ReadType:` instead of `Read type:`. - The virtual row is disabled for `FINAL` in 26.3 (#124468 is not there), so `FINAL` is not read in order through `INNER JOIN`, and the case is removed. Co-Authored-By: Claude Fable 5.2 <[email protected]>
It is the change of `buildInputOrderFromSortDescription` from #110725, which is not in 26.3. A key column fixed by `WHERE` and not listed in `ORDER BY` is skipped without a `MatchInfo`, and the virtual row builder takes the following columns from wrong key columns. In 26.3 the virtual row was disabled only if the skipped column was the first one, so `WHERE b = 1 ORDER BY a, c` over the key `(a, b, c)` returned rows in a wrong order with `read_in_order_use_virtual_row`. It is needed for #130893: with the virtual row kept for a fixed column listed in `ORDER BY`, `WHERE a = 1 AND b = 2 ORDER BY a, c` would get into the same case. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…y column fixed by `WHERE` and listed in `ORDER BY`
Cherry pick #130893 to 26.8: Keep the read-in-order virtual row for a key column fixed by `WHERE` and listed in `ORDER BY`
…y column fixed by `WHERE` and listed in `ORDER BY`
Backport #130893 to 26.8: Keep the read-in-order virtual row for a key column fixed by `WHERE` and listed in `ORDER BY`
Since #94103 a sorting key column fixed by the filter and listed in
ORDER BY(WHERE a = 1 ORDER BY a, b) disabled the read-in-order virtual row, so such a query was no longer read in order through anINNER JOIN. Keep the virtual row when the read goes in the direction of that column anyway. Also do not treat the key column as fixed when only a function of it is:WHERE toDate(ts) = ... ORDER BY toDate(ts), bover the key(ts, b)returned rows in a wrong order.Related: #94103
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a wrong order of rows for a query like
SELECT ... WHERE toDate(ts) = '2024-01-01' ORDER BY toDate(ts), b LIMIT 5over a table with the sorting key(ts, b)whenoptimize_read_in_orderis enabled. A query that filters a leading sorting key column by equality and lists it inORDER BY(WHERE a = 1 ORDER BY a, b) is read in order through anINNER JOINagain.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/130893]
Version info
26.11.1.2463-master(included in26.11and later)26.10.5.43,26.9.31.8,26.8.39.18