apache/beam · error · IllegalArgumentException
ApproximateUnique needs a sampleSize >= 16 for an…
Error message
ApproximateUnique needs a sampleSize >= 16 for an estimation error <= 50%. In general, the estimation error is about 2 / sqrt(sampleSize).
What it means
ApproximateUnique.Globally requires sampleSize >= 16 because the HyperLogLog-based estimator's error grows as ~2/sqrt(sampleSize); below 16 the promised error bound is meaningless, so the constructor throws IllegalArgumentException.
Solutions
- Set sampleSize to at least 16 (choose based on 2/sqrt(sampleSize) error tolerance)
- Use the ApproximateUnique.globally(double error) overload to derive sampleSize from a desired error
- Clamp user-provided sample sizes: Math.max(16, sampleSize)
Example fix
// before ApproximateUnique.globally(10) // after ApproximateUnique.globally(16) // or ApproximateUnique.globally(0.05)
Defensive patterns
Strategy: validation
Validate before calling
if (sampleSize < 16) throw new IllegalArgumentException("sampleSize must be >= 16 for ApproximateUnique"); Try / catch
try { ApproximateUnique.globally(n); }
catch (IllegalArgumentException e) { /* clamp and retry: */ p = p.apply(ApproximateUnique.globally(16)); } Prevention
- Never pass sampleSize below 16; document the floor in pipeline config
- Prefer the double-error overload globally(error) which derives a valid sampleSize
- Clamp external/config-driven sample sizes with Math.max(16, value)
When it happens
Trigger: Constructing ApproximateUnique.Globally with a sample size of 15 or less, e.g. via new ApproximateUnique.Globally(8) or ApproximateUnique.globally(n) with n < 16.
Common situations: Developers tune sampleSize down for performance without realizing the minimum, or pass a placeholder 0/1 value while scaffolding a pipeline.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- ApproximateUnique needs an estimation error between 1%…
- Expecting exactly one field, found
- Initial capacity < 0
- Invalid Firestore document name
- Iterable at position
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/eba2cf48cc3a6041.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/ApproximateUnique.java:179
* @param <T> the type of the elements in the input {@code PCollection}
*/
public static final class Globally<T> extends PTransform<PCollection<T>, PCollection<Long>> {
/**
* The number of entries in the statistical sample; the higher this number, the more accurate
* the estimate will be.
*/
private final long sampleSize;
/** The desired maximum estimation error or null if not specified. */
private final @Nullable Double maximumEstimationError;
/**
* @see ApproximateUnique#globally(int)
*/
public Globally(int sampleSize) {
if (sampleSize < 16) {
throw new IllegalArgumentException(
"ApproximateUnique needs a sampleSize "
+ ">= 16 for an estimation error <= 50%. "
+ "In general, the estimation "
+ "error is about 2 / sqrt(sampleSize).");
}
this.sampleSize = sampleSize;
this.maximumEstimationError = null;
}
/**
* @see ApproximateUnique#globally(double)
*/
public Globally(double maximumEstimationError) {
if (maximumEstimationError < 0.01 || maximumEstimationError > 0.5) {
throw new IllegalArgumentException(
"ApproximateUnique needs an " + "estimation error between 1% (0.01) and 50% (0.5).");
}View on GitHub (pinned to 12126d8942)