apache/cassandra · error · InvalidRequestException

aggregate functions cannot be used as arguments of…

Error message

aggregate functions cannot be used as arguments of aggregate functions

What it means

Aggregate functions (sum, count, avg, ...) cannot be nested inside other aggregate functions in a SELECT. newFactory for an aggregate Function checks the argument selector factories and throws if any argument is itself an aggregation, since Cassandra's selection layer has no nested-aggregation semantics.

Solutions

  1. Flatten the query to a single aggregate, e.g. SELECT sum(x) instead of sum(count(x)).
  2. Compute the inner aggregate in one query and apply the outer aggregation in application code.
  3. Use a Materialized View or separate table to pre-aggregate, then aggregate over that.

Example fix

// before
SELECT sum(count(value)) FROM measurements;
// after
SELECT sum(value) FROM measurements; // or count(value) first, then sum client-side
Defensive patterns

Strategy: validation

Validate before calling

Pattern NESTED_AGG = Pattern.compile("(?i)(sum|count|avg|min|max)\\s*\\(\\s*(sum|count|avg|min|max)\\s*\\(");
if (NESTED_AGG.matcher(cql).find())
    throw new IllegalArgumentException("nested aggregates are not supported in CQL");

Try / catch

try { session.execute(stmt); }
catch (InvalidRequestException e) {
    if (e.getMessage().contains("arguments of aggregate functions")) { /* flatten or split the aggregation */ }
    else throw e;
}

Prevention

When it happens

Trigger: A CQL query like SELECT sum(count(x)) FROM t or SELECT avg(sum(col)) — building an AbstractFunctionSelector for an aggregate whose arguments include another aggregate selector.

Common situations: SQL developers attempt nested aggregations that would be legal (or at least parsed) in relational databases; Cassandra requires two-step aggregation.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/1643846ef3d6ee93. Report an issue: GitHub.

Appendix: source

Thrown at src/java/org/apache/cassandra/cql3/selection/AbstractFunctionSelector.java:153

     * with each function call.
     */
    private Arguments args;
    protected final List<Selector> argSelectors;

    @Override
    public void prepare(FunctionContext context)
    {
        args = fun.newArguments(context);
        for (Selector selector : argSelectors)
            selector.prepare(context);
    }

    public static Factory newFactory(final Function fun, final SelectorFactories factories) throws InvalidRequestException
    {
        if (fun.isAggregate())
        {
            if (factories.doesAggregation())
                throw new InvalidRequestException("aggregate functions cannot be used as arguments of aggregate functions");
        }

        return new Factory()
        {
            protected String getColumnName()
            {
                return fun.columnName(factories.getColumnNames());
            }

            protected AbstractType<?> getReturnType()
            {
                return fun.returnType();
            }

            protected void addColumnMapping(SelectionColumnMapping mapping, ColumnSpecification resultsColumn)
            {
                SelectionColumnMapping tmpMapping = SelectionColumnMapping.newMapping();
                for (Factory factory : factories)

View on GitHub (pinned to 88fd0f6a0e)