prestodb/presto · error · PrestoException
INVALID_FUNCTION_ARGUMENT
INVALID_FUNCTION_ARGUMENT
Error message
Entropy count argument must be non-negative
What it means
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.
Source
Thrown at presto-main-base/src/main/java/com/facebook/presto/operator/aggregation/EntropyAggregation.java:54
*/
@AggregationFunction("entropy")
@Description("Takes non-negative count inputs, and computes the log-2 entropy of their fractions when normalized to sum to 1.")
public final class EntropyAggregation
{
private EntropyAggregation() {}
/**
* @note If count is negative, the value of the aggregation will be null; if
* count is 0, this is a no-op (since, in the context of entropy, 0 log(0) = 0; if count is null,
* this is a no op.
*/
@InputFunction
public static void input(
@AggregationState EntropyState state,
@SqlType(StandardTypes.BIGINT) long count)
{
if (count < 0) {
throw new PrestoException(
INVALID_FUNCTION_ARGUMENT,
"Entropy count argument must be non-negative");
}
if (count == 0) {
return;
}
state.setSumC(state.getSumC() + count);
state.setSumCLogC(state.getSumCLogC() + count * Math.log(count));
}
@CombineFunction
public static void combine(@AggregationState EntropyState state, @AggregationState EntropyState otherState)
{
state.setSumC(state.getSumC() + otherState.getSumC());
state.setSumCLogC(state.getSumCLogC() + otherState.getSumCLogC());
}
View on GitHub (pinned to 55bb57d202)
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.
Example fix
// before SELECT entropy(cnt) FROM events; // after SELECT entropy(GREATEST(cnt, 0)) FROM events; -- or WHERE cnt >= 0
Defensive patterns
Strategy: validation
Validate before calling
-- ensure count is non-negative before aggregating: SELECT entropy(GREATEST(cnt, 0)) FROM t; -- or: WHERE cnt >= 0
Try / catch
try {
rs = stmt.executeQuery("SELECT entropy(cnt) FROM t");
} catch (SQLException e) {
if (e.getMessage() != null && e.getMessage().contains("Entropy count argument must be non-negative")) {
throw new IllegalArgumentException("entropy() requires cnt >= 0", e);
}
throw e;
} Prevention
- 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()
When it happens
Trigger: 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.
Common situations: Computing entropy over delta/derived columns that can go negative; dirty data with negative counts; sign errors in expressions feeding the aggregate.
Related errors
- INVALID_FUNCTION_ARGUMENT
- INVALID_FUNCTION_ARGUMENT
- INVALID_FUNCTION_ARGUMENT
- INVALID_FUNCTION_ARGUMENT
- INVALID_FUNCTION_ARGUMENT
AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04).
Data as JSON: /api/errors/76ea69028e5e35cf.
Report an issue: GitHub.