Skip to content

Improve canceling queries in DistinctTransform - #108588

Open
rvasin wants to merge 75 commits into
ClickHouse:masterfrom
arenadata:ADQM-4692
Open

rvasin wants to merge 75 commits into
ClickHouse:masterfrom
arenadata:ADQM-4692

Conversation

@rvasin

@rvasin rvasin commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Description

Reduces the cancellation latency of DISTINCT queries that deduplicate with the hash-set DistinctTransform (the fallback used when the input is not sorted). Before this change the transform only observed cancellation between chunks: once it started hashing a large chunk, KILL QUERY (and cancellation in general) had to wait for the whole hash table build of that chunk to finish. Now the transform polls the query cancellation flag inside its row loops and stops within one batch of rows.

Related: #111061

Changelog category (leave one):

  • Improvement

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

Reduced cancellation latency of DISTINCT queries that use the hash-set DistinctTransform: KILL QUERY now interrupts hash table construction within a single chunk instead of finishing the current chunk first.


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

@rvasin

rvasin commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

@tiandiwonder, @scanhex12, @vdimir could you please assign can be tested label to this PR?

Note: in this PR I am using a simple functional test as in other tests for query cancellation:

  • 03984_kill_query_with_fill.sh
  • 03985_kill_query_dict_load.sh
  • 04028_kill_query_expression_cancelled.sh

If needed I may rewrite it into integration test to check as cancellation path.

@scanhex12 scanhex12 added the can be tested Allows running workflows for external contributors label Jun 26, 2026
@scanhex12
scanhex12 requested a review from Copilot June 26, 2026 10:57
@scanhex12 scanhex12 self-assigned this Jun 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [485162b]

Summary: ❌

job_name test_name status info comment
Stress test (arm_asan_ubsan, s3) FAIL
Logical error: Duplicate announcement received for replica number A (STID: 8765-7cc2) FAIL cidb

AI Review

Summary

The PR adds cancellation polling to the hash-based DistinctTransform and its nullable and LowCardinality scans. Request changes: whole-chunk key preparation and mask intersection still escape the promised 4096-row cancellation bound.

Findings

⚠️ Majors

  • src/Processors/Transforms/DistinctSetFilter.cpp:289: Method::State prepares composite fixed-width keys for the entire chunk before the first cancellation poll. For two UInt64 keys, HashMethodKeysFixed calls packFixedBatch over every input row, so cancellation during preparation still waits for whole-chunk work. Construct a ColumnsHashing::SubRangeState per polled batch. Existing finding.
  • src/Processors/Transforms/DistinctSetFilter.cpp:654: a single nullable LowCardinality key with skip_null_keys = true can produce both keep and lc_mask. Even after the LC mask scan observes cancellation, lines 664–667 intersect these masks across the entire chunk before the cancellation return. Cancellation during that intersection is also unpolled. Return immediately after cancelled prepasses and poll the intersection in batches. New finding.
Tests
  • ⚠️ Add focused coverage for cancellation during composite fixed-key preparation; the current numeric test uses only one UInt64 key.
  • ⚠️ Cover cancellation with a single LowCardinality(Nullable(...)) key and skip_null_keys = true, proving that the mask intersection does not scan the remaining chunk. The two-key LC-null case bypasses the single-key mask optimization; the plain LC cases have no keep mask.
Final Verdict

⚠️ Request changes — bound fixed-key preparation and nullable LC mask intersection, with focused regression coverage for both paths.

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

Copilot AI left a comment

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.

Pull request overview

This PR reduces query cancellation latency for the fallback DistinctTransform by adding in-loop cancellation checks, so KILL QUERY / client cancel can interrupt DISTINCT processing without waiting for a full chunk to finish.

Changes:

  • Add cancellation checks inside DistinctTransform hot loops (including LowCardinality optimization path) and at the start of transform.
  • Add a stateless query test that starts a large DISTINCT query and cancels it via KILL QUERY.
  • Add the expected output reference for the new test.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/Processors/Transforms/DistinctTransform.cpp Adds cancellation checks so DISTINCT processing can be interrupted during per-row work within a chunk.
tests/queries/0_stateless/04411_kill_query_distinct_transform.sh Introduces a regression test that launches a heavy DISTINCT query and cancels it.
tests/queries/0_stateless/04411_kill_query_distinct_transform.reference Expected output (OK) for the new test.

@rvasin
rvasin requested a review from scanhex12 July 13, 2026 13:45
@rvasin

rvasin commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

@scanhex12 could you please review the PR again? The failed tests are not related.

Comment thread src/Server/GRPCServer.cpp Resolved
Comment thread src/Server/GRPCServer.cpp Resolved
Comment thread src/Server/GRPCServer.cpp Resolved
Comment thread src/Server/GRPCServer.cpp Resolved
Comment thread src/Server/GRPCServer.cpp Resolved
Comment thread src/Server/GRPCServer.cpp Resolved
@abashkeev abashkeev added the comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. label Sep 4, 2026
else
{
for (size_t i = 0; i < rows; ++i)
for (size_t begin = 0; begin < rows; begin += CANCELLATION_POLL_ROWS)

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.

[P2] Bound fixed-key preparation as well as insertion

For an unsorted DISTINCT over two UInt64 columns, Method::State at line 286 is HashMethodKeysFixed. Its constructor calls packFixedBatch over every row in the chunk before either of these new polling loops is reached. A KILL QUERY received during that allocation/packing pass therefore still waits for the whole chunk, contrary to the stated one-batch cancellation bound. The single-number and LowCardinality test cases do not exercise this preparation path.

Please make preparation interruptible too, for example by constructing ColumnsHashing::SubRangeState for each polled row batch, and add focused coverage for a composite fixed-width key.

if (!can_insert && key_columns_pos.size() == 1)
{
lc_mask = lc_filter.buildMaskIfApplicable(*column_ptrs[0], num_rows);
lc_mask = lc_filter.buildMaskIfApplicable(*column_ptrs[0], num_rows, cancellation_flag);

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.

[P2] Stop cancelled LC prepasses before intersecting full-chunk masks

For a single LowCardinality(Nullable(...)) key with skip_null_keys = true (the preliminary IN set-build path), markLowCardinalityNullRows creates a nonempty keep mask when the chunk contains a NULL. If cancellation is observed during that scan or during buildMaskIfApplicable, the latter can return a nonempty, partially built lc_mask. Execution then still runs the num_rows-iteration mask intersection at lines 664–667 before reaching the cancellation return at line 700. Cancellation during the intersection is likewise only observed after the whole scan. Thus a large chunk still incurs whole-chunk work after cancellation, despite the new 4096-row polling bound.

Return immediately after a prepass observes cancellation, and poll the mask intersection in batches too. Please cover a single nullable LowCardinality key with skip_null_keys: the new two-key LC-null test bypasses the single-key mask optimization, and the plain LC tests have no keep mask.

@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 2026

This branch has not been deployed

No deployments
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 comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. external Author is not a member of the ClickHouse organization pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants