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

  1. Pass k within 8..MAX_K (clamp: LEAST(200, GREATEST(8, k)))
  2. Compute k client-side and validate the range before submitting the query
  3. 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

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


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/84e9733203b24f25. Report an issue: GitHub.