Skip to content

Fix stale virtual row conversion kept when ORDER BY contains constants - #119195

Merged
alexey-milovidov merged 2 commits into
masterfrom
vdimir/vrow-prefix-logical-error
Sep 12, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
vdimir/vrow-prefix-logical-error

Conversation

@vdimir

@vdimir vdimir commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

Related: #117330
Related: #106740

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 merge (logical error Virtual row does not cover sort column in debug builds) when reading in order with virtual rows and ORDER BY contains constants, e.g. SELECT DISTINCT a, b FROM t ORDER BY 'x', a, 'd' COLLATE 'cs', b on a table with key (a, b).

Details

ReadFromMergeTree::requestReadingInOrder drops the virtual row conversion when a later in-order request (preliminary DISTINCT, aggregation in order) widens the read prefix established by ORDER BY, because the conversion covers only the original prefix (#106740). The guard compared the conversion's output column count with the new prefix size, but constant ORDER BY columns are emitted as fixed outputs backed by no key column, so a leading constant inflates the count and the stale virtual row survives the widening.

With ORDER BY 'x', a, 'd' COLLATE 'cs', b on key (a, b), the match stops at the collated constant: the conversion has one key input and two outputs, DISTINCT widens the read to (a, b), and the preliminary merge over (a, b) receives a virtual row covering only a. Debug builds throw Virtual row does not cover sort column 'b'; release builds default-fill b in the announced boundary and may mis-order the merge.

The guard now compares the number of required (primary key) columns of the conversion instead.

Found by the AST fuzzer in #117330: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117330&sha=d7024f86ae088de297ac9ff510ea4663acd1faab&name_0=PR&name_1=AST+fuzzer+%28amd_debug%2C+targeted%2C+old_compatibility%29

🤖 Generated with Claude Code


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

…ersion

`ReadFromMergeTree::requestReadingInOrder` drops the virtual row conversion when a later
in-order request (preliminary DISTINCT, aggregation in order) widens the read prefix that
the ORDER BY established, because the conversion covers only the original prefix. The guard
compared the conversion's output column count with the new prefix size, but constant ORDER BY
columns are emitted as fixed outputs backed by no key column. With `ORDER BY 'x', a, 'd' COLLATE 'cs', b`
on key `(a, b)` the match stops at the collated constant, the conversion has one key input and
two outputs, and the widening to `(a, b)` kept the stale one-column virtual row. The preliminary
merge over `(a, b)` then received it: `Virtual row does not cover sort column 'b'` in debug
builds, a default-filled boundary in release builds.

Compare the number of required (primary key) columns instead.

CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117330&sha=d7024f86ae088de297ac9ff510ea4663acd1faab&name_0=PR&name_1=AST+fuzzer+%28amd_debug%2C+targeted%2C+old_compatibility%29
PR: #117330

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d13ab8c]

Summary: ✅


AI Review

Summary

This PR fixes ReadFromMergeTree::requestReadingInOrder so a widened in-order re-request drops a stale virtual-row conversion based on the number of required primary-key inputs instead of the number of emitted output columns. The regression test is focused and exercises the reported ORDER BY-with-constants case, checks that the unsafe virtual-row plan is removed, and verifies both sort directions against the unoptimized path. I did not find a remaining correctness gap in the current diff.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit d13ab8c.

Metric Baseline Current Δ
Lines 89.00% 89.00% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.30% 81.30% +0.00%

Changed lines: Changed C/C++ lines covered: 3/3 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 10, 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 Sep 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing d13ab8cda with master 1a9e5b0b0 (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.

Compile time of recompiled translation units

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

Job report

@alexey-milovidov alexey-milovidov self-assigned this Sep 12, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 12, 2026
Merged via the queue into master with commit 993bbfb Sep 12, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the vdimir/vrow-prefix-logical-error branch September 12, 2026 11:43
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 12, 2026
alexey-milovidov added a commit that referenced this pull request Sep 23, 2026
Conflict resolution:
- `MergingSortedAlgorithm.cpp`: master only fixed the message whitespace of
  `checkVirtualRowCoversSortDescription`; this branch removes that check, keep it removed.
- `ReadFromMergeTree::requestReadingInOrder`: keep this branch's handling of a re-request
  (keep the conversion on widening, reject a re-request that would revoke it), drop master's
  output/input-count based reset from #119195, and keep master's new early return for a prefix
  longer than the sorting key (#116364).
- `04549_virtual_row_distinct_in_order_boundary`: take master's smaller data (#117742), keep this
  branch's cases, and keep master's EXPLAIN pair but assert that the widened read keeps its virtual
  row conversion, as this branch intends.
- `05153_virtual_row_widened_prefix_constant_order_by` (new on master): the widened read now keeps
  the one-column conversion, and the results still match the unoptimized read.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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... 🍃 green ci 🌿 Fixing flaky tests in CI pr-bugfix Pull request with bugfix, not backported by default 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.

3 participants