{"record":{"id":"69838ebc3215ec1c","repo":"prestodb/presto","slug":"invalid-function-argument-69838e","errorCode":"INVALID_FUNCTION_ARGUMENT","errorMessage":"GROUP BY does not support lambda expressions, please use GROUP BY # instead","messagePattern":"GROUP BY does not support lambda expressions, please use GROUP BY # instead","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-parser/src/main/java/com/facebook/presto/sql/tree/GroupingElement.java","lineNumber":46,"sourceCode":"        super(location);\n    }\n\n    public abstract List<Expression> getExpressions();\n\n    @Override\n    protected <R, C> R accept(AstVisitor<R, C> visitor, C context)\n    {\n        return visitor.visitGroupingElement(this, context);\n    }\n\n    void validateExpressions(List<Expression> expressions)\n    {\n        expressions.forEach(expression -> ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<Void>()\n        {\n            @Override\n            public Expression rewriteLambdaExpression(LambdaExpression node, Void context, ExpressionTreeRewriter<Void> treeRewriter)\n            {\n                throw new PrestoException(INVALID_FUNCTION_ARGUMENT, \"GROUP BY does not support lambda expressions, please use GROUP BY # instead\");\n            }\n        }, expression, null));\n    }\n}\n","sourceCodeStart":28,"sourceCodeEnd":51,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-parser/src/main/java/com/facebook/presto/sql/tree/GroupingElement.java#L28-L51","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nSELECT date_trunc('day', ts) FROM t GROUP BY x -> x; // lambda rejected\n// after\nSELECT date_trunc('day', ts) AS d FROM t GROUP BY 1;","handlingStrategy":"try-catch","validationCode":"// Reject lambdas in GROUP BY clauses before submitting\nPattern LAMBDA_IN_GROUP_BY = Pattern.compile(\"GROUP\\\\s+BY\\\\s+[^;]*->\", Pattern.CASE_INSENSITIVE);\nif (LAMBDA_IN_GROUP_BY.matcher(sql).find()) {\n    throw new IllegalArgumentException(\"GROUP BY cannot contain lambda expressions\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    return sqlParser.createStatement(sql);\n} catch (PrestoException e) {\n    if (e.getErrorCode().getName().equals(\"INVALID_FUNCTION_ARGUMENT\")\n            && e.getMessage().contains(\"GROUP BY does not support lambda\")) {\n        throw new QuerySemanticError(\"Rewrite the GROUP BY without lambdas; use GROUP BY # for ordinals\", e);\n    }\n    throw e;\n}","preventionTips":["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"],"tags":["sql","group-by","lambda","presto-exception"],"backgroundTag":"group-by-lambda-unsupported","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}