New functions `isPrime` and `isProbablePrime` by nihalzp · Pull Request #104234 · ClickHouse/ClickHouse · GitHub
Skip to content

New functions isPrime and isProbablePrime#104234

Merged
nihalzp merged 7 commits into
ClickHouse:masterfrom
nihalzp:functions-is-prime
May 11, 2026
Merged

New functions isPrime and isProbablePrime#104234
nihalzp merged 7 commits into
ClickHouse:masterfrom
nihalzp:functions-is-prime

Conversation

@nihalzp

@nihalzp nihalzp commented May 6, 2026

Copy link
Copy Markdown
Member

isPrime(n) returns 1 if n is prime and 0 otherwise. It supports unsigned integer types up to UInt64 and always returns an exact result.

Examples:

SELECT isPrime(17); -- 1
SELECT isPrime(18); -- 0
SELECT isPrime(toUInt64(18446744073709551557)); -- 1

isProbablePrime(n[, rounds]) (inspired by Java) returns 0 if n is definitely composite and 1 if n is probably prime. It gives exact results for unsigned integer types up to UInt64, and also supports probabilistic checks for UInt128 and UInt256. The optional rounds argument controls the confidence for wider integers.

Examples:

SELECT isProbablePrime(17); -- 1
SELECT isProbablePrime(18); -- 0
SELECT isProbablePrime(toUInt128('170141183460469231731687303715884105727')); -- 1
SELECT isProbablePrime(toUInt256('57896044618658097711785492504343953926634992332820282019728792003956564819949'), 5); -- 1

Changelog category (leave one):

  • New Feature

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

New functions isPrime and isProbablePrime for primality checks. isPrime returns an exact result for unsigned integers up to UInt64. isProbablePrime also supports UInt128 and UInt256; for those wider types, 0 means definitely composite and 1 means probably prime. The optional second argument of isProbablePrime, rounds, controls the confidence, with the default of 25 rounds bounding the false-positive rate for a random composite below 10^-15.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.510

@clickhouse-gh

clickhouse-gh Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label May 6, 2026
@antaljanosbenjamin antaljanosbenjamin self-assigned this May 7, 2026
Comment thread src/Common/Primality.h Outdated
}

template <typename T>
UInt8 isProbablePrimeWide(const T & num, unsigned rounds)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here it doesn't make a big difference, but when you want to have separate overloads of the same function for multiple types, you can use concepts that utilizes overload resolution. The only reasonable place I see it useful is instead of doing the manual dispatch in isProbablyPrime for wide vs other integers. However it is not so important, I just thought let's mention it, maybe you haven't thought about it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing it out! I have improved it.

@clickhouse-gh

clickhouse-gh Bot commented May 10, 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.60% 76.60% +0.00%

Changed lines: 93.06% (161/173) · Uncovered code

Full report · Diff report

@nihalzp

nihalzp commented May 11, 2026

Copy link
Copy Markdown
Member Author

@groeneai Investigate the CI failures.

@groeneai

Copy link
Copy Markdown
Contributor

@nihalzp Both failures are pre-existing flaky tests, not caused by this PR. Details:

01710_projection_additional_filtersStateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel)

Chronic master regression introduced ~2026-05-05.

Metric Value
Master hits in last 30 days 14 (every day 5/5–5/10)
Unrelated PRs affected 30+ (e.g. #102351, #96978, #96130, #104475, #104228, #100146, …)
Tracking Issue #104219 (labeled flaky test, assigned @azat)
Active fix PR #104296 by @azat"Fix trivial count optimization with additional_table_filters with parallel replicas and analyzer" — OPEN, MERGEABLE

The failure is exclusively on amd_llvm_coverage, ParallelReplicas, s3 storage — a combination this PR doesn't touch. Root cause is in the analyzer's trivial count optimization interacting with parallel replicas + additional_table_filters.

04103_user_network_bandwidth_throttlerStateless tests (amd_tsan, parallel, 2/2)

Known flaky test, CI's own diagnosis says "not reproducible":

--- Randomized settings diagnosis ---
Step 1: Re-running with the same randomized settings (budget: 60s)...
  Runs: 5, Failed: 0, Passed: 5, Other: 0
Result: All reruns passed. The failure is not reproducible (likely a transient issue).
Metric Value
Master hits in last 30 days 5 (4/22, 4/24, 4/27, 5/2, 5/6) across amd_tsan / amd_msan / s3-storage variants
Unrelated PRs affected 30+
Tracking Issue #103422 (auto-generated, assigned @tiandiwonder)
Origin Introduced by PR #103080 (April 2026), 59 failures during its own CI

The diff is a single counter flip: read 1 1 1read 1 1 0 — a race in the read-network-throttler test that doesn't depend on the new function code.

Summary

Neither failure touches src/Common/Primality.h, src/Functions/isPrime*.cpp, or the new test files. PR is functionally clean — your code already has @antaljanosbenjamin's approval. The two red checks here are infrastructure debt being tracked separately and will clear once #104296 merges.

Nothing to do on your end; safe to merge from a correctness standpoint.

@groeneai (session cron:clickhouse-ci-task-worker:20260511-051500)

@nihalzp

nihalzp commented May 11, 2026

Copy link
Copy Markdown
Member Author

@nihalzp nihalzp added this pull request to the merge queue May 11, 2026
Merged via the queue into ClickHouse:master with commit 144a47e May 11, 2026
162 of 165 checks passed
@nihalzp nihalzp deleted the functions-is-prime branch May 11, 2026 09:17
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 11, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

@nihalzp, this was merged with red CI, which we never do. If the CI is red, stop working on features and work on CI instead.

@alexey-milovidov

Copy link
Copy Markdown
Member

If we paid attention to the CI, we had a chance to find this #104633 before merging.

@nihalzp

nihalzp commented May 12, 2026

Copy link
Copy Markdown
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature Pull request with new product feature 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.

5 participants