Skip to content

Improve canceling queries in DistinctSortedStreamTransform - #111061

Open
rvasin wants to merge 15 commits into
ClickHouse:masterfrom
arenadata:ADQM-4787
Open

rvasin wants to merge 15 commits into
ClickHouse:masterfrom
arenadata:ADQM-4787

Conversation

@rvasin

@rvasin rvasin commented Jul 20, 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):

Reduced cancellation latency for DISTINCT queries that use DistinctSortedStreamTransform: KILL QUERY now interrupts hash table lookups every 4096 rows and post-chunk processing instead of waiting for the entire sorted block to complete.


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

@rvasin

rvasin commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

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

@scanhex12 scanhex12 added the can be tested Allows running workflows for external contributors label Jul 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8efd148]

Summary: ✅


AI Review

Summary

Adds cancellation polling to DistinctSortedStreamTransform while hashing, skipping equal-key ranges, and processing chunk continuations. Request changes: both continuation regression tests can pass without reaching the continuation path, leaving that material part of the change unproven.

Missing context / blind spots
  • ⚠️ No local ClickHouse binary/build is available, and the fetched CI reports for head 8efd14844ec7dbc80321cfcd25792aaa25f9b7ac supplied no individual integration-test results. The execution traces below come from the current source; a focused run of the corrected continuation cases would close this validation gap.
Findings

⚠️ Majors

  • tests/integration/test_distinct_sorted_stream_transform_kill_query/test.py:195 — The continuation fixtures still cancel in the first chunk. These small tables use Compact parts with the default 8192-row index granularity. MergeTreeReaderCompact::canReadIncompleteGranules returns false, so MergeTreeRangeReader rounds reads to whole granules despite max_block_size=3000 or 4000. Consequently, all 6000 rows of test_other_cont can arrive in chunk 1 and trigger the hashing pause at row 4096. Likewise, test_cont reaches its outer-loop pause at row 6000 in its first granule. Both expected markers match those first-chunk polls, so the assertions do not establish that continueWithPrevRange ran. This is the still-present existing continuation-coverage finding, reopened rather than duplicated.

    Control the actual chunk boundaries, for example by forcing Wide parts with min_bytes_for_wide_part=0, and verify that cancellation reaches the carried-over prefix. Update the expected markers at lines 156 and 212 accordingly: with the intended block sizes, the first pauses are the continuation checks at DistinctSortedStreamTransform.cpp:191 and :202, which emit Cancelled at the carried-over prefix poll and Cancelled while hashing the carried-over prefix, respectively.

Final Verdict

Status: ⚠️ Request changes. Make both continuation fixtures reach a second chunk deterministically, assert the corresponding continuation markers, and run those focused cases successfully.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 20, 2026
@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: 52/59 (88.14%) · Uncovered code

Full report · Diff report

Comment on lines +140 to +145
# Runs of 3000: chunk boundary at row 9999 falls within k=3 (rows 9000-11999).
# Chunk 2 continuation sets range_begin=2000. The first post-continuation
# run (k=4, rows 12000-14999) crosses i=4096 inside buildFilterForRange.
# Without the fix, the outer throttled check does not fire
# (range_begin - last_checked_row = 3000 < 4096) and subsequent runs
# (k=5, k=6) process hash table work after cancellation.

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 regression still does not reach the second-chunk continueWithPrevRange scenario described in the comment above. With max_block_size = 10000, chunk 1 already contains runs [0, 3000), [3000, 6000), [6000, 9000), [9000, 10000), and buildFilterForRange hits distinct_sorted_stream_transform_pause at i = 4096 inside the second run. Because that failpoint is PAUSEABLE_ONCE, SYSTEM WAIT FAILPOINT ... PAUSE unblocks there and KILL QUERY lands before chunk 2 starts, so this test never exercises the "cancellation observed inside one continued run, but later runs still hash" bug.

Please either keep the first chunk below the first i & 0xFFF == 0 poll, or use a separate failpoint / assertion that proves the query actually reached the intended second-chunk path before it was killed.

@clickhouse-gh clickhouse-gh Bot added the comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. label Sep 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 8efd14844 with master 5d779130d (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of ea91d0f12; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

4 object files changed (+86.77 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/Transforms/DistinctSortedStreamTransform.cpp.o 249.43 KiB 334.92 KiB +85.48 KiB (+34.27%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

@rvasin

rvasin commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

@tiandiwonder, @scanhex12, @shankar-iyer, @george-larionov I have restarted working on this pull request after some time and I have implemented changed suggested b t clichouse-gh in July-August 2026. Today I have my changes but clickhouse-gh did not review it. Is it possible to trigger clickhouse-gh to review this pull request again?

@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.

3 participants