Repository navigation
Conversation
With read-in-order on a prefix of the sorting key, the pipeline is PartialSortingTransform -> FinishSortingTransform -> LimitTransform. FinishSortingTransform emits a group of rows with an equal prefix only when it receives a row of the next group. PartialSortingTransform has its own top-K threshold filter, armed when 1500 <= limit <= block rows. After the first block it drops every row that is not below the threshold. Under read-in-order every row of a later prefix group is above it, so all of them were dropped, the transform output only empty chunks, FinishSortingTransform never saw a group end and the query read the whole table (issue ClickHouse#125296: LIMIT 1500..65409 read all 6M rows of TPC-H lineitem, LIMIT 1499 and 65410 stopped early). PartialSortingTransform now knows the sorted prefix size (passed from SortingStep::finishSorting) and keeps the last row of a chunk when its prefix is greater than the threshold's prefix. That row can never be in the result (at least `limit` rows not above the threshold were already passed downstream), but it marks the group end, so FinishSortingTransform emits the group and LimitTransform stops the read. Later rows are still dropped, so the filter keeps its memory benefit, and read termination stays in LimitTransform, so exact_rows_before_limit is unchanged. Same defect class as ClickHouse#100235 one layer up (reader-side __topKFilter). Closes ClickHouse#125296 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) plus my own cold review (2 nits, not blocking).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-11:20261012-122700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261012-105900 |
|
Workflow [PR], commit [db09a15] Summary: ❌
AI ReviewSummaryThe PR preserves a row from the next sorted-prefix group so Missing context / blind spots
Findings❌ Blockers
Tests
Final VerdictStatus: ❌ Block Correct the collated threshold comparisons and add focused coverage, or explicitly link a tracking issue for this pre-existing defect. |
The flaky check failed it with "Test runs too long (> 180s)" on amd_debug (216 s, p50 25 s; amd_binary max 155 s). Its CI query_log shows the two OPTIMIZE FINAL merges of 1M rows dominate (up to 106 s each under contention), followed by the 1M-row inserts and full sorts. Shrink both tables from 1,000,000 to 100,000 rows (large groups from 100,000 to 10,000 rows, still more than one 8192-row block) and the read bound from 500,000 to 50,000. The bound needed half of a 1M table only because a Compact part with a random index_granularity up to 65536 is read in whole granules; clamp index_granularity to at most 8192 with a Random settings limits line instead. Measured locally, debug build, randomized settings: - fixed build, 141 runs: read_rows max 21,522 (bound 50,000), all OK except the local-only session_timezone 'zoneinfo/UTC' artifact; - base build (6425af9a), 49 runs: read_rows 100,000 in every run, the three read lines fail in every run; - old vs new test, 32 runs each, -j 16 on a server pinned to 8 CPUs: p50 1.35 s -> 0.37 s, max 2.06 s -> 0.48 s. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
📊 Cloud Performance Report ✅ AI verdict: This PR only changes PartialSortingTransform when it is built by SortingStep::finishSorting, which is the ORDER BY-a-key-prefix-with-LIMIT read-in-order path. It keeps the last row of each block so FinishSortingTransform can still stop reading early. None of the flagged queries (ClickBench Q14, Q30, Q33, Q34 and TPC-H Q1, Q7) run that path: they are GROUP BY aggregations or joins with top-N or no LIMIT, and the edit to the regular threshold comparison keeps the same behaviour. The improvements of -10.5% to -21.0% are therefore marked not_sure: they are uniform across unrelated query shapes, both benchmarks ran on the same machine for the source build, and Q33/Q34 sit inside master's current range. The PR's own effect would only show in the new read_in_order_prefix_limit perf test. clickbenchFlagged queries (4 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_officialFlagged queries (2 of 22)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. Debug info
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units13 translation units recompiled, 59 s compile time in total, 13 of them have a recent master baseline. |
CI finish ledger - 8e087e9Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261012-223016 |
|
@groeneai Refresh branch to latest master |
…x-limit-threshold Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - d9707efEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The Fast test failure dropped the rest of the matrix (167 jobs skipped), so this head has no other test results. Session id: cron:our-pr-ci-monitor:20261020-130014 |
…x-limit-threshold Resolve conflicts with ClickHouse#124829: the PartialSortingTransform constructor now takes threshold_collector_ before sorted_prefix_size_, and finishSorting passes both. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 4aa53c1Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261103-200006 |
|
Dear @shankar-iyer, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
|
@groeneai Refresh to latest master |
…x-limit-threshold
|
Merged the latest master ( Checked on it: |
| for (size_t i = 0; i < num_columns; ++i) | ||
| { | ||
| int res = sort_description[i].direction * raw_block_columns[i]->compareAt(min_block_index, 0, *threshold_columns[i], sort_description[i].nulls_direction); | ||
| int res = sort_description[i].direction * raw_block_columns[i]->compareAt(row, 0, *threshold_columns[i], sort_description[i].nulls_direction); |
There was a problem hiding this comment.
[Blocker] Preserve collated ordering in the threshold filter
SortingStep::finishSorting can receive ORDER BY a, b COLLATE 'en' with an uncollated sorted prefix a. However, getFilterMask uses bytewise compareColumn, and this helper uses bytewise compareAt even for the full-description comparison at line 308. Those comparisons disagree with sortBlock, which honors the collator, so the filter can discard rows belonging in the result.
For example, on a table ordered by (a, c), let the first a = 0 group contain 70,000 rows with b = 'b', followed by 30,000 with b = 'ä', then an a = 1 group. With LIMIT 1500 and 8192-row blocks, the first block establishes (0, 'b') as the threshold. All subsequent (0, 'ä') rows are discarded, although 'ä' sorts before 'b' under 'en'. Keeping an a = 1 boundary row cannot restore those winners.
This is pre-existing in the touched path. A smaller read-only reproduction on ClickHouse 26.10.3.7 returns b\t1500; changing only the limit to 1499 correctly returns ä\t1499:
SELECT b, count()
FROM
(
SELECT intDiv(number, 100000) AS a,
if(number < 70000, 'b', 'ä') AS b
FROM numbers(200000)
ORDER BY a, b COLLATE 'en'
LIMIT 1500
)
GROUP BY b;Please honor collation in both threshold operations, or disable this filter for collated sort descriptions, and cover a group spanning multiple blocks. If this pre-existing defect is handled separately, link its tracking issue explicitly.
There was a problem hiding this comment.
This is pre-existing and not reached by this change: the query above returns b 1500 on a current master build too, and the new comparison only covers the sorted prefix, which never holds a collated column (optimizeReadInOrder stops at a collator). Collation in both getFilterMask and compareWithThreshold is fixed separately in #125421.
Closes: #125296
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
ORDER BYover a prefix of the table's sorting key with aLIMITbetween 1500 andmax_block_size(for exampleORDER BY l_orderkey, l_extendedprice LIMIT 10000on a table sorted byl_orderkey) now stops reading as early as it does for other limits, instead of reading the whole table. Closes #125296.Description
With
optimize_read_in_orderon a prefix of the sorting key the sort runs asPartialSortingTransform -> FinishSortingTransform -> Limit.FinishSortingTransformoutputs a group of rows with an equal prefix only when it receives a row of the next group.PartialSortingTransformhas its own top-K threshold filter, armed when1500 <= limit <= block rows. After the first block it drops every row that is not below the threshold, which under read-in-order is every row of every later prefix group, soFinishSortingTransformnever saw a group end and the query read the whole table. This explains both boundaries in the issue: below 1500 the filter is off, abovemax_block_sizeno block reacheslimitrows.PartialSortingTransformnow gets the sorted prefix size fromSortingStep::finishSorting, and when the last row of a chunk has a prefix greater than the threshold's, it keeps that one row. It is never part of the result (at leastlimitrows not above the threshold were already passed on), but it marks the end of the group, soFinishSortingTransformemits it andLimitstops the read. Later rows are still dropped, so the filter keeps its memory benefit, and the read still stops inLimit, soexact_rows_before_limitis unchanged. Same defect class as #100235, one layer up.Validation (debug build, 6M rows with 4 rows per prefix value,
max_threads = 1):ORDER BY a, b LIMIT 1500read 6,000,000 rows before and 24,576 after; withmax_block_size = 8192every limit from 1500 to 8192 read 6,000,000 rows before and 16,384 after. Results are identical to a full sort. The new test fails on master and passes with the fix (84/84 randomized runs). The new perf test reads 20M rows before and 57K after.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/125415]