Skip to content

Stop exchange sinks when the consumer needs no more data - #114806

Merged
davenger merged 5 commits into
masterfrom
distributed_plan_limit_early_stop
Aug 17, 2026
Merged

davenger merged 5 commits into
masterfrom
distributed_plan_limit_early_stop

Conversation

@davenger

Copy link
Copy Markdown
Member

A distributed-plan query with a satisfied LIMIT kept all upstream stages running to completion: an exchange sink consumed and dropped the rest of the stream after the consumer sent NoMoreDataNeeded, so the client received the result while the query kept running until cancelled. Now the sink closes its input instead, and the stop propagates backward through the stages the same way a local LimitTransform stops its producers. The in-memory exchange reader also detaches when its output port is closed, not only on cancellation.

The new test covers a LIMIT in the middle of the plan: the stage that satisfies it finishes early and the stop crosses two exchange layers down to the scans.

Changelog category (leave one):

  • Experimental Feature

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

A distributed plan query (make_distributed_plan) now stops its upstream stages when a LIMIT is satisfied, instead of computing the full result and discarding it.

🤖 Generated with Claude Code

@clickhouse-gh

clickhouse-gh Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [67db020]

Summary: ✅


AI Review

Summary

This PR makes in-memory and Streaming distributed-plan exchanges stop pulling once a downstream LIMIT no longer needs more rows, and it adds a stateless test that now covers both local and remote streaming transport. I found one scope gap: the change is presented as a general make_distributed_plan LIMIT early-stop fix, but Persisted exchanges are still a supported/default mode and still compute the full upstream result.

PR Metadata
  • Changelog category is acceptable for an experimental distributed-plan change.
  • Changelog entry currently overstates the scope. Suggested replacement: A make_distributed_plan query using in-memory or Streaming exchanges now stops its upstream stages when a LIMIT is satisfied, instead of computing the full result and discarding it.
Findings
  • ⚠️ Major: [tests/queries/0_stateless/04891_distributed_plan_limit_early_stop.sql:48] The new contract is only implemented for in-memory / Streaming exchanges. QueryPlanOptimizationSettings.cpp:366-372 still auto-selects Persisted when the streaming listener is not configured, and ExchangeViaTemporaryFiles (src/QueryPipeline/DistributedPlanExecutor.cpp:168-199) still has no consumer-driven stop path, so those plans still run upstream stages to completion and discard the rest.
    Suggested fix: either extend or reject the Persisted path for this scenario, or narrow the PR/changelog/test claim to explicitly say the optimization applies only to in-memory / Streaming exchanges.
Missing context / blind spots
  • ⚠️ The full build/test matrix is still pending; green amd / arm stateless coverage would close the remaining runtime blind spot.
Final Verdict

Needs follow-up before merge: the implementation and test are fine for in-memory / Streaming exchanges, but the user-facing contract currently over-promises relative to the still-supported Persisted path.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.80% 86.80% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.20% 79.20% +0.00%

Changed lines: Changed C/C++ lines covered: 31/32 (96.88%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label Aug 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 67db020f8 with master 1fa79a4d9 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 693.33 MiB 690.32 MiB -3.02 MiB (-0.44%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

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

Job report

)
SETTINGS make_distributed_plan = 1, enable_parallel_replicas = 0, distributed_plan_execute_locally = 0,
distributed_plan_default_shuffle_join_bucket_count = 3, distributed_plan_default_reader_bucket_count = 3,
distributed_plan_max_rows_to_broadcast = 0, distributed_plan_force_exchange_kind = 'Streaming',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

distributed_plan_execute_locally = 0 can still run this query through Persisted exchanges: that is the auto-selected kind whenever the streaming listener is not configured (QueryPlanOptimizationSettings.cpp:366-372), and users can force it explicitly. This test only exercises Streaming, but ExchangeViaTemporaryFiles still just writes the whole stream (DistributedPlanExecutor.cpp:168-199), so the new user-facing promise is false in that supported mode: those plans still run the upstream stages to completion and discard the rest. Can we either scope the changelog / PR claim to in-memory / Streaming exchanges, or handle or reject the Persisted path too?

@CheSema CheSema self-assigned this Aug 17, 2026
@davenger
davenger added this pull request to the merge queue Aug 17, 2026
Merged via the queue into master with commit cb139c6 Aug 17, 2026
533 of 536 checks passed
@davenger
davenger deleted the distributed_plan_limit_early_stop branch August 17, 2026 09:48
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-experimental Experimental 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.

3 participants