{"record":{"id":"c587f358be0f701e","repo":"hibernate/hibernate-orm","slug":"function-s-has-d-parameters-but-d-arguments","errorCode":null,"errorMessage":"Function %s() has %d parameters, but %d arguments given","messagePattern":"Function (.+?)\\(\\) has (.+?) parameters, but (.+?) arguments given","errorType":"exception","errorClass":"FunctionArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/dialect/function/AvgFunction.java","lineNumber":160,"sourceCode":"\t\t}\n\t}\n\n\t@Override\n\tpublic String getArgumentListSignature() {\n\t\treturn \"(NUMERIC arg)\";\n\t}\n\n\tpublic static class Validator implements ArgumentsValidator {\n\n\t\tpublic static final ArgumentsValidator INSTANCE = new Validator();\n\n\t\t@Override\n\t\tpublic void validate(\n\t\t\t\tList<? extends SqmTypedNode<?>> arguments,\n\t\t\t\tString functionName,\n\t\t\t\tBindingContext bindingContext) {\n\t\t\tif ( arguments.size() != 1 ) {\n\t\t\t\tthrow new FunctionArgumentException(\n\t\t\t\t\t\tString.format(\n\t\t\t\t\t\t\t\tLocale.ROOT,\n\t\t\t\t\t\t\t\t\"Function %s() has %d parameters, but %d arguments given\",\n\t\t\t\t\t\t\t\tfunctionName,\n\t\t\t\t\t\t\t\t1,\n\t\t\t\t\t\t\t\targuments.size()\n\t\t\t\t\t\t)\n\t\t\t\t);\n\t\t\t}\n\t\t\tfinal var expressible = arguments.get( 0 ).getExpressible();\n\t\t\tif ( expressible != null ) {\n\t\t\t\tfinal var domainType = expressible.getSqmType();\n\t\t\t\tif ( domainType != null ) {\n\t\t\t\t\tfinal var jdbcType = getJdbcType( domainType, bindingContext.getTypeConfiguration() );\n\t\t\t\t\tif ( !isNumeric( jdbcType ) ) {\n\t\t\t\t\t\tthrow new FunctionArgumentException(\n\t\t\t\t\t\t\t\tString.format(\n\t\t\t\t\t\t\t\t\t\t\"Parameter %d of function '%s()' has type '%s', but argument is of type '%s'\",","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/dialect/function/AvgFunction.java#L142-L178","documentation":"AvgFunction's built-in ArgumentsValidator rejects any avg() invocation whose argument count is not exactly 1. HQL/JPQL avg() is defined over a single numeric expression, so avg() with zero or 2+ arguments fails during query compilation with a FunctionArgumentException before any SQL is generated.","triggerScenarios":"Compiling an HQL/JPQL/criteria query containing avg() with the wrong arity, e.g. 'select avg(e.salary, e.bonus) from Employee e' or 'select avg() ...'; also happens when a CriteriaBuilder avg() call is built with the wrong parameter by accident.","commonSituations":"Typos or copy-paste in JPQL aggregates; frameworks that build dynamic HQL and concatenate an expression list into avg(); migrating raw SQL like AVG(a) OVER (PARTITION BY b) to HQL and keeping extra arguments.","solutions":["Pass exactly one numeric expression to avg(): 'select avg(e.salary) from Employee e'","For multi-argument needs, compute (a+b)/2 yourself or use avg over distinct subqueries instead of stuffing arguments into avg()","If you translated from native SQL with OVER clauses, drop the extra args and rely on HQL aggregate semantics or native queries"],"exampleFix":"// before\nList<Double> r = session.createQuery(\"select avg(e.salary, e.bonus) from Employee e\", Double.class).list();\n\n// after\nList<Double> r = session.createQuery(\"select avg(e.salary + e.bonus) from Employee e\", Double.class).list();","handlingStrategy":"validation","validationCode":"// wrap dynamic HQL construction\nString avgArg = singleNumericExpression; // exactly one expression, validated upstream\nString hql = \"select avg(\" + avgArg + \") from \" + entity;\nassert avgArg.split(\",\").length == 1;","typeGuard":null,"tryCatchPattern":"catch (FunctionArgumentException e) {\n    // rewrite or reject the query; arity errors are deterministic, never retry\n    throw new IllegalArgumentException(\"Bad aggregate in generated query: \" + hql, e);\n}","preventionTips":["For generated HQL, build aggregates through helper methods that take exactly one expression parameter","Lint JPQL strings for avg(...,...) in code review","Test query-building code paths with unit tests before hitting the DB"],"tags":["hibernate","hql","jpql","avg-function","query-validation"],"backgroundTag":"hql-function-argument-count-mismatch","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}