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
- Group by a column or plain expression, not a lambda
- Use GROUP BY # (positional reference) if you meant an ordinal, e.g. GROUP BY 1
- 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
- Never put lambda expressions in GROUP BY; compute them in a subquery first
- Use GROUP BY <ordinal> (e.g. GROUP BY 1) for positional grouping
- Validate generated GROUP BY key lists against an allowlist of column refs
- Document lambda-allowed contexts (transform/filter in SELECT/JOIN only) for query generators
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
- MUST_BE_AGGREGATE_OR_GROUP_BY
- INVALID_PROCEDURE_ARGUMENTS
- TOO_MANY_GROUPING_SETS
- INVALID_ORDINAL
- MUST_BE_COLUMN_REFERENCE
AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04).
Data as JSON: /api/errors/69838ebc3215ec1c.
Report an issue: GitHub.