Skip to content

Revert "Revert "New functions isPrime and isProbablePrime"" - #104639

Merged
alexey-milovidov merged 6 commits into
masterfrom
revert-104638-revert-104234-functions-is-prime
May 13, 2026
Merged

alexey-milovidov merged 6 commits into
masterfrom
revert-104638-revert-104234-functions-is-prime

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented May 11, 2026 •

Copy link
Copy Markdown
Member

Reverts #104638

Need to include changes from #104633 into this PR.

Version info

  • Merged into: 26.5.1.588

@alexey-milovidov
alexey-milovidov requested a review from nihalzp May 11, 2026 21:11
@clickhouse-gh

clickhouse-gh Bot commented May 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b00776d]

Summary: ✅


AI Review

Summary

This PR reintroduces isPrime and isProbablePrime, including deterministic wide-integer Miller-Rabin checks, bounded rounds, and stress-test constraints to avoid long-running sanitizer executions. I did not find unresolved correctness, safety, or coverage issues in the current diff, and the earlier concern about the 4^(-rounds) wording under deterministic witness seeding is addressed in the updated documentation and comments.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label May 11, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

@nihalzp, tests have failed. Please fix 03822_variant_distinct_filter and FunctionsStress.

alexey-milovidov and others added 3 commits May 12, 2026 01:22
`isProbablePrime` on `UInt128`/`UInt256` delegated to `boost::multiprecision::miller_rabin_test`, which has no
cancellation hook. Under heavy sanitizer + ThreadFuzzer instrumentation, a single round can stretch into
minutes; combined with the server-side AST fuzzer mutating the `rounds` literal, the function hangs the
stress test's `hung_check` for the full 30-minute window.

This commit:
- Adapts Boost's Miller-Rabin loop locally and calls `QueryStatus::checkTimeLimit` between rounds, so
  `KILL QUERY` and `max_execution_time` both interrupt the function.
- Rejects `rounds > 256` with `BAD_ARGUMENTS`. `4^-256 < 10^-150` already, so larger values add no
  meaningful confidence and only widen the worst-case runtime under the AST fuzzer.

See CI report on the AST-fuzzer-re-enablement PR:
#101354
https://s3.amazonaws.com/clickhouse-test-reports/PRs/101354/f0038d26f27dc2d179136ae371e632746bab3c30/stress_test_arm_tsan/hung_check.log

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
A single `Primality::isProbablePrime` call on a wide `UInt128`/`UInt256`
input runs many Miller-Rabin rounds; under the ASan+UBSan instrumentation
of `function_prop_fuzzer`, this can take longer than the test's 30 second
stop deadline, and the worker is then reported as stuck. The cancellation
hook added in #104633 cannot help here: the stress test has no
`QueryStatus`, so `check_cancelled` is a no-op.

Add a hard constraint that caps the first argument at 1000, forcing the
function through its fast bitmap / UInt64 path on every iteration. The
function is still exercised on all supported input types - just on small
values.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104639&sha=708387ba14ed6c25931b4d202c9198fc9d6cb223&name_0=PR&name_1=Unit%20tests%20%28asan_ubsan%2C%20function_prop_fuzzer%29
PR: #104639

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Comment thread src/Functions/isProbablePrime.cpp Resolved
@nihalzp nihalzp self-assigned this May 12, 2026
@nihalzp

nihalzp commented May 12, 2026

Copy link
Copy Markdown
Member

Thanks! Limiting max_rounds to 256 should fix the issue since for a fixed round and integer_type, number of operations is bounded. There is no need to do manual probabilistic prime testing; Boost version is clearly superior as it is well-tested and optimized.

I will edit on this PR to make the changes.

The textbook Miller-Rabin bound assumes uniformly random independent
witnesses. `isProbablePrime` seeds the witness RNG deterministically from
the input so the same `(n, rounds)` pair reproduces the same result, which
means a composite that passes the specific witness sequence will return
`1` on every call rather than failing independently with probability
`4^(-rounds)`. Soften the documentation and add a code comment to reflect
this — the bound now describes typical accuracy across inputs, not a
per-call probability for a fixed input.
@clickhouse-gh

clickhouse-gh Bot commented May 12, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 90.99% (202/222) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 13, 2026
Merged via the queue into master with commit 8203940 May 13, 2026
485 of 487 checks passed
@alexey-milovidov
alexey-milovidov deleted the revert-104638-revert-104234-functions-is-prime branch May 13, 2026 01:43
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 13, 2026
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.

3 participants