Skip to content

Use projections with parallel replicas when optimize_aggregation_in_order is enabled - #123098

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:pr-projections-aggregation-in-order-123061
Oct 1, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:pr-projections-aggregation-in-order-123061

Conversation

@groeneai

@groeneai groeneai commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #123061
Related: #82807
Related: #122853

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

With parallel replicas, optimize_aggregation_in_order no longer disables projections: aggregate and normal projections, and _minmax_count_projection for count() with a partition predicate, are used as with optimize_aggregation_in_order = 0. Closes #123061.

Description

With parallel replicas, canUseProjectionForReadingStep rejected every projection when optimize_aggregation_in_order or force_aggregation_in_order was set (#82807), even when nothing is aggregated in order. In the issue's repro the aggregation reads 1,000,000 rows instead of 10 and the partition count() 33,334 instead of 1; with force_optimize_projection / max_rows_to_read they fail with PROJECTION_NOT_USED / TOO_MANY_ROWS, as 22 tests did in the ParallelReplicas job of #122853 (report).

The check covered one case: with a full aggregate projection the initiator's local plan can aggregate in order over the projection read; memory-bound merging then sends force_aggregation_in_order to the followers, which cannot use the projection in that mode and read the base table on the same coordinator stream (@ nickitat on #82807: either the projection or memory-bound merging).

This PR removes the setting check and, for parallel replicas, skips aggregation in order over an aggregate projection read (only_merge), like the other parallel-replicas skips in optimizeReadInOrder.cpp. Initiator and followers then build their optimize_aggregation_in_order = 0 plan (identical EXPLAIN). Without the skip, a 1M-group projection query hits LOGICAL_ERROR (... for unknown stream) 10 of 10 times.

The new test fails on master, and its EXPLAIN line fails without the skip; 50/50 randomized runs, also in the ParallelReplicas configuration. With optimize_aggregation_in_order = 1 forced there, the 23 tests from the issue and #122853 fail on master and pass here, and none of 465 projection, aggregation-in-order and parallel-replicas tests goes from OK to FAIL, so these tests do not need the #122853 pin.

If you prefer to keep aggregation in order over the projection, the alternative is to skip memory-bound merging there instead; I did not, as that plan shape does not exist today.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.1139-master (included in 26.10 and later)

groeneai and others added 2 commits September 30, 2026 14:45
…rder is enabled

`canUseProjectionForReadingStep` rejected every projection of a parallel
replicas read when `optimize_aggregation_in_order` or
`force_aggregation_in_order` was set (added by ClickHouse#82807). It keyed on the
setting, so aggregate and normal projections, `_minmax_count_projection`
and the exact count were lost for every parallel replicas query of a user
who keeps the setting on, and the whole table was read.

The check stood in for one asymmetry. With a full aggregate projection the
aggregation becomes `only_merge`, and `optimizeAggregationInOrder` may then
aggregate in order over the projection read, which is sorted by the
projection's keys. The local plan's root aggregation then uses memory-bound
merging, so `enableMemoryBoundMerging` enforces aggregation in order on the
followers. With `force_aggregation_in_order` the followers' aggregation has
a sort description, `AggregatingStep::canUseProjection()` is false there,
and they read the base table on the same coordinator stream as the
initiator's projection read: a different coordination mode, or parts the
initiator never announced. Without the setting check and without the skip
below, a `GROUP BY k` over a 1M-group projection of a table `ORDER BY v`
fails with `LOGICAL_ERROR` "Got read request from replica N for unknown
stream" every time.

So instead of rejecting projections, do not aggregate in order over an
aggregate projection read when reading with parallel replicas, like the
other parallel replicas skips in `optimizeReadInOrder.cpp` (JOIN, LIMIT BY,
UNION). The initiator and the followers then build the plan they build
with `optimize_aggregation_in_order = 0`; `EXPLAIN PLAN` and
`EXPLAIN PIPELINE` are identical. The `!support_projection` half of the
check is unchanged.

Closes ClickHouse#123061.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the only_merge read-in-order skip comment to one line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Sep 30, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding), plus my own review.

# Sev Finding Verdict Evidence / action
1 ❌ During a rolling upgrade one version reads the projection and the other the base table on the same stream, so a base-reading follower gets projection mark ranges and reads them from the base part (projectionsCommon.cpp) DISAGREE The coordinator only assigns a replica parts it announced under the same <part>.<projection> name: DefaultCoordinator visibility (ParallelReplicasReadingCoordinator.cpp:435, checked at 549, 568, 665, 727) and InOrderCoordinator::handleRequest (1126). The replica whose plan differs gets no work, and the result is correct.
2 💡 Residual of 1: if one side reads the base table in order (table sorted by the GROUP BY keys) while the other reads the projection, Coordination mode mismatch is thrown until all replicas agree (mixed versions, or MATERIALIZE PROJECTION still running on a replica) noted Same outcome as other parallel-replicas in-order changes and as ORDER BY read-in-order over a normal projection today; not gated here.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-10:20260930-145600

@clickhouse-gh

clickhouse-gh Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9b5bad2]

Summary: ✅


AI Review

Summary

This PR removes the blanket optimize_aggregation_in_order / force_aggregation_in_order rejection for projection use under task-based parallel replicas, and instead declines read-in-order only on the only_merge aggregate-projection path so initiator and followers keep the same coordinator mode. The changed planner/runtime interactions line up, the prior discussion about mixed projection/base visibility is answered by the coordinator’s <part>.<projection> visibility checks, and the new stateless coverage exercises the aggregate-projection, normal-projection, and count-shortcut cases. I do not see a remaining contract violation in the current code.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 9b5bad2.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.60% 79.60% +0.00%

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

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Sep 30, 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 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9b5bad21f with master 120be6b5f (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 8dc18243e; 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

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

Job report

@clickhouse-gh

clickhouse-gh Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 36 queries analysed

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
  • StressHouse run: a6a9df6e-3ea7-407c-8d50-c913d4c30dab
  • MIRAI run: 944ee1a5-25ed-4227-9f49-d3ced5117952
  • PR check IDs:
    • clickbench_2559971_1790789361
    • clickbench_2559977_1790789361
    • clickbench_2559991_1790789361
    • tpch_adapted_1_official_2560015_1790789361
    • tpch_adapted_1_official_2560030_1790789362
    • tpch_adapted_1_official_2560045_1790789361

@alexey-milovidov alexey-milovidov self-assigned this Oct 1, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 1, 2026
Merged via the queue into ClickHouse:master with commit 77ec958 Oct 1, 2026
149 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 1, 2026
nickitat added a commit that referenced this pull request Nov 15, 2026
…jobs

The pin worked around #123061:
with parallel replicas, `optimize_aggregation_in_order` rejected every projection,
including `_minmax_count_projection`. It was added on 2026-09-30, one day before
the fix #123098 was merged, and is
obsolete now, so the setting is randomized again in both halves of the jobs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-performance Pull request with some performance improvements 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.

optimize_aggregation_in_order = 1 disables all projections and metadata-only count() under parallel replicas

3 participants