Skip to content

Improve canceling queries in nested expression functions in FilterTransform - #112705

Merged
alexey-milovidov merged 110 commits into
masterfrom
continue-pr-103705-filter-transform-cancel
Oct 11, 2026
Merged

alexey-milovidov merged 110 commits into
masterfrom
continue-pr-103705-filter-transform-cancel

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 31, 2026 •

Copy link
Copy Markdown
Member

Related: #103705

Changelog category (leave one):

  • Improvement

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

Improve canceling queries by KILL QUERY and Ctrl+C in clickhouse-client while a long-running function is evaluated in a filter expression: FilterTransform (WHERE, including the FilterSortedStreamByRange path) and TotalsHavingTransform (HAVING with WITH TOTALS) now forward cancellation into nested expression functions.

This supersedes #103705 by @rvasin (Roman Vasin), whose commits are preserved here — all credit for the feature goes to them. The original branch is in a fork with maintainer edits disabled, so the merge conflicts with master and the remaining review feedback could not be pushed there.

On top of the original PR, this PR:

  • Merges master and resolves the conflicts.
  • FilterSortedStreamByRange holds a private FilterTransform and calls its transform directly, but the pipeline cancels only the outer processor. Now FilterSortedStreamByRange::onCancel forwards the cancellation into the inner transform, so cancelExecution reaches functions of range-filter predicates from PartsSplitter.
  • TotalsHavingTransform evaluates the HAVING expression (for the main stream and the totals row) with the cancellation flag, onCancel cancels function execution, and the transform returns before the totals/filter bookkeeping when cancelled.
  • Adds pre-evaluation cancellation checks in FilterTransform, ExpressionTransform and TotalsHavingTransform for a task that was dispatched before the cancellation and started after it.
  • Extends the same treatment to ConvertingTransform, AddingDefaultsTransform, CheckConstraintsTransform, FillingTransform (WITH FILL ... INTERPOLATE) and the IEJoin residual JOIN ON predicate.
  • ExpressionActions::execute takes a pointer to the processor's cancellation flag instead of a std::function callback.

There are no dedicated tests: per review, tests for cancellation are slow and flaky, and these paths are exercised by the Stress tests and fuzzers. The earlier KILL QUERY tests and the pause failpoints added for them were removed.


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

Version info

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

rvasin and others added 26 commits April 28, 2026 12:22
# Conflicts:
#	src/Common/FailPoint.cpp
#	src/Processors/Transforms/FilterTransform.cpp
…lsHavingTransform

Address review: `FilterSortedStreamByRange` holds a private `FilterTransform`
and calls its `transform` directly, but the pipeline only cancels the outer
processor, so the inner `isCancelled` flag never became true and
`cancelExecution` was never forwarded to functions of the filter expression.
Forward the cancellation from `FilterSortedStreamByRange::onCancel`.

Address review: `HAVING` evaluates its predicate in `TotalsHavingTransform`
via `expression->execute` without a cancellation callback, so a long-running
function in `HAVING` remained uninterruptible by `KILL QUERY` or Ctrl+C in
`clickhouse-client`. Mirror the `FilterTransform` changes: cancel function
execution in `onCancel`, pass a `check_cancelled` callback to `execute`, and
return before the totals/filter bookkeeping when cancelled.
…sHavingTransform`

Uses a new `totals_having_transform_pause` failpoint placed after the
`HAVING` expression execution, mirroring the existing
`04612_kill_query_filter_pause` test for `FilterTransform`.
@clickhouse-gh

clickhouse-gh Bot commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d81412b]

Summary: ✅


AI Review

Summary

This PR threads processor cancellation into several expression-evaluating transforms so KILL QUERY / Ctrl+C can stop long-running nested functions instead of waiting for the current action to finish. Most of the earlier cancellation gaps are closed on the current head, but one previously reported QueryConditionCache race is still present, so I would not merge this as-is.

Findings

⚠️ Majors

  • [src/Processors/Transforms/FilterTransform.cpp:459] writeIntoQueryConditionCache still checks isCancelled() only once at entry, then unconditionally writes buffered ranges in the final-flush path at src/Processors/Transforms/FilterTransform.cpp:477 and the part-switch path at src/Processors/Transforms/FilterTransform.cpp:502. If KILL QUERY lands after the entry guard, a cancelled query still warms QueryConditionCache, so this post-cancel side effect is not actually suppressed on the current head.
  • Suggested fix: restore a second isCancelled() checkpoint immediately before each query_condition_cache->write(...), not just at function entry.
Final Verdict

Changes requested: one previously fixed cancellation/cache race is still present on the current head.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 31, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=112705&sha=4d90e5864c5a3e79cabf0f713a4a1486d909e8cd&name_0=PR&name_1=Stress%20test%20%28arm_tsan%29 (Logical error: Unexpected return type from moduloLegacy, STID 1611-3fe5, in FillingTransform::interpolate) and provide a fix in a separate PR. It also shows up in many other PRs and on master in CIDB, and it looks like #121254 (open fix: #121230). If the fix is already in progress, link it here.

@groeneai

groeneai commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator

The fix is already in progress: #121230 (same bug as #121254). The fatal query of this run is the INTERPOLATE + shadowed lambda argument shape:

SELECT [1, nan, 3, 4], number, toUInt64(-2) AS value FROM numbers(1) ORDER BY number ASC NULLS LAST WITH FILL FROM 0 TO 2 INTERPOLATE (`value` AS (value + 1 AS d) + (arrayMap(value -> moduloLegacy(d, value), [1])[1]))

CIDB: STID 1611-3fe5 has 23 failures since 2026-09-19 (20 PRs, 2 master runs), only in Stress test (amd_tsan) / Stress test (arm_tsan). So I am not opening a separate PR. The remaining red on #121230 (targeted AST fuzzer, Trying to execute PLACEHOLDER action) is pre-existing on master and is fixed by my #122397.

The fill loops only `break` on cancellation, and an interrupted `INTERPOLATE`
leaves an empty block, which `insertFromFillingRow` turns into defaults. So a
`KILL QUERY` could still publish a partial or default-filled chunk of the
cancelled range. Clear the chunk in `transform` when the transform is
cancelled; the soft `timeout_overflow_mode = 'break'` path keeps its partial
result.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Status update (2026-10-06): the only red check was CH Inc sync — the private sync PR had a conflict in the failpoint list in src/Common/FailPoint.cpp against private master. I merged private master and the current PR head (8d95e256242) into the sync branch, keeping the union of the failpoints; the sync PR is mergeable again and its diff against private master matches this PR's 67 files. No review threads are open, and nothing in this PR needed to change.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Status update (2026-10-07): the only red check was CH Inc sync, which reported tests failed. None of the failing tests in the private sync PR touch this change:

I merged current master (no conflicts) in a725086 to pick up the revert. clickhouse builds locally. The sync PR will re-run on the new head.

alexey-milovidov and others added 2 commits October 7, 2026 14:17
…s residual is evaluated

Add the `iejoin_residual_before_expression_pause` failpoint right before the
cancellation check in `IEJoinAlgorithm::evaluateResidualMask`, and a test that
kills a `LEFT` `ie_join` parked there. The residual is a `throwIf` that fails on
every pair, so evaluating it after the kill reports
`FUNCTION_THROW_IF_VALUE_IS_NON_ZERO` instead of `QUERY_WAS_CANCELLED`, and
emitting the batch or the unmatched rows after the kill would print rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…filter-transform-cancel

Conflicts in `src/Common/FailPoint.cpp` and `TotalsHavingTransform.cpp`: master added its own
cancellation checks to `TotalsHavingTransform` together with the failpoints
`totals_having_transform_pause` (inside the `addToTotals` loop) and
`totals_having_transform_drop_cancelled_chunk`. This PR used the same name
`totals_having_transform_pause` for the pause after the `HAVING` expression, which would have
made the `PAUSEABLE_ONCE` failpoint fire at the wrong place in `05317_kill_query_totals_having`.
Renamed the PR's failpoint to `totals_having_transform_after_expression_pause` (and updated
`04658_kill_query_having_totals_pause` and `04905_kill_query_cancelled_before_expression`),
and kept both sides' cancellation checks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Status update (2026-10-08):

  • The only red check, Stateless tests (arm_asan_ubsan, azure, sequential), was the whole job hitting its 9000 s timeout at test 1169/1191. Every test that ran passed. The same timeout hit 13 of the last 331 master runs of this job in 7 days (for example 6a1d7a26756), and the job is being split in Split Stateless tests (arm_asan_ubsan, azure, sequential) into 2 batches #123849. So this failure is not caused by this PR.
  • Merged current master (b80550a). Master had added its own cancellation checks to TotalsHavingTransform, and it reused the failpoint name totals_having_transform_pause inside the addToTotals loop. This PR used the same name for the pause after the HAVING expression, so the PAUSEABLE_ONCE failpoint would have fired at the wrong place in master's 05317_kill_query_totals_having. I renamed this PR's failpoint to totals_having_transform_after_expression_pause and updated 04658 and 04905 to match. Both sides' checks are kept.
  • After the merge I ran these tests locally and all passed: 05317_kill_query_totals_having, 04658, 04661, 04671, 04905, 04906 and 04612.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Status update (2026-10-08): every Praktika check is green on b80550a24d2. The only red check was CH Inc sync (Failed. Needs manual intervention). The robot could not merge the new head into the private sync branch: private master conflicted in src/Common/FailPoint.cpp and src/Processors/Transforms/TotalsHavingTransform.cpp. Upstream master had added its own cancellation checks to TotalsHavingTransform, and this PR resolved the same conflicts in b80550a24d2. I merged private master into the sync branch and then merged b80550a24d2. The resolution mirrors the public one: the PR's version of TotalsHavingTransform.cpp, since that file is identical on both master branches, plus the union of the failpoints. https://github.com/ClickHouse/clickhouse-private/pull/66786 is MERGEABLE again, and its diff against private master matches this PR's 69 files exactly. No review threads are open, and nothing in this PR needed to change.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

Tests for cancellations have little value. They are also slow and flaky, so the net value can be even negative.
Please remove all the tests for cancellation and the artificial failpoints as well.

We will be okay merging without the tests as long as the code is clear.

Thanks to the Stress test and Fuzzers, we, de facto, test for these paths.

@alexey-milovidov alexey-milovidov self-assigned this Oct 9, 2026
…d for them

Per the review comment at #112705 (comment), tests for
cancellation are slow and flaky and have little value, while the Stress tests and fuzzers already
exercise these paths. Remove all the `KILL QUERY` tests added by this PR, the pause failpoints they
used (including `wasm_guest_pause` and the `_wasm_signal_ready` host function with the
`infinite_loop_signal` function in `faulty.wasm`), and the cancellation re-checks that existed only
to close the windows opened by those failpoints.

The cancellation forwarding into the expression functions and the cancellation checks around
`ExpressionActions::execute` stay.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Done in db07ba0: removed all 23 cancellation tests added by this PR, every pause failpoint they used (including wasm_guest_pause, the _wasm_signal_ready host function and infinite_loop_signal in faulty.wasm), and the duplicate cancellation re-checks that only existed to close the windows those failpoints opened. The PR now touches 19 source files and no tests. The cancellation forwarding into expression functions and the checks around ExpressionActions::execute are unchanged. The full build passes locally. I also updated the PR description.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Status update (2026-10-10): the two red checks on db07ba073c0 were tests unrelated to this change, and both already have fixes on master:

Merged master in d81412b (no conflicts, clickhouse builds) to pull those fixes in. There are no unresolved review threads.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This looks correct and good enough.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 11, 2026
Merged via the queue into master with commit 9d68563 Oct 11, 2026
164 checks passed
@alexey-milovidov
alexey-milovidov deleted the continue-pr-103705-filter-transform-cancel branch October 11, 2026 02:41
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 11, 2026
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-ClickHouse__ClickHouse that referenced this pull request Nov 12, 2026
… mask

`StringBytesUniqImpl::process` kept a 32-byte `UInt64 mask[4]` on the stack and,
for every row, zeroed it with one 32-byte store, did a load-or-store per byte and
reloaded all 32 bytes for the popcount. On aarch64 the speed of that per-row
store/reload depends on the alignment of `mask`, which is decided by the stack
depth at the call, i.e. by unrelated caller frames: `executeImpl` is identical in
8 arm builds, 4 fast and 4 slow, and the slow ones are exactly those where `mask`
lands 32-byte aligned (depth at entry mod 32 = 16). ClickHouse#124599 (thread pool worker
frame 592 -> 608 bytes) flipped master to fast, ClickHouse#112705
(`ExpressionTransform::transform` 400 -> 480 bytes) flipped it back to slow, so
`string_bytes_functions #0` runs at 0.050 or 0.061 s per arm binary and is
reported `slower` on PRs whose binary draws the other mode than its reference
(21 PRs plus master in 30 days on arm_release, none on amd_release).

Count with a per-call table of the last row number in which each byte value
occurred instead. It is zeroed once per `executeImpl` call and never cleared
between rows, so the only per-row stack traffic left is the 8-byte, 8-aligned
load and store of the table entry per byte. `alignas` on `mask` was rejected:
the 32-byte-aligned placement is the slow one, so it would pin every build to
the slow mode. A register-resident mask was measured 1.2-1.85x slower on x86.

`FunctionStringBytes::executeImpl` now calls `process` on a local `Impl`, and
`StringBytesEntropyImpl::process` becomes a `const` member; entropy's machine
code 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

comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. pr-improvement Pull request with some product 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.

4 participants