Skip to content

Revert some changes of "Revert "Revert "New functions isPrime and isProbablePrime""" - #104806

Merged
nihalzp merged 4 commits into
ClickHouse:masterfrom
nihalzp:cleanup-is-prime
May 14, 2026
Merged

nihalzp merged 4 commits into
ClickHouse:masterfrom
nihalzp:cleanup-is-prime

Conversation

@nihalzp

@nihalzp nihalzp commented May 13, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Reverts some changes of #104633 and #104639. Both tried to fix the problem of query cancellation but had too defensive checks. Only limiting rounds to 256 is enough. See #104639 (comment).

Version info

  • Merged into: 26.5.1.657

@clickhouse-gh

clickhouse-gh Bot commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ced35d0]

Summary: ✅


AI Review

Summary

This PR reverts the custom wide-integer Miller-Rabin implementation in Primality::isProbablePrime and restores boost::multiprecision::miller_rabin_test while keeping a hard cap of 256 rounds. The current code still has one live operational issue: cancellation responsiveness for expensive UInt128/UInt256 evaluation was removed.

Findings

⚠️ Majors

  • [src/Functions/isProbablePrime.cpp:90] The executeImpl row loop now runs wide-value Primality::isProbablePrime with no periodic checkTimeLimit in the loop and no cancellation probe inside the callee. As a result, KILL QUERY and max_execution_time can be delayed until the full block finishes when evaluating large blocks of expensive wide integers.
    Suggested fix: keep the revert to boost::multiprecision::miller_rabin_test, but restore a coarse cancellation probe (for example every N rows) in this loop.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    1. Add periodic cancellation checking for the UInt128/UInt256 execution path (coarse-grained is sufficient).

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label May 13, 2026
Comment thread src/Functions/isProbablePrime.cpp Resolved
Comment thread src/Functions/isProbablePrime.cpp Resolved
@nihalzp

nihalzp commented May 13, 2026

Copy link
Copy Markdown
Member Author

@groeneai 03823_optimize_dry_run_projections failure is unrelated. But why it fails?

@groeneai

Copy link
Copy Markdown
Collaborator

Hey @nihalzp — confirmed unrelated, here's why:

The failure is a transient 180s timeout on SYSTEM FLUSH LOGS query_log, not a real test failure. The runner even auto-reran with the same randomized settings and all 3 reruns passed:

--- Randomized settings diagnosis ---
Step 1: Re-running with the same randomized settings (budget: 60s)...
  Runs: 3, Failed: 0, Passed: 3, Other: 0
Result: All reruns passed. The failure is not reproducible (likely a transient issue).

The actual test logic completed — the four "parts before/after dry run" assertion blocks all match the reference. The failure is at line 27:

SYSTEM FLUSH LOGS query_log;   -- query 16, line 27 — timed out after 180s

Code: 159. DB::Exception: Timeout exceeded (180 s) while flushing system log 'DB::SystemLogQueue<DB::QueryLogElement>'

Cross-PR scope (CIDB, 30 days): Zero master failures. Three PRs hit it:

Context — this is the same WasmEdge-timeout pattern Algunenano just flagged in #104866 (04039_merge_tree_snapshot_teardown_race), #104873 (00945_bloom_filter_index), and #104874 (01721_join_implicit_cast_long_oldanalyzer) — all Stateless tests (amd_msan, WasmEdge, parallel) timeouts. MSAN + WasmEdge is the slowest combination in CI; tests that do SYSTEM FLUSH LOGS near a busy query_log occasionally bump the 180s log-flush timeout.

Your PR is purely about reverting defensive checks on isPrime/isProbablePrime — there's no plausible interaction with MergeTree projection dry-run, system logs, or WasmEdge. Safe to ignore; just retry the failed job.

@clickhouse-gh

clickhouse-gh Bot commented May 14, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 76.60% 76.50% -0.10%

Changed lines: 81.82% (36/44) | lost baseline coverage: 7 line(s) · Uncovered code

Full report · Diff report

@nihalzp
nihalzp added this pull request to the merge queue May 14, 2026
Merged via the queue into ClickHouse:master with commit 0144a49 May 14, 2026
329 of 330 checks passed
@nihalzp
nihalzp deleted the cleanup-is-prime branch May 14, 2026 19:09
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 14, 2026
pull Bot pushed a commit to peijunlin2008/ClickHouse that referenced this pull request Aug 17, 2026
isProbablePrime on UInt128/UInt256 evaluated a whole block of rows inside one
executeImpl call with no cancellation point. The pipeline samples cancellation
only between blocks, so neither max_execution_time nor KILL QUERY could
interrupt it, and each wide row costs milliseconds in
boost::multiprecision::miller_rabin_test.

Measured on master f9801d5, 10000 UInt256 rows at rounds = 64 against
max_execution_time = 1:

  Code: 159. Timeout exceeded: elapsed 203157.095 ms, maximum: 1000 ms.

203x past its own deadline; the UInt128 carrier measured 382467 ms against the
same deadline. Observed in CI as a Stress test (amd_msan) hung check with one
query at elapsed = 1796 s, is_cancelled = 1 and max_execution_time = 10.

The pre-existing rounds <= 256 cap already bounds a single call, so it does not
help here: what was unbounded is the loop over the block. That is also why
restoring the cancellable Miller-Rabin loop reverted in 736c7ee would not
have fixed this - it would have added a checkpoint inside an already-bounded
82 ms call. Boost is retained, in line with the reasoning recorded on ClickHouse#104806.

Primality.h takes an optional check_cancellation callback, invoked once per call
just before the Miller-Rabin rounds, i.e. after the three cheap exits. The
callback is opaque there, so Common stays free of ProcessList - the same split
Base58.h already uses. isProbablePrime.cpp owns the query-status side via
makeCancellationCheck and additionally charges a CancellationBudget per wide
row, which bounds a block made only of cheap-exit rows (measured 0.04-0.26 us
each) without making those rows pay for a check.

Two mutation builds confirm both halves are load-bearing: with the callback
removed the rounds cases overshoot again (203846 ms), and with the budget
removed the cheap-exit case overshoots (10149 ms).

The narrow path and the cheap exits are untouched and unslowed: 1M rows
measured 3766 -> 3632 ms and 373 -> 360 ms, both within noise.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog 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