prestodb/presto · error · PrestoException

INVALID_FUNCTION_ARGUMENT

INVALID_FUNCTION_ARGUMENT

Error message

GROUP BY does not support lambda expressions, please use GROUP BY # instead

What it means

During AST rewriting of GROUP BY clauses, GroupingElement.rewriteLambdaExpression rejects lambda expressions: GROUP BY in Presto operates on grouping sets/columns and lambdas (as used in functions like transform/filter) are meaningless there. It throws a PrestoException with code INVALID_FUNCTION_ARGUMENT directing users to the GROUP BY # ordinal syntax if they intended positional grouping.

Source

Thrown at presto-parser/src/main/java/com/facebook/presto/sql/tree/GroupingElement.java:46

        super(location);
    }

    public abstract List<Expression> getExpressions();

    @Override
    protected <R, C> R accept(AstVisitor<R, C> visitor, C context)
    {
        return visitor.visitGroupingElement(this, context);
    }

    void validateExpressions(List<Expression> expressions)
    {
        expressions.forEach(expression -> ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<Void>()
        {
            @Override
            public Expression rewriteLambdaExpression(LambdaExpression node, Void context, ExpressionTreeRewriter<Void> treeRewriter)
            {
                throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "GROUP BY does not support lambda expressions, please use GROUP BY # instead");
            }
        }, expression, null));
    }
}

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Group by a column or plain expression, not a lambda
  2. Use GROUP BY # (positional reference) if you meant an ordinal, e.g. GROUP BY 1
  3. Move the lambda-based computation into a subquery/CTE and group by its output column

Example fix

// before
SELECT date_trunc('day', ts) FROM t GROUP BY x -> x; // lambda rejected
// after
SELECT date_trunc('day', ts) AS d FROM t GROUP BY 1;
Defensive patterns

Strategy: try-catch

Validate before calling

// Reject lambdas in GROUP BY clauses before submitting
Pattern LAMBDA_IN_GROUP_BY = Pattern.compile("GROUP\\s+BY\\s+[^;]*->", Pattern.CASE_INSENSITIVE);
if (LAMBDA_IN_GROUP_BY.matcher(sql).find()) {
    throw new IllegalArgumentException("GROUP BY cannot contain lambda expressions");
}

Try / catch

try {
    return sqlParser.createStatement(sql);
} catch (PrestoException e) {
    if (e.getErrorCode().getName().equals("INVALID_FUNCTION_ARGUMENT")
            && e.getMessage().contains("GROUP BY does not support lambda")) {
        throw new QuerySemanticError("Rewrite the GROUP BY without lambdas; use GROUP BY # for ordinals", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Parsing or building a query whose GROUP BY clause contains a lambda expression, e.g. GROUP BY x -> x + 1, which reaches the ExpressionTreeRewriter during parse-tree post-processing.

Common situations: Confusing lambda syntax from SELECT/JOIN expressions with grouping keys, machine-generated GROUP BY clauses that copied an expression list including lambdas, attempting positional grouping with the wrong syntax.

Related errors


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