Repository navigation
Fix stale virtual row conversion kept when ORDER BY contains constants - #119195
Conversation
…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]>
|
Workflow [PR], commit [d13ab8c] Summary: ✅
AI ReviewSummaryThis PR fixes Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit d13ab8c.
Changed lines: Changed C/C++ lines covered: 3/3 (100.00%) · 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 units7 translation units recompiled, 27 s compile time in total, 7 of them have a recent master baseline. |
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]>
Related: #117330
Related: #106740
Changelog category (leave one):
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 columnin debug builds) when reading in order with virtual rows andORDER BYcontains constants, e.g.SELECT DISTINCT a, b FROM t ORDER BY 'x', a, 'd' COLLATE 'cs', bon a table with key(a, b).Details
ReadFromMergeTree::requestReadingInOrderdrops the virtual row conversion when a later in-order request (preliminaryDISTINCT, aggregation in order) widens the read prefix established byORDER 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 constantORDER BYcolumns 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', bon key(a, b), the match stops at the collated constant: the conversion has one key input and two outputs,DISTINCTwidens the read to(a, b), and the preliminary merge over(a, b)receives a virtual row covering onlya. Debug builds throwVirtual row does not cover sort column 'b'; release builds default-fillbin 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]