{"record":{"id":"84e9733203b24f25","repo":"prestodb/presto","slug":"invalid-arguments-84e973","errorCode":"INVALID_ARGUMENTS","errorMessage":"k value must satisfy 8 <= k <= %d: %d","messagePattern":"k value must satisfy 8 <= k <= (.+?): (.+?)","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/operator/aggregation/sketch/kll/KllSketchWithKAggregationFunction.java","lineNumber":121,"sourceCode":"    @OutputFunction(\"kllsketch(T)\")\n    public static void output(@AggregationState KllSketchAggregationState state, BlockBuilder out)\n    {\n        if (state.getSketch() == null) {\n            out.appendNull();\n            return;\n        }\n        VARBINARY.writeSlice(out, Slices.wrappedBuffer(state.getSketch().toByteArray()));\n    }\n\n    @SuppressWarnings({\"rawtypes\", \"unchecked\"})\n    private static void initializeSketch(KllSketchAggregationState state, Type type, long k)\n    {\n        if (state.getSketch() != null) {\n            return;\n        }\n\n        if (k < 8 || k > MAX_K) {\n            throw new PrestoException(INVALID_ARGUMENTS, format(\"k value must satisfy 8 <= k <= %d: %d\", MAX_K, k));\n        }\n\n        KllSketchAggregationState.SketchParameters parameters = KllSketchAggregationState.getSketchParameters(type);\n        KllItemsSketch sketch = KllItemsSketch.newHeapInstance((int) k, parameters.getComparator(), parameters.getSerde());\n\n        state.setSketch(sketch);\n        state.setConversion(parameters.getConversion());\n        state.addMemoryUsage(() -> getEstimatedKllInMemorySize(sketch, state.getType().getJavaType()));\n    }\n}\n","sourceCodeStart":103,"sourceCodeEnd":132,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/aggregation/sketch/kll/KllSketchWithKAggregationFunction.java#L103-L132","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nSELECT kll_sketch_with_k_agg(x, 500)\n// after\nSELECT kll_sketch_with_k_agg(x, LEAST(200, GREATEST(8, 500)))","handlingStrategy":"validation","validationCode":"boolean validK(long k) { return k >= 8 && k <= 200; } // MAX_K per DataSketches KLL\nlong clampedK = Math.max(8, Math.min(200, requestedK));","typeGuard":null,"tryCatchPattern":"try { run(sql); } catch (PrestoException e) { if (e.getMessage().contains(\"k value must satisfy\")) { clampKAndRetry(); } else throw e; }","preventionTips":["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"],"tags":["presto","sketch","argument-validation","datasketches"],"backgroundTag":"parameter-out-of-range","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}