Skip to content

Keep the read-in-order virtual row for a key column fixed by WHERE and listed in ORDER BY - #130893

Merged
vdimir merged 1 commit into
masterfrom
vdimir/read-in-order-fixed-key-virtual-row
Nov 16, 2026
Merged

vdimir merged 1 commit into
masterfrom
vdimir/read-in-order-fixed-key-virtual-row

Conversation

@vdimir

@vdimir vdimir commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

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 an INNER 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), b over the key (ts, b) returned rows in a wrong order.

Related: #94103

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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 5 over a table with the sorting key (ts, b) when optimize_read_in_order is enabled. A query that filters a leading sorting key column by equality and lists it in ORDER BY (WHERE a = 1 ORDER BY a, b) is read in order through an INNER JOIN again.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/130893]

Version info

  • Merged into: 26.11.1.2463-master (included in 26.11 and later)
  • Backported to: 26.10.5.43, 26.9.31.8, 26.8.39.18

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]>
@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f148849]

Summary: ✅


AI Review

Summary

Reviewed #130893 at f1488499939cd38b6e41d7d430d46d8e86762ce0. The change restores virtual-row bounds for fixed sorting-key columns when their direction permits it, and prevents incorrect ordering when only a non-injective function of a key is fixed. No actionable findings. The new regression test matches its reference using this PR's CI binary; 2,880 additional comparisons against optimize_read_in_order = 0 also pass.

Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 11, 2026
@vdimir vdimir added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Nov 11, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Nov 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f14884999 with master f90840438 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.68, programs/clickhouse-keeper ×0.64 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 5.6 s 14.8 s +11.0 s (+291%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.3 s 7.7 s +5.4 s (+238%)
Compile time of recompiled translation units

7 translation units recompiled, 21 s compile time in total, 7 of them have a recent master baseline.

Job report

@vdimir

vdimir commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

@groeneai, please find an expert to review this PR and kindly ask them to take a look.

@groeneai

Copy link
Copy Markdown
Collaborator

@NIKTONIKTO717, could you please take a look at this PR? It changes the read-in-order fixed-column logic you fixed in #126019: an ORDER BY column is now skipped as fixed only when its sorting key column is fixed too (WHERE toDate(ts) = ... ORDER BY toDate(ts), b over the key (ts, b) returned rows in a wrong order), and the virtual row is kept for a fixed ORDER BY column when the read goes in that column's direction.

@vdimir
vdimir requested a review from NIKTONIKTO717 November 13, 2026 16:55
@NIKTONIKTO717 NIKTONIKTO717 self-assigned this Nov 13, 2026

@ch-samuel-reviewer-1 ch-samuel-reviewer-1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to move all tests to some other already existing test file, for example 03789_read_in_order_constant_column_direction?

@vdimir

vdimir commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

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.

@vdimir
vdimir added this pull request to the merge queue Nov 16, 2026

@NIKTONIKTO717 NIKTONIKTO717 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Merged via the queue into master with commit 5720190 Nov 16, 2026
203 checks passed
@vdimir
vdimir deleted the vdimir/read-in-order-fixed-key-virtual-row branch November 16, 2026 18:27
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 16, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 16, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 16, 2026
Backport #130893 to 26.10: Keep the read-in-order virtual row for a key column fixed by `WHERE` and listed in `ORDER BY`
clickhouse-gh Bot added a commit that referenced this pull request Nov 16, 2026
Backport #130893 to 26.9: Keep the read-in-order virtual row for a key column fixed by `WHERE` and listed in `ORDER BY`
robot-ch-test-poll added a commit that referenced this pull request Nov 17, 2026
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`
vdimir added a commit that referenced this pull request Nov 17, 2026
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]>
vdimir added a commit that referenced this pull request Nov 17, 2026
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]>
vdimir added a commit that referenced this pull request Nov 17, 2026
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]>
vdimir added a commit that referenced this pull request Nov 17, 2026
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]>
robot-clickhouse added a commit that referenced this pull request Nov 17, 2026
…y column fixed by `WHERE` and listed in `ORDER BY`
robot-ch-test-poll added a commit that referenced this pull request Nov 17, 2026
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`
robot-clickhouse added a commit that referenced this pull request Nov 17, 2026
…y column fixed by `WHERE` and listed in `ORDER BY`
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 17, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 17, 2026
Backport #130893 to 26.8: Keep the read-in-order virtual row for a key column fixed by `WHERE` and listed in `ORDER BY`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants