{"record":{"id":"1125079b2a47a220","repo":"prestodb/presto","slug":"invalid-parameter-usage","errorCode":"INVALID_PARAMETER_USAGE","errorMessage":"Incorrect number of parameters: expected %s but found %s","messagePattern":"Incorrect number of parameters: expected (.+?) but found (.+?)","errorType":"error_code","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"presto-analyzer/src/main/java/com/facebook/presto/sql/analyzer/BuiltInQueryPreparer.java","lineNumber":145,"sourceCode":"        if (analyzerOptions.isLogFormattedQueryEnabled()) {\n            formattedQuery = Optional.of(getFormattedQuery(statement, parameters));\n        }\n        return new BuiltInPreparedQuery(wrappedStatement, statement, parameters, formattedQuery, prepareSql, distributedProcedureName);\n    }\n\n    private static String getFormattedQuery(Statement statement, List<Expression> parameters)\n    {\n        String formattedQuery = formatSql(\n                statement,\n                parameters.isEmpty() ? Optional.empty() : Optional.of(parameters));\n        return format(\"-- Formatted Query:%n%s\", formattedQuery);\n    }\n\n    private static void validateParameters(Statement node, List<Expression> parameterValues)\n    {\n        int parameterCount = getParameterCount(node);\n        if (parameterValues.size() != parameterCount) {\n            throw new SemanticException(INVALID_PARAMETER_USAGE, node, \"Incorrect number of parameters: expected %s but found %s\", parameterCount, parameterValues.size());\n        }\n        for (Expression expression : parameterValues) {\n            verifyExpressionIsConstant(ImmutableSet.of(), expression);\n        }\n    }\n\n    public static class BuiltInPreparedQuery\n            extends PreparedQuery\n    {\n        private final Statement statement;\n        private final Statement wrappedStatement;\n        private final List<Expression> parameters;\n        private final Optional<QualifiedObjectName> distributedProcedureName;\n\n        public BuiltInPreparedQuery(\n                Statement wrappedStatement,\n                Statement statement, List<Expression> parameters,\n                Optional<String> formattedQuery, Optional<String> prepareSql,","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-analyzer/src/main/java/com/facebook/presto/sql/analyzer/BuiltInQueryPreparer.java#L127-L163","documentation":"Before analyzing a statement with parameters, the preparer validates that the number of supplied parameter values exactly equals the number of '?' placeholders found in the statement. A mismatch throws SemanticException with code INVALID_PARAMETER_USAGE.","triggerScenarios":"Calling prepareQuery with a statement containing N '?' placeholders but a preparedStatements/parameterValues list whose size differs from N; commonly via EXECUTE of a prepared statement with the wrong number of USING values.","commonSituations":"EXECUTE stmt USING fewer/more values than placeholders; client drivers binding parameter lists that don't match the SQL text; dynamic SQL generation with incorrect parameter counts.","solutions":["Count the '?' placeholders in the statement and supply exactly that many parameter values","Check the EXECUTE ... USING value list matches the PREPARE statement's placeholder count","Regenerate the SQL or parameter list if the query text was changed without updating bindings"],"exampleFix":"// before\nPREPARE p FROM SELECT * FROM t WHERE a = ? AND b = ?;\nEXECUTE p USING 1;\n// after\nEXECUTE p USING 1, 2;","handlingStrategy":"validation","validationCode":"int placeholders = getParameterCount(statement);\nif (parameterValues.size() != placeholders) {\n    throw new IllegalArgumentException(\"Expected \" + placeholders + \" parameters but got \" + parameterValues.size());\n}","typeGuard":null,"tryCatchPattern":"try {\n    preparer.prepareQuery(options, sql, params, warnings);\n} catch (SemanticException e) {\n    if (e.getCode() == SemanticErrorCode.INVALID_PARAMETER_USAGE) { /* log placeholder count vs supplied values and re-bind */ }\n    throw e;\n}","preventionTips":["Count '?' placeholders and parameter values in one place when building dynamic SQL","Validate EXECUTE ... USING arity against the PREPARE statement at client side","Write tests asserting placeholder count for every prepared statement template"],"tags":["parameters","prepared-statement","semantic-error","placeholder-count"],"backgroundTag":"parameter-count-mismatch","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"}