prestodb/presto · error · PrestoException

INVALID_FUNCTION_ARGUMENT

INVALID_FUNCTION_ARGUMENT

Error message

second argument of max_n/min_n must be positive

What it means

INVALID_FUNCTION_ARGUMENT thrown by the max_n/min_n aggregation's input function when the second argument n is not positive (n <= 0) on the first invocation that allocates the heap. These aggregations keep a TypedHeap of the n largest/smallest values, so n must be >= 1 and <= MAX_NUMBER_OF_VALUES.

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/operator/aggregation/AbstractMinMaxNAggregationFunction.java:128

        Class<? extends Accumulator> accumulatorClass = AccumulatorCompiler.generateAccumulatorClass(
                Accumulator.class,
                metadata,
                classLoader);
        Class<? extends GroupedAccumulator> groupedAccumulatorClass = AccumulatorCompiler.generateAccumulatorClass(
                GroupedAccumulator.class,
                metadata,
                classLoader);
        return new BuiltInAggregationFunctionImplementation(getSignature().getNameSuffix(), inputTypes, ImmutableList.of(intermediateType), outputType,
                true, false, metadata, accumulatorClass, groupedAccumulatorClass);
    }

    public static void input(BlockComparator comparator, Type type, MinMaxNState state, Block block, long n, int blockIndex)
    {
        TypedHeap heap = state.getTypedHeap();
        if (heap == null) {
            if (n <= 0) {
                throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "second argument of max_n/min_n must be positive");
            }
            checkCondition(n <= MAX_NUMBER_OF_VALUES, INVALID_FUNCTION_ARGUMENT, "second argument of max_n/min_n must be less than or equal to %s; found %s", MAX_NUMBER_OF_VALUES, n);
            heap = new TypedHeap(comparator, type, toIntExact(n));
            state.setTypedHeap(heap);
        }
        else {
            checkCondition(n == heap.getCapacity(), INVALID_FUNCTION_ARGUMENT, "Count argument is not constant: found multiple values [%s, %s]", n, heap.getCapacity());
        }
        long startSize = heap.getEstimatedSize();
        heap.add(block, blockIndex);
        state.addMemoryUsage(heap.getEstimatedSize() - startSize);
    }

    public static void combine(MinMaxNState state, MinMaxNState otherState)
    {
        TypedHeap otherHeap = otherState.getTypedHeap();
        if (otherHeap == null) {
            return;

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Ensure the second argument is a positive constant, e.g. max_n(x, 5) instead of max_n(x, 0).
  2. If n comes from a column, filter or coerce: max_n(x, GREATEST(n, 1)) or WHERE n > 0.
  3. Check that any expression computing n cannot evaluate to 0 or negative (integer division truncation, sign errors).
  4. Wrap with TRY() if you want the offending rows to yield NULL instead of failing the query.

Example fix

// before
SELECT max_n(value, 0) FROM t;
// after
SELECT max_n(value, 1) FROM t; -- or a positive n appropriate to the analysis
Defensive patterns

Strategy: validation

Validate before calling

-- ensure n is a positive constant, or guard a column-derived n:
SELECT max_n(value, GREATEST(n, 1)) FROM t; -- or WHERE n > 0

Try / catch

try {
    rs = stmt.executeQuery("SELECT max_n(v, " + n + ") ...");
} catch (SQLException e) {
    if (e.getMessage() != null && e.getMessage().contains("second argument of max_n/min_n must be positive")) {
        throw new IllegalArgumentException("n must be >= 1, got: " + n, e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling max_n(value, n) or min_n(value, n) where n is a constant or column value <= 0, evaluated on the first row processed for a group. Note n is only validated when the heap is not yet allocated, so per-row variable n only errors on the first non-positive value per group.

Common situations: Passing a negative or zero constant literal; supplying n from a column with zero/negative values; mathematical expressions that evaluate to 0 or negative for some groups.

Related errors


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