Skip to content

Allow cancellation of scalar subqueries and other analysis-time pipelines#100230

Open
Algunenano wants to merge 8 commits intoClickHouse:masterfrom
Algunenano:cancel_subquery_tcp_local
Open

Allow cancellation of scalar subqueries and other analysis-time pipelines#100230
Algunenano wants to merge 8 commits intoClickHouse:masterfrom
Algunenano:cancel_subquery_tcp_local

Conversation

@Algunenano
Copy link
Copy Markdown
Member

@Algunenano Algunenano commented Mar 20, 2026

Closes #1576
Closes #98163

Co-authored with @YjyJeff, based on #98163.

During query analysis, several pipelines run synchronously before the main query pipeline starts: scalar subqueries, IN subquery set building, GLOBAL IN/JOIN materialization, and PARALLEL WITH queries. Previously, these could not be cancelled via Ctrl+C because nobody was reading Cancel packets from the TCP socket during analysis.

This PR enables cancellation by using the existing interactive_cancel_callback to periodically poll for Cancel packets during these analysis-time pipelines.

For clickhouse-local, a cancel callback is set that checks the signal handler flag and cancels through the ProcessListElement when SIGINT is received.

The client is also fixed to buffer Progress packets that arrive before the output format is created (from scalar subqueries during analysis), and replay them once the format is available. This ensures JSON statistics correctly include rows read by scalar subqueries, and the progress bar updates during scalar subquery execution. The same fix is applied to LocalConnection which now always tracks progress and sends a final Progress packet, fixing rows_read: 0 in clickhouse-local JSON output.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Allow cancellation of scalar subqueries and other analysis-time pipelines via Ctrl+C. Previously, pressing Ctrl+C during a long-running scalar subquery had no effect until the subquery completed. Also fix the progress bar and JSON statistics to correctly report rows read during scalar subquery execution, both in clickhouse-client and clickhouse-local. Co-authored with @YjyJeff.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

🤖 Generated with Claude Code

YjyJeff and others added 7 commits March 20, 2026 18:44
Scalar subqueries are executed during the query analysis phase,
where the interactive cancel callback was not being checked.
This made it impossible to cancel queries like
`SELECT (SELECT max(number) FROM system.numbers) + 1` using
Ctrl+C or KILL QUERY.

Poll the `getInteractiveCancelCallback` between timed pulls
(100ms timeout) during scalar subquery execution, in both the
new analyzer and the old interpreter paths.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Use the single `interactive_cancel_callback` for all subquery pipelines
(scalar subqueries, prepared sets, GLOBAL IN/JOIN, PARALLEL WITH) instead
of a separate `subquery_cancel_callback`. Remove the wrapping pattern that
converted the callback return value into an exception.

Fix `sendProgress` in `TCPHandler` to compute deltas via a `sent_progress`
tracker instead of resetting `state.progress` with
`fetchValuesAndResetPiecewiseAtomically`. This allows the cancel callback
to safely send progress during subquery execution without losing
accumulated statistics.

Fix the client to buffer Progress packets received before `output_format`
is created (e.g. from scalar subqueries during analysis) and replay them
once the output format is available.

Also cancel through the `ProcessListElement` in `processCancel` so all
registered pipeline executors see the cancellation.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Mar 20, 2026

Workflow [PR], commit [9fb0d3a]

Summary:

job_name test_name status info comment
AST fuzzer (arm_asan_ubsan) failure
UndefinedBehaviorSanitizer: undefined behavior (STID: 2599-55b4) FAIL cidb

AI Review

Summary

This PR adds cancellation polling for analysis-time pipelines (scalar subqueries, set building, PARALLEL WITH, and shard subquery materialization), wires local cancellation through ProcessListElement, and fixes early Progress handling in clickhouse-client/clickhouse-local so JSON stats include analysis-time reads. I did not find high-confidence correctness, safety, concurrency, or performance regressions in the changed code paths.

Missing context

  • ⚠️ No CI logs or benchmark reports were provided in the review context, so runtime/performance validation could not be independently verified here.

ClickHouse Rules

Item Status Notes
Deletion logging
Serialization versioning
Core-area scrutiny
No test removal
Experimental gate
No magic constants
Backward compatibility
SettingsChangesHistory.cpp
PR metadata quality
Safe rollout
Compilation time

Final Verdict

  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 20, 2026
…pipelines

The `cancelQuery` call on the `ProcessListElement` was too aggressive: it
cancels ALL registered pipeline executors, not just the one that received
the Cancel packet. This breaks parallel replica queries where multiple
executors share the same `ProcessListElement`.

Cancellation of scalar subqueries still works correctly: `processCancel`
throws `QUERY_WAS_CANCELLED_BY_CLIENT`, which propagates through the
cancel callback up through the pull loop.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@novikd novikd self-assigned this Mar 20, 2026
@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Mar 20, 2026

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 24.00% 24.00% +0.00%
Branches 76.40% 76.40% +0.00%

PR changed lines: PR changed-lines coverage: 98.10% (155/158, 0 noise lines excluded)
Diff coverage report
Uncovered code

@Algunenano Algunenano requested a review from novikd March 24, 2026 11:28
@Algunenano
Copy link
Copy Markdown
Member Author

@novikd Can you please review this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot cancel queries when scalar subqueries are being calculated

3 participants