{"record":{"id":"1643846ef3d6ee93","repo":"apache/cassandra","slug":"aggregate-functions-cannot-be-used-as-arguments-of","errorCode":null,"errorMessage":"aggregate functions cannot be used as arguments of aggregate functions","messagePattern":"aggregate functions cannot be used as arguments of aggregate functions","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/selection/AbstractFunctionSelector.java","lineNumber":153,"sourceCode":"     * with each function call.\n     */\n    private Arguments args;\n    protected final List<Selector> argSelectors;\n\n    @Override\n    public void prepare(FunctionContext context)\n    {\n        args = fun.newArguments(context);\n        for (Selector selector : argSelectors)\n            selector.prepare(context);\n    }\n\n    public static Factory newFactory(final Function fun, final SelectorFactories factories) throws InvalidRequestException\n    {\n        if (fun.isAggregate())\n        {\n            if (factories.doesAggregation())\n                throw new InvalidRequestException(\"aggregate functions cannot be used as arguments of aggregate functions\");\n        }\n\n        return new Factory()\n        {\n            protected String getColumnName()\n            {\n                return fun.columnName(factories.getColumnNames());\n            }\n\n            protected AbstractType<?> getReturnType()\n            {\n                return fun.returnType();\n            }\n\n            protected void addColumnMapping(SelectionColumnMapping mapping, ColumnSpecification resultsColumn)\n            {\n                SelectionColumnMapping tmpMapping = SelectionColumnMapping.newMapping();\n                for (Factory factory : factories)","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/selection/AbstractFunctionSelector.java#L135-L171","documentation":"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.","triggerScenarios":"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.","commonSituations":"SQL developers attempt nested aggregations that would be legal (or at least parsed) in relational databases; Cassandra requires two-step aggregation.","solutions":["Flatten the query to a single aggregate, e.g. SELECT sum(x) instead of sum(count(x)).","Compute the inner aggregate in one query and apply the outer aggregation in application code.","Use a Materialized View or separate table to pre-aggregate, then aggregate over that."],"exampleFix":"// before\nSELECT sum(count(value)) FROM measurements;\n// after\nSELECT sum(value) FROM measurements; // or count(value) first, then sum client-side","handlingStrategy":"validation","validationCode":"Pattern NESTED_AGG = Pattern.compile(\"(?i)(sum|count|avg|min|max)\\\\s*\\\\(\\\\s*(sum|count|avg|min|max)\\\\s*\\\\(\");\nif (NESTED_AGG.matcher(cql).find())\n    throw new IllegalArgumentException(\"nested aggregates are not supported in CQL\");","typeGuard":null,"tryCatchPattern":"try { session.execute(stmt); }\ncatch (InvalidRequestException e) {\n    if (e.getMessage().contains(\"arguments of aggregate functions\")) { /* flatten or split the aggregation */ }\n    else throw e;\n}","preventionTips":["Keep one aggregate level per SELECT in Cassandra.","Do outer aggregation in application code or with a two-step query.","Lint user-supplied CQL for nested aggregate patterns."],"tags":["cassandra","cql","aggregation","selectors"],"backgroundTag":"unsupported-operation","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}