Skip to content

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

Closed
rvasin wants to merge 23 commits into
ClickHouse:masterfrom
arenadata:ADQM-4457
Closed

rvasin wants to merge 23 commits into
ClickHouse:masterfrom
arenadata:ADQM-4457

Conversation

@rvasin

@rvasin rvasin commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Improvement

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

Improve canceling queries in nested expression functions in FilterTransform by KILL QUERY and cancel query (Ctrl+C) in clickhouse-client.

@rvasin

rvasin commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

Could someone please assign can be tested label?

@rvasin

rvasin commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

@Algunenano @murphy-4o @vdimir please assign can be tested label.

@vdimir vdimir added the can be tested Allows running workflows for external contributors label May 26, 2026
@vdimir vdimir self-assigned this May 26, 2026
@rvasin

rvasin commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

@vdimir over a week ago the PR was approved but not merged. Why? Do you have any questions/suggestions about this PR? Let me know and I will implement it.

@vdimir

vdimir commented Jun 5, 2026

Copy link
Copy Markdown
Member

Could you merge master?

@rvasin

rvasin commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

@vdimir I have merged the master as requested.

@clickhouse-gh

clickhouse-gh Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cc974a0]

Summary: ❌

job_name test_name status info comment
Integration tests (arm_binary, distributed plan, 2/4) FAIL
test_storage_s3_queue/test_system_stop.py::test_stopped_table_releases_hash_ring_slot FAIL cidb
test_storage_s3_queue/test_system_stop.py::test_refresh_survives_unready_dependencies FAIL cidb
Upgrade check (amd_release) FAIL
Changed settings are not reflected in the settings changes history (see changed_settings.txt) FAIL cidb
New settings are not reflected in settings changes history (see new_settings.txt) FAIL cidb
Error message in clickhouse-server.log (see upgrade_error_messages.txt) FAIL cidb
AST fuzzer (amd_debug, targeted) ERROR
AST fuzzer (amd_debug, targeted, old_compatibility) ERROR

AI Review

Summary

This PR makes FilterTransform cancellation more responsive by forwarding cancelExecution into filter functions, bailing out before QueryConditionCache bookkeeping when a filter query is cancelled, and adding targeted regressions for WHERE / WASM / cache behavior. The WHERE path looks materially better, but two sibling filter implementations still bypass the new cancellation contract, so I would request changes before merging.

Findings

⚠️ Majors

  • [src/Processors/Transforms/FilterTransform.h:37] FilterSortedStreamByRange embeds its own FilterTransform helper and never forwards pipeline cancellation to it, so the new isCancelled() / cancelExecution logic is bypassed on the range-filter path used by PartsSplitter. Long-running functions inside those border predicates can still ignore KILL QUERY and clickhouse-client cancellation.
    Suggested fix: override FilterSortedStreamByRange::onCancel and call filter_transform.cancel(...) or otherwise share the cancellation state with the inner helper.
  • [src/Processors/Transforms/FilterTransform.cpp:199] HAVING still runs its predicate through TotalsHavingTransform::expression->execute without a cancellation callback or function-level cancelExecution, at TotalsHavingTransform.cpp:196 and :301. Queries with deep nested functions or interruptible UDFs in HAVING therefore remain unkillable inside one execute call even though the WHERE path is fixed.
    Suggested fix: mirror the FilterTransform / ExpressionTransform cancellation handling in TotalsHavingTransform, and stop before totals / filter bookkeeping when the execute call returns on cancellation.
Final Verdict
  • Status: ⚠️ Request changes

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jun 5, 2026
@rvasin

rvasin commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

@vdimir please advice what should I do with this PR? I may merge master again.

@clickhouse-gh

clickhouse-gh Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Dear @vdimir, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@alexey-milovidov

Copy link
Copy Markdown
Member

@rvasin, try to resolve the remaining review comments: #103705 (comment)


void transform(Chunk & chunk) override;

void onCancel() noexcept override;

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.

This only fixes processors whose executor cancels a real FilterTransform. FilterSortedStreamByRange keeps a separate FilterTransform member and calls filter_transform.transform(chunk) directly at src/Processors/Transforms/FilterSortedStreamByRange.cpp:27 and :41, but the pipeline only ever calls cancel() / onCancel() on the outer FilterSortedStreamByRange. That leaves the inner helper's isCancelled() flag false, so its new check_cancelled callback and cancelExecution forwarding never fire. Queries that go through the range-filter path from src/Processors/QueryPlan/PartsSplitter.cpp:1235 therefore still cannot interrupt a long-running function inside the filter expression.

Please forward cancellation into filter_transform from FilterSortedStreamByRange::onCancel (or otherwise share the cancellation state) before relying on this fix there.


if (expression)
expression->execute(block, num_rows_before_filtration);
expression->execute(block, num_rows_before_filtration, false, false, [this]() { return isCancelled(); });

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.

This closes the WHERE path, but HAVING still evaluates its predicate in TotalsHavingTransform, which calls expression->execute(finalized_block, num_rows) and expression->execute(block, num_rows) at src/Processors/Transforms/TotalsHavingTransform.cpp:196 and :301 without a cancellation callback or any onCancel forwarding. A long-running UDF or deep nested function in HAVING therefore remains uninterruptible by KILL QUERY or clickhouse-client cancellation, so the nested-expression cancellation hole is still open on the sibling filter stage.

Please mirror the FilterTransform changes there too, and make the cancelled execute path return before the totals / filter bookkeeping continues.

@clickhouse-gh

clickhouse-gh Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.20% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 78.30% 78.30% +0.00%

Changed lines: Changed C/C++ lines covered: 33/34 (97.06%) · Uncovered code

Full report · Diff report

@alexey-milovidov

Copy link
Copy Markdown
Member

Thank you for this improvement, @rvasin! The branch had merge conflicts with master and two unresolved review threads, but maintainer edits are disabled on the fork, so the resolution could not be pushed here. Superseded by #112705, which preserves your commits and authorship, resolves the conflicts, addresses the remaining review feedback (FilterSortedStreamByRange cancellation forwarding and TotalsHavingTransform HAVING cancellation), and adds a test for the HAVING path. Please review the follow-up there.

@rvasin

rvasin commented Aug 1, 2026

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov I am on vocations this week. I will restart working on Monday 3rd August on other tasks then. As for this task the merge conflicts were due to merging of my another pool request for LimitByTransform. In this pull request, it was an interesting finding about clickhouse-gh - at this stage/point it had no questions about FilterTranform C++ code and tests, but it asked to implement cancellation improvements in two additional processors FilterSortedStreamByRange and TotalsHavingTransform. While suggestion is completely logical and correct I though we need to keep moving by small increments/steps and implement improvement of one processor in one pull requests. Because if we continue to follow to clickhouse-gh then we implement C++ improvement of FilterSortedStreamByRange and TotalsHavingTransform then it will ask to add tests for it and so on. I was afraid that we may never finish. I thought about asking ClickHouse team about it (should I follow all clickhouse-gh suggestions)? But then I have went to the vocation.

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-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants