Repository navigation
Speed up wide-table sorting by skipping per-row replicated-column check in merge - #110627
alexey-milovidov merged 5 commits into
Conversation
…columns PR ClickHouse#101036 added a per-row, per-column `raw_columns[i]->isReplicated()` check to `MergedData::insertRow` / `insertRows` to wrap the destination in `ColumnReplicated` when a source is replicated (needed only for merges fed by a JOIN with `enable_lazy_columns_replication = 1`). For a plain sort with no JOIN, no column is ever `ColumnReplicated`, so this virtual call runs once per column per row purely as overhead (e.g. 18 columns x hundreds of thousands of rows in the `sort_wide_table` benchmark) and is visible as a slowdown on ARM. Add a `may_have_replicated_columns` hint to `MergedData` (default `true`, so every algorithm keeps the wrapping behavior unless it opts out). `MergingSortedAlgorithm` sets it to `false` in `initialize` when no input column is replicated, and raises it back to `true` monotonically in `consume` if a late-arriving chunk brings a replicated column, before those rows can reach `insertRow`. When the hint is `false`, `insertRow` / `insertRows` take a tight loop identical to the pre-ClickHouse#101036 behavior; the wrapping path is unchanged when the hint is `true`. The lazy-replication optimization is fully preserved: a replicated source can only enter the merge via `initialize` or `consume`, both of which set the hint before insertion. Added unit tests for the fast path and for restoring the hint. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-3:20260715-231500 |
|
cc @KochetovNicolai @vdimir — could you review? This removes a per-row |
|
Workflow [PR], commit [2cae3b6] Summary: ✅
AI ReviewSummaryThis PR adds a Tests
Final Verdict✅ No new inline findings on the code. The implementation looks correct; only the claimed ARM speedup still needs the benchmark result from the current head. LLVM Coverage ReportMeasured on commit 2cae3b6.
Changed lines: Changed C/C++ lines covered: 163/165 (98.79%) · Uncovered code |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
…-hint fast path The new test drives the actual algorithm transition the optimization depends on: initialize the fast path with non-replicated inputs, then deliver a late ColumnReplicated chunk via consume and assert the rows merge correctly. Unlike the MergedData-only tests, it never toggles the hint itself, so it fails if the consume flag-raise is dropped or reordered (verified: without the raise it aborts in insertRow on the type mismatch). Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Added `MergedDataReplicated.MergingSortedAlgorithmRaisesHintOnLateReplicatedChunk` (commit 86a81c4). It drives the real transition: initialize the fast path with non-replicated inputs, then deliver a late `ColumnReplicated` chunk to a source via `consume` and merge to completion, asserting all rows come out sorted with correct values (including the two from the replicated chunk). The late chunk carries two rows whose keys straddle the other source (20 < 50 < 60), so it goes through the row-by-row `insertRow` path rather than the direct `insertChunk` fast-forward. The test never calls `setMayHaveReplicatedColumns` itself, so it fails if the `consume` flag-raise is dropped or reordered. Verified locally: with the raise it passes; with the raise removed it aborts in `insertRow` on the plain-destination / replicated-source type mismatch, while the manual-toggle tests stay green. |
CI finish ledger — 86a81c4Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. None are caused by this PR (which only touches the
Session id: cron:our-pr-ci-monitor:20260716-170000 |
CI finish ledger owner update — 86a81c4The Root cause is the EXCHANGE TABLES vs concurrent SELECT race tracked in #110133. Owner: fixing PR #109747 (ours, open, matches this exact partial-result abort path) and #110048 (external, open, root-cause read consistency). Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110627&sha=86a81c495a2d988bb38babfe1046ea713e37b37c&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20old%20analyzer%2C%20s3%20storage%2C%20DBReplicated%2C%20WasmEdge%2C%20parallel%2C%202%2F3%29 Not caused by this PR (which only touches the MergingSortedAlgorithm/MergedData sort fast-path). Session id: cron:clickhouse-worker-slot-4:20260716-171500 |
CI finish ledger — 86a81c4Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Session id: cron:our-pr-ci-monitor:20260717-070000 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 163/165 (98.79%) · Uncovered code |
CI finish ledger - dcb0255Every failure below has an owner: a fixing PR, or a full-effort fix task whose
Session id: cron:our-pr-ci-monitor:20260729-083000 |
@groeneai are these issues known, especially ubsan in stress tests? |
|
@vdimir yes, all three are known and each has an owner. The UBSan one you singled out (STID
None of the three touches merge code. This PR changes only |
|
Fixing PR for the Keeper That was the one row that had no fixing PR when I posted the ledger, so I promised to link it here once it opened. The assertion fires while the Keeper request dispatcher is being torn down, which is why the carrier test on this branch was The other two rows are unchanged in substance: #112045 for the |
|
Dear @vdimir, 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. |
The workflow run on the previous head produced no test result at all: the praktika config job could not reach the CI database, retried five times, and was killed at its 600 s timeout, so every dependee job was dropped. Merging current master gives the PR a fresh head to run on and rebases the five-week-old branch on top of the current MergingSortedAlgorithm and MergedData code in one commit.
|
The workflow run on the previous head ( I merged current master to give it a fresh head to run on. That commit is a plain merge with no source change: the diff is still the same four files, +292/-21. Since master refactored The aarch64 numbers for CI is running: https://github.com/ClickHouse/ClickHouse/actions/runs/33957272804 |
Build profile diff (arm_release)Comparing Binary sizes
The official master build is compiled with Object file sizes
|
| Object file | Master | PR | Δ |
|---|---|---|---|
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/Cascades/Rules/AggregationPushdown.cpp.o |
new | 278.38 KiB | +278.38 KiB |
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.
Compile time of recompiled translation units
349 translation units recompiled, 3123 s compile time in total, 348 of them have a recent master baseline.
Median compile-time ratio to the baselines is ×1.11 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +265.0 s (+9%) in total before that adjustment.
Translation units without a recent master baseline:
src/Processors/QueryPlan/Optimizations/Cascades/Rules/AggregationPushdown.cpp: 8.4 s
Related: #107611
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Speed up sorting of wide tables (
ORDER BYover many columns) by skipping a per-rowColumnReplicatedcheck in the merge step when no column can be replicated. This removes overhead that was noticeable on ARM.Description
Addresses #107611 (ARM regression on
sort_wide_tablequery1:SELECT * FROM sort_wide_16 ORDER BY k1, k2 FORMAT Null).The reported regression scope is the
ColumnReplicatedwrapping added toMergedDataby #101036. That change added a per-row, per-columnraw_columns[i]->isReplicated()check toMergedData::insertRow/insertRows, wrapping the destination inColumnReplicatedwhen a source is replicated. That wrapping is only needed when the merge is fed by a JOIN withenable_lazy_columns_replication = 1. For a plain sort with no JOIN, no column is everColumnReplicated, so the virtual call runs once per column per row (18 columns x ~500K rows forsort_wide_16) as pure overhead in the hot row-by-row insertion loop.Fix, aligned with the existing design (keep the wrapping where it is genuinely needed):
may_have_replicated_columnshint toMergedData, defaulttrue, so every algorithm keeps the current wrapping behavior unless it explicitly opts out. When the hint isfalse,insertRow/insertRowstake a tight loop identical to the pre-Fix ColumnReplicated type mismatch crash in merge algorithms #101036 behavior; whentrue, the wrapping path is byte-for-byte unchanged.MergingSortedAlgorithmlowers the hint tofalseininitializewhen no input column isColumnReplicated, and raises it back totruemonotonically inconsumeif a late-arriving chunk brings a replicated column — always before those rows can reachinsertRow. A replicated source can only enter the merge throughinitializeorconsume, so the wrapping is never skipped when it is needed.MergedData(Aggregating, Summing, Collapsing, Replacing, VersionedCollapsing, GraphiteRollup) leave the hint at its defaulttrueand are unaffected.Validation on x86 (the ARM Neoverse-V2 numbers must come from the arm_release Performance Comparison job, which is the authoritative measurement):
MergedDataReplicated.FastPathSkipsWrappingForRegularColumnsandRestoringFlagReenablesWrapping, plus all 5 pre-existingMergedDataReplicatedregression tests, pass.enable_lazy_columns_replication = 1followed byORDER BY(forcesColumnReplicatedinto the merge) produces results identical toenable_lazy_columns_replication = 0and stays correctly sorted.03580_external_merge_sort_with_lazy_columns,03709_replicated_columns_right_join,03701_replicated_column_short_circuit_filter,03352_lazy_column_filter_by_uint8pass.Workflow [PR]
Sync PR [sync-upstream/pr/110627]