{"record":{"id":"76ea69028e5e35cf","repo":"prestodb/presto","slug":"invalid-function-argument-76ea69","errorCode":"INVALID_FUNCTION_ARGUMENT","errorMessage":"Entropy count argument must be non-negative","messagePattern":"Entropy count argument must be non-negative","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/operator/aggregation/EntropyAggregation.java","lineNumber":54,"sourceCode":" */\n@AggregationFunction(\"entropy\")\n@Description(\"Takes non-negative count inputs, and computes the log-2 entropy of their fractions when normalized to sum to 1.\")\npublic final class EntropyAggregation\n{\n    private EntropyAggregation() {}\n\n    /**\n     * @note If count is negative, the value of the aggregation will be null; if\n     * count is 0, this is a no-op (since, in the context of entropy, 0 log(0) = 0; if count is null,\n     * this is a no op.\n     */\n    @InputFunction\n    public static void input(\n            @AggregationState EntropyState state,\n            @SqlType(StandardTypes.BIGINT) long count)\n    {\n        if (count < 0) {\n            throw new PrestoException(\n                    INVALID_FUNCTION_ARGUMENT,\n                    \"Entropy count argument must be non-negative\");\n        }\n\n        if (count == 0) {\n            return;\n        }\n        state.setSumC(state.getSumC() + count);\n        state.setSumCLogC(state.getSumCLogC() + count * Math.log(count));\n    }\n\n    @CombineFunction\n    public static void combine(@AggregationState EntropyState state, @AggregationState EntropyState otherState)\n    {\n        state.setSumC(state.getSumC() + otherState.getSumC());\n        state.setSumCLogC(state.getSumCLogC() + otherState.getSumCLogC());\n    }\n","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/aggregation/EntropyAggregation.java#L36-L72","documentation":"INVALID_FUNCTION_ARGUMENT thrown by the entropy aggregation's input function when the count argument is negative. entropy() treats its BIGINT argument as a count of observations feeding the running entropy computation; a negative count is meaningless and invalid. Zero counts are allowed and short-circuit to a no-op.","triggerScenarios":"Calling entropy(count) where count is a BIGINT value < 0 — from a negative literal, a column holding negative values, or an expression (e.g. a difference or delta) that yields a negative number on some rows.","commonSituations":"Computing entropy over delta/derived columns that can go negative; dirty data with negative counts; sign errors in expressions feeding the aggregate.","solutions":["Filter out negative counts: SELECT entropy(cnt) FROM t WHERE cnt >= 0.","Clamp with GREATEST(cnt, 0) if negative values should be treated as zero.","Fix the upstream expression so counts are guaranteed non-negative.","Use TRY(entropy(...)) only if you want the whole aggregation to degrade to NULL rather than fail."],"exampleFix":"// before\nSELECT entropy(cnt) FROM events;\n// after\nSELECT entropy(GREATEST(cnt, 0)) FROM events; -- or WHERE cnt >= 0","handlingStrategy":"validation","validationCode":"-- ensure count is non-negative before aggregating:\nSELECT entropy(GREATEST(cnt, 0)) FROM t; -- or: WHERE cnt >= 0","typeGuard":null,"tryCatchPattern":"try {\n    rs = stmt.executeQuery(\"SELECT entropy(cnt) FROM t\");\n} catch (SQLException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"Entropy count argument must be non-negative\")) {\n        throw new IllegalArgumentException(\"entropy() requires cnt >= 0\", e);\n    }\n    throw e;\n}","preventionTips":["Validate the count source data contains no negatives before the query","Clamp derived/delta expressions with GREATEST(x, 0)","Prefer WHERE cnt >= 0 filtering when negative rows are invalid data","Add a data-quality check on columns feeding entropy()"],"tags":["aggregation","sql","invalid-argument"],"backgroundTag":"invalid-aggregation-argument","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"}