Skip to content

Parallel replicas support projection - #82807

Merged
nickitat merged 41 commits into
ClickHouse:masterfrom
zoomxi:support_projection
Aug 19, 2025
Merged

nickitat merged 41 commits into
ClickHouse:masterfrom
zoomxi:support_projection

Conversation

@zoomxi

@zoomxi zoomxi commented Jun 28, 2025 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • New Feature

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

Reading from projections is implemented for parallel replicas. A new setting parallel_replicas_support_projection has been added to control whether projection support is enabled. To simplify the implementation, support for projection is only enabled when parallel_replicas_local_plan is active.


This PR closes #82659.

Changes Overview

  • Coordinator + ReadPool Changes

    • Previous Behavior: The visibility of a part across replicas was determined by the part's part_info.
    • New Behavior: For projection parts, the visibility is now controlled by the parent part's part_info + projection_name.
  • Query Plan Changes for Projection Optimization

    • Single Stream:
      • If all parent parts are filtered out by projections, read the projections using parallel replicas.
    • Two Streams:
      • Read the projection only on the initiator; read the remaining parts using parallel replicas.
    • Min-Max Projection:
      • Read the projection only on the initiator.
    • Exact Count Optimization:
      • Read the projection only on the initiator; read the remaining parts (if any) using parallel replicas.

Documentation entry for user-facing changes

  • [*] Documentation is written (mandatory for new features)

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jun 28, 2025
@clickhouse-gh

clickhouse-gh Bot commented Jun 28, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [11eb49c]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_binary, old analyzer, s3 storage, DatabaseReplicated, parallel) failure
03409_variant_type_values_format_field_conversion FAIL
Exception in test runner FAIL
Killed by signal (in clickhouse-server.log or clickhouse-server.err.log) FAIL
Fatal messages (in clickhouse-server.log or clickhouse-server.err.log) FAIL

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Jun 28, 2025
@zoomxi
zoomxi force-pushed the support_projection branch 13 times, most recently from c7220dd to 6f968f4 Compare July 5, 2025 13:19
@zoomxi zoomxi changed the title [WIP] Parallel replicas support projection Parallel replicas support projection Jul 5, 2025
@zoomxi

zoomxi commented Jul 5, 2025

Copy link
Copy Markdown
Contributor Author

@zoomxi
zoomxi force-pushed the support_projection branch from 6f968f4 to 3442cfe Compare July 7, 2025 08:40
@zoomxi

zoomxi commented Jul 8, 2025

Copy link
Copy Markdown
Contributor Author

PTAL @alexey-milovidov

@zoomxi
zoomxi force-pushed the support_projection branch 3 times, most recently from 128b892 to 4a0e891 Compare July 14, 2025 07:20
@zoomxi
zoomxi force-pushed the support_projection branch from 4a0e891 to 7fff9e3 Compare July 15, 2025 01:22
@nickitat nickitat self-assigned this Jul 15, 2025
Comment thread src/Core/Settings.cpp Resolved
Comment thread src/Core/SettingsChangesHistory.cpp Resolved
@zoomxi
zoomxi requested a review from nickitat July 16, 2025 08:14
@zoomxi

zoomxi commented Aug 11, 2025

Copy link
Copy Markdown
Contributor Author

Stateless tests (amd_binary, ParallelReplicas, s3 storage, parallel) 02941_projections_external_aggregation
This issue is quite subtle.
When reading from both projection and ordinary data simultaneously, if the local plan initiates the read slower than other replicas, it may fail to obtain the range for the ordinary data. As a result, the local plan will only read from the projection.
The previous aggregator_has_temporary_data logic only checked whether there was temporary_data when reading ordinary data, which could cause data loss during external aggregation when reading from the projection.

@zoomxi

zoomxi commented Aug 11, 2025

Copy link
Copy Markdown
Contributor Author

PTAL @nickitat

@zoomxi
zoomxi requested a review from nickitat August 11, 2025 13:24
@devcrafter

Copy link
Copy Markdown
Member

Stateless tests (amd_binary, ParallelReplicas, s3 storage, parallel) 02941_projections_external_aggregation This issue is quite subtle. When reading from both projection and ordinary data simultaneously, if the local plan initiates the read slower than other replicas, it may fail to obtain the range for the ordinary data. As a result, the local plan will only read from the projection. The previous aggregator_has_temporary_data logic only checked whether there was temporary_data when reading ordinary data, which could cause data loss during external aggregation when reading from the projection.

It means we should fix the test right?

@zoomxi

zoomxi commented Aug 12, 2025 •

Copy link
Copy Markdown
Contributor Author

It means we should fix the test right?

It has been fixed in this commit 99dceb4, where the issue was resolved and test case was added.
This commit reflects post-approval changes, so i recorded the reason for the failed test case.

@nickitat

nickitat commented Aug 12, 2025 •

Copy link
Copy Markdown
Member

Unfortunately, the flaky check failed in private, because some tests were too slow. You can check out the similar check in public: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=82807&sha=cc237effe453837180de766d261e33bc6c729007&name_0=PR&name_1=Stateless+tests+%28amd_asan%2C+flaky+check%29 (sort by duration). Let's make the new tests (03560_*) a little faster, so they don't timeout under sanitizers.

&& query_settings[Setting::parallel_replicas_local_plan]
&& query_settings[Setting::parallel_replicas_support_projection];

/// AggregationInOrder may cause local and remote replicas to use different CoordinationModes, which is currently unsupported.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have test which demo the case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the following three test cases all cover and validate the scenario:
01710_projection_aggregation_in_order
01710_projections_optimize_aggregation_in_order
01710_projections_partial_optimize_aggregation_in_order

@zoomxi
zoomxi requested a review from devcrafter August 15, 2025 02:42
@zoomxi

zoomxi commented Aug 19, 2025

Copy link
Copy Markdown
Contributor Author

Stateless tests (amd_binary, old analyzer, s3 storage, DatabaseReplicated, parallel)
Next task callback is not set for query

@nickitat

Copy link
Copy Markdown
Member

@nickitat
nickitat added this pull request to the merge queue Aug 19, 2025
Merged via the queue into ClickHouse:master with commit a97b717 Aug 19, 2025
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 19, 2025
@zoomxi
zoomxi deleted the support_projection branch August 28, 2025 02:47
superdiaodiao pushed a commit to superdiaodiao/ClickHouse that referenced this pull request Oct 1, 2026
…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]>
subkanthi pushed a commit to subkanthi/ClickHouse that referenced this pull request Nov 17, 2026
…ections with parallel replicas

The aggregate and normal projection optimizers decided whether a coordinated read is the
initiator's own share (and so must return what a projection or a prepared source answers)
from `QueryPlanOptimizationSettings::is_parallel_replicas_initiator_with_projection_support`,
computed once from the context the whole plan is optimized with. The initiator's local plan
is optimized with a copy of the OUTER settings, so when parallel replicas are enabled only in
a subquery's SETTINGS the flag is false there: the initiator emitted a NullSource for the
`_minmax_count_projection`, `_statistics_projection` and `_exact_count_projection` blocks and
skipped the projection read beside parts without the projection, while the followers do the
same by design. `min`/`max`/`count` came back as 0 (since ClickHouse#82807), `sum`/`avg` as 0/nan (since
ClickHouse#123950), and projection-answered rows were dropped.

Use `ReadFromMergeTree::isParallelReplicasLocalPlanForInitiator()`, which reads the read's own
context and is already used by the same code for the empty-ranges announcement (and its
follower twin for the follower failpoints), and remove the plan-wide field. For a query whose
outer and subquery contexts agree the answer is unchanged.

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 pr-feature Pull request with new product feature 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.

Cannot use projection when parallel replicas is enabled.

5 participants