prestodb/presto · error · PrestoException
INVALID_ARGUMENTS
INVALID_ARGUMENTS
Error message
k value must satisfy 8 <= k <= %d: %d
What it means
KllSketchWithKAggregationFunction.initializeSketch validates the user-supplied k (sketch accuracy/size trade-off) before creating a KllItemsSketch. The DataSketches KLL implementation requires 8 <= k <= MAX_K; values outside this range make the sketch invalid, so the library throws INVALID_ARGUMENTS.
Source
Thrown at presto-main-base/src/main/java/com/facebook/presto/operator/aggregation/sketch/kll/KllSketchWithKAggregationFunction.java:121
@OutputFunction("kllsketch(T)")
public static void output(@AggregationState KllSketchAggregationState state, BlockBuilder out)
{
if (state.getSketch() == null) {
out.appendNull();
return;
}
VARBINARY.writeSlice(out, Slices.wrappedBuffer(state.getSketch().toByteArray()));
}
@SuppressWarnings({"rawtypes", "unchecked"})
private static void initializeSketch(KllSketchAggregationState state, Type type, long k)
{
if (state.getSketch() != null) {
return;
}
if (k < 8 || k > MAX_K) {
throw new PrestoException(INVALID_ARGUMENTS, format("k value must satisfy 8 <= k <= %d: %d", MAX_K, k));
}
KllSketchAggregationState.SketchParameters parameters = KllSketchAggregationState.getSketchParameters(type);
KllItemsSketch sketch = KllItemsSketch.newHeapInstance((int) k, parameters.getComparator(), parameters.getSerde());
state.setSketch(sketch);
state.setConversion(parameters.getConversion());
state.addMemoryUsage(() -> getEstimatedKllInMemorySize(sketch, state.getType().getJavaType()));
}
}
View on GitHub (pinned to 55bb57d202)
Solutions
- Pass k within 8..MAX_K (clamp: LEAST(200, GREATEST(8, k)))
- Compute k client-side and validate the range before submitting the query
- If higher accuracy is needed, combine multiple sketches rather than exceeding MAX_K
Example fix
// before SELECT kll_sketch_with_k_agg(x, 500) // after SELECT kll_sketch_with_k_agg(x, LEAST(200, GREATEST(8, 500)))
Defensive patterns
Strategy: validation
Validate before calling
boolean validK(long k) { return k >= 8 && k <= 200; } // MAX_K per DataSketches KLL
long clampedK = Math.max(8, Math.min(200, requestedK)); Try / catch
try { run(sql); } catch (PrestoException e) { if (e.getMessage().contains("k value must satisfy")) { clampKAndRetry(); } else throw e; } Prevention
- Clamp k to 8..200 wherever it is computed dynamically
- Document the k range in the aggregation's SQL docs
- Unit-test k-boundary values in query builders
When it happens
Trigger: Calling the k-parameterized KLL sketch aggregation with k < 8 or k > MAX_K (200 in DataSketches), reached from input via initializeSketch when the state has no sketch yet.
Common situations: Users copying k values from other sketch libraries with different bounds; dynamic SQL computing k from a formula that exceeds limits; typos like k=1000.
Related errors
- INVALID_FUNCTION_ARGUMENT
- INVALID_FUNCTION_ARGUMENT
- INVALID_ARGUMENTS
- array1 and array2 cannot be null and should have same length
- arrayOffset is negative
AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04).
Data as JSON: /api/errors/84e9733203b24f25.
Report an issue: GitHub.